rustkernel-core 0.4.0

Core abstractions, traits, and registry for RustKernels GPU kernel library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Test kernels for validation and benchmarking.
//!
//! This module provides simple kernels for testing the kernel framework:
//! - `VectorAdd`: Batch kernel for vector addition
//! - `EchoKernel`: Ring kernel for message echo (latency testing)

use crate::domain::Domain;
use crate::error::Result;
use crate::kernel::KernelMetadata;
use crate::traits::{BatchKernel, GpuKernel};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};

// ============================================================================
// VectorAdd Batch Kernel
// ============================================================================

/// Input for vector addition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VectorAddInput {
    /// First vector.
    pub a: Vec<f32>,
    /// Second vector.
    pub b: Vec<f32>,
}

/// Output from vector addition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VectorAddOutput {
    /// Result vector (a + b).
    pub result: Vec<f32>,
}

/// Simple vector addition kernel.
///
/// This is a batch kernel that adds two vectors element-wise.
/// Used for testing and validation of the kernel framework.
#[derive(Debug, Clone)]
pub struct VectorAdd {
    metadata: KernelMetadata,
}

impl VectorAdd {
    /// Create a new VectorAdd kernel.
    #[must_use]
    pub fn new() -> Self {
        Self {
            metadata: KernelMetadata::batch("core/vector-add", Domain::Core)
                .with_description("Element-wise vector addition")
                .with_throughput(10_000_000)
                .with_latency_us(10.0),
        }
    }

    /// Perform vector addition (CPU implementation).
    fn add_vectors(a: &[f32], b: &[f32]) -> Vec<f32> {
        a.iter().zip(b.iter()).map(|(x, y)| x + y).collect()
    }
}

impl Default for VectorAdd {
    fn default() -> Self {
        Self::new()
    }
}

impl GpuKernel for VectorAdd {
    fn metadata(&self) -> &KernelMetadata {
        &self.metadata
    }

    fn validate(&self) -> Result<()> {
        Ok(())
    }
}

#[async_trait]
impl BatchKernel<VectorAddInput, VectorAddOutput> for VectorAdd {
    async fn execute(&self, input: VectorAddInput) -> Result<VectorAddOutput> {
        self.validate_input(&input)?;
        let result = Self::add_vectors(&input.a, &input.b);
        Ok(VectorAddOutput { result })
    }

    fn validate_input(&self, input: &VectorAddInput) -> Result<()> {
        if input.a.len() != input.b.len() {
            return Err(crate::error::KernelError::validation(format!(
                "Vector lengths must match: a.len()={}, b.len()={}",
                input.a.len(),
                input.b.len()
            )));
        }
        Ok(())
    }
}

// ============================================================================
// Echo Ring Kernel
// ============================================================================

/// Echo request message.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EchoRequest {
    /// Message to echo back.
    pub message: String,
    /// Sequence number for ordering.
    pub sequence: u64,
}

/// Echo response message.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EchoResponse {
    /// Echoed message.
    pub message: String,
    /// Original sequence number.
    pub sequence: u64,
    /// Timestamp of processing (nanoseconds since kernel start).
    pub processed_ns: u64,
}

/// Echo kernel state.
#[derive(Debug, Clone, Default)]
pub struct EchoState {
    /// Number of messages processed.
    pub messages_processed: u64,
    /// Start time for latency measurement.
    pub start_ns: u64,
}

/// Simple echo kernel for latency testing.
///
/// This is a ring kernel that echoes back messages with timing information.
/// Used for testing message round-trip latency.
#[derive(Debug, Clone)]
pub struct EchoKernel {
    metadata: KernelMetadata,
}

impl EchoKernel {
    /// Create a new EchoKernel.
    #[must_use]
    pub fn new() -> Self {
        Self {
            metadata: KernelMetadata::ring("core/echo", Domain::Core)
                .with_description("Message echo for latency testing")
                .with_throughput(1_000_000)
                .with_latency_us(0.5),
        }
    }

    /// Process an echo request.
    pub fn process(state: &mut EchoState, request: EchoRequest) -> EchoResponse {
        state.messages_processed += 1;

        // Simple timestamp (would use HLC in real implementation)
        let processed_ns = state.messages_processed * 100; // Placeholder

        EchoResponse {
            message: request.message,
            sequence: request.sequence,
            processed_ns,
        }
    }

    /// Initialize state.
    pub fn initialize() -> EchoState {
        EchoState {
            messages_processed: 0,
            start_ns: 0,
        }
    }
}

impl Default for EchoKernel {
    fn default() -> Self {
        Self::new()
    }
}

impl GpuKernel for EchoKernel {
    fn metadata(&self) -> &KernelMetadata {
        &self.metadata
    }
}

// ============================================================================
// Matrix Multiply Batch Kernel (for benchmarking)
// ============================================================================

/// Input for matrix multiplication.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MatMulInput {
    /// First matrix (row-major, dimensions rows_a x cols_a).
    pub a: Vec<f32>,
    /// Second matrix (row-major, dimensions cols_a x cols_b).
    pub b: Vec<f32>,
    /// Rows in matrix A.
    pub rows_a: usize,
    /// Columns in matrix A (= rows in matrix B).
    pub cols_a: usize,
    /// Columns in matrix B.
    pub cols_b: usize,
}

/// Output from matrix multiplication.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MatMulOutput {
    /// Result matrix (row-major, dimensions rows_a x cols_b).
    pub result: Vec<f32>,
}

/// Matrix multiplication kernel.
///
/// This is a batch kernel that multiplies two matrices.
/// Used for benchmarking compute throughput.
#[derive(Debug, Clone)]
pub struct MatMul {
    metadata: KernelMetadata,
}

impl MatMul {
    /// Create a new MatMul kernel.
    #[must_use]
    pub fn new() -> Self {
        Self {
            metadata: KernelMetadata::batch("core/matmul", Domain::Core)
                .with_description("Matrix multiplication (GEMM)")
                .with_throughput(1_000_000)
                .with_latency_us(50.0)
                .with_gpu_native(true),
        }
    }

    /// Perform matrix multiplication (naive CPU implementation).
    fn matmul(a: &[f32], b: &[f32], rows_a: usize, cols_a: usize, cols_b: usize) -> Vec<f32> {
        let mut result = vec![0.0f32; rows_a * cols_b];

        for i in 0..rows_a {
            for j in 0..cols_b {
                let mut sum = 0.0f32;
                for k in 0..cols_a {
                    sum += a[i * cols_a + k] * b[k * cols_b + j];
                }
                result[i * cols_b + j] = sum;
            }
        }

        result
    }
}

impl Default for MatMul {
    fn default() -> Self {
        Self::new()
    }
}

impl GpuKernel for MatMul {
    fn metadata(&self) -> &KernelMetadata {
        &self.metadata
    }
}

#[async_trait]
impl BatchKernel<MatMulInput, MatMulOutput> for MatMul {
    async fn execute(&self, input: MatMulInput) -> Result<MatMulOutput> {
        self.validate_input(&input)?;
        let result = Self::matmul(&input.a, &input.b, input.rows_a, input.cols_a, input.cols_b);
        Ok(MatMulOutput { result })
    }

    fn validate_input(&self, input: &MatMulInput) -> Result<()> {
        let expected_a = input.rows_a * input.cols_a;
        let expected_b = input.cols_a * input.cols_b;

        if input.a.len() != expected_a {
            return Err(crate::error::KernelError::validation(format!(
                "Matrix A size mismatch: expected {}, got {}",
                expected_a,
                input.a.len()
            )));
        }

        if input.b.len() != expected_b {
            return Err(crate::error::KernelError::validation(format!(
                "Matrix B size mismatch: expected {}, got {}",
                expected_b,
                input.b.len()
            )));
        }

        Ok(())
    }
}

// ============================================================================
// Reduce Sum Kernel
// ============================================================================

/// Input for sum reduction.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReduceSumInput {
    /// Data to sum.
    pub data: Vec<f32>,
}

/// Output from sum reduction.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReduceSumOutput {
    /// Sum of all elements.
    pub sum: f64,
    /// Count of elements.
    pub count: usize,
}

/// Sum reduction kernel.
///
/// Reduces a vector to its sum. Used for testing parallel reduction patterns.
#[derive(Debug, Clone)]
pub struct ReduceSum {
    metadata: KernelMetadata,
}

impl ReduceSum {
    /// Create a new ReduceSum kernel.
    #[must_use]
    pub fn new() -> Self {
        Self {
            metadata: KernelMetadata::batch("core/reduce-sum", Domain::Core)
                .with_description("Parallel sum reduction")
                .with_throughput(100_000_000)
                .with_latency_us(5.0),
        }
    }
}

impl Default for ReduceSum {
    fn default() -> Self {
        Self::new()
    }
}

impl GpuKernel for ReduceSum {
    fn metadata(&self) -> &KernelMetadata {
        &self.metadata
    }
}

#[async_trait]
impl BatchKernel<ReduceSumInput, ReduceSumOutput> for ReduceSum {
    async fn execute(&self, input: ReduceSumInput) -> Result<ReduceSumOutput> {
        let sum: f64 = input.data.iter().map(|&x| f64::from(x)).sum();
        Ok(ReduceSumOutput {
            sum,
            count: input.data.len(),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::kernel::KernelMode;

    #[tokio::test]
    async fn test_vector_add() {
        let kernel = VectorAdd::new();
        assert_eq!(kernel.metadata().id, "core/vector-add");
        assert_eq!(kernel.metadata().mode, KernelMode::Batch);

        let input = VectorAddInput {
            a: vec![1.0, 2.0, 3.0],
            b: vec![4.0, 5.0, 6.0],
        };

        let output = kernel.execute(input).await.unwrap();
        assert_eq!(output.result, vec![5.0, 7.0, 9.0]);
    }

    #[tokio::test]
    async fn test_vector_add_validation() {
        let kernel = VectorAdd::new();

        let input = VectorAddInput {
            a: vec![1.0, 2.0],
            b: vec![1.0, 2.0, 3.0],
        };

        let result = kernel.execute(input).await;
        assert!(result.is_err());
    }

    #[test]
    fn test_echo_kernel() {
        let kernel = EchoKernel::new();
        assert_eq!(kernel.metadata().id, "core/echo");
        assert_eq!(kernel.metadata().mode, KernelMode::Ring);

        let mut state = EchoKernel::initialize();
        let request = EchoRequest {
            message: "Hello".to_string(),
            sequence: 1,
        };

        let response = EchoKernel::process(&mut state, request);
        assert_eq!(response.message, "Hello");
        assert_eq!(response.sequence, 1);
        assert_eq!(state.messages_processed, 1);
    }

    #[tokio::test]
    async fn test_matmul() {
        let kernel = MatMul::new();

        // 2x2 * 2x2 matrix multiplication
        let input = MatMulInput {
            a: vec![1.0, 2.0, 3.0, 4.0],
            b: vec![5.0, 6.0, 7.0, 8.0],
            rows_a: 2,
            cols_a: 2,
            cols_b: 2,
        };

        let output = kernel.execute(input).await.unwrap();
        // [1,2] * [5,6] = [1*5+2*7, 1*6+2*8] = [19, 22]
        // [3,4]   [7,8]   [3*5+4*7, 3*6+4*8]   [43, 50]
        assert_eq!(output.result, vec![19.0, 22.0, 43.0, 50.0]);
    }

    #[tokio::test]
    async fn test_reduce_sum() {
        let kernel = ReduceSum::new();

        let input = ReduceSumInput {
            data: vec![1.0, 2.0, 3.0, 4.0, 5.0],
        };

        let output = kernel.execute(input).await.unwrap();
        assert!((output.sum - 15.0).abs() < 1e-6);
        assert_eq!(output.count, 5);
    }
}