PersistentMessage

Derive Macro PersistentMessage 

Source
#[derive(PersistentMessage)]
{
    // Attributes available to this derive:
    #[persistent_message]
}
Expand description

Derive macro for implementing the PersistentMessage trait.

This macro enables type-based dispatch within persistent GPU kernels by generating handler_id, inline payload serialization, and deserialization.

§Requirements

The struct must:

  • Already implement RingMessage (use #[derive(RingMessage)])
  • Be #[repr(C)] for safe memory layout
  • Be Copy + Clone for inline payload serialization

§Attributes

On the struct:

  • handler_id = N (required) - CUDA dispatch handler ID (0-255)
  • requires_response = true/false (optional) - Whether this message expects a response

§Example

use ringkernel_derive::{RingMessage, PersistentMessage};

#[derive(RingMessage, PersistentMessage, Clone, Copy)]
#[repr(C)]
#[message(type_id = 1001)]
#[persistent_message(handler_id = 1, requires_response = true)]
pub struct FraudCheckRequest {
    pub transaction_id: u64,
    pub amount: f32,
    pub account_id: u32,
}

// Generated implementations:
// - handler_id() returns 1
// - requires_response() returns true
// - to_inline_payload() serializes the struct to [u8; 32] if it fits
// - from_inline_payload() deserializes from bytes
// - payload_size() returns the struct size

§Size Validation

For inline payload serialization, structs must be <= 32 bytes. Larger structs will return None from to_inline_payload().

§CUDA Integration

The handler_id maps to a switch case in generated CUDA code:

switch (msg->handler_id) {
    case 1: handle_fraud_check(msg, state, response); break;
    case 2: handle_aggregate(msg, state, response); break;
    // ...
}