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
//! The `KernelArg` trait: a uniform way to hand values to
//! `cuLaunchKernel` / `cudaLaunchKernel`, which expect `void**` — an array
//! of pointers to each argument.
//!
//! Implementors must be [`crate::DeviceRepr`] so we know the layout is
//! ABI-stable, and must produce a pointer whose pointee will remain valid
//! for the duration of the kernel launch.
use c_void;
use crateDeviceRepr;
/// A value that can be marshalled into the `void** kernelParams` slot of
/// `cuLaunchKernel` / `cudaLaunchKernel`.
///
/// # Safety
///
/// Implementors must uphold:
///
/// 1. [`Self::as_kernel_arg_ptr`] returns a pointer whose pointee is a valid
/// `DeviceRepr` value of the correct type for the target kernel slot.
/// 2. The returned pointer remains valid until the kernel launch has been
/// submitted to the stream (not necessarily completed — the runtime
/// copies argument bytes during submission).
/// 3. Concurrent kernel launches using the same argument value must tolerate
/// shared access; in practice this means the referent is either `Copy`
/// or borrowed immutably for the duration of submission.
pub unsafe
// SAFETY: &T where T: DeviceRepr points to a valid, ABI-stable value.
// The launch API reads argument bytes during submission, so an immutable
// borrow for the duration of `launch()` is enough.
unsafe
// SAFETY: same as above; &mut grants even stricter exclusive access.
unsafe