Struct HostBuffer

Source
pub struct HostBuffer<T: Copy + 'static> { /* private fields */ }
Expand description

A host buffer.

§Performance

Host buffers are managed by CUDA and can be used for pinned memory transfer. Pinned memory transfer speeds are usually higher compared to paged memory transfers. Pinned memory buffers are especially important for this crate because the runtime thread must do the least amount of CPU work possible. Paged transfers do require the host to move data into a CUDA managed buffer first (an extra memory copy) whilst pinned transfers do not.

Implementations§

Source§

impl<T: Copy + 'static> HostBuffer<T>

Source

pub async fn new(num_elements: usize) -> Self

Allocates memory on the host. This creates a pinned buffer. Any transfers to and from this buffer automatically become pinned transfers, and will be much faster.

CUDA documentation

§Arguments
  • num_elements - Number of elements to allocate.
Source

pub async fn from_slice(slice: &[T]) -> Self

Allocates memory on the host and copies the provided data into it.

This creates a pinned buffer. Any transfers to and from this buffer automatically become pinned transfers, and will be much faster.

This is a convenience function that allows the caller to quickly put data into a host buffer. It is roughly similar to buffer.copy_from_slice(slice).

§Arguments
  • slice - Data to copy into the new host buffer.
Source

pub async fn copy_from( &mut self, other: &DeviceBuffer<T>, stream: &Stream, ) -> Result<(), Error>

Copies memory from the provided device buffer to this buffer.

This function synchronizes the stream implicitly.

CUDA documentation

§Pinned transfer

This function is guaranteed to produce a pinned transfer on the runtime thread.

§Stream ordered semantics

This function uses stream ordered semantics. It can only be guaranteed to complete sequentially relative to operations scheduled on the same stream or the default stream.

§Arguments
  • other - Device buffer to copy from.
  • stream - Stream to use.
Source

pub async unsafe fn copy_from_async( &mut self, other: &DeviceBuffer<T>, stream: &Stream, ) -> Result<(), Error>

Copies memory from the provided device buffer to this buffer.

CUDA documentation

§Pinned transfer

This function is guaranteed to produce a pinned transfer on the runtime thread.

§Stream ordered semantics

This function uses stream ordered semantics. It can only be guaranteed to complete sequentially relative to operations scheduled on the same stream or the default stream.

§Safety

This function is unsafe because the operation might not have completed when the function returns, and thus the state of the buffer is undefined.

§Arguments
  • other - Device buffer to copy from.
  • stream - Stream to use.
Source

pub async fn copy_to( &self, other: &mut DeviceBuffer<T>, stream: &Stream, ) -> Result<(), Error>

Copies memory from this buffer to the provided device buffer.

This function synchronizes the stream implicitly.

CUDA documentation

§Pinned transfer

This function is guaranteed to produce a pinned transfer on the runtime thread.

§Stream ordered semantics

This function uses stream ordered semantics. It can only be guaranteed to complete sequentially relative to operations scheduled on the same stream or the default stream.

§Arguments
  • other - Device buffer to copy to.
  • stream - Stream to use.
Source

pub async unsafe fn copy_to_async( &self, other: &mut DeviceBuffer<T>, stream: &Stream, ) -> Result<(), Error>

Copies memory from this buffer to the provided device buffer.

CUDA documentation

§Pinned transfer

This function is guaranteed to produce a pinned transfer on the runtime thread.

§Stream ordered semantics

This function uses stream ordered semantics. It can only be guaranteed to complete sequentially relative to operations scheduled on the same stream or the default stream.

§Safety

This function is unsafe because the operation might not have completed when the function returns, and thus the state of the buffer is undefined.

§Arguments
  • other - Device buffer to copy to.
  • stream - Stream to use.
Source

pub fn copy_from_slice(&mut self, slice: &[T])

Copy data into the host buffer from a slice.

§Synchronization safety

This call is only synchronization-safe if all streams that have previously been used for copy operations either from or to this host buffer have been synchronized, and no operations have been scheduled since.

§Arguments
  • slice - Data to copy into the new host buffer.
§Example
let mut host_buffer = HostBuffer::<u8>::new(100).await;
let some_data = vec![10; 100];
host_buffer.copy_from_slice(&some_data);
Source

pub fn to_vec(&self) -> Vec<T>

Copy the data to a Vec and return it.

Source

pub fn num_elements(&self) -> usize

Get number of elements in buffer.

Source

pub fn inner(&self) -> &HostBuffer<T>

Access the inner synchronous implementation of HostBuffer.

Source

pub fn inner_mut(&mut self) -> &mut HostBuffer<T>

Access the inner synchronous implementation of HostBuffer.

Auto Trait Implementations§

§

impl<T> Freeze for HostBuffer<T>

§

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

§

impl<T> Send for HostBuffer<T>

§

impl<T> Sync for HostBuffer<T>

§

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

§

impl<T> UnwindSafe for HostBuffer<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.