pub struct DeviceBuffer<T: Copy + 'static> { /* private fields */ }
Expand description
A buffer on the device.
§Example
Copying data from a HostBuffer
to a DeviceBuffer
:
let stream = Stream::new().await.unwrap();
let all_ones = vec![1_u8; 100];
let host_buffer = HostBuffer::<u8>::from_slice(&all_ones).await;
let mut device_buffer = DeviceBuffer::<u8>::new(100, &stream).await;
device_buffer.copy_from(&host_buffer, &stream).await.unwrap();
Implementations§
Source§impl<T: Copy + 'static> DeviceBuffer<T>
impl<T: Copy + 'static> DeviceBuffer<T>
Sourcepub async fn new(num_elements: usize, stream: &Stream) -> Self
pub async fn new(num_elements: usize, stream: &Stream) -> Self
Allocates memory on the device.
§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
num_elements
- Number of elements to allocate.stream
- Stream to use.
Sourcepub async fn from_slice(slice: &[T], stream: &Stream) -> Result<Self, Error>
pub async fn from_slice(slice: &[T], stream: &Stream) -> Result<Self, Error>
Allocate memory on the device, and copy data from host into it.
This function creates a temporary HostBuffer
, copies the slice into it, then finally
copies the data from the host buffer to the DeviceBuffer
.
The given stream is automatically synchronized, since the temporary host buffer might otherwise be dropped before the copy can complete.
§Arguments
slice
- Data to copy into the buffer.stream
- Stream to use.
Sourcepub async fn copy_from(
&mut self,
other: &HostBuffer<T>,
stream: &Stream,
) -> Result<(), Error>
pub async fn copy_from( &mut self, other: &HostBuffer<T>, stream: &Stream, ) -> Result<(), Error>
Copies memory from the provided pinned host buffer to this buffer.
This function synchronizes the stream implicitly.
§Pinned transfer
The other buffer (of type HostBuffer
) is always a pinned buffer. 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
- Buffer to copy from.stream
- Stream to use.
Sourcepub async unsafe fn copy_from_async(
&mut self,
other: &HostBuffer<T>,
stream: &Stream,
) -> Result<(), Error>
pub async unsafe fn copy_from_async( &mut self, other: &HostBuffer<T>, stream: &Stream, ) -> Result<(), Error>
Copies memory from the provided pinned host buffer to this buffer.
§Pinned transfer
The other buffer (of type HostBuffer
) is always a pinned buffer. 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
- Buffer to copy from.stream
- Stream to use.
Sourcepub async fn copy_to(
&self,
other: &mut HostBuffer<T>,
stream: &Stream,
) -> Result<(), Error>
pub async fn copy_to( &self, other: &mut HostBuffer<T>, stream: &Stream, ) -> Result<(), Error>
Copies memory from this buffer to the provided pinned host buffer.
This function synchronizes the stream implicitly.
§Pinned transfer
The other buffer (of type HostBuffer
) is always a pinned buffer. 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
- Buffer to copy to.stream
- Stream to use.
Sourcepub async unsafe fn copy_to_async(
&self,
other: &mut HostBuffer<T>,
stream: &Stream,
) -> Result<(), Error>
pub async unsafe fn copy_to_async( &self, other: &mut HostBuffer<T>, stream: &Stream, ) -> Result<(), Error>
Copies memory from this buffer to the provided pinned host buffer.
§Pinned transfer
The other buffer (of type HostBuffer
) is always a pinned buffer. 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
- Buffer to copy to.stream
- Stream to use.
Sourcepub async fn fill_with_byte(
&mut self,
value: u8,
stream: &Stream,
) -> Result<(), Error>
pub async fn fill_with_byte( &mut self, value: u8, stream: &Stream, ) -> Result<(), Error>
Sourcepub fn num_elements(&self) -> usize
pub fn num_elements(&self) -> usize
Get number of elements in buffer.
Sourcepub fn inner(&self) -> &DeviceBuffer<T>
pub fn inner(&self) -> &DeviceBuffer<T>
Access the inner synchronous implementation of DeviceBuffer
.
Sourcepub fn inner_mut(&mut self) -> &mut DeviceBuffer<T>
pub fn inner_mut(&mut self) -> &mut DeviceBuffer<T>
Access the inner synchronous implementation of DeviceBuffer
.