cuda-async 0.3.1

Safe Async CUDA support via Async Rust.
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
 * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! CUDA kernel launch builder with argument marshalling.
//!
//! [`AsyncKernelLaunchBuilder`] safely accumulates a kernel function reference,
//! launch attributes, and type-erased argument pointers. An explicit unsafe
//! call to [`AsyncKernelLaunchBuilder::finalize_unchecked`] supplies the raw
//! launch geometry and returns an immutable [`AsyncKernelLaunch`] that can be
//! scheduled as a [`DeviceOperation`].
//!
//! Arguments are heap-allocated via [`KernelArgument::push_arg`] and freed when
//! the launcher is dropped after submission. This keeps the pointed-to values
//! alive until `cuLaunchKernel` / `cuLaunchKernelEx` has copied the launch
//! parameter values out of the host-side argument array.
//!
//! [`DeviceOperation`]: crate::simt::device_operation::DeviceOperation

use crate::simt::device_context::with_default_device_policy;
use crate::simt::device_future::DeviceFuture;
use crate::simt::device_operation::{DeviceOperation, ExecutionContext};
use crate::simt::error::DeviceError;
use crate::simt::scheduling_policies::SchedulingPolicy;
use cuda_core::simt::LaunchConfig;
use cuda_core::{CudaFunction, CudaStream};
use std::ffi::c_void;
use std::future::IntoFuture;
use std::marker::PhantomData;
use std::sync::Arc;

/// Safe, inert builder for an unchecked CUDA kernel launch.
///
/// Building a launch does not enqueue GPU work. The builder deliberately does
/// not implement [`DeviceOperation`] or [`IntoFuture`]: raw launch geometry
/// must first cross the explicit unsafe boundary at
/// [`finalize_unchecked`](Self::finalize_unchecked).
///
/// ```compile_fail,E0277
/// use cuda_async::simt::device_operation::DeviceOperation;
/// use cuda_async::simt::launch::AsyncKernelLaunchBuilder;
///
/// fn schedule<O: DeviceOperation>(_operation: O) {}
///
/// fn cannot_schedule(builder: AsyncKernelLaunchBuilder<'static>) {
///     schedule(builder);
/// }
/// ```
///
/// Builders stay on the thread that assembles their type-erased argument
/// storage. Only the immutable finalized operation is `Send`:
///
/// ```compile_fail,E0277
/// use cuda_async::simt::launch::AsyncKernelLaunchBuilder;
///
/// fn assert_send<T: Send>() {}
/// assert_send::<AsyncKernelLaunchBuilder<'static>>();
/// ```
#[derive(Debug)]
pub struct AsyncKernelLaunchBuilder<'a> {
    /// Handle to the compiled device function.
    func: Arc<CudaFunction>,
    /// Heap-allocated, type-erased argument storage passed to the CUDA driver.
    args: KernelArgStorage,
    /// Optional thread-block cluster dimensions for `cuLaunchKernelEx`.
    cluster_dim: Option<(u32, u32, u32)>,
    /// When `true`, launch via `cuLaunchKernelEx` with
    /// `CU_LAUNCH_ATTRIBUTE_COOPERATIVE` set, which guarantees the whole grid
    /// is co-resident on the device (required for `cuda_device::grid::sync()`).
    cooperative: bool,
    /// Ties borrowed device buffers to the lazy launch operation.
    _borrows: PhantomData<&'a mut ()>,
}

/// Immutable, runnable CUDA kernel launch.
///
/// This type can only be created by consuming an
/// [`AsyncKernelLaunchBuilder`]. It exposes no safe geometry or argument
/// setters, so its safety assumptions cannot be invalidated after the raw
/// launch configuration has been accepted.
///
/// ```compile_fail,E0599
/// use cuda_async::simt::launch::AsyncKernelLaunch;
/// use cuda_core::simt::LaunchConfig;
///
/// fn cannot_reconfigure(launch: &mut AsyncKernelLaunch<'_>, config: LaunchConfig) {
///     launch.set_launch_config(config);
/// }
/// ```
#[derive(Debug)]
pub struct AsyncKernelLaunch<'a> {
    /// Handle to the compiled device function.
    func: Arc<CudaFunction>,
    /// Heap-allocated, type-erased argument storage passed to the CUDA driver.
    args: KernelArgStorage,
    /// Complete grid/block dimensions and dynamic shared-memory size.
    cfg: LaunchConfig,
    /// Optional thread-block cluster dimensions for `cuLaunchKernelEx`.
    cluster_dim: Option<(u32, u32, u32)>,
    /// Whether this launch requests cooperative grid residency.
    cooperative: bool,
    /// Ties borrowed device buffers to the lazy launch operation.
    _borrows: PhantomData<&'a mut ()>,
}

/// # Safety
///
/// The argument pointers refer to heap allocations owned by the launch.
/// Non-`Copy` values enter that storage only through `KernelArgument: Send`;
/// copied scalar values have no destructor. The runnable type exposes none of
/// those erased values to safe Rust. The `Arc<CudaFunction>` is `Send + Sync`,
/// and `_borrows` preserves source lifetimes captured while building.
unsafe impl<'a> Send for AsyncKernelLaunch<'a> {}

#[derive(Debug, Default)]
struct KernelArgStorage {
    ptrs: Vec<*mut c_void>,
    drops: Vec<unsafe fn(*mut c_void)>,
}

impl Drop for KernelArgStorage {
    fn drop(&mut self) {
        for (arg, drop_arg) in self.ptrs.drain(..).zip(self.drops.drain(..)) {
            unsafe { drop_arg(arg) };
        }
    }
}

impl KernelArgStorage {
    fn push_send_arg<T: Send>(&mut self, arg: Box<T>) {
        unsafe fn drop_box<T>(arg: *mut c_void) {
            let _ = unsafe { Box::from_raw(arg as *mut T) };
        }

        self.ptrs.push(Box::into_raw(arg) as *mut c_void);
        self.drops.push(drop_box::<T>);
    }

    fn push_scalar_arg<T: Copy>(&mut self, arg: T) {
        unsafe fn drop_copy_box<T: Copy>(arg: *mut c_void) {
            let _ = unsafe { Box::from_raw(arg as *mut T) };
        }

        self.ptrs.push(Box::into_raw(Box::new(arg)) as *mut c_void);
        self.drops.push(drop_copy_box::<T>);
    }

    fn as_mut_slice(&mut self) -> &mut [*mut c_void] {
        &mut self.ptrs
    }
}

impl<'a> AsyncKernelLaunchBuilder<'a> {
    /// Creates an inert builder for `func` with no arguments or launch
    /// attributes.
    pub fn new(func: Arc<CudaFunction>) -> Self {
        Self {
            func,
            args: KernelArgStorage::default(),
            cluster_dim: None,
            cooperative: false,
            _borrows: PhantomData,
        }
    }

    /// Appends a by-value `Copy` argument to the kernel launch packet.
    ///
    /// This is the typed-module path used for scalar, raw-pointer, custom
    /// `Copy` structs, and `Copy` closure arguments.
    #[inline(always)]
    pub fn push_scalar_arg<T: Copy + 'a>(&mut self, arg: T) -> &mut Self {
        self.args.push_scalar_arg(arg);
        self
    }

    /// Appends a kernel argument. The value is heap-allocated and its pointer
    /// stored for the driver call.
    ///
    /// Scalars like `u32`, `f32`, `u64` etc. are auto-boxed -- no need to
    /// wrap them in `Box::new`.
    ///
    /// The allocated storage remains alive until launch submission finishes or
    /// the builder is dropped without launching.
    ///
    /// Values with thread-affine ownership cannot be hidden in the erased
    /// storage that will later become a `Send` operation:
    ///
    /// ```compile_fail,E0277
    /// use cuda_async::simt::launch::AsyncKernelLaunchBuilder;
    /// use std::rc::Rc;
    ///
    /// fn reject_rc(mut builder: AsyncKernelLaunchBuilder<'static>) {
    ///     builder.push_arg(Box::new(Rc::new(1_u32)));
    /// }
    /// ```
    #[inline(always)]
    pub fn push_arg<T: KernelArgument>(&mut self, arg: T) -> &mut Self {
        arg.push_arg(self);
        self
    }

    /// Appends multiple kernel arguments at once from a tuple.
    ///
    /// Equivalent to chained [`push_arg`](Self::push_arg) calls but allows
    /// grouping all arguments in a single expression:
    ///
    /// ```ignore
    /// launch.push_args((m, n, k, alpha, a_ptr, a_len, b_ptr, b_len, beta, c_ptr, c_len));
    /// ```
    ///
    /// Supports tuples up to 32 elements.
    #[inline(always)]
    pub fn push_args<T: KernelArguments>(&mut self, args: T) -> &mut Self {
        args.push_args(self);
        self
    }

    /// Sets thread-block cluster dimensions for a cluster launch.
    pub fn set_cluster_dim(&mut self, cluster_dim: (u32, u32, u32)) -> &mut Self {
        self.cluster_dim = Some(cluster_dim);
        self
    }

    /// Marks this launch as cooperative (`CU_LAUNCH_ATTRIBUTE_COOPERATIVE`).
    ///
    /// A cooperative launch guarantees every block in the grid is co-resident
    /// on the device, which is the precondition for grid-wide barriers like
    /// `cuda_device::grid::sync()`. May be combined with
    /// [`set_cluster_dim`](Self::set_cluster_dim); both attributes are then
    /// passed to `cuLaunchKernelEx` in the same call.
    pub fn set_cooperative(&mut self, cooperative: bool) -> &mut Self {
        self.cooperative = cooperative;
        self
    }

    /// Accepts an unverified raw launch configuration and seals this builder
    /// into an immutable, schedulable operation.
    ///
    /// Prefer the generated typed module APIs for application code. They check
    /// a kernel's declared launch contract before constructing the runnable
    /// operation.
    ///
    /// # Safety
    ///
    /// The caller must prove all facts that a typed prepared launch normally
    /// checks:
    ///
    /// - `cfg` must match the kernel's indexing dimensionality, launch bounds,
    ///   block shape, and dynamic shared-memory requirements;
    /// - the cluster and cooperative attributes must be legal for the kernel;
    /// - the function signature must exactly match the arguments, in order,
    ///   type, size, and alignment;
    /// - referenced device allocations must remain valid for the complete GPU
    ///   operation and satisfy Rust's aliasing requirements; and
    /// - the scheduling policy must select a stream from the CUDA context that
    ///   owns `func`.
    ///
    /// Violating these requirements can cause memory corruption, data races,
    /// or CUDA driver errors.
    ///
    /// ```compile_fail,E0133
    /// use cuda_async::simt::launch::AsyncKernelLaunchBuilder;
    /// use cuda_core::simt::LaunchConfig;
    ///
    /// fn raw_finalize_requires_unsafe(
    ///     builder: AsyncKernelLaunchBuilder<'static>,
    ///     config: LaunchConfig,
    /// ) {
    ///     let _ = builder.finalize_unchecked(config);
    /// }
    /// ```
    pub unsafe fn finalize_unchecked(self, cfg: LaunchConfig) -> AsyncKernelLaunch<'a> {
        AsyncKernelLaunch {
            func: self.func,
            args: self.args,
            cfg,
            cluster_dim: self.cluster_dim,
            cooperative: self.cooperative,
            _borrows: self._borrows,
        }
    }
}

impl<'a> AsyncKernelLaunch<'a> {
    /// Submits the kernel to `stream` via `cuLaunchKernel`, or via
    /// `cuLaunchKernelEx` when cluster dimensions or a cooperative launch
    /// were requested.
    ///
    /// # Safety
    ///
    /// - `self.func` must refer to a kernel loaded from the same CUDA context
    ///   that owns `stream`.
    /// - All argument pointers in `self.args` must point to correctly typed and
    ///   aligned host-side values for the kernel's formal parameters.
    /// - The pointed-to argument values must remain valid until launch
    ///   submission returns. The stream-aware launch helper binds the correct
    ///   context before calling into the CUDA driver.
    ///
    /// # Errors
    ///
    /// Returns [`DeviceError::Driver`] if context binding or launch submission
    /// fails.
    unsafe fn launch(mut self, stream: &Arc<CudaStream>) -> Result<(), DeviceError> {
        let cfg = self.cfg;
        let result = match (self.cluster_dim, self.cooperative) {
            (Some(cluster_dim), true) => unsafe {
                cuda_core::launch_kernel_ex_cooperative_on_stream(
                    self.func.as_ref(),
                    cfg.grid_dim,
                    cfg.block_dim,
                    cfg.shared_mem_bytes,
                    cluster_dim,
                    stream.as_ref(),
                    self.args.as_mut_slice(),
                )
            },
            (Some(cluster_dim), false) => unsafe {
                cuda_core::launch_kernel_ex_on_stream(
                    self.func.as_ref(),
                    cfg.grid_dim,
                    cfg.block_dim,
                    cfg.shared_mem_bytes,
                    cluster_dim,
                    stream.as_ref(),
                    self.args.as_mut_slice(),
                )
            },
            (None, true) => unsafe {
                cuda_core::launch_kernel_cooperative_on_stream(
                    self.func.as_ref(),
                    cfg.grid_dim,
                    cfg.block_dim,
                    cfg.shared_mem_bytes,
                    stream.as_ref(),
                    self.args.as_mut_slice(),
                )
            },
            (None, false) => unsafe {
                cuda_core::launch_kernel_on_stream(
                    self.func.as_ref(),
                    cfg.grid_dim,
                    cfg.block_dim,
                    cfg.shared_mem_bytes,
                    stream.as_ref(),
                    self.args.as_mut_slice(),
                )
            },
        };
        result.map_err(DeviceError::Driver)?;
        Ok(())
    }
}

/// Owns resources for a lazy async kernel launch and returns them when the GPU
/// work has completed.
#[derive(Debug)]
pub struct OwnedAsyncKernelLaunch<R: Send> {
    launch: AsyncKernelLaunch<'static>,
    resources: R,
}

unsafe impl<R: Send> Send for OwnedAsyncKernelLaunch<R> {}

impl<R: Send> OwnedAsyncKernelLaunch<R> {
    /// Creates an owned async kernel operation from a prepared launch and the
    /// resources that must stay alive until completion.
    pub fn new(launch: AsyncKernelLaunch<'static>, resources: R) -> Self {
        Self { launch, resources }
    }
}

/// Trait for types that can be marshalled into a CUDA kernel argument list.
///
/// Implementors heap-allocate the value and push a `*mut c_void` into the
/// launcher's argument vector.
///
/// Implemented for all common scalar primitives (`u8`–`u64`, `i8`–`i64`,
/// `f32`, `f64`, `usize`, `isize`, `bool`) so callers can write
/// `.push_arg(42u32)` without manual `Box::new`.
pub trait KernelArgument: Send {
    /// Heap-allocates `self` and appends the pointer to `launcher.args`.
    fn push_arg(self, launcher: &mut AsyncKernelLaunchBuilder<'_>);
}

/// Passes the box's raw pointer directly as a kernel argument.
///
/// This is the low-level escape hatch. Prefer passing scalars directly -- they
/// are auto-boxed via the blanket scalar impls.
impl<T: Send + 'static> KernelArgument for Box<T> {
    fn push_arg(self, launcher: &mut AsyncKernelLaunchBuilder<'_>) {
        launcher.args.push_send_arg(self);
    }
}

macro_rules! impl_scalar_kernel_arg {
    ($($t:ty),*) => {
        $(
            impl KernelArgument for $t {
                #[inline(always)]
                fn push_arg(self, launcher: &mut AsyncKernelLaunchBuilder<'_>) {
                    launcher.push_scalar_arg(self);
                }
            }
        )*
    };
}

impl_scalar_kernel_arg!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, f32, f64, bool);

// ---------------------------------------------------------------------------
// KernelArguments — push multiple heterogeneous args in a single call
// ---------------------------------------------------------------------------

/// Trait for a group of kernel arguments that can be pushed together.
///
/// Implemented for tuples of [`KernelArgument`] types up to arity 32, enabling
/// `launch.push_args((dim_m, dim_n, alpha, ptr, len))` as an alternative to
/// chained `.push_arg()` calls.
#[diagnostic::on_unimplemented(
    message = "cannot push `{Self}` as kernel arguments",
    note = "KernelArguments is implemented for tuples of KernelArgument types up to 32 elements"
)]
pub trait KernelArguments {
    /// Pushes every element into `launcher` in order.
    fn push_args(self, launcher: &mut AsyncKernelLaunchBuilder<'_>);
}

macro_rules! impl_kernel_args_tuple {
    // Base case: empty tuple
    () => {
        impl KernelArguments for () {
            #[inline(always)]
            fn push_args(self, _launcher: &mut AsyncKernelLaunchBuilder<'_>) {}
        }
    };
    // Recursive case: (A, B, C, ...) where each element is a KernelArgument
    ($($idx:tt : $T:ident),+) => {
        impl<$($T: KernelArgument),+> KernelArguments for ($($T,)+) {
            #[inline(always)]
            fn push_args(self, launcher: &mut AsyncKernelLaunchBuilder<'_>) {
                $(launcher.push_arg(self.$idx);)+
            }
        }
    };
}

impl_kernel_args_tuple!();
impl_kernel_args_tuple!(0: A);
impl_kernel_args_tuple!(0: A, 1: B);
impl_kernel_args_tuple!(0: A, 1: B, 2: C);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P, 16: Q);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P, 16: Q, 17: R);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P, 16: Q, 17: R, 18: S);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P, 16: Q, 17: R, 18: S, 19: T);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P, 16: Q, 17: R, 18: S, 19: T, 20: U);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P, 16: Q, 17: R, 18: S, 19: T, 20: U, 21: V);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P, 16: Q, 17: R, 18: S, 19: T, 20: U, 21: V, 22: W);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P, 16: Q, 17: R, 18: S, 19: T, 20: U, 21: V, 22: W, 23: X);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P, 16: Q, 17: R, 18: S, 19: T, 20: U, 21: V, 22: W, 23: X, 24: Y);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P, 16: Q, 17: R, 18: S, 19: T, 20: U, 21: V, 22: W, 23: X, 24: Y, 25: Z);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P, 16: Q, 17: R, 18: S, 19: T, 20: U, 21: V, 22: W, 23: X, 24: Y, 25: Z, 26: AA);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P, 16: Q, 17: R, 18: S, 19: T, 20: U, 21: V, 22: W, 23: X, 24: Y, 25: Z, 26: AA, 27: AB);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P, 16: Q, 17: R, 18: S, 19: T, 20: U, 21: V, 22: W, 23: X, 24: Y, 25: Z, 26: AA, 27: AB, 28: AC);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P, 16: Q, 17: R, 18: S, 19: T, 20: U, 21: V, 22: W, 23: X, 24: Y, 25: Z, 26: AA, 27: AB, 28: AC, 29: AD);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P, 16: Q, 17: R, 18: S, 19: T, 20: U, 21: V, 22: W, 23: X, 24: Y, 25: Z, 26: AA, 27: AB, 28: AC, 29: AD, 30: AE);
impl_kernel_args_tuple!(0: A, 1: B, 2: C, 3: D, 4: E, 5: F, 6: G, 7: H, 8: I, 9: J, 10: K, 11: L, 12: M, 13: N, 14: O, 15: P, 16: Q, 17: R, 18: S, 19: T, 20: U, 21: V, 22: W, 23: X, 24: Y, 25: Z, 26: AA, 27: AB, 28: AC, 29: AD, 30: AE, 31: AF);

/// Launches the kernel on the stream bound to the execution context.
impl<'a> DeviceOperation for AsyncKernelLaunch<'a> {
    type Output = ();

    unsafe fn execute(self, ctx: &ExecutionContext) -> Result<(), DeviceError> {
        unsafe { self.launch(ctx.get_cuda_stream()) }
    }
}

/// Schedules the kernel launch via the thread-local default scheduling policy.
impl<'a> IntoFuture for AsyncKernelLaunch<'a> {
    type Output = Result<(), DeviceError>;
    type IntoFuture = DeviceFuture<(), AsyncKernelLaunch<'a>>;
    fn into_future(self) -> Self::IntoFuture {
        match with_default_device_policy(|policy| policy.schedule(self)) {
            Ok(Ok(future)) => future,
            Ok(Err(e)) | Err(e) => DeviceFuture::failed(e),
        }
    }
}

impl<R: Send + 'static> DeviceOperation for OwnedAsyncKernelLaunch<R> {
    type Output = R;

    unsafe fn execute(self, ctx: &ExecutionContext) -> Result<R, DeviceError> {
        let Self { launch, resources } = self;
        unsafe { launch.launch(ctx.get_cuda_stream()) }?;
        Ok(resources)
    }
}

impl<R: Send + 'static> IntoFuture for OwnedAsyncKernelLaunch<R> {
    type Output = Result<R, DeviceError>;
    type IntoFuture = DeviceFuture<R, OwnedAsyncKernelLaunch<R>>;

    fn into_future(self) -> Self::IntoFuture {
        match with_default_device_policy(|policy| policy.schedule(self)) {
            Ok(Ok(future)) => future,
            Ok(Err(e)) | Err(e) => DeviceFuture::failed(e),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};

    #[repr(C)]
    #[derive(Clone, Copy, Debug, PartialEq)]
    struct TestParams {
        scale: f32,
        bias: i32,
    }

    #[test]
    fn scalar_arg_storage_accepts_custom_copy_value() {
        let mut storage = KernelArgStorage::default();
        let params = TestParams {
            scale: 2.0,
            bias: 3,
        };

        storage.push_scalar_arg(params);

        assert_eq!(storage.ptrs.len(), 1);
        assert_eq!(unsafe { *(storage.ptrs[0] as *const TestParams) }, params);
    }

    #[test]
    fn arg_storage_drops_values_with_their_original_type() {
        struct DropCounter(Arc<AtomicUsize>);

        impl Drop for DropCounter {
            fn drop(&mut self) {
                self.0.fetch_add(1, Ordering::Relaxed);
            }
        }

        let drops = Arc::new(AtomicUsize::new(0));
        let mut storage = KernelArgStorage::default();

        storage.push_send_arg(Box::new(DropCounter(Arc::clone(&drops))));
        assert_eq!(drops.load(Ordering::Relaxed), 0);

        drop(storage);
        assert_eq!(drops.load(Ordering::Relaxed), 1);
    }
}