Skip to main content

DeviceBuffer

Struct DeviceBuffer 

Source
pub struct DeviceBuffer<T: DeviceRepr> { /* private fields */ }
Expand description

Owned, typed allocation of device memory (Runtime API).

Implementations§

Source§

impl<T: DeviceRepr> DeviceBuffer<T>

Source

pub fn new(len: usize) -> Result<Self>

Allocate an uninitialized buffer of len elements on the current device.

Source

pub fn zeros(len: usize) -> Result<Self>

Allocate and zero-fill.

Source

pub fn from_slice(src: &[T]) -> Result<Self>

Allocate and synchronously copy src from host memory.

Source

pub fn copy_from_host(&self, src: &[T]) -> Result<()>

Synchronous H2D copy.

Source

pub fn copy_to_host(&self, dst: &mut [T]) -> Result<()>

Synchronous D2H copy.

Source

pub fn copy_from_host_async(&self, src: &[T], stream: &Stream) -> Result<()>

Asynchronous H2D copy on stream.

Source

pub fn copy_to_host_async(&self, dst: &mut [T], stream: &Stream) -> Result<()>

Asynchronous D2H copy on stream.

Source

pub fn len(&self) -> usize

Number of elements.

Source

pub fn byte_size(&self) -> usize

Size in bytes.

Source

pub fn is_empty(&self) -> bool

true if zero elements.

Source

pub fn as_raw(&self) -> *mut c_void

Raw device pointer. Use with care.

Source

pub fn as_device_ptr(&self) -> u64

Raw device pointer as the u64 value kernels expect. Convenience wrapper around as_raw.

Source§

impl<T: DeviceRepr> DeviceBuffer<T>

Source

pub fn new_async(len: usize, stream: &Stream) -> Result<Self>

Asynchronously allocate len elements on stream from the device’s default memory pool (CUDA 11.2+).

The buffer retains stream (a cheap Arc clone), so it also frees stream-ordered: Drop enqueues cudaFreeAsync on stream, ordered by the driver after every operation already submitted to it — including a kernel still reading the buffer. That makes “launch on the stream, then let the buffer drop” safe by construction, with no host synchronize and no retention pool: the per-op stream.synchronize() that today keeps scratch / output buffers alive can be dropped. Freed blocks return to the device’s stream-ordered memory pool for reuse across a chain.

Precondition (single-stream): this ordering guarantee holds only for work submitted to this same stream. If the buffer is used on a different stream, the Drop free on the origin stream is not automatically ordered after that work — record an event on the other stream and have the origin stream wait on it before the buffer drops, or free explicitly with free_async on a suitably-ordered stream. Using the buffer only on its origin stream needs no such care.

free_async is still available if you want to free at an explicit point rather than at scope exit.

Source

pub fn zeros_async(len: usize, stream: &Stream) -> Result<Self>

Stream-ordered allocate-and-zero: cudaMallocAsync on stream followed by cudaMemsetAsync on the same stream, so the buffer is zeroed in stream order before any subsequent op observes it.

This is the async counterpart of zeros, and the constructor to use for output buffers that should free stream-ordered: like new_async the buffer retains stream and reclaims via cudaFreeAsync on Drop, so an output written by a pipelined kernel can be evicted without a host synchronize or an executor-side lifetime guard.

Requires CUDA 11.2+.

Source

pub fn free_async(self, stream: &Stream) -> Result<()>

Free this buffer asynchronously on stream. Consumes self so the sync Drop does not also free.

Source

pub fn memset_async(&self, value: u8, stream: &Stream) -> Result<()>

Asynchronous memset of self to byte value value on stream.

Trait Implementations§

Source§

impl<T: DeviceRepr> Debug for DeviceBuffer<T>

Source§

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

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

impl<T: DeviceRepr> Drop for DeviceBuffer<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T: DeviceRepr + Send> Send for DeviceBuffer<T>

Auto Trait Implementations§

§

impl<T> !Sync for DeviceBuffer<T>

§

impl<T> Freeze for DeviceBuffer<T>

§

impl<T> RefUnwindSafe for DeviceBuffer<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for DeviceBuffer<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for DeviceBuffer<T>

§

impl<T> UnwindSafe for DeviceBuffer<T>
where T: UnwindSafe,

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