Skip to main content

DeviceBuffer

Struct DeviceBuffer 

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

A contiguous buffer of T elements allocated in GPU device memory.

The buffer owns the underlying CUdeviceptr allocation and frees it on drop. All copy operations validate that source and destination lengths match, returning CudaError::InvalidValue on mismatch.

Implementations§

Source§

impl<T: Copy> DeviceBuffer<T>

Source

pub fn view_as<U: Copy>(&self) -> CudaResult<BufferView<'_, U>>

Reinterprets this buffer as a different element type U (immutable).

The total byte size of the buffer must be evenly divisible by size_of::<U>(). The resulting view has byte_size / size_of::<U>() elements.

§Errors

Returns CudaError::InvalidValue if:

  • size_of::<U>() is zero (ZST).
  • The buffer’s byte size is not divisible by size_of::<U>().
Source

pub fn view_as_mut<U: Copy>(&mut self) -> CudaResult<BufferViewMut<'_, U>>

Reinterprets this buffer as a different element type U (mutable).

The total byte size of the buffer must be evenly divisible by size_of::<U>(). The resulting view has byte_size / size_of::<U>() elements.

§Errors

Returns CudaError::InvalidValue if:

  • size_of::<U>() is zero (ZST).
  • The buffer’s byte size is not divisible by size_of::<U>().
Source§

impl<T: Copy> DeviceBuffer<T>

Source

pub fn alloc(n: usize) -> CudaResult<Self>

Allocates a device buffer capable of holding n elements of type T.

§Errors
Source

pub fn zeroed(n: usize) -> CudaResult<Self>

Allocates a device buffer of n elements and zero-initialises every byte.

This is equivalent to alloc followed by a cuMemsetD8_v2 call that writes 0 to every byte.

§Errors

Same as alloc, plus any error from cuMemsetD8_v2.

Source

pub fn from_host(data: &[T]) -> CudaResult<Self>

Allocates a device buffer and copies the contents of data into it.

The resulting buffer has the same length as the input slice.

§Errors
Source

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

Copies data from a host slice into this device buffer (synchronous).

The slice length must exactly match the buffer length.

§Errors
Source

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

Copies this device buffer’s contents into a host slice (synchronous).

The slice length must exactly match the buffer length.

§Errors
Source

pub fn copy_from_device(&mut self, src: &DeviceBuffer<T>) -> CudaResult<()>

Copies the entire contents of another device buffer into this one.

Both buffers must have the same length.

§Errors
Source

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

Asynchronously copies data from a host slice into this device buffer.

The copy is enqueued on stream and may not be complete when this function returns. The caller must ensure that src remains valid (i.e., is not moved or dropped) until the stream has been synchronised. For guaranteed correctness, prefer using a PinnedBuffer as the source.

§Errors
Source

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

Asynchronously copies this device buffer’s contents into a host slice.

The copy is enqueued on stream and may not be complete when this function returns. The caller must ensure that dst remains valid and is not read until the stream has been synchronised. For guaranteed correctness, prefer using a PinnedBuffer as the destination.

§Errors
Source

pub fn len(&self) -> usize

Returns the number of T elements in this buffer.

Source

pub fn is_empty(&self) -> bool

Returns true if the buffer contains zero elements.

In practice this is always false because alloc rejects zero-length allocations.

Source

pub fn byte_size(&self) -> usize

Returns the total size of the allocation in bytes.

Source

pub fn as_device_ptr(&self) -> CUdeviceptr

Returns the raw CUdeviceptr handle for this buffer.

This is useful when passing the pointer to kernel launch parameters or other low-level driver calls.

Source

pub fn slice(&self, offset: usize, len: usize) -> CudaResult<DeviceSlice<'_, T>>

Returns a borrowed DeviceSlice referencing a sub-range of this buffer starting at element offset and spanning len elements.

§Errors

Returns CudaError::InvalidValue if the requested range exceeds the buffer bounds (i.e., offset + len > self.len()).

Trait Implementations§

Source§

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

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

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

Source§

impl<T: Copy + Sync> Sync for DeviceBuffer<T>

Auto Trait Implementations§

§

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