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) -> CudaResult<Self>
pub fn new(device: &Device) -> CudaResult<Self>
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) -> CudaResult<Self>
pub fn with_flags(device: &Device, flags: u32) -> CudaResult<Self>
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 fn set_current(&self) -> CudaResult<()>
pub fn set_current(&self) -> CudaResult<()>
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() -> CudaResult<Option<CUcontext>>
pub fn current_raw() -> CudaResult<Option<CUcontext>>
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) -> CudaResult<()>
pub fn synchronize(&self) -> CudaResult<()>
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) -> CudaResult<R>where
F: FnOnce() -> CudaResult<R>,
pub fn scoped<F, R>(&self, f: F) -> CudaResult<R>where
F: FnOnce() -> CudaResult<R>,
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.
§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);Sourcepub fn is_current(&self) -> CudaResult<bool>
pub fn is_current(&self) -> CudaResult<bool>
Returns true if this context is the current context on the calling
thread.
§Errors
Returns an error if the driver call fails.