pub struct Matrix<'a, T = f32, D: Device = CPU, S: Shape = ()> {
pub data: Buffer<'a, T, D, S>,
pub dims: (usize, usize),
}Expand description
A matrix using Buffer described with rows and columns
§Example
The following example creates a zeroed (or values set to default) Matrix with the given dimensions.
use custos::CPU;
use custos_math::Matrix;
let device = CPU::new();
let m = Matrix::<i32>::new(&device, (5, 8));
assert_eq!(m.rows(), 5);
assert_eq!(m.cols(), 8);
assert_eq!(m.size(), 5*8);
assert_eq!(m.read(), vec![0; 5*8])Fields§
§data: Buffer<'a, T, D, S>§dims: (usize, usize)Implementations§
Source§impl<'a, T, D: Device, S: Shape> Matrix<'a, T, D, S>
impl<'a, T, D: Device, S: Shape> Matrix<'a, T, D, S>
Sourcepub fn new(device: &'a D, dims: (usize, usize)) -> Matrix<'a, T, D, S>where
D: Alloc<'a, T, S>,
pub fn new(device: &'a D, dims: (usize, usize)) -> Matrix<'a, T, D, S>where
D: Alloc<'a, T, S>,
Returns an empty matrix with the specified dimensions (rows, cols).
§Example
use custos::CPU;
use custos_math::Matrix;
let device = CPU::new();
let m = Matrix::<f64>::new(&device, (20, 10));
assert_eq!(m.size(), 20*10);
assert_eq!(m.read(), vec![0.0; 20*10])Examples found in repository?
More examples
6fn main() {
7 let device = CPU::new();
8
9 let a = Matrix::<i16>::new(&device, (100, 100));
10 let b = Matrix::<i16>::new(&device, (100, 100));
11
12 let out = a + b;
13 let ptr = {
14 let cache = device.cache.borrow();
15 let mut node = Ident::new(100 * 100);
16 node.idx = 0;
17 cache.nodes.get(&node).unwrap().ptr
18 };
19
20 assert!(ptr == out.as_buf().ptr.ptr as *mut u8);
21}pub fn device(&self) -> &'a D
Sourcepub fn as_buf(&self) -> &Buffer<'a, T, D, S>
pub fn as_buf(&self) -> &Buffer<'a, T, D, S>
Returns a reference to the underlying buffer.
§Example
use custos::{CPU, Read};
use custos_math::Matrix;
let device = CPU::new();
let a = Matrix::from((&device, (2, 3), [1., 2., 3., 3., 2., 1.,]));
let read = a.read();
assert_eq!(vec![1., 2., 3., 3., 2., 1.,], read);Examples found in repository?
6fn main() {
7 let device = CPU::new();
8
9 let a = Matrix::<i16>::new(&device, (100, 100));
10 let b = Matrix::<i16>::new(&device, (100, 100));
11
12 let out = a + b;
13 let ptr = {
14 let cache = device.cache.borrow();
15 let mut node = Ident::new(100 * 100);
16 node.idx = 0;
17 cache.nodes.get(&node).unwrap().ptr
18 };
19
20 assert!(ptr == out.as_buf().ptr.ptr as *mut u8);
21}pub fn to_buf(self) -> Buffer<'a, T, D, S>
Sourcepub fn as_buf_mut(&mut self) -> &mut Buffer<'a, T, D, S>
pub fn as_buf_mut(&mut self) -> &mut Buffer<'a, T, D, S>
Returns a mutable reference to the underlying buffer.
pub fn dims(&self) -> (usize, usize)
pub fn reshape(&mut self, dims: (usize, usize))
Sourcepub fn rows(&self) -> usize
pub fn rows(&self) -> usize
Returns the row count of the matrix.
§Example
use custos::CPU;
use custos_math::Matrix;
let device = CPU::new();
let matrix = Matrix::<i32>::new(&device, (2, 5));
assert_eq!(matrix.rows(), 2)Sourcepub fn cols(&self) -> usize
pub fn cols(&self) -> usize
Returns the column count of the matrix.
§Example
use custos::CPU;
use custos_math::Matrix;
let device = CPU::new();
let matrix = Matrix::<i32>::new(&device, (2, 5));
assert_eq!(matrix.cols(), 5)Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the number of elements in the matrix: rows * cols
§Example
use custos::CPU;
use custos_math::Matrix;
let device = CPU::new();
let matrix = Matrix::<u16>::new(&device, (4, 12));
assert_eq!(matrix.size(), 48)pub fn as_slice(&self) -> &[T]where
D: MainMemory,
pub fn as_mut_slice(&mut self) -> &mut [T]where
D: MainMemory,
Sourcepub fn read(&'a self) -> D::Read<'a>
pub fn read(&'a self) -> D::Read<'a>
Uses VecRead and current global device to read Matrix
§Example
use custos::CPU;
use custos_math::Matrix;
let device = CPU::new();
let a = Matrix::from((&device, (2, 2), [5, 7, 2, 10,]));
assert_eq!(a.read(), vec![5, 7, 2, 10])Examples found in repository?
17fn with_select(cpu: &CPU) {
18 let a: Matrix<i32> = Matrix::from((cpu, (2, 3), [1, 2, 3, 4, 5, 6]));
19 let b = Matrix::from((cpu, (2, 3), [6, 5, 4, 3, 2, 1]));
20
21 let c = a + b;
22 assert_eq!(c.read(), vec![7, 7, 7, 7, 7, 7]);
23}
24
25fn specify_device(cpu: &CPU) {
26 //device is specified in every operation
27 let a = Matrix::from((cpu, (2, 2), [0.25f32, 0.5, 0.75, 1.]));
28 let b = Matrix::from((cpu, (2, 2), [1., 2., 3., 4.]));
29
30 let c_cpu = cpu.mul(&a, &b);
31 assert_eq!(cpu.read(&c_cpu), vec![0.25, 1., 2.25, 4.,]);
32}
33
34fn using_opencl() -> custos::Result<()> {
35 //OpenCL device (GPU)
36 let cl = OpenCL::new(0)?;
37
38 let a = Matrix::from((&cl, (2, 2), [0.25f32, 0.5, 0.75, 1.]));
39 let b = Matrix::from((&cl, (2, 2), [1., 2., 3., 4.]));
40
41 let c = a * b;
42 assert_eq!(c.read(), vec![0.25, 1., 2.25, 4.,]);
43 Ok(())
44}More examples
Sourcepub fn read_to_vec(&self) -> Vec<T>
pub fn read_to_vec(&self) -> Vec<T>
Uses VecRead and current global device to read Matrix
§Example
use custos::CPU;
use custos_math::Matrix;
let device = CPU::new();
let a = Matrix::from((&device, (2, 2), [5, 7, 2, 10,]));
assert_eq!(a.read(), vec![5, 7, 2, 10])Sourcepub fn shallow(&self) -> Matrix<'a, T, D, S>where
D::Ptr<T, S>: ShallowCopy,
pub fn shallow(&self) -> Matrix<'a, T, D, S>where
D::Ptr<T, S>: ShallowCopy,
Creates a shallow copy of &self.
Sourcepub fn shallow_or_clone(&self) -> Matrix<'a, T, D, S>
pub fn shallow_or_clone(&self) -> Matrix<'a, T, D, S>
Creates a shallow copy or a deep copy of &self, depening on whether the realloc feature is activated.
Source§impl<T, D: IsShapeIndep, S: Shape> Matrix<'_, T, D, S>
impl<T, D: IsShapeIndep, S: Shape> Matrix<'_, T, D, S>
Source§impl<'a, T, D: ActivationOps<T, S>, S: Shape> Matrix<'a, T, D, S>
impl<'a, T, D: ActivationOps<T, S>, S: Shape> Matrix<'a, T, D, S>
pub fn tanh(&self) -> Matrix<'a, T, D, S>
pub fn tanh_grad(&self) -> Matrix<'a, T, D, S>
pub fn relu(&self) -> Matrix<'a, T, D, S>
pub fn relu_mut(&mut self)
pub fn relu_grad(&self) -> Matrix<'a, T, D, S>
pub fn relu_grad_mut(&mut self)
pub fn sigmoid(&self) -> Matrix<'a, T, D, S>
Sourcepub fn sigmoid_grad(&self) -> Matrix<'a, T, D, S>
pub fn sigmoid_grad(&self) -> Matrix<'a, T, D, S>
uses pre-computed sigmoid activation
Source§impl<'a, T, D: SoftmaxOps<T>> Matrix<'a, T, D>
impl<'a, T, D: SoftmaxOps<T>> Matrix<'a, T, D>
Source§impl<'a, T, D: DiagflatOp<T>> Matrix<'a, T, D>
impl<'a, T, D: DiagflatOp<T>> Matrix<'a, T, D>
Source§impl<'a, T, D: Device, LS: Shape> Matrix<'a, T, D, LS>
impl<'a, T, D: Device, LS: Shape> Matrix<'a, T, D, LS>
Sourcepub fn gemm<RS: Shape, OS: Shape>(
&self,
rhs: &Matrix<'a, T, D, RS>,
) -> Matrix<'a, T, D, OS>where
D: Gemm<T, LS, RS, OS, D>,
pub fn gemm<RS: Shape, OS: Shape>(
&self,
rhs: &Matrix<'a, T, D, RS>,
) -> Matrix<'a, T, D, OS>where
D: Gemm<T, LS, RS, OS, D>,
Matrix multiplication. Uses current global device.
§Example
use custos::CPU;
use custos_math::Matrix;
let device = CPU::new();
let a = Matrix::from((&device, (2, 3), [1., 2., 3., 4., 5., 6.,]));
let b = Matrix::from((&device, (3, 2), [6., 5., 4., 3., 2., 1.,]));
let c = a.gemm(&b);
println!("c: {c:?}");
assert_eq!(c.read(), vec![20., 14., 56., 41.,]);Source§impl<'a, T, D, S> Matrix<'a, T, D, S>where
D: AdditionalOps<T, S>,
S: Shape,
impl<'a, T, D, S> Matrix<'a, T, D, S>where
D: AdditionalOps<T, S>,
S: Shape,
Methods from Deref<Target = Buffer<'a, T, D, S>>§
pub fn device(&self) -> &'a D
pub fn read(&'a self) -> <D as Read<T, D, S>>::Read<'a>
Sourcepub fn read_to_vec(&self) -> Vec<T>
pub fn read_to_vec(&self) -> Vec<T>
Reads the contents of the buffer and writes them into a vector.
If it is certain whether a CPU, or an unified CPU + OpenCL Buffer, is used, calling .as_slice() (or deref/mut to &/mut [&T]) is probably preferred.
§Example
use custos::{CPU, Buffer};
let device = CPU::new();
let buf = Buffer::from((&device, [1, 2, 3, 4]));
assert_eq!(buf.read_to_vec(), vec![1, 2, 3, 4]);Sourcepub fn write(&mut self, data: &[T])
pub fn write(&mut self, data: &[T])
Writes a slice to the Buffer. With a CPU buffer, the slice is just copied to the slice of the buffer.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements contained in Buffer.
§Example
use custos::{CPU, Buffer};
let device = CPU::new();
let a = Buffer::<i32, _>::new(&device, 10);
assert_eq!(a.len(), 10)Sourcepub unsafe fn shallow(&self) -> Buffer<'a, T, D, S>
pub unsafe fn shallow(&self) -> Buffer<'a, T, D, S>
Creates a shallow copy of &self.
§Safety
Itself, this function does not need to be unsafe.
However, declaring this function as unsafe highlights the violation of creating two or more owners for one resource.
Furthermore, the resulting Buffer can outlive self.
Sourcepub unsafe fn shallow_or_clone(&self) -> Buffer<'a, T, D, S>
pub unsafe fn shallow_or_clone(&self) -> Buffer<'a, T, D, S>
Returns a shallow copy of &self, if the realloc feature is deactivated.
If the realloc feature is activated, it returns a deep copy / clone.
§Safety
Itself, this function does not need to be unsafe.
However, declaring this function as unsafe highlights the violation of possibly creating two or more owners for one resource.
Furthermore, the resulting Buffer can outlive self.
pub fn id(&self) -> Ident
pub fn as_dims<'b, O>(&self) -> &Buffer<'b, T, D, O>where
O: Shape,
pub fn as_dims_mut<'b, O>(&mut self) -> &mut Buffer<'b, T, D, O>where
O: Shape,
Sourcepub fn ptrs(&self) -> (*const T, *mut c_void, u64)
pub fn ptrs(&self) -> (*const T, *mut c_void, u64)
Returns all types of pointers. (host, OpenCL, CUDA)
Sourcepub fn ptrs_mut(&mut self) -> (*mut T, *mut c_void, u64)
pub fn ptrs_mut(&mut self) -> (*mut T, *mut c_void, u64)
Returns all types of pointers. (host, OpenCL, CUDA)
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if Buffer is created without a slice.
§Example
use custos::{CPU, Buffer};
let a = Buffer::<i32, ()>::from(5);
assert!(a.is_empty())Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Returns a CPU slice. This does not work with CUDA or raw OpenCL buffers.
Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Returns a mutable CPU slice.
Sourcepub fn host_ptr_mut(&mut self) -> *mut T
pub fn host_ptr_mut(&mut self) -> *mut T
Returns a non null host pointer
Trait Implementations§
Source§impl<'a, T, D, S: Shape> Add<&Matrix<'a, T, D, S>> for Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
impl<'a, T, D, S: Shape> Add<&Matrix<'a, T, D, S>> for Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
Source§impl<'a, T, D, S: Shape> Add<Matrix<'a, T, D, S>> for &Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
impl<'a, T, D, S: Shape> Add<Matrix<'a, T, D, S>> for &Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
Source§impl<'a, T, D> Add<T> for Matrix<'a, T, D>where
D: AdditionalOps<T>,
impl<'a, T, D> Add<T> for Matrix<'a, T, D>where
D: AdditionalOps<T>,
Source§impl<T, D, S: Shape> AddAssign<&Matrix<'_, T, D, S>> for Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
impl<T, D, S: Shape> AddAssign<&Matrix<'_, T, D, S>> for Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
Source§fn add_assign(&mut self, rhs: &Self)
fn add_assign(&mut self, rhs: &Self)
+= operation. Read moreSource§impl<'a, T, S, D> AddAssign<T> for Matrix<'a, T, D, S>where
S: Shape,
D: ScalarAssign<T, S>,
impl<'a, T, S, D> AddAssign<T> for Matrix<'a, T, D, S>where
S: Shape,
D: ScalarAssign<T, S>,
Source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
+= operation. Read moreSource§impl<T, D, S: Shape> AddAssign for Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
impl<T, D, S: Shape> AddAssign for Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<'a, T> AsClCvoidPtr for &Matrix<'a, T, OpenCL>
impl<'a, T> AsClCvoidPtr for &Matrix<'a, T, OpenCL>
Source§impl<'a, T> AsClCvoidPtr for Matrix<'a, T, OpenCL>
impl<'a, T> AsClCvoidPtr for Matrix<'a, T, OpenCL>
Source§impl<'a, T: Default + Copy + Debug, D> Debug for Matrix<'a, T, D>where
D: Read<T, D> + 'a,
Available on non-crate feature no-std only.
impl<'a, T: Default + Copy + Debug, D> Debug for Matrix<'a, T, D>where
D: Read<T, D> + 'a,
no-std only.Source§impl<T, S, D> DivAssign<T> for Matrix<'_, T, D, S>where
S: Shape,
D: ScalarAssign<T, S>,
impl<T, S, D> DivAssign<T> for Matrix<'_, T, D, S>where
S: Shape,
D: ScalarAssign<T, S>,
Source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
/= operation. Read moreSource§impl<'a, T> From<(&'a CPU, *mut T, (usize, usize))> for Matrix<'a, T>
Available on crate feature cpu only.
impl<'a, T> From<(&'a CPU, *mut T, (usize, usize))> for Matrix<'a, T>
cpu only.Source§impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, (usize, usize), &[T])> for Matrix<'a, T, D>
impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, (usize, usize), &[T])> for Matrix<'a, T, D>
Source§impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, (usize, usize), &Vec<T>)> for Matrix<'a, T, D>
Available on non-crate feature no-std only.
impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, (usize, usize), &Vec<T>)> for Matrix<'a, T, D>
no-std only.Source§impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep, const N: usize> From<(&'a D, (usize, usize), [T; N])> for Matrix<'a, T, D>
impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep, const N: usize> From<(&'a D, (usize, usize), [T; N])> for Matrix<'a, T, D>
Source§impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, (usize, usize), Vec<T>)> for Matrix<'a, T, D>
Available on non-crate feature no-std only.
impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, (usize, usize), Vec<T>)> for Matrix<'a, T, D>
no-std only.Source§impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, usize, usize, &[T])> for Matrix<'a, T, D>
impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, usize, usize, &[T])> for Matrix<'a, T, D>
Source§impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep, const N: usize> From<(&'a D, usize, usize, [T; N])> for Matrix<'a, T, D>
impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep, const N: usize> From<(&'a D, usize, usize, [T; N])> for Matrix<'a, T, D>
Source§impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, usize, usize, Vec<T>)> for Matrix<'a, T, D>
Available on non-crate feature no-std only.
impl<'a, T: Copy, D: Alloc<'a, T> + IsShapeIndep> From<(&'a D, usize, usize, Vec<T>)> for Matrix<'a, T, D>
no-std only.Source§impl<'a, 'b, T> From<(&'a OpenCL, Matrix<'b, T>)> for Matrix<'a, T, OpenCL>
Available on crate feature opencl only.
impl<'a, 'b, T> From<(&'a OpenCL, Matrix<'b, T>)> for Matrix<'a, T, OpenCL>
opencl only.Source§impl<'a, T> From<(*mut T, (usize, usize))> for Matrix<'a, T>
Available on crate feature cpu only.
impl<'a, T> From<(*mut T, (usize, usize))> for Matrix<'a, T>
cpu only.Source§impl<'a, T, D: Device, S: Shape> From<(Buffer<'a, T, D, S>, (usize, usize))> for Matrix<'a, T, D, S>
impl<'a, T, D: Device, S: Shape> From<(Buffer<'a, T, D, S>, (usize, usize))> for Matrix<'a, T, D, S>
Source§impl<'a, T, D: Device, S: Shape> From<(Buffer<'a, T, D, S>, usize, usize)> for Matrix<'a, T, D, S>
impl<'a, T, D: Device, S: Shape> From<(Buffer<'a, T, D, S>, usize, usize)> for Matrix<'a, T, D, S>
Source§impl<'a, T, D, S: Shape> Mul<&Matrix<'a, T, D, S>> for Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
impl<'a, T, D, S: Shape> Mul<&Matrix<'a, T, D, S>> for Matrix<'a, T, D, S>where
D: BaseOps<T, S>,
Source§impl<T, D, S: Shape> MulAssign<&Matrix<'_, T, D, S>> for Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
impl<T, D, S: Shape> MulAssign<&Matrix<'_, T, D, S>> for Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
Source§fn mul_assign(&mut self, rhs: &Self)
fn mul_assign(&mut self, rhs: &Self)
*= operation. Read moreSource§impl<T, S, D> MulAssign<T> for Matrix<'_, T, D, S>where
S: Shape,
D: ScalarAssign<T, S>,
impl<T, S, D> MulAssign<T> for Matrix<'_, T, D, S>where
S: Shape,
D: ScalarAssign<T, S>,
Source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
*= operation. Read moreSource§impl<T, D, S: Shape> MulAssign for Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
impl<T, D, S: Shape> MulAssign for Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl<T, D, S: Shape> SubAssign<&Matrix<'_, T, D, S>> for &mut Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
impl<T, D, S: Shape> SubAssign<&Matrix<'_, T, D, S>> for &mut Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
Source§fn sub_assign(&mut self, rhs: &Matrix<'_, T, D, S>)
fn sub_assign(&mut self, rhs: &Matrix<'_, T, D, S>)
-= operation. Read moreSource§impl<T, D, S: Shape> SubAssign<&Matrix<'_, T, D, S>> for Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
impl<T, D, S: Shape> SubAssign<&Matrix<'_, T, D, S>> for Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
Source§fn sub_assign(&mut self, rhs: &Self)
fn sub_assign(&mut self, rhs: &Self)
-= operation. Read moreSource§impl<T, D, S: Shape> SubAssign<Matrix<'_, T, D, S>> for &mut Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
impl<T, D, S: Shape> SubAssign<Matrix<'_, T, D, S>> for &mut Matrix<'_, T, D, S>where
D: AssignOps<T, S, D>,
Source§fn sub_assign(&mut self, rhs: Matrix<'_, T, D, S>)
fn sub_assign(&mut self, rhs: Matrix<'_, T, D, S>)
-= operation. Read more