pub struct PrimaryContext { /* private fields */ }Expand description
RAII wrapper for a CUDA primary context.
A primary context is the per-device, reference-counted context managed
by the CUDA driver. Unlike a regular Context,
the primary context is shared among all callers that retain it on the
same device.
PrimaryContext::retain increments the driver’s reference count and
PrimaryContext::release decrements it. When the count reaches zero,
the driver destroys the context.
Implementations§
Source§impl PrimaryContext
impl PrimaryContext
Sourcepub fn retain(device: &Device) -> CudaResult<Self>
pub fn retain(device: &Device) -> CudaResult<Self>
Retains the primary context on the given device.
If the primary context does not yet exist, the driver creates it.
Each call to retain increments an internal reference count. The
context remains alive until all retainers call release.
§Errors
Returns an error if the driver cannot be loaded or the retain fails (e.g., invalid device).
Sourcepub fn release(self) -> CudaResult<()>
pub fn release(self) -> CudaResult<()>
Releases this primary context, decrementing the driver’s reference count.
When the last retainer releases, the driver destroys the context and
frees its resources. After calling this method the PrimaryContext
is consumed and cannot be used further.
§Errors
Returns an error if the release call fails.
Sourcepub fn set_flags(&self, flags: u32) -> CudaResult<()>
pub fn set_flags(&self, flags: u32) -> CudaResult<()>
Sets the flags for the primary context.
The flags control scheduling behaviour (e.g., spin, yield, blocking).
See context::flags for available values.
This must be called before the primary context is made active
(i.e., before any retain or before all retainers have released).
If the primary context is already active, this returns
CudaError::PrimaryContextActive.
§Errors
Returns an error if the flags cannot be set.
Sourcepub fn get_state(&self) -> CudaResult<(bool, u32)>
pub fn get_state(&self) -> CudaResult<(bool, u32)>
Returns the current state of the primary context.
Returns (active, flags) where:
activeistrueif the context is currently retained by at least one caller.flagsare the scheduling flags currently in effect.
§Errors
Returns an error if the state query fails.
Sourcepub fn reset(&self) -> CudaResult<()>
pub fn reset(&self) -> CudaResult<()>
Resets the primary context on this device.
This destroys all allocations, modules, and state associated with the primary context. The context is then re-created the next time it is retained.
§Errors
Returns an error if the reset fails.