pub struct DeviceArray<T> { /* private fields */ }Expand description
A minimal, shaped device array with dtype. Currently CPU-backed.
Implementations§
Source§impl<T: Copy> DeviceArray<T>
impl<T: Copy> DeviceArray<T>
Sourcepub fn from_cpu_slice(shape: &[usize], dtype: DType, data: &[T]) -> Self
pub fn from_cpu_slice(shape: &[usize], dtype: DType, data: &[T]) -> Self
Create a DeviceArray from a CPU slice and explicit shape/dtype.
§Examples
let data = vec![1.0f32, 2.0, 3.0, 4.0];
let arr = scir_gpu::DeviceArray::from_cpu_slice(&[2,2], scir_gpu::DType::F32, &data);
assert_eq!(arr.shape(), &[2,2]);Sourcepub fn to_cpu_vec(&self) -> Vec<T>
pub fn to_cpu_vec(&self) -> Vec<T>
Copy data back to a CPU-owned Vec<T>.
§Examples
let data = vec![1i32,2,3];
let arr = scir_gpu::DeviceArray::from_cpu_slice(&[3], scir_gpu::DType::F32, &data);
let back = arr.to_cpu_vec();
assert_eq!(back, vec![1,2,3]);Sourcepub fn shape(&self) -> &[usize]
pub fn shape(&self) -> &[usize]
Return the logical shape of the array.
§Examples
let data = vec![0u8; 6];
let arr = scir_gpu::DeviceArray::from_cpu_slice(&[2,3], scir_gpu::DType::F32, &data);
assert_eq!(arr.shape(), &[2,3]);Source§impl<T: Copy> DeviceArray<T>
impl<T: Copy> DeviceArray<T>
Source§impl<T> DeviceArray<T>
impl<T> DeviceArray<T>
Sourcepub fn add_scalar(&self, alpha: T) -> Self
pub fn add_scalar(&self, alpha: T) -> Self
Add a scalar to each element (CPU baseline).
§Examples
let data = vec![1.0f32, 2.0, 3.0];
let arr = scir_gpu::DeviceArray::from_cpu_slice(&[3], scir_gpu::DType::F32, &data);
let out = arr.add_scalar(1.0f32);
assert_eq!(out.to_cpu_vec(), vec![2.0f32, 3.0, 4.0]);Sourcepub fn mul_scalar(&self, alpha: T) -> Self
pub fn mul_scalar(&self, alpha: T) -> Self
Multiply each element by a scalar (CPU baseline).
§Examples
let data = vec![1.0f32, 2.0, 3.0];
let arr = scir_gpu::DeviceArray::from_cpu_slice(&[3], scir_gpu::DType::F32, &data);
let out = arr.mul_scalar(2.0f32);
assert_eq!(out.to_cpu_vec(), vec![2.0f32, 4.0, 6.0]);Source§impl<T> DeviceArray<T>
impl<T> DeviceArray<T>
Sourcepub fn add(&self, other: &Self) -> Result<Self, GpuError>
pub fn add(&self, other: &Self) -> Result<Self, GpuError>
Elementwise addition between arrays (CPU baseline).
§Examples
let a = scir_gpu::DeviceArray::from_cpu_slice(&[3], scir_gpu::DType::F32, &[1.0f32,2.0,3.0]);
let b = scir_gpu::DeviceArray::from_cpu_slice(&[3], scir_gpu::DType::F32, &[0.5f32,1.5,2.5]);
let c = a.add(&b).unwrap();
assert_eq!(c.to_cpu_vec(), vec![1.5f32, 3.5, 5.5]);Source§impl DeviceArray<f32>
impl DeviceArray<f32>
Sourcepub fn add_scalar_auto(&self, alpha: f32) -> Self
pub fn add_scalar_auto(&self, alpha: f32) -> Self
Elementwise add-scalar, with CUDA dispatch when available.
§Examples
let data = vec![1.0f32, 2.0, 3.0];
let mut a = scir_gpu::DeviceArray::from_cpu_slice(&[3], scir_gpu::DType::F32, &data);
a.to_device(scir_gpu::Device::Cpu).unwrap();
let out = a.add_scalar_auto(1.0);
assert_eq!(out.to_cpu_vec(), vec![2.0f32, 3.0, 4.0]);Sourcepub fn add_auto(&self, other: &Self) -> Result<Self, GpuError>
pub fn add_auto(&self, other: &Self) -> Result<Self, GpuError>
Elementwise add of two arrays, with CUDA dispatch when available.
§Examples
let a = scir_gpu::DeviceArray::from_cpu_slice(&[2], scir_gpu::DType::F32, &[1.0f32,2.0]);
let b = scir_gpu::DeviceArray::from_cpu_slice(&[2], scir_gpu::DType::F32, &[0.5f32,1.5]);
let c = a.add_auto(&b).unwrap();
assert_eq!(c.to_cpu_vec(), vec![1.5f32, 3.5]);Sourcepub fn mul_scalar_auto(&self, alpha: f32) -> Self
pub fn mul_scalar_auto(&self, alpha: f32) -> Self
Elementwise mul-scalar with device dispatch.
§Examples
let data = vec![1.0f32, 2.0, 3.0];
let a = scir_gpu::DeviceArray::from_cpu_slice(&[3], scir_gpu::DType::F32, &data);
let out = a.mul_scalar_auto(2.0);
assert_eq!(out.to_cpu_vec(), vec![2.0f32, 4.0, 6.0]);Trait Implementations§
Source§impl<T: Clone> Clone for DeviceArray<T>
impl<T: Clone> Clone for DeviceArray<T>
Source§fn clone(&self) -> DeviceArray<T>
fn clone(&self) -> DeviceArray<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl<T> Freeze for DeviceArray<T>
impl<T> RefUnwindSafe for DeviceArray<T>where
T: RefUnwindSafe,
impl<T> Send for DeviceArray<T>where
T: Send,
impl<T> Sync for DeviceArray<T>where
T: Sync,
impl<T> Unpin for DeviceArray<T>where
T: Unpin,
impl<T> UnwindSafe for DeviceArray<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more