baracuda-driver 0.0.1-alpha.13

Safe Rust wrappers for the CUDA Driver API (devices, contexts, streams, events, memory, kernels, graphs).
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
//! CUDA arrays + texture / surface objects.
//!
//! A [`Array`] is an opaque on-device layout optimized for texture fetches
//! and surface loads/stores. Host↔Array copies go through the 2-D memcpy
//! path (see [`crate::memcpy2d`]), with `CUmemorytype::ARRAY` on whichever
//! side the array sits on.
//!
//! Texture and surface objects are created *from* an array (or a pitched
//! device pointer, for linear textures). This module exposes the modern
//! "object" API (CUDA 5+); the legacy reference-based API is not wrapped.

use core::ffi::c_void;
use core::mem::size_of;
use std::sync::Arc;

use baracuda_cuda_sys::types::{
    CUarray_format, CUDA_ARRAY_DESCRIPTOR, CUDA_RESOURCE_DESC, CUDA_TEXTURE_DESC,
};
use baracuda_cuda_sys::{driver, CUarray, CUsurfObject, CUtexObject};
use baracuda_types::DeviceRepr;

use crate::context::Context;
use crate::error::{check, Result};

/// A 2-D CUDA array. Element format is chosen at creation; channels are
/// typically 1, 2, or 4.
pub struct Array {
    inner: Arc<ArrayInner>,
}

struct ArrayInner {
    handle: CUarray,
    width: usize,
    height: usize,
    format: u32,
    num_channels: u32,
    #[allow(dead_code)]
    context: Context,
}

unsafe impl Send for ArrayInner {}
unsafe impl Sync for ArrayInner {}

impl core::fmt::Debug for ArrayInner {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Array")
            .field("width", &self.width)
            .field("height", &self.height)
            .field("format", &self.format)
            .field("channels", &self.num_channels)
            .finish_non_exhaustive()
    }
}

impl core::fmt::Debug for Array {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.inner.fmt(f)
    }
}

impl Clone for Array {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

/// Element format shorthand for common single-channel arrays. Build a
/// multi-channel descriptor by passing a [`CUarray_format`] constant and a
/// channel count directly to [`Array::new`].
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ArrayFormat {
    U8,
    U16,
    U32,
    I8,
    I16,
    I32,
    F16,
    F32,
}

impl ArrayFormat {
    #[inline]
    fn raw(self) -> u32 {
        match self {
            ArrayFormat::U8 => CUarray_format::UNSIGNED_INT8,
            ArrayFormat::U16 => CUarray_format::UNSIGNED_INT16,
            ArrayFormat::U32 => CUarray_format::UNSIGNED_INT32,
            ArrayFormat::I8 => CUarray_format::SIGNED_INT8,
            ArrayFormat::I16 => CUarray_format::SIGNED_INT16,
            ArrayFormat::I32 => CUarray_format::SIGNED_INT32,
            ArrayFormat::F16 => CUarray_format::HALF,
            ArrayFormat::F32 => CUarray_format::FLOAT,
        }
    }

    /// Width of a single texel (one channel) in bytes.
    #[inline]
    pub fn bytes_per_channel(self) -> usize {
        match self {
            ArrayFormat::U8 | ArrayFormat::I8 => 1,
            ArrayFormat::U16 | ArrayFormat::I16 | ArrayFormat::F16 => 2,
            ArrayFormat::U32 | ArrayFormat::I32 | ArrayFormat::F32 => 4,
        }
    }
}

impl Array {
    /// Allocate a `width Ă— height` 2-D array with `num_channels` elements of
    /// `format` per texel. Set `height = 0` for a 1-D array.
    pub fn new(
        context: &Context,
        width: usize,
        height: usize,
        format: ArrayFormat,
        num_channels: u32,
    ) -> Result<Self> {
        assert!(
            matches!(num_channels, 1 | 2 | 4),
            "CUDA arrays require 1, 2, or 4 channels (got {num_channels})",
        );
        context.set_current()?;
        let d = driver()?;
        let cu = d.cu_array_create()?;
        let desc = CUDA_ARRAY_DESCRIPTOR {
            width,
            height,
            format: format.raw(),
            num_channels,
        };
        let mut handle: CUarray = core::ptr::null_mut();
        check(unsafe { cu(&mut handle, &desc) })?;
        Ok(Self {
            inner: Arc::new(ArrayInner {
                handle,
                width,
                height,
                format: format.raw(),
                num_channels,
                context: context.clone(),
            }),
        })
    }

    /// Raw `CUarray`. Use with care.
    #[inline]
    pub fn as_raw(&self) -> CUarray {
        self.inner.handle
    }
    #[inline]
    pub fn width(&self) -> usize {
        self.inner.width
    }
    #[inline]
    pub fn height(&self) -> usize {
        self.inner.height
    }
    #[inline]
    pub fn num_channels(&self) -> u32 {
        self.inner.num_channels
    }
    /// Element width in bytes (channel size Ă— channel count).
    pub fn bytes_per_element(&self) -> usize {
        let ch_size = match self.inner.format {
            CUarray_format::UNSIGNED_INT8 | CUarray_format::SIGNED_INT8 => 1,
            CUarray_format::UNSIGNED_INT16
            | CUarray_format::SIGNED_INT16
            | CUarray_format::HALF => 2,
            _ => 4,
        };
        ch_size * (self.inner.num_channels as usize)
    }

    /// Synchronous host→array 2-D copy. `host` must contain exactly
    /// `width Ă— height` elements of type `T`; `T` size must match
    /// `self.bytes_per_element()`.
    pub fn copy_from_host<T: DeviceRepr>(&self, host: &[T]) -> Result<()> {
        assert_eq!(
            size_of::<T>(),
            self.bytes_per_element(),
            "host element type must match array texel size",
        );
        let h = self.inner.height.max(1);
        assert_eq!(host.len(), self.inner.width * h);
        let d = driver()?;
        let cu = d.cu_memcpy_2d()?;
        let p = baracuda_cuda_sys::types::CUDA_MEMCPY2D {
            src_memory_type: baracuda_cuda_sys::types::CUmemorytype::HOST,
            src_host: host.as_ptr() as *const c_void,
            src_pitch: self.inner.width * self.bytes_per_element(),
            dst_memory_type: baracuda_cuda_sys::types::CUmemorytype::ARRAY,
            dst_array: self.inner.handle,
            width_in_bytes: self.inner.width * self.bytes_per_element(),
            height: h,
            ..Default::default()
        };
        check(unsafe { cu(&p) })
    }

    /// Query this array's descriptor back from CUDA. Useful for arrays
    /// you received from an external source and don't have shape info for.
    pub fn descriptor(&self) -> Result<CUDA_ARRAY_DESCRIPTOR> {
        let d = driver()?;
        let cu = d.cu_array_get_descriptor()?;
        let mut desc = CUDA_ARRAY_DESCRIPTOR::default();
        check(unsafe { cu(&mut desc, self.inner.handle) })?;
        Ok(desc)
    }

    /// Query the array's memory-allocation size + alignment requirements
    /// on `device`. Useful when backing an array with a VMM allocation.
    pub fn memory_requirements(
        &self,
        device: &crate::Device,
    ) -> Result<baracuda_cuda_sys::types::CUDA_ARRAY_MEMORY_REQUIREMENTS> {
        let d = driver()?;
        let cu = d.cu_array_get_memory_requirements()?;
        let mut req = baracuda_cuda_sys::types::CUDA_ARRAY_MEMORY_REQUIREMENTS::default();
        check(unsafe { cu(&mut req, self.inner.handle, device.as_raw()) })?;
        Ok(req)
    }

    /// Query the array's sparse-tile properties. Meaningful on sparse /
    /// tiled arrays created with the `SPARSE` flag.
    pub fn sparse_properties(
        &self,
    ) -> Result<baracuda_cuda_sys::types::CUDA_ARRAY_SPARSE_PROPERTIES> {
        let d = driver()?;
        let cu = d.cu_array_get_sparse_properties()?;
        let mut sp = baracuda_cuda_sys::types::CUDA_ARRAY_SPARSE_PROPERTIES::default();
        check(unsafe { cu(&mut sp, self.inner.handle) })?;
        Ok(sp)
    }

    /// Return the raw `CUarray` handle of plane `plane_idx` of a
    /// multi-planar array (YUV / NV12). The plane is owned by `self` —
    /// the raw handle must NOT be passed to `cuArrayDestroy`. Use
    /// together with [`Array::descriptor_of_raw`] if you need shape info.
    pub fn plane_raw(&self, plane_idx: u32) -> Result<CUarray> {
        let d = driver()?;
        let cu = d.cu_array_get_plane()?;
        let mut out: CUarray = core::ptr::null_mut();
        check(unsafe { cu(&mut out, self.inner.handle, plane_idx) })?;
        Ok(out)
    }

    /// Helper: query the `CUDA_ARRAY_DESCRIPTOR` of a raw array handle
    /// (e.g. a plane returned by [`Array::plane_raw`]).
    ///
    /// # Safety
    ///
    /// `handle` must be a live `CUarray`.
    pub unsafe fn descriptor_of_raw(handle: CUarray) -> Result<CUDA_ARRAY_DESCRIPTOR> { unsafe {
        let d = driver()?;
        let cu = d.cu_array_get_descriptor()?;
        let mut desc = CUDA_ARRAY_DESCRIPTOR::default();
        check(cu(&mut desc, handle))?;
        Ok(desc)
    }}

    /// Synchronous array→host 2-D copy.
    pub fn copy_to_host<T: DeviceRepr>(&self, host: &mut [T]) -> Result<()> {
        assert_eq!(
            size_of::<T>(),
            self.bytes_per_element(),
            "host element type must match array texel size",
        );
        let h = self.inner.height.max(1);
        assert_eq!(host.len(), self.inner.width * h);
        let d = driver()?;
        let cu = d.cu_memcpy_2d()?;
        let p = baracuda_cuda_sys::types::CUDA_MEMCPY2D {
            src_memory_type: baracuda_cuda_sys::types::CUmemorytype::ARRAY,
            src_array: self.inner.handle,
            dst_memory_type: baracuda_cuda_sys::types::CUmemorytype::HOST,
            dst_host: host.as_mut_ptr() as *mut c_void,
            dst_pitch: self.inner.width * self.bytes_per_element(),
            width_in_bytes: self.inner.width * self.bytes_per_element(),
            height: h,
            ..Default::default()
        };
        check(unsafe { cu(&p) })
    }
}

impl Drop for ArrayInner {
    fn drop(&mut self) {
        if self.handle.is_null() {
            return;
        }
        if let Ok(d) = driver() {
            if let Ok(cu) = d.cu_array_destroy() {
                let _ = unsafe { cu(self.handle) };
            }
        }
    }
}

/// A texture object — a read-only, filtered view onto a CUDA array.
pub struct TextureObject {
    handle: CUtexObject,
    _array: Array,
}

impl core::fmt::Debug for TextureObject {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("TextureObject")
            .field("handle", &self.handle)
            .finish_non_exhaustive()
    }
}

unsafe impl Send for TextureObject {}
unsafe impl Sync for TextureObject {}

/// Configuration for [`TextureObject::new`].
#[derive(Copy, Clone, Debug)]
pub struct TextureDesc {
    pub address_mode: [TextureAddressMode; 3],
    pub filter_mode: TextureFilterMode,
    pub read_normalized: bool,
    pub normalized_coords: bool,
}

impl Default for TextureDesc {
    fn default() -> Self {
        Self {
            address_mode: [TextureAddressMode::Clamp; 3],
            filter_mode: TextureFilterMode::Point,
            read_normalized: false,
            normalized_coords: false,
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum TextureAddressMode {
    Wrap,
    Clamp,
    Mirror,
    Border,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum TextureFilterMode {
    Point,
    Linear,
}

impl TextureAddressMode {
    fn raw(self) -> u32 {
        use baracuda_cuda_sys::types::CUaddress_mode;
        match self {
            TextureAddressMode::Wrap => CUaddress_mode::WRAP,
            TextureAddressMode::Clamp => CUaddress_mode::CLAMP,
            TextureAddressMode::Mirror => CUaddress_mode::MIRROR,
            TextureAddressMode::Border => CUaddress_mode::BORDER,
        }
    }
}

impl TextureFilterMode {
    fn raw(self) -> u32 {
        use baracuda_cuda_sys::types::CUfilter_mode;
        match self {
            TextureFilterMode::Point => CUfilter_mode::POINT,
            TextureFilterMode::Linear => CUfilter_mode::LINEAR,
        }
    }
}

impl TextureObject {
    /// Create a texture object that reads from `array`. Uses point filtering
    /// and clamp addressing by default; override with [`TextureObject::with_desc`].
    pub fn new(array: &Array) -> Result<Self> {
        Self::with_desc(array, TextureDesc::default())
    }

    pub fn with_desc(array: &Array, desc: TextureDesc) -> Result<Self> {
        let d = driver()?;
        let cu = d.cu_tex_object_create()?;
        let res_desc = CUDA_RESOURCE_DESC::from_array(array.as_raw());
        let mut flags: core::ffi::c_uint = 0;
        const CU_TRSF_READ_AS_INTEGER: core::ffi::c_uint = 0x01;
        const CU_TRSF_NORMALIZED_COORDINATES: core::ffi::c_uint = 0x02;
        if !desc.read_normalized {
            flags |= CU_TRSF_READ_AS_INTEGER;
        }
        if desc.normalized_coords {
            flags |= CU_TRSF_NORMALIZED_COORDINATES;
        }
        let tex_desc = CUDA_TEXTURE_DESC {
            address_mode: [
                desc.address_mode[0].raw(),
                desc.address_mode[1].raw(),
                desc.address_mode[2].raw(),
            ],
            filter_mode: desc.filter_mode.raw(),
            flags,
            ..Default::default()
        };
        let mut handle: CUtexObject = 0;
        check(unsafe { cu(&mut handle, &res_desc, &tex_desc, core::ptr::null()) })?;
        Ok(Self {
            handle,
            _array: array.clone(),
        })
    }

    #[inline]
    pub fn as_raw(&self) -> CUtexObject {
        self.handle
    }
}

impl Drop for TextureObject {
    fn drop(&mut self) {
        if self.handle == 0 {
            return;
        }
        if let Ok(d) = driver() {
            if let Ok(cu) = d.cu_tex_object_destroy() {
                let _ = unsafe { cu(self.handle) };
            }
        }
    }
}

/// A surface object — a read/write view onto a CUDA array.
pub struct SurfaceObject {
    handle: CUsurfObject,
    _array: Array,
}

impl core::fmt::Debug for SurfaceObject {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("SurfaceObject")
            .field("handle", &self.handle)
            .finish_non_exhaustive()
    }
}

unsafe impl Send for SurfaceObject {}
unsafe impl Sync for SurfaceObject {}

impl SurfaceObject {
    pub fn new(array: &Array) -> Result<Self> {
        let d = driver()?;
        let cu = d.cu_surf_object_create()?;
        let res_desc = CUDA_RESOURCE_DESC::from_array(array.as_raw());
        let mut handle: CUsurfObject = 0;
        check(unsafe { cu(&mut handle, &res_desc) })?;
        Ok(Self {
            handle,
            _array: array.clone(),
        })
    }

    #[inline]
    pub fn as_raw(&self) -> CUsurfObject {
        self.handle
    }
}

impl Drop for SurfaceObject {
    fn drop(&mut self) {
        if self.handle == 0 {
            return;
        }
        if let Ok(d) = driver() {
            if let Ok(cu) = d.cu_surf_object_destroy() {
                let _ = unsafe { cu(self.handle) };
            }
        }
    }
}