singe-cuda 0.1.0

Safe Rust wrappers for the CUDA driver and runtime APIs, including runtime compilation of CUDA C++ and NVTX annotations.
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
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! Green contexts provision subsets of GPU resources for work submission.

use std::{
    fmt::{self, Debug, Formatter},
    mem, ptr,
    result::Result as StdResult,
    sync::Arc,
};

use singe_core::{impl_enum_display, report_cleanup_failure};
use singe_cuda_sys::driver as sys;

use crate::{
    context::Context,
    device::Device,
    error::{Error, Result},
    event::Event,
    try_ffi,
    utility::to_usize,
};

/// Data for SM-related resources.
/// Properties are supplied by the driver.
#[derive(Clone)]
pub struct SmResource {
    raw: sys::CUdevResource,
    device: Device,
}

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

/// Input data for splitting SMs into structured groups.
#[derive(Debug, Clone, Copy)]
pub struct SmGroup {
    /// The amount of SMs available in this resource.
    pub sm_count: u32,
    /// The amount of co-scheduled SMs grouped together for locality purposes.
    pub coscheduled_sm_count: u32,
    /// When possible, combine co-scheduled groups into larger groups of this size.
    pub preferred_coscheduled_sm_count: u32,
    /// Locality domain to allocate the SM group from.
    pub locality_domain_id: Option<u32>,
    /// Allow unused SMs to be backfilled into this group.
    pub backfill: bool,
}

impl SmResource {
    /// Gets the SM resources available to the device.
    ///
    /// # Errors
    ///
    /// Returns an error if CUDA cannot query SM resources for `device`, including an invalid device ordinal, an uninitialized driver, or unsupported resource provisioning.
    pub fn query(device: Device) -> Result<Self> {
        let mut raw = sys::CUdevResource::default();
        // SAFETY: `raw` is a local the driver writes through an exclusive pointer, and the driver validates the device ordinal.
        unsafe {
            try_ffi!(sys::cuDeviceGetDevResource(
                device.id(),
                &raw mut raw,
                sys::CUdevResourceType::CU_DEV_RESOURCE_TYPE_SM
            ))?;
        }
        Ok(Self { raw, device })
    }

    /// Gets the SM resources available to an ordinary CUDA context.
    ///
    /// # Errors
    ///
    /// Returns an error if CUDA cannot query the context's SM resource descriptor, including an unsupported resource type or invalid native context.
    pub fn for_context(context: &Context) -> Result<Self> {
        let mut raw = sys::CUdevResource::default();
        // SAFETY: `context` keeps its native context alive for the call, and `raw` is a local output.
        unsafe {
            try_ffi!(sys::cuCtxGetDevResource(
                context.as_raw(),
                &raw mut raw,
                sys::CUdevResourceType::CU_DEV_RESOURCE_TYPE_SM
            ))?;
        }
        Ok(Self {
            raw,
            device: context.device(),
        })
    }

    /// Returns the number of streaming multiprocessors available in this resource.
    #[must_use]
    pub fn sm_count(&self) -> u32 {
        // SAFETY: the resource type is SM, so the `sm` field is the active union member.
        unsafe { self.raw.__bindgen_anon_1.sm.smCount }
    }

    /// Returns the locality domain the SMs must be located on, if the resource names one.
    /// With backfill enabled, SMs may be assigned from other locality domains.
    #[must_use]
    pub fn locality_domain_id(&self) -> Option<u32> {
        // SAFETY: the resource type is SM, so the `sm` field is the active union member.
        let sm = unsafe { self.raw.__bindgen_anon_1.sm };
        (sm.flags & sys::CUdevSmResourceGroup_flags::CU_DEV_SM_RESOURCE_GROUP_LOCALITY_DOMAIN_ID.0
            != 0)
            .then_some(sm.localityDomainId)
    }

    /// Returns the minimum number of streaming multiprocessors required to partition this resource.
    #[must_use]
    pub fn minimum_partition_size(&self) -> u32 {
        // SAFETY: the resource type is SM, so the `sm` field is the active union member.
        unsafe { self.raw.__bindgen_anon_1.sm.minSmPartitionSize }
    }

    /// Returns the number of streaming multiprocessors guaranteed to be co-scheduled on the same GPU processing cluster.
    ///
    /// The resource's SM count is a multiple of this value unless backfill is enabled.
    #[must_use]
    pub fn coscheduled_alignment(&self) -> u32 {
        // SAFETY: the resource type is SM, so the `sm` field is the active union member.
        unsafe { self.raw.__bindgen_anon_1.sm.smCoscheduledAlignment }
    }

    /// Splits an SM resource into groups with a minimum SM count.
    ///
    /// # Errors
    ///
    /// Returns an error if `minimum_count` or `maximum_groups` is zero, or CUDA cannot split the resource, such as for an unsupported partition size.
    /// Successful results include the groups actually produced and the remainder.
    pub fn split_by_count(
        &self,
        minimum_count: u32,
        maximum_groups: u32,
    ) -> Result<(Vec<Self>, Self)> {
        if minimum_count == 0 {
            return Err(Error::not_positive("minimum count"));
        }

        if maximum_groups == 0 {
            return Err(Error::not_positive("maximum groups"));
        }

        let mut groups = maximum_groups;
        let mut results = vec![sys::CUdevResource::default(); to_usize(groups, "maximum groups")?];
        let mut remainder = sys::CUdevResource::default();
        // SAFETY: `results` holds `groups` descriptors for the driver to write, `self.raw` is an SM descriptor the driver produced, and the other outputs are locals.
        unsafe {
            try_ffi!(sys::cuDevSmResourceSplitByCount(
                results.as_mut_ptr(),
                &raw mut groups,
                &raw const self.raw,
                &raw mut remainder,
                0,
                minimum_count
            ))?;
        }

        results.truncate(to_usize(groups, "split group count")?);
        Ok((
            results
                .into_iter()
                .map(|raw| Self {
                    raw,
                    device: self.device,
                })
                .collect(),
            Self {
                raw: remainder,
                device: self.device,
            },
        ))
    }

    /// Splits an SM resource into structured groups and an unstructured remainder.
    ///
    /// # Errors
    ///
    /// Returns an error if the group count does not fit a `u32`, or CUDA cannot split the resource, such as for invalid group constraints or insufficient resources.
    pub fn split(&self, groups: &[SmGroup]) -> Result<(Vec<Self>, Self)> {
        let count = groups
            .len()
            .try_into()
            .map_err(|_| Error::out_of_range("group count"))?;
        let mut parameters = groups
            .iter()
            .map(|g| sys::CU_DEV_SM_RESOURCE_GROUP_PARAMS {
                smCount: g.sm_count,
                coscheduledSmCount: g.coscheduled_sm_count,
                preferredCoscheduledSmCount: g.preferred_coscheduled_sm_count,
                flags: if g.backfill {
                    sys::CUdevSmResourceGroup_flags::CU_DEV_SM_RESOURCE_GROUP_BACKFILL.0
                } else {
                    0
                } | if g.locality_domain_id.is_some() {
                    sys::CUdevSmResourceGroup_flags::CU_DEV_SM_RESOURCE_GROUP_LOCALITY_DOMAIN_ID.0
                } else {
                    0
                },
                localityDomainId: g.locality_domain_id.unwrap_or(0),
                ..Default::default()
            })
            .collect::<Vec<_>>();
        let mut results = vec![sys::CUdevResource::default(); groups.len()];
        let mut remainder = sys::CUdevResource::default();
        // SAFETY: `results` and `parameters` each hold `count` elements, `self.raw` is an SM descriptor the driver produced, and `remainder` is a local output.
        unsafe {
            try_ffi!(sys::cuDevSmResourceSplit(
                results.as_mut_ptr(),
                count,
                &raw const self.raw,
                &raw mut remainder,
                0,
                parameters.as_mut_ptr()
            ))?;
        }
        Ok((
            results
                .into_iter()
                .map(|raw| Self {
                    raw,
                    device: self.device,
                })
                .collect(),
            Self {
                raw: remainder,
                device: self.device,
            },
        ))
    }
}

/// Sharing scope for workqueues.
#[derive(Debug, Clone, Copy)]
#[repr(u32)]
#[non_exhaustive]
pub enum WorkqueueSharingScope {
    /// Use all shared workqueue resources across all contexts.
    /// Default driver behaviour.
    DeviceContext = sys::CUdevWorkqueueConfigScope::CU_WORKQUEUE_SCOPE_DEVICE_CTX.0,
    /// When possible, use non-overlapping workqueue resources with other balanced green contexts.
    BalancedGreenContext = sys::CUdevWorkqueueConfigScope::CU_WORKQUEUE_SCOPE_GREEN_CTX_BALANCED.0,
}

/// Data for workqueue configuration related resources.
#[derive(Clone)]
pub struct WorkqueueConfig {
    raw: sys::CUdevResource,
    device: Device,
}

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

impl WorkqueueConfig {
    /// Gets the workqueue configuration resources available to the device.
    ///
    /// # Errors
    ///
    /// Returns an error if CUDA cannot query workqueue configuration resources for `device`, including an invalid device ordinal, an uninitialized driver, or unsupported resource provisioning.
    pub fn query(device: Device) -> Result<Self> {
        let mut raw = sys::CUdevResource::default();
        // SAFETY: `raw` is a local the driver writes through an exclusive pointer, and the driver validates the device ordinal.
        unsafe {
            try_ffi!(sys::cuDeviceGetDevResource(
                device.id(),
                &raw mut raw,
                sys::CUdevResourceType::CU_DEV_RESOURCE_TYPE_WORKQUEUE_CONFIG
            ))?;
        }
        Ok(Self { raw, device })
    }

    /// Sets the expected maximum number of concurrent stream-ordered workloads.
    #[must_use]
    pub fn with_concurrency_limit(mut self, limit: u32) -> Self {
        self.raw.__bindgen_anon_1.wqConfig.wqConcurrencyLimit = limit;
        self
    }

    /// Sets the sharing scope for the workqueue resources.
    #[must_use]
    pub fn with_sharing_scope(mut self, scope: WorkqueueSharingScope) -> Self {
        self.raw.__bindgen_anon_1.wqConfig.sharingScope = match scope {
            WorkqueueSharingScope::DeviceContext => {
                sys::CUdevWorkqueueConfigScope::CU_WORKQUEUE_SCOPE_DEVICE_CTX
            }
            WorkqueueSharingScope::BalancedGreenContext => {
                sys::CUdevWorkqueueConfigScope::CU_WORKQUEUE_SCOPE_GREEN_CTX_BALANCED
            }
        };
        self
    }
}

/// A green context with a specified set of resources.
///
/// A context handle obtained from [`Self::context_handle`] can be current to only one thread at a time.
#[derive(Debug)]
pub struct GreenContext {
    handle: sys::CUgreenCtx,
    device: Device,
}

/// Raw ownership of a green context and its device.
///
/// Destroy `handle` with CUDA, or re-adopt the value with [`GreenContext::from_raw`].
#[derive(Debug)]
pub struct RawGreenContext {
    /// Native green-context handle.
    pub handle: sys::CUgreenCtx,
    /// Device whose resources the green context provisions.
    pub device: Device,
}

// SAFETY: the wrapper never makes the green context current, and the driver calls it makes on the handle are thread-safe from any thread.
unsafe impl Send for GreenContext {}

// SAFETY: shared methods only pass the handle to thread-safe driver calls, and destruction needs the last owner.
unsafe impl Sync for GreenContext {}

impl GreenContext {
    /// Creates a green context from SM resources and an optional workqueue configuration, retaining the device primary context.
    ///
    /// # Errors
    ///
    /// Returns an error if `resources` is empty, the resources or the workqueue refer to different devices, the descriptor count does not fit a `u32`, or CUDA cannot generate the descriptor or create the green context.
    pub fn create(
        resources: &[SmResource],
        workqueue: Option<WorkqueueConfig>,
        default_stream: bool,
    ) -> Result<Arc<Self>> {
        let device = resources
            .first()
            .ok_or_else(|| Error::empty("sm resources"))?
            .device;
        if resources.iter().any(|r| r.device != device) {
            return Err(Error::out_of_range("sm resource device"));
        }

        let mut raw = resources.iter().map(|r| r.raw).collect::<Vec<_>>();
        if let Some(workqueue) = workqueue {
            if workqueue.device != device {
                return Err(Error::out_of_range("workqueue device"));
            }

            raw.push(workqueue.raw);
        }

        let mut descriptor = ptr::null_mut();
        // SAFETY: `raw` holds the counted descriptors the driver produced for one device, and `descriptor` is a local output.
        unsafe {
            try_ffi!(sys::cuDevResourceGenerateDesc(
                &raw mut descriptor,
                raw.as_mut_ptr(),
                raw.len()
                    .try_into()
                    .map_err(|_| Error::out_of_range("resource descriptor count"))?
            ))?;
        }

        let mut handle = ptr::null_mut();
        let flags = if default_stream {
            sys::CUgreenCtxCreate_flags::CU_GREEN_CTX_DEFAULT_STREAM.0
        } else {
            0
        };
        // SAFETY: `descriptor` was just generated for `device`'s resources, and `handle` is a local output.
        unsafe {
            try_ffi!(sys::cuGreenCtxCreate(
                &raw mut handle,
                descriptor,
                device.id(),
                flags
            ))?;
        }

        if handle.is_null() {
            return Err(Error::NullHandle);
        }
        Ok(Arc::new(Self { handle, device }))
    }

    /// Returns the borrowed native green-context handle without transferring ownership.
    #[must_use]
    pub fn as_raw(&self) -> sys::CUgreenCtx {
        self.handle
    }

    /// Returns the device whose resources this green context provisions.
    #[must_use]
    pub fn device(&self) -> Device {
        self.device
    }

    /// Takes ownership of a green context.
    ///
    /// # Safety
    ///
    /// `raw.handle` must be live, uniquely owned, and associated with `raw.device`.
    ///
    /// # Errors
    ///
    /// Returns an error if `raw.handle` is null.
    #[expect(
        clippy::needless_pass_by_value,
        reason = "taking the raw owner by value transfers ownership of its handle"
    )]
    pub unsafe fn from_raw(raw: RawGreenContext) -> Result<Arc<Self>> {
        if raw.handle.is_null() {
            return Err(Error::NullHandle);
        }
        Ok(Arc::new(Self {
            handle: raw.handle,
            device: raw.device,
        }))
    }

    /// Transfers ownership of the green context and its device to the caller.
    ///
    /// # Errors
    ///
    /// Returns an error holding the green context unchanged if another owner, such as a [`GreenStream`], still retains it.
    pub fn into_raw(this: Arc<Self>) -> StdResult<RawGreenContext, Arc<Self>> {
        let mut owner = Arc::try_unwrap(this)?;
        Ok(RawGreenContext {
            handle: mem::replace(&mut owner.handle, ptr::null_mut()),
            device: owner.device,
        })
    }

    /// Returns the unique ID associated with this green context.
    ///
    /// # Errors
    ///
    /// Returns an error if CUDA cannot query the green context's ID, including an invalid native handle or unavailable driver support.
    pub fn id(&self) -> Result<u64> {
        let mut id = 0;
        // SAFETY: `self` owns the live green context, and `id` is a local output.
        unsafe {
            try_ffi!(sys::cuGreenCtxGetId(self.handle, &raw mut id))?;
        }
        Ok(id)
    }

    /// Returns a context handle representing this green context.
    ///
    /// The returned handle is borrowed and must not be destroyed or used after this owner is dropped.
    /// It can be current to only one thread at a time.
    ///
    /// # Errors
    ///
    /// Returns an error if CUDA cannot convert this green-context handle to its ordinary context representation.
    /// The returned handle does not acquire an independent ownership reference.
    pub fn context_handle(&self) -> Result<sys::CUcontext> {
        let mut context = ptr::null_mut();
        // SAFETY: `self` owns the live green context, and `context` is a local output.
        unsafe {
            try_ffi!(sys::cuCtxFromGreenCtx(&raw mut context, self.handle))?;
        }
        Ok(context)
    }

    /// Gets the SM resources provisioned for this green context.
    ///
    /// # Errors
    ///
    /// Returns an error if CUDA cannot query this green context's provisioned SM resources.
    pub fn resources(&self) -> Result<SmResource> {
        let mut raw = sys::CUdevResource::default();
        // SAFETY: `self` owns the live green context, and `raw` is a local output.
        unsafe {
            try_ffi!(sys::cuGreenCtxGetDevResource(
                self.handle,
                &raw mut raw,
                sys::CUdevResourceType::CU_DEV_RESOURCE_TYPE_SM
            ))?;
        }
        Ok(SmResource {
            raw,
            device: self.device,
        })
    }

    /// Records an event in this green context.
    ///
    /// # Errors
    ///
    /// Returns an error if CUDA rejects the event recording, including invalid or incompatible event and green-context handles or prior asynchronous failures.
    pub fn record_event(&self, event: &Event) -> Result<()> {
        // SAFETY: `self` owns the live green context, and `event` keeps its native event alive for the call.
        unsafe { try_ffi!(sys::cuGreenCtxRecordEvent(self.handle, event.as_raw())) }
    }

    /// Makes all future work submitted to this green context wait for the event.
    ///
    /// # Errors
    ///
    /// Returns an error if CUDA cannot order future green-context work after the event, including invalid handles, unsupported ordering, or prior asynchronous failures.
    pub fn wait_event(&self, event: &Event) -> Result<()> {
        // SAFETY: `self` owns the live green context, and `event` keeps its native event alive for the call.
        unsafe { try_ffi!(sys::cuGreenCtxWaitEvent(self.handle, event.as_raw())) }
    }

    /// Creates a nonblocking stream for use in this green context.
    ///
    /// # Errors
    ///
    /// Returns an error if CUDA cannot create a nonblocking stream for this green context with the requested priority.
    /// The successful stream retains the green context.
    pub fn create_stream(self: &Arc<Self>, priority: i32) -> Result<GreenStream> {
        let mut handle = ptr::null_mut();
        // SAFETY: `self` owns the live green context, which the returned stream retains, and `handle` is a local output.
        unsafe {
            try_ffi!(sys::cuGreenCtxStreamCreate(
                &raw mut handle,
                self.handle,
                sys::CUstream_flags::CU_STREAM_NON_BLOCKING.0,
                priority
            ))?;
        }
        Ok(GreenStream {
            handle,
            context: Arc::clone(self),
        })
    }
}

impl Drop for GreenContext {
    fn drop(&mut self) {
        // SAFETY: a non-null handle is owned by this wrapper alone, and every stream that retained it has dropped.
        unsafe {
            if !self.handle.is_null()
                && let Err(error) = try_ffi!(sys::cuGreenCtxDestroy(self.handle))
            {
                report_cleanup_failure("destroy", "cuda green context", &error);
            }
        }
    }
}

/// A stream retaining the green context that provisioned its resources.
#[derive(Debug)]
pub struct GreenStream {
    handle: sys::CUstream,
    context: Arc<GreenContext>,
}

// SAFETY: a `CUstream` may be used and destroyed from any thread, and the retained green context is `Send + Sync`.
unsafe impl Send for GreenStream {}

// SAFETY: shared methods only pass the handle to thread-safe driver calls, and the wrapper has no interior mutability.
unsafe impl Sync for GreenStream {}

impl GreenStream {
    /// Returns the borrowed native green-context stream handle.
    #[must_use]
    pub fn as_raw(&self) -> sys::CUstream {
        self.handle
    }

    /// Returns the green context retained by this stream.
    #[must_use]
    pub fn context(&self) -> &Arc<GreenContext> {
        &self.context
    }

    /// Waits until all work in this stream has completed.
    ///
    /// # Errors
    ///
    /// Returns an error if CUDA cannot synchronize the stream or reports an error from preceding asynchronous work.
    pub fn synchronize(&self) -> Result<()> {
        // SAFETY: `self` owns the live stream, whose green context it retains.
        unsafe { try_ffi!(sys::cuStreamSynchronize(self.handle)) }
    }
}

impl Drop for GreenStream {
    fn drop(&mut self) {
        // SAFETY: this wrapper owns the stream, and `self.context` keeps its green context alive until after the destroy.
        unsafe {
            if let Err(error) = try_ffi!(sys::cuStreamDestroy(self.handle)) {
                report_cleanup_failure("destroy", "cuda green stream", &error);
            }
        }
    }
}

impl_enum_display!(WorkqueueSharingScope, {
    WorkqueueSharingScope::DeviceContext => "CU_WORKQUEUE_SCOPE_DEVICE_CTX",
    WorkqueueSharingScope::BalancedGreenContext => "CU_WORKQUEUE_SCOPE_GREEN_CTX_BALANCED",
});