1use 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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub struct PitchedPtr {
18 ptr: *mut (),
19 pub pitch: usize,
21 pub x_size: usize,
23 pub y_size: usize,
25}
26
27impl PitchedPtr {
28 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 #[must_use]
44 pub const fn ptr(self) -> *mut () {
45 self.ptr
46 }
47}
48
49#[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#[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#[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#[derive(Debug, Clone, Copy)]
92pub struct HostNodeParams {
93 func: HostFunction,
94 user_data: *mut (),
95}
96
97impl HostNodeParams {
98 pub const unsafe fn new(func: HostFunction, user_data: *mut ()) -> Self {
104 Self { func, user_data }
105 }
106
107 #[must_use]
109 pub const fn function(self) -> HostFunction {
110 self.func
111 }
112
113 #[must_use]
115 pub const fn user_data(self) -> *mut () {
116 self.user_data
117 }
118}
119
120#[derive(Debug, Clone, Copy)]
124pub struct MemoryCopy1DNodeParams {
125 dst: *mut (),
126 src: *const (),
127 count: usize,
128 kind: MemoryCopyKind,
129}
130
131impl MemoryCopyToSymbolNodeParams {
132 #[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 #[must_use]
156 pub const fn symbol(self) -> *const () {
157 self.symbol
158 }
159
160 #[must_use]
162 pub const fn src(self) -> *const () {
163 self.src
164 }
165
166 #[must_use]
168 pub const fn count(self) -> usize {
169 self.count
170 }
171
172 #[must_use]
174 pub const fn offset(self) -> usize {
175 self.offset
176 }
177
178 #[must_use]
180 pub const fn kind(self) -> MemoryCopyKind {
181 self.kind
182 }
183}
184
185impl MemoryCopyFromSymbolNodeParams {
186 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 #[must_use]
209 pub const fn dst(self) -> *mut () {
210 self.dst
211 }
212
213 #[must_use]
215 pub const fn symbol(self) -> *const () {
216 self.symbol
217 }
218
219 #[must_use]
221 pub const fn count(self) -> usize {
222 self.count
223 }
224
225 #[must_use]
227 pub const fn offset(self) -> usize {
228 self.offset
229 }
230
231 #[must_use]
233 pub const fn kind(self) -> MemoryCopyKind {
234 self.kind
235 }
236}
237
238impl MemoryCopy1DNodeParams {
239 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 #[must_use]
260 pub const fn dst(self) -> *mut () {
261 self.dst
262 }
263
264 #[must_use]
266 pub const fn src(self) -> *const () {
267 self.src
268 }
269
270 #[must_use]
272 pub const fn count(self) -> usize {
273 self.count
274 }
275
276 #[must_use]
278 pub const fn kind(self) -> MemoryCopyKind {
279 self.kind
280 }
281}
282
283impl MemoryCopy3DNodeParams {
284 #[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 #[must_use]
314 pub const fn src_array(self) -> Option<ArrayHandle> {
315 self.src_array
316 }
317
318 #[must_use]
320 pub const fn src_pos(self) -> Position {
321 self.src_pos
322 }
323
324 #[must_use]
326 pub const fn src_ptr(self) -> PitchedPtr {
327 self.src_ptr
328 }
329
330 #[must_use]
332 pub const fn dst_array(self) -> Option<ArrayHandle> {
333 self.dst_array
334 }
335
336 #[must_use]
338 pub const fn dst_pos(self) -> Position {
339 self.dst_pos
340 }
341
342 #[must_use]
344 pub const fn dst_ptr(self) -> PitchedPtr {
345 self.dst_ptr
346 }
347
348 #[must_use]
350 pub const fn extent(self) -> Extent {
351 self.extent
352 }
353
354 #[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}