|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +RustKernels is a GPU-accelerated kernel library for financial services, analytics, and compliance workloads. It's a Rust port of the DotCompute GPU kernel library, built on the RustCompute (RingKernel) framework. |
| 8 | + |
| 9 | +**Key dependency**: RustCompute is located at `../../RustCompute/RustCompute/` (relative path from workspace root). |
| 10 | + |
| 11 | +## Build Commands |
| 12 | + |
| 13 | +```bash |
| 14 | +# Build entire workspace |
| 15 | +cargo build --workspace |
| 16 | + |
| 17 | +# Build specific domain crate |
| 18 | +cargo build --package rustkernel-graph |
| 19 | + |
| 20 | +# Check with all features |
| 21 | +cargo check --all-features |
| 22 | + |
| 23 | +# Format code |
| 24 | +cargo fmt --all |
| 25 | + |
| 26 | +# Lint |
| 27 | +cargo clippy --all-targets --all-features -- -D warnings |
| 28 | +``` |
| 29 | + |
| 30 | +## Test Commands |
| 31 | + |
| 32 | +```bash |
| 33 | +# Run all tests |
| 34 | +cargo test --workspace |
| 35 | + |
| 36 | +# Run tests for specific domain |
| 37 | +cargo test --package rustkernel-graph |
| 38 | +cargo test --package rustkernel-ml |
| 39 | +cargo test --package rustkernel-compliance |
| 40 | +cargo test --package rustkernel-risk |
| 41 | + |
| 42 | +# Run single test |
| 43 | +cargo test --package rustkernel-graph test_pagerank_metadata |
| 44 | + |
| 45 | +# Run tests with all features |
| 46 | +cargo test --workspace --all-features |
| 47 | + |
| 48 | +# Run benchmarks |
| 49 | +cargo bench --package rustkernel |
| 50 | +``` |
| 51 | + |
| 52 | +## Architecture |
| 53 | + |
| 54 | +### Workspace Structure |
| 55 | + |
| 56 | +18 crates organized by concern: |
| 57 | + |
| 58 | +- **`rustkernel`** - Facade crate, re-exports all domains |
| 59 | +- **`rustkernel-core`** - Core traits, registry, licensing, K2K coordination |
| 60 | +- **`rustkernel-derive`** - Proc macros (`#[gpu_kernel]`, `#[derive(KernelMessage)]`) |
| 61 | +- **`rustkernel-cli`** - CLI tool for kernel management |
| 62 | +- **14 domain crates** - One per business domain (graph, ml, compliance, etc.) |
| 63 | + |
| 64 | +### Kernel Execution Modes |
| 65 | + |
| 66 | +Two execution modes with different latency/overhead tradeoffs: |
| 67 | + |
| 68 | +1. **Batch Kernels** (`BatchKernel<I, O>` trait) |
| 69 | + - CPU-orchestrated, 10-50μs launch overhead |
| 70 | + - State in CPU memory, launched on-demand |
| 71 | + - Use for heavy periodic computation |
| 72 | + |
| 73 | +2. **Ring Kernels** (`RingKernelHandler<M, R>` trait) |
| 74 | + - GPU-persistent actors, 100-500ns message latency |
| 75 | + - State permanently in GPU memory |
| 76 | + - Use for high-frequency operations |
| 77 | + |
| 78 | +### Core Traits (`rustkernel-core/src/traits.rs`) |
| 79 | + |
| 80 | +```rust |
| 81 | +// All kernels implement GpuKernel for metadata |
| 82 | +trait GpuKernel: Send + Sync + Debug { |
| 83 | + fn metadata(&self) -> &KernelMetadata; |
| 84 | + fn validate(&self) -> Result<()>; |
| 85 | +} |
| 86 | + |
| 87 | +// Batch execution |
| 88 | +trait BatchKernel<I, O>: GpuKernel { |
| 89 | + async fn execute(&self, input: I) -> Result<O>; |
| 90 | +} |
| 91 | + |
| 92 | +// Ring (persistent actor) execution |
| 93 | +trait RingKernelHandler<M, R>: GpuKernel |
| 94 | +where M: RingMessage, R: RingMessage { |
| 95 | + async fn handle(&self, ctx: &mut RingContext, msg: M) -> Result<R>; |
| 96 | +} |
| 97 | + |
| 98 | +// Multi-pass algorithms (PageRank, K-Means) |
| 99 | +trait IterativeKernel<S, I, O>: GpuKernel { |
| 100 | + fn initial_state(&self, input: &I) -> S; |
| 101 | + async fn iterate(&self, state: &mut S, input: &I) -> Result<IterationResult<O>>; |
| 102 | + fn converged(&self, state: &S, threshold: f64) -> bool; |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +### K2K (Kernel-to-Kernel) Messaging |
| 107 | + |
| 108 | +Cross-kernel coordination in `rustkernel-core/src/k2k.rs`: |
| 109 | + |
| 110 | +- `IterativeState` - Track convergence across iterations |
| 111 | +- `ScatterGatherState` - Parallel worker patterns |
| 112 | +- `FanOutTracker` - Broadcast patterns |
| 113 | +- `PipelineTracker` - Multi-stage processing |
| 114 | + |
| 115 | +### Ring Message Type IDs |
| 116 | + |
| 117 | +Each domain has a reserved range for Ring message type IDs: |
| 118 | +- Graph: 200-299 |
| 119 | +- Compliance: 300-399 |
| 120 | +- Temporal: 400-499 |
| 121 | +- Risk: 600-699 |
| 122 | +- ML: 700-799 |
| 123 | + |
| 124 | +### Domain Crate Structure |
| 125 | + |
| 126 | +Each domain crate follows this pattern: |
| 127 | +``` |
| 128 | +rustkernel-{domain}/ |
| 129 | +├── src/ |
| 130 | +│ ├── lib.rs # Module exports, register_all() |
| 131 | +│ ├── messages.rs # Batch kernel input/output types |
| 132 | +│ ├── ring_messages.rs # Ring message types with #[derive(RingMessage)] |
| 133 | +│ ├── types.rs # Common domain types |
| 134 | +│ └── {feature}.rs # Kernel implementations |
| 135 | +``` |
| 136 | + |
| 137 | +### Ring Message Definition Pattern |
| 138 | + |
| 139 | +```rust |
| 140 | +use ringkernel_derive::RingMessage; |
| 141 | +use rkyv::{Archive, Serialize, Deserialize}; |
| 142 | + |
| 143 | +#[derive(Debug, Clone, Archive, Serialize, Deserialize, RingMessage)] |
| 144 | +#[archive(check_bytes)] |
| 145 | +#[message(type_id = 200)] // Unique within domain range |
| 146 | +pub struct MyRequest { |
| 147 | + #[message(id)] |
| 148 | + pub id: MessageId, |
| 149 | + pub data: u64, |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +**Important**: `MessageId` is a tuple struct. Use `MessageId(value)` not `MessageId::new()`. |
| 154 | + |
| 155 | +### Fixed-Point Arithmetic |
| 156 | + |
| 157 | +Ring messages use fixed-point for GPU-compatible numerics: |
| 158 | +```rust |
| 159 | +// 8 decimal places |
| 160 | +fn to_fixed_point(value: f64) -> i64 { (value * 100_000_000.0) as i64 } |
| 161 | +fn from_fixed_point(fp: i64) -> f64 { fp as f64 / 100_000_000.0 } |
| 162 | +``` |
| 163 | + |
| 164 | +## Adding a New Kernel |
| 165 | + |
| 166 | +1. Define kernel struct implementing `GpuKernel` |
| 167 | +2. Implement `BatchKernel<I, O>` or `RingKernelHandler<M, R>` |
| 168 | +3. Add input/output types to `messages.rs` |
| 169 | +4. For Ring kernels, add messages to `ring_messages.rs` with unique type IDs |
| 170 | +5. Add tests |
| 171 | + |
| 172 | +## Licensing System |
| 173 | + |
| 174 | +Enterprise licensing in `rustkernel-core/src/license.rs`: |
| 175 | +- `DevelopmentLicense` - All features enabled (default for local dev) |
| 176 | +- Domain-based validation via `LicenseValidator` trait |
| 177 | +- Feature gating at kernel registration and activation time |
0 commit comments