Skip to main content

Context

Struct Context 

Source
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 scope

Implementations§

Source§

impl Context

Source

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).

Source

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)?;
Source

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.

Source

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.

Source

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.

Source

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);
Source

pub fn device(&self) -> &Device

Get a reference to the Device this context was created on.

Source

pub fn raw(&self) -> CUcontext

Get the raw CUcontext handle for use with FFI calls.

Source

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.

Trait Implementations§

Source§

impl Debug for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Context

Source§

fn drop(&mut self)

Destroy the CUDA context.

Errors during destruction are logged via tracing::warn but never propagated (destructors must not panic).

Source§

impl Send for Context

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more