pub struct Context { /* private fields */ }Expand description
RAII wrapper for a CUDA context.
A context is created on a specific Device and becomes the active
context for the calling thread. When the Context is dropped,
cuCtxDestroy_v2 is called automatically.
§Examples
use oxicuda_driver::context::Context;
use oxicuda_driver::device::Device;
oxicuda_driver::init()?;
let dev = Device::get(0)?;
let ctx = Context::new(&dev)?;
println!("Context on device {}", ctx.device().ordinal());
ctx.synchronize()?;
// ctx is destroyed when it goes out of scopeImplementations§
Source§impl Context
impl Context
Sourcepub fn new(device: &Device) -> Result<Context, CudaError>
pub fn new(device: &Device) -> Result<Context, CudaError>
Create a new context on the given device with default flags
(flags::SCHED_AUTO).
The new context is automatically pushed onto the calling thread’s context stack and becomes the current context.
§Errors
Returns an error if the driver cannot create the context (e.g., device is invalid, out of resources).
Sourcepub fn with_flags(device: &Device, flags: u32) -> Result<Context, CudaError>
pub fn with_flags(device: &Device, flags: u32) -> Result<Context, CudaError>
Create a new context on the given device with specific scheduling flags.
See the flags module for available values. Multiple flags can be
combined with bitwise OR.
§Errors
Returns an error if the driver cannot create the context.
§Examples
use oxicuda_driver::context::{Context, flags};
use oxicuda_driver::device::Device;
oxicuda_driver::init()?;
let dev = Device::get(0)?;
let ctx = Context::with_flags(&dev, flags::SCHED_BLOCKING_SYNC)?;Sourcepub unsafe fn from_raw_borrowed(raw: CUcontext, device: Device) -> Context
pub unsafe fn from_raw_borrowed(raw: CUcontext, device: Device) -> Context
Wraps an already-live CUcontext in a non-owning Context.
Unlike Context::new / Context::with_flags, the returned wrapper
does not own the underlying context: on drop it never calls
cuCtxDestroy, and it is not entered into the live-context generation
registry. Its sole purpose is to hand an externally-owned context (for
example a device primary context retained via
PrimaryContext) to APIs that
require an Arc<Context> lifetime token, without transferring ownership
of the context’s lifetime.
§Safety
The caller must guarantee that raw refers to a valid, live CUDA
context and that it remains live for at least as long as this Context
wrapper — and any resource (stream, module, …) created from it — is in
use. Because the wrapper is untracked, resources created while it is
current fall back to the driver’s default “destroy on drop” behaviour,
so the borrowed context must outlive them.
Sourcepub fn set_current(&self) -> Result<(), CudaError>
pub fn set_current(&self) -> Result<(), CudaError>
Set this context as the current context for the calling thread.
Any previous context on this thread is detached (but not destroyed).
§Errors
Returns an error if the driver call fails.
Sourcepub fn current_raw() -> Result<Option<CUcontext>, CudaError>
pub fn current_raw() -> Result<Option<CUcontext>, CudaError>
Get the raw handle of the current context for the calling thread.
Returns None if no context is bound to the current thread.
§Errors
Returns an error if the driver call fails.
Sourcepub fn synchronize(&self) -> Result<(), CudaError>
pub fn synchronize(&self) -> Result<(), CudaError>
Block until all pending GPU operations in this context have completed.
This sets the context as current before synchronising to ensure the correct context is targeted.
§Errors
Returns an error if any GPU operation failed or the driver call fails.
Sourcepub fn scoped<F, R>(&self, f: F) -> Result<R, CudaError>
pub fn scoped<F, R>(&self, f: F) -> Result<R, CudaError>
Execute a closure with this context set as current, then restore the previous context.
This is useful when temporarily switching contexts. The previous context (if any) is restored even if the closure returns an error or panics — restoration is performed by an RAII guard at scope exit.
§Errors
Propagates any error from the closure. Context-restoration errors are logged but do not override the closure result.
§Examples
use oxicuda_driver::context::Context;
use oxicuda_driver::device::Device;
oxicuda_driver::init()?;
let dev = Device::get(0)?;
let ctx = Context::new(&dev)?;
let result = ctx.scoped(|| {
// ctx is current here
Ok(42)
})?;
assert_eq!(result, 42);