DeviceArray

Struct DeviceArray 

Source
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>

Source

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]);
Source

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]);
Source

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

pub fn dtype(&self) -> DType

Return the element data type.

§Examples
let data = vec![0u8; 4];
let arr = scir_gpu::DeviceArray::from_cpu_slice(&[4], scir_gpu::DType::F32, &data);
assert!(matches!(arr.dtype(), scir_gpu::DType::F32));
Source

pub fn device(&self) -> Device

Return the current device of this array.

§Examples
let data = vec![1u8,2,3,4];
let arr = scir_gpu::DeviceArray::from_cpu_slice(&[4], scir_gpu::DType::F32, &data);
assert!(matches!(arr.device(), scir_gpu::Device::Cpu));
Source§

impl<T: Copy> DeviceArray<T>

Source

pub fn to_device(&mut self, device: Device) -> Result<(), GpuError>

Move the array to a device (CPU only, when CUDA is disabled).

Source§

impl<T> DeviceArray<T>
where T: Copy + NumAssign,

Source

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]);
Source

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>
where T: Copy + NumAssign,

Source

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>

Source

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]);
Source

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]);
Source

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>

Source§

fn clone(&self) -> DeviceArray<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for DeviceArray<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto 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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.