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>
impl<T: Copy + 'static> HostBuffer<T>
Sourcepub async fn new(num_elements: usize) -> Self
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.
§Arguments
num_elements
- Number of elements to allocate.
Sourcepub async fn from_slice(slice: &[T]) -> Self
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.
Sourcepub async fn copy_from(
&mut self,
other: &DeviceBuffer<T>,
stream: &Stream,
) -> Result<(), Error>
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.
§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.
Sourcepub async unsafe fn copy_from_async(
&mut self,
other: &DeviceBuffer<T>,
stream: &Stream,
) -> Result<(), Error>
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.
§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.
Sourcepub async fn copy_to(
&self,
other: &mut DeviceBuffer<T>,
stream: &Stream,
) -> Result<(), Error>
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.
§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.
Sourcepub async unsafe fn copy_to_async(
&self,
other: &mut DeviceBuffer<T>,
stream: &Stream,
) -> Result<(), Error>
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.
§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.
Sourcepub fn copy_from_slice(&mut self, slice: &[T])
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);
Sourcepub fn num_elements(&self) -> usize
pub fn num_elements(&self) -> usize
Get number of elements in buffer.
Sourcepub fn inner(&self) -> &HostBuffer<T>
pub fn inner(&self) -> &HostBuffer<T>
Access the inner synchronous implementation of HostBuffer
.
Sourcepub fn inner_mut(&mut self) -> &mut HostBuffer<T>
pub fn inner_mut(&mut self) -> &mut HostBuffer<T>
Access the inner synchronous implementation of HostBuffer
.