use crate::ffi;
use crate::memory::DeviceBuffer;
use crate::runtime::Future;
use crate::stream::Stream;
type Result<T> = std::result::Result<T, crate::error::Error>;
pub struct HostBuffer<T: Copy + 'static> {
inner: ffi::memory::HostBuffer<T>,
}
impl<T: Copy + 'static> HostBuffer<T> {
pub async fn new(num_elements: usize) -> Self {
let inner = Future::new(move || ffi::memory::HostBuffer::<T>::new(num_elements)).await;
Self { inner }
}
pub async fn from_slice(slice: &[T]) -> Self {
let mut this = Self::new(slice.len()).await;
this.copy_from_slice(slice);
this
}
#[cfg(feature = "ndarray")]
pub async fn from_array<D: ndarray::Dimension>(array: &ndarray::ArrayView<'_, T, D>) -> Self {
let mut this = Self::new(array.len()).await;
this.copy_from_array(array);
this
}
#[inline(always)]
pub async fn copy_from(&mut self, other: &DeviceBuffer<T>, stream: &Stream) -> Result<()> {
other.copy_to(self, stream).await
}
#[inline(always)]
pub async unsafe fn copy_from_async(
&mut self,
other: &DeviceBuffer<T>,
stream: &Stream,
) -> Result<()> {
other.copy_to_async(self, stream).await
}
#[inline(always)]
pub async fn copy_to(&self, other: &mut DeviceBuffer<T>, stream: &Stream) -> Result<()> {
other.copy_from(self, stream).await
}
#[inline(always)]
pub async unsafe fn copy_to_async(
&self,
other: &mut DeviceBuffer<T>,
stream: &Stream,
) -> Result<()> {
other.copy_from_async(self, stream).await
}
#[inline(always)]
pub fn copy_from_slice(&mut self, slice: &[T]) {
self.inner.copy_from_slice(slice);
}
#[cfg(feature = "ndarray")]
#[inline(always)]
pub fn copy_from_array<D: ndarray::Dimension>(&mut self, array: &ndarray::ArrayView<T, D>) {
self.inner.copy_from_array(array)
}
#[inline(always)]
pub fn to_vec(&self) -> Vec<T> {
self.inner.to_vec()
}
#[cfg(feature = "ndarray")]
#[inline(always)]
pub fn to_array_with_shape<D: ndarray::Dimension>(
&self,
shape: impl Into<ndarray::StrideShape<D>>,
) -> ndarray::Array<T, D> {
self.inner.to_array_with_shape::<D>(shape)
}
#[inline(always)]
pub fn num_elements(&self) -> usize {
self.inner.num_elements
}
#[inline(always)]
pub fn inner(&self) -> &ffi::memory::HostBuffer<T> {
&self.inner
}
#[inline(always)]
pub fn inner_mut(&mut self) -> &mut ffi::memory::HostBuffer<T> {
&mut self.inner
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_new() {
let buffer = HostBuffer::<u32>::new(100).await;
assert_eq!(buffer.num_elements(), 100);
assert_eq!(buffer.to_vec().len(), 100);
}
#[tokio::test]
async fn test_from_slice() {
let all_ones = vec![1_u32; 200];
let buffer = HostBuffer::from_slice(all_ones.as_slice()).await;
assert_eq!(buffer.num_elements(), 200);
let data = buffer.to_vec();
assert_eq!(data.len(), 200);
assert!(data.into_iter().all(|v| v == 1_u32));
}
#[tokio::test]
async fn test_copy() {
let stream = Stream::new().await.unwrap();
let all_ones = vec![1_u32; 100];
let host_buffer = HostBuffer::from_slice(all_ones.as_slice()).await;
let mut device_buffer = DeviceBuffer::<u32>::new(100, &stream).await;
unsafe {
host_buffer
.copy_to_async(&mut device_buffer, &stream)
.await
.unwrap();
}
let mut return_host_buffer = HostBuffer::<u32>::new(100).await;
unsafe {
return_host_buffer
.copy_from_async(&device_buffer, &stream)
.await
.unwrap();
}
stream.synchronize().await.unwrap();
assert_eq!(return_host_buffer.num_elements(), 100);
let return_data = return_host_buffer.to_vec();
assert_eq!(return_data.len(), 100);
assert!(return_data.into_iter().all(|v| v == 1_u32));
}
#[tokio::test]
#[should_panic]
async fn test_it_panics_when_copying_invalid_size() {
let stream = Stream::new().await.unwrap();
let host_buffer = HostBuffer::<u32>::new(100).await;
let mut device_buffer = DeviceBuffer::<u32>::new(101, &Stream::null()).await;
let _ = unsafe { host_buffer.copy_to_async(&mut device_buffer, &stream).await };
}
}