oxicuda-launch 0.1.3

OxiCUDA Launch - Type-safe GPU kernel launch infrastructure
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! Named kernel arguments for enhanced debuggability and type safety.
//!
//! This module provides [`NamedKernelArgs`], a trait that extends
//! [`KernelArgs`] with human-readable argument names, and [`ArgBuilder`],
//! a builder that constructs argument pointer arrays with associated
//! names for logging and debugging.
//!
//! # Motivation
//!
//! Standard kernel arguments are positional tuples with no names. When
//! debugging kernel launches, it is helpful to know which argument
//! corresponds to which kernel parameter. `NamedKernelArgs` bridges
//! this gap by associating names with arguments.
//!
//! # Example
//!
//! ```rust
//! use oxicuda_launch::named_args::ArgBuilder;
//!
//! let a_ptr: u64 = 0x1000;
//! let n: u32 = 1024;
//! let mut builder = ArgBuilder::new();
//! builder.add("a_ptr", &a_ptr).add("n", &n);
//! assert_eq!(builder.names(), &["a_ptr", "n"]);
//! let ptrs = builder.build();
//! assert_eq!(ptrs.len(), 2);
//! ```

use std::ffi::c_void;

use crate::kernel::KernelArgs;

// ---------------------------------------------------------------------------
// NamedKernelArgs trait
// ---------------------------------------------------------------------------

/// Extension of [`KernelArgs`] that provides argument metadata.
///
/// Types implementing this trait can report the names and count of
/// their kernel arguments, which is useful for debugging, logging,
/// and validation.
///
/// # Safety
///
/// Implementors must uphold the same invariants as [`KernelArgs`].
/// The names returned by `arg_names` must correspond one-to-one with
/// the pointers returned by `as_param_ptrs`.
pub unsafe trait NamedKernelArgs: KernelArgs {
    /// Returns the names of all kernel arguments in order.
    fn arg_names() -> &'static [&'static str];

    /// Returns the number of kernel arguments.
    fn arg_count() -> usize {
        Self::arg_names().len()
    }
}

// ---------------------------------------------------------------------------
// ArgEntry
// ---------------------------------------------------------------------------

/// An entry in the argument builder, holding a pointer and its name.
#[derive(Debug)]
struct ArgEntry {
    /// Pointer to the argument value.
    ptr: *mut c_void,
    /// Human-readable name for the argument.
    name: String,
}

// ---------------------------------------------------------------------------
// ArgBuilder
// ---------------------------------------------------------------------------

/// A builder for constructing named kernel argument arrays.
///
/// Collects typed argument values along with their names, then
/// produces the `Vec<*mut c_void>` array needed by `cuLaunchKernel`.
///
/// # Example
///
/// ```rust
/// use oxicuda_launch::named_args::ArgBuilder;
///
/// let x: f32 = 3.14;
/// let n: u32 = 512;
/// let mut builder = ArgBuilder::new();
/// builder.add("x", &x).add("n", &n);
/// assert_eq!(builder.arg_count(), 2);
/// let ptrs = builder.build();
/// assert_eq!(ptrs.len(), 2);
/// ```
pub struct ArgBuilder {
    /// Collected argument entries.
    args: Vec<ArgEntry>,
}

impl ArgBuilder {
    /// Creates a new empty argument builder.
    #[inline]
    pub fn new() -> Self {
        Self { args: Vec::new() }
    }

    /// Creates a new argument builder with the given initial capacity.
    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            args: Vec::with_capacity(capacity),
        }
    }

    /// Adds a named argument to the builder.
    ///
    /// The pointer to `val` is stored. The caller must ensure that `val`
    /// remains valid (not moved or dropped) until the kernel launch
    /// using the built pointer array completes.
    ///
    /// Returns `&mut Self` for method chaining.
    pub fn add<T: Copy>(&mut self, name: &str, val: &T) -> &mut Self {
        self.args.push(ArgEntry {
            ptr: val as *const T as *mut c_void,
            name: name.to_owned(),
        });
        self
    }

    /// Builds the argument pointer array for `cuLaunchKernel`.
    ///
    /// Returns the raw pointer array. The names are consumed; use
    /// [`names`](Self::names) before calling `build` if you need them.
    pub fn build(self) -> Vec<*mut c_void> {
        self.args.into_iter().map(|entry| entry.ptr).collect()
    }

    /// Returns the names of all added arguments in order.
    pub fn names(&self) -> Vec<&str> {
        self.args.iter().map(|entry| entry.name.as_str()).collect()
    }

    /// Returns the number of arguments added so far.
    #[inline]
    pub fn arg_count(&self) -> usize {
        self.args.len()
    }

    /// Returns `true` if no arguments have been added.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.args.is_empty()
    }

    /// Returns a human-readable summary of the arguments.
    pub fn summary(&self) -> String {
        let parts: Vec<String> = self
            .args
            .iter()
            .map(|entry| format!("{}={:p}", entry.name, entry.ptr))
            .collect();
        format!("ArgBuilder[{}]", parts.join(", "))
    }
}

impl Default for ArgBuilder {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for ArgBuilder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ArgBuilder")
            .field("count", &self.args.len())
            .field("names", &self.names())
            .finish()
    }
}

// Implement NamedKernelArgs for () (no arguments).
//
// SAFETY: Returns an empty name array, consistent with the empty
// pointer array from KernelArgs for ().
unsafe impl NamedKernelArgs for () {
    fn arg_names() -> &'static [&'static str] {
        &[]
    }
}

// ---------------------------------------------------------------------------
// FixedNamedArgs — stack-allocated named argument array
// ---------------------------------------------------------------------------

/// A single named argument entry: a static string name paired with a raw
/// const pointer to the argument value (already on the stack in the caller).
///
/// The pointer is `*const c_void` so no allocation occurs; it is the
/// caller's responsibility to ensure the pointed-to value outlives the
/// kernel launch.
#[derive(Debug, Clone, Copy)]
pub struct NamedArgEntry {
    /// The human-readable name of this kernel argument.
    pub name: &'static str,
    /// Raw const pointer to the argument value.
    pub ptr: *const c_void,
}

// SAFETY: NamedArgEntry only holds a raw pointer to a value that lives
// at least as long as the kernel launch (caller guarantees this).
// The pointer is never dereferenced inside this module.
unsafe impl Send for NamedArgEntry {}
unsafe impl Sync for NamedArgEntry {}

/// A const-generic, stack-allocated array of named kernel arguments.
///
/// `FixedNamedArgs<N>` stores exactly `N` [`NamedArgEntry`] values on
/// the stack — zero heap allocation, zero indirection overhead compared
/// to a plain positional tuple.
///
/// # Invariants
///
/// Every pointed-to value must remain valid (not moved or dropped) until
/// after the kernel launch that consumes this struct completes.
///
/// # Example
///
/// ```rust
/// use oxicuda_launch::named_args::{FixedNamedArgs, NamedArgEntry};
///
/// let n: u32 = 1024;
/// let alpha: f32 = 2.0;
///
/// let args = FixedNamedArgs::new([
///     NamedArgEntry { name: "n",     ptr: &n     as *const u32 as *const std::ffi::c_void },
///     NamedArgEntry { name: "alpha", ptr: &alpha as *const f32  as *const std::ffi::c_void },
/// ]);
///
/// assert_eq!(args.len(), 2);
/// assert_eq!(args.names()[0], "n");
/// assert_eq!(args.names()[1], "alpha");
/// ```
pub struct FixedNamedArgs<const N: usize> {
    /// The argument entries, stored inline on the stack.
    entries: [NamedArgEntry; N],
}

impl<const N: usize> FixedNamedArgs<N> {
    /// Creates a new `FixedNamedArgs` from an array of [`NamedArgEntry`] values.
    #[inline]
    pub const fn new(entries: [NamedArgEntry; N]) -> Self {
        Self { entries }
    }

    /// Returns the number of arguments.
    #[inline]
    pub const fn len(&self) -> usize {
        N
    }

    /// Returns `true` if there are no arguments.
    #[inline]
    pub const fn is_empty(&self) -> bool {
        N == 0
    }

    /// Returns the argument names in declaration order.
    pub fn names(&self) -> [&'static str; N] {
        let mut out = [""; N];
        for (i, entry) in self.entries.iter().enumerate() {
            out[i] = entry.name;
        }
        out
    }

    /// Returns a mutable array of `*mut c_void` pointers suitable for
    /// passing directly to `cuLaunchKernel` as the `kernelParams` array.
    ///
    /// # Safety
    ///
    /// The returned pointers are valid only as long as the original values
    /// that were passed when constructing the entries remain in scope.
    pub fn as_ptr_array(&self) -> [*mut c_void; N] {
        let mut out = [std::ptr::null_mut::<c_void>(); N];
        for (i, entry) in self.entries.iter().enumerate() {
            // Cast const → mut: cuLaunchKernel's ABI takes `void**` but
            // never mutates the argument values.
            out[i] = entry.ptr as *mut c_void;
        }
        out
    }

    /// Returns an immutable slice over the entries for inspection.
    #[inline]
    pub fn entries(&self) -> &[NamedArgEntry; N] {
        &self.entries
    }
}

impl<const N: usize> std::fmt::Debug for FixedNamedArgs<N> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut ds = f.debug_struct("FixedNamedArgs");
        ds.field("len", &N);
        for entry in &self.entries {
            ds.field(entry.name, &entry.ptr);
        }
        ds.finish()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn arg_builder_new_empty() {
        let builder = ArgBuilder::new();
        assert!(builder.is_empty());
        assert_eq!(builder.arg_count(), 0);
    }

    #[test]
    fn arg_builder_add_and_names() {
        let x: u32 = 42;
        let y: f64 = 2.78;
        let mut builder = ArgBuilder::new();
        builder.add("x", &x).add("y", &y);
        assert_eq!(builder.arg_count(), 2);
        assert_eq!(builder.names(), vec!["x", "y"]);
    }

    #[test]
    fn arg_builder_build_pointer_count() {
        let a: u64 = 0x1000;
        let b: u32 = 512;
        let c: f32 = 1.0;
        let mut builder = ArgBuilder::new();
        builder.add("a", &a).add("b", &b).add("c", &c);
        let ptrs = builder.build();
        assert_eq!(ptrs.len(), 3);
    }

    #[test]
    fn arg_builder_build_pointers_valid() {
        let val: u32 = 99;
        let mut builder = ArgBuilder::new();
        builder.add("val", &val);
        let ptrs = builder.build();
        assert_eq!(ptrs.len(), 1);
        let read_back = unsafe { *(ptrs[0] as *const u32) };
        assert_eq!(read_back, 99);
    }

    #[test]
    fn arg_builder_summary() {
        let x: u32 = 10;
        let mut builder = ArgBuilder::new();
        builder.add("x", &x);
        let s = builder.summary();
        assert!(s.starts_with("ArgBuilder["));
        assert!(s.contains("x="));
    }

    #[test]
    fn arg_builder_debug() {
        let builder = ArgBuilder::new();
        let dbg = format!("{builder:?}");
        assert!(dbg.contains("ArgBuilder"));
        assert!(dbg.contains("count"));
    }

    #[test]
    fn arg_builder_default() {
        let builder = ArgBuilder::default();
        assert!(builder.is_empty());
    }

    #[test]
    fn arg_builder_with_capacity() {
        let builder = ArgBuilder::with_capacity(8);
        assert!(builder.is_empty());
        assert_eq!(builder.arg_count(), 0);
    }

    #[test]
    fn named_kernel_args_trait_exists() {
        // Verify the trait compiles with the safety requirement.
        fn assert_named<T: NamedKernelArgs>() {
            let _names = T::arg_names();
            let _count = T::arg_count();
        }
        // We cannot call assert_named without an implementor, but
        // the existence of the function is enough.
        let _ = assert_named::<()> as *const ();
    }

    // -----------------------------------------------------------------------
    // FixedNamedArgs tests
    // -----------------------------------------------------------------------

    #[test]
    fn fixed_named_args_zero_size() {
        let args: FixedNamedArgs<0> = FixedNamedArgs::new([]);
        assert!(args.is_empty());
        assert_eq!(args.len(), 0);
        let ptrs = args.as_ptr_array();
        assert_eq!(ptrs.len(), 0);
    }

    #[test]
    fn fixed_named_args_single_u32() {
        let n: u32 = 1024;
        let args = FixedNamedArgs::new([NamedArgEntry {
            name: "n",
            ptr: &n as *const u32 as *const c_void,
        }]);
        assert_eq!(args.len(), 1);
        assert!(!args.is_empty());
        assert_eq!(args.names(), ["n"]);

        let ptrs = args.as_ptr_array();
        assert_eq!(ptrs.len(), 1);
        // Verify the pointer round-trips to the original value.
        let read_back = unsafe { *(ptrs[0] as *const u32) };
        assert_eq!(read_back, 1024);
    }

    #[test]
    fn fixed_named_args_two_entries_order_preserved() {
        let n: u32 = 512;
        let alpha: f32 = std::f32::consts::PI;
        let args = FixedNamedArgs::new([
            NamedArgEntry {
                name: "n",
                ptr: &n as *const u32 as *const c_void,
            },
            NamedArgEntry {
                name: "alpha",
                ptr: &alpha as *const f32 as *const c_void,
            },
        ]);

        assert_eq!(args.len(), 2);
        let names = args.names();
        assert_eq!(names[0], "n");
        assert_eq!(names[1], "alpha");

        let ptrs = args.as_ptr_array();
        let n_back = unsafe { *(ptrs[0] as *const u32) };
        let a_back = unsafe { *(ptrs[1] as *const f32) };
        assert_eq!(n_back, 512);
        assert!((a_back - std::f32::consts::PI).abs() < f32::EPSILON);
    }

    #[test]
    fn fixed_named_args_no_size_overhead_vs_entry_array() {
        use std::mem::size_of;
        // FixedNamedArgs<N> must have the same size as [NamedArgEntry; N].
        assert_eq!(
            size_of::<FixedNamedArgs<4>>(),
            size_of::<[NamedArgEntry; 4]>(),
            "FixedNamedArgs<4> must not add any size overhead"
        );
        assert_eq!(
            size_of::<FixedNamedArgs<1>>(),
            size_of::<[NamedArgEntry; 1]>(),
            "FixedNamedArgs<1> must not add any size overhead"
        );
    }

    #[test]
    fn fixed_named_args_ptr_array_length_matches_n() {
        let a: u64 = 0x1000_0000;
        let b: u32 = 256;
        let c: f64 = 1.0;
        let args = FixedNamedArgs::new([
            NamedArgEntry {
                name: "a",
                ptr: &a as *const u64 as *const c_void,
            },
            NamedArgEntry {
                name: "b",
                ptr: &b as *const u32 as *const c_void,
            },
            NamedArgEntry {
                name: "c",
                ptr: &c as *const f64 as *const c_void,
            },
        ]);
        let ptrs = args.as_ptr_array();
        assert_eq!(ptrs.len(), 3);
    }

    #[test]
    fn fixed_named_args_debug_contains_len() {
        let n: u32 = 42;
        let args = FixedNamedArgs::new([NamedArgEntry {
            name: "n",
            ptr: &n as *const u32 as *const c_void,
        }]);
        let dbg = format!("{args:?}");
        assert!(dbg.contains("FixedNamedArgs"), "Debug output: {dbg}");
        assert!(
            dbg.contains('1') || dbg.contains("len"),
            "Debug output: {dbg}"
        );
    }

    #[test]
    fn fixed_named_args_entries_accessor() {
        let x: f32 = 7.0;
        let args = FixedNamedArgs::new([NamedArgEntry {
            name: "x",
            ptr: &x as *const f32 as *const c_void,
        }]);
        let entries = args.entries();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].name, "x");
    }

    // ---------------------------------------------------------------------------
    // Quality gate tests (CPU-only)
    // ---------------------------------------------------------------------------

    #[test]
    fn named_kernel_args_empty() {
        // NamedKernelArgs for () has no args: arg_names is empty, arg_count is 0.
        let names = <() as NamedKernelArgs>::arg_names();
        assert!(names.is_empty(), "() must have no arg names");
        let count = <() as NamedKernelArgs>::arg_count();
        assert_eq!(count, 0, "() must have arg_count == 0");
    }

    #[test]
    fn named_kernel_args_add_and_count() {
        // After adding 3 named args to ArgBuilder, arg_count() == 3.
        let a: u32 = 1;
        let b: f64 = 2.0;
        let c: u64 = 3;
        let mut builder = ArgBuilder::new();
        builder.add("a", &a).add("b", &b).add("c", &c);
        assert_eq!(
            builder.arg_count(),
            3,
            "ArgBuilder with 3 args must report arg_count == 3"
        );
        // Names must be in insertion order
        let names = builder.names();
        assert_eq!(names[0], "a");
        assert_eq!(names[1], "b");
        assert_eq!(names[2], "c");
    }
}