pub struct Global;
Expand description
Global Context
Inspired by Rust’s Allocator syntax, Global
is a ZST that will be treated as the default context for most operations requiring of an OpenCL context or command queue.
Like with the Allocator API, you can specify a global context with the #[global_context]
macro.
use blaze_rs::prelude::*;
#[global_context]
static CONTEXT : SimpleContext = SimpleContext::default();
#[test]
fn with_global () -> Result<()> {
// Initialize two buffers
let buffer : Buffer<i32> = Buffer::new(&[1, 2, 3, 4, 5], MemAccess::READ_ONLY, false)?;
let buffer2 : Buffer<i32> = Buffer::new(&[5, 4, 3, 2, 1], MemAccess::WRITE_ONLY, false)?;
// Read the full contents of both buffers
let read = buffer.read_blocking(.., None)?;
let read2 = buffer2.read_blocking(.., Some(core::slice::from_ref(&read)))?;
assert_eq!(read.as_slice(), &[5, 4, 3, 2, 1]);
assert_eq!(read2.as_slice(), &[1, 2, 3, 4, 5]);
Ok(())
}
#[test]
fn without_global () -> Result<()> {
// Initialize a context.
let ctx = SimpleContext::default()?;
// Initialize two buffers
let buffer : Buffer<i32, &SimpleContext> = Buffer::new_in(&ctx, &[1, 2, 3, 4, 5], MemAccess::READ_ONLY, false)?;
let buffer2 : Buffer<i32, &SimpleContext> = Buffer::new_in(&ctx, &[5, 4, 3, 2, 1], MemAccess::WRITE_ONLY, false)?;
// Read the full contents of both buffers
let read = buffer.read_blocking(.., None)?;
let read2 = buffer2.read_blocking(.., Some(core::slice::from_ref(&read)))?;
assert_eq!(read.as_slice(), &[5, 4, 3, 2, 1]);
assert_eq!(read2.as_slice(), &[1, 2, 3, 4, 5]);
Ok(())
}
Note that unlike with the
Allocator
API, no default global context is set, so you’ll need to specify one explicitly if you want to use it.
Implementations§
Methods from Deref<Target = RawContext>§
pub fn id(&self) -> cl_context
pub unsafe fn retain(&self) -> Result<()>
sourcepub fn reference_count(&self) -> Result<u32>
pub fn reference_count(&self) -> Result<u32>
Return the context reference count.
sourcepub fn num_devices(&self) -> Result<u32>
pub fn num_devices(&self) -> Result<u32>
Return the number of devices in context.
sourcepub fn devices(&self) -> Result<Vec<RawDevice>>
pub fn devices(&self) -> Result<Vec<RawDevice>>
Return the list of devices and sub-devices in context.
sourcepub fn greatest_common_version(&self) -> Result<Version>
pub fn greatest_common_version(&self) -> Result<Version>
Returns the greatest common OpenCL version of this context’s devices.
sourcepub fn properties(&self) -> Result<ContextProperties>
pub fn properties(&self) -> Result<ContextProperties>
Return the properties argument specified in creation
pub fn on_destruct(&self, f: impl 'static + FnOnce() + Send) -> Result<()>
Available on crate feature
cl3
only.pub unsafe fn on_destruct_raw( &self, f: unsafe extern "C" fn(context: cl_context, user_data: *mut c_void), user_data: *mut c_void ) -> Result<()>
Available on crate feature
cl3
only.Trait Implementations§
source§impl Context for Global
impl Context for Global
source§fn next_queue(&self) -> &CommandQueue
fn next_queue(&self) -> &CommandQueue
Returns the next
CommandQueue
, as per context implementationsource§fn as_raw(&self) -> &RawContext
fn as_raw(&self) -> &RawContext
Returns a reference to the underlying
RawContext
source§fn queues(&self) -> &[CommandQueue]
fn queues(&self) -> &[CommandQueue]
Returns a slice with all of the
Context
’s command queuessource§fn finish_all(&self) -> Result<()>
fn finish_all(&self) -> Result<()>
Finishes all the
CommandQueue
s in the context.impl Copy for Global
Auto Trait Implementations§
impl RefUnwindSafe for Global
impl Send for Global
impl Sync for Global
impl Unpin for Global
impl UnwindSafe for Global
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more