Skip to main content

singe_cuda/graph/
raw.rs

1//! Raw CUDA graph node parameters and pointer-based operations.
2
3use std::ptr;
4
5use singe_cuda_sys::{driver, runtime};
6
7use crate::{
8    graph::{Extent, Position},
9    memory::{ArrayHandle, MemoryCopyKind},
10    types::HostFunction,
11};
12
13/// A raw base pointer with the byte pitch and logical dimensions of a pitched allocation.
14///
15/// This value does not retain the allocation or establish initialized access.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub struct PitchedPtr {
18    ptr: *mut (),
19    /// Byte distance between the start of adjacent rows.
20    pub pitch: usize,
21    /// Logical allocation width in bytes.
22    pub x_size: usize,
23    /// Logical allocation height in rows.
24    pub y_size: usize,
25}
26
27impl PitchedPtr {
28    /// Creates pitched pointer parameters from a raw device or mapped host pointer.
29    ///
30    /// # Safety
31    ///
32    /// `ptr` must be valid for every row described by `pitch`, `x_size`, and `y_size` when CUDA evaluates the graph node using this value.
33    pub const unsafe fn new(ptr: *mut (), pitch: usize, x_size: usize, y_size: usize) -> Self {
34        Self {
35            ptr,
36            pitch,
37            x_size,
38            y_size,
39        }
40    }
41
42    /// Returns the raw allocation base address without granting host accessibility.
43    #[must_use]
44    pub const fn ptr(self) -> *mut () {
45        self.ptr
46    }
47}
48
49/// Raw array or pitched-pointer operands, positions, and extent for a three-dimensional graph copy.
50///
51/// This descriptor retains no operand owners; its constructor's storage contract applies to every graph execution that uses it.
52#[derive(Debug, Clone, Copy)]
53pub struct MemoryCopy3DNodeParams {
54    src_array: Option<ArrayHandle>,
55    src_pos: Position,
56    src_ptr: PitchedPtr,
57    dst_array: Option<ArrayHandle>,
58    dst_pos: Position,
59    dst_ptr: PitchedPtr,
60    extent: Extent,
61    kind: MemoryCopyKind,
62}
63
64/// Raw parameters for copying bytes to a CUDA symbol in a graph node.
65///
66/// The descriptor retains neither the symbol's code owner nor the source allocation.
67#[derive(Debug, Clone, Copy)]
68pub struct MemoryCopyToSymbolNodeParams {
69    symbol: *const (),
70    src: *const (),
71    count: usize,
72    offset: usize,
73    kind: MemoryCopyKind,
74}
75
76/// Raw parameters for copying bytes from a CUDA symbol in a graph node.
77///
78/// The descriptor retains neither the symbol's code owner nor the destination allocation.
79#[derive(Debug, Clone, Copy)]
80pub struct MemoryCopyFromSymbolNodeParams {
81    dst: *mut (),
82    symbol: *const (),
83    count: usize,
84    offset: usize,
85    kind: MemoryCopyKind,
86}
87
88/// A native host-node callback and its raw user-data pointer.
89///
90/// This descriptor owns neither callback code nor user data.
91#[derive(Debug, Clone, Copy)]
92pub struct HostNodeParams {
93    func: HostFunction,
94    user_data: *mut (),
95}
96
97impl HostNodeParams {
98    /// Creates host callback node parameters from a raw user-data pointer.
99    ///
100    /// # Safety
101    ///
102    /// `user_data` must remain valid for `func` according to CUDA host-node callback rules until no graph execution can invoke the callback.
103    pub const unsafe fn new(func: HostFunction, user_data: *mut ()) -> Self {
104        Self { func, user_data }
105    }
106
107    /// Returns the native host callback wrapper.
108    #[must_use]
109    pub const fn function(self) -> HostFunction {
110        self.func
111    }
112
113    /// Returns the unowned user-data pointer passed to the callback.
114    #[must_use]
115    pub const fn user_data(self) -> *mut () {
116        self.user_data
117    }
118}
119
120/// Raw pointers, a byte count, and a direction for a one-dimensional graph copy.
121///
122/// This descriptor retains no allocation owners.
123#[derive(Debug, Clone, Copy)]
124pub struct MemoryCopy1DNodeParams {
125    dst: *mut (),
126    src: *const (),
127    count: usize,
128    kind: MemoryCopyKind,
129}
130
131impl MemoryCopyToSymbolNodeParams {
132    /// Creates symbol-copy node parameters from a raw source pointer.
133    ///
134    /// # Safety
135    ///
136    /// `symbol` must identify a valid CUDA symbol and `src` must be valid for reads of `count` bytes according to `kind` when CUDA evaluates the graph node using this value.
137    #[must_use]
138    pub const unsafe fn new(
139        symbol: *const (),
140        src: *const (),
141        count: usize,
142        offset: usize,
143        kind: MemoryCopyKind,
144    ) -> Self {
145        Self {
146            symbol,
147            src,
148            count,
149            offset,
150            kind,
151        }
152    }
153
154    /// Returns the CUDA destination symbol address.
155    #[must_use]
156    pub const fn symbol(self) -> *const () {
157        self.symbol
158    }
159
160    /// Returns the raw source address.
161    #[must_use]
162    pub const fn src(self) -> *const () {
163        self.src
164    }
165
166    /// Returns the number of bytes to copy.
167    #[must_use]
168    pub const fn count(self) -> usize {
169        self.count
170    }
171
172    /// Returns the destination byte offset within the symbol.
173    #[must_use]
174    pub const fn offset(self) -> usize {
175        self.offset
176    }
177
178    /// Returns the configured memory-copy direction.
179    #[must_use]
180    pub const fn kind(self) -> MemoryCopyKind {
181        self.kind
182    }
183}
184
185impl MemoryCopyFromSymbolNodeParams {
186    /// Creates symbol-copy node parameters from a raw destination pointer.
187    ///
188    /// # Safety
189    ///
190    /// `symbol` must identify a valid CUDA symbol and `dst` must be valid for writes of `count` bytes according to `kind` when CUDA evaluates the graph node using this value.
191    pub const unsafe fn new(
192        dst: *mut (),
193        symbol: *const (),
194        count: usize,
195        offset: usize,
196        kind: MemoryCopyKind,
197    ) -> Self {
198        Self {
199            dst,
200            symbol,
201            count,
202            offset,
203            kind,
204        }
205    }
206
207    /// Returns the raw destination address.
208    #[must_use]
209    pub const fn dst(self) -> *mut () {
210        self.dst
211    }
212
213    /// Returns the CUDA source symbol address.
214    #[must_use]
215    pub const fn symbol(self) -> *const () {
216        self.symbol
217    }
218
219    /// Returns the number of bytes to copy.
220    #[must_use]
221    pub const fn count(self) -> usize {
222        self.count
223    }
224
225    /// Returns the source byte offset within the symbol.
226    #[must_use]
227    pub const fn offset(self) -> usize {
228        self.offset
229    }
230
231    /// Returns the configured memory-copy direction.
232    #[must_use]
233    pub const fn kind(self) -> MemoryCopyKind {
234        self.kind
235    }
236}
237
238impl MemoryCopy1DNodeParams {
239    /// Creates one-dimensional memcpy node parameters from raw pointers.
240    ///
241    /// # Safety
242    ///
243    /// `dst` and `src` must be valid for `count` bytes according to `kind` when CUDA evaluates the graph node using this value.
244    pub const unsafe fn new(
245        dst: *mut (),
246        src: *const (),
247        count: usize,
248        kind: MemoryCopyKind,
249    ) -> Self {
250        Self {
251            dst,
252            src,
253            count,
254            kind,
255        }
256    }
257
258    /// Returns the raw destination address.
259    #[must_use]
260    pub const fn dst(self) -> *mut () {
261        self.dst
262    }
263
264    /// Returns the raw source address.
265    #[must_use]
266    pub const fn src(self) -> *const () {
267        self.src
268    }
269
270    /// Returns the number of bytes to copy.
271    #[must_use]
272    pub const fn count(self) -> usize {
273        self.count
274    }
275
276    /// Returns the configured memory-copy direction.
277    #[must_use]
278    pub const fn kind(self) -> MemoryCopyKind {
279        self.kind
280    }
281}
282
283impl MemoryCopy3DNodeParams {
284    /// Creates three-dimensional memcpy node parameters from raw array and pitched pointer operands.
285    ///
286    /// # Safety
287    ///
288    /// Every array and pointer operand selected by `kind` must remain valid for the region described by the positions, pitched pointers, and extent whenever CUDA evaluates a graph node using this value.
289    #[must_use]
290    pub const unsafe fn new(
291        src_array: Option<ArrayHandle>,
292        src_pos: Position,
293        src_ptr: PitchedPtr,
294        dst_array: Option<ArrayHandle>,
295        dst_pos: Position,
296        dst_ptr: PitchedPtr,
297        extent: Extent,
298        kind: MemoryCopyKind,
299    ) -> Self {
300        Self {
301            src_array,
302            src_pos,
303            src_ptr,
304            dst_array,
305            dst_pos,
306            dst_ptr,
307            extent,
308            kind,
309        }
310    }
311
312    /// Returns the source array handle, when the source is an array.
313    #[must_use]
314    pub const fn src_array(self) -> Option<ArrayHandle> {
315        self.src_array
316    }
317
318    /// Returns the source starting position.
319    #[must_use]
320    pub const fn src_pos(self) -> Position {
321        self.src_pos
322    }
323
324    /// Returns the pitched source parameters, used when no source array is selected.
325    #[must_use]
326    pub const fn src_ptr(self) -> PitchedPtr {
327        self.src_ptr
328    }
329
330    /// Returns the destination array handle, when the destination is an array.
331    #[must_use]
332    pub const fn dst_array(self) -> Option<ArrayHandle> {
333        self.dst_array
334    }
335
336    /// Returns the destination starting position.
337    #[must_use]
338    pub const fn dst_pos(self) -> Position {
339        self.dst_pos
340    }
341
342    /// Returns the pitched destination parameters, used when no destination array is selected.
343    #[must_use]
344    pub const fn dst_ptr(self) -> PitchedPtr {
345        self.dst_ptr
346    }
347
348    /// Returns the three-dimensional copy extent.
349    #[must_use]
350    pub const fn extent(self) -> Extent {
351        self.extent
352    }
353
354    /// Returns the configured memory-copy direction.
355    #[must_use]
356    pub const fn kind(self) -> MemoryCopyKind {
357        self.kind
358    }
359}
360
361impl From<PitchedPtr> for runtime::cudaPitchedPtr {
362    fn from(value: PitchedPtr) -> Self {
363        Self {
364            ptr: value.ptr().cast(),
365            pitch: value.pitch as _,
366            xsize: value.x_size as _,
367            ysize: value.y_size as _,
368        }
369    }
370}
371
372impl From<&MemoryCopy3DNodeParams> for runtime::cudaMemcpy3DParms {
373    fn from(value: &MemoryCopy3DNodeParams) -> Self {
374        Self {
375            srcArray: value
376                .src_array()
377                .map_or(ptr::null_mut(), ArrayHandle::as_raw),
378            srcPos: value.src_pos().into(),
379            srcPtr: value.src_ptr().into(),
380            dstArray: value
381                .dst_array()
382                .map_or(ptr::null_mut(), ArrayHandle::as_raw),
383            dstPos: value.dst_pos().into(),
384            dstPtr: value.dst_ptr().into(),
385            extent: value.extent().into(),
386            kind: value.kind().into(),
387        }
388    }
389}
390
391impl From<&HostNodeParams> for driver::CUDA_HOST_NODE_PARAMS {
392    fn from(value: &HostNodeParams) -> Self {
393        Self {
394            fn_: value.function().as_raw(),
395            userData: value.user_data().cast(),
396        }
397    }
398}