CPU

Struct CPU 

Source
pub struct CPU {
    pub cache: RefCell<Cache<CPU>>,
    pub graph: RefCell<Graph>,
}
Expand description

A CPU is used to perform calculations on the host CPU. To make new operations invocable, a trait providing new functions should be implemented for CPU.

§Example

use custos::{CPU, Read, Buffer};

let device = CPU::new();
let a = Buffer::from((&device, [1, 2, 3]));

let out = device.read(&a);

assert_eq!(out, vec![1, 2, 3]);

Fields§

§cache: RefCell<Cache<CPU>>§graph: RefCell<Graph>

Implementations§

Source§

impl CPU

Source

pub fn new() -> CPU

Creates an CPU with an InternCPU that holds an empty vector of pointers.

Examples found in repository?
examples/cpu_device.rs (line 7)
6fn main() {
7    let device = CPU::new();
8    let a = Matrix::<f32>::new(&device, (5, 5));
9    let b = Matrix::from((&device, (5, 5), vec![1.3; 5 * 5]));
10
11    let out = device.add(&a, &b);
12
13    assert_eq!(device.read(&out), vec![1.3; 5 * 5]);
14}
More examples
Hide additional examples
examples/simple.rs (line 9)
6fn main() -> custos::Result<()> {
7    //select() ... sets CPU as 'global device'
8    // -> when device is not specified in an operation, the 'global device' is used
9    let cpu = CPU::new();
10
11    with_select(&cpu);
12    specify_device(&cpu);
13
14    using_opencl()
15}
examples/gemm.rs (line 4)
3fn main() {
4    let device = CPU::new();
5
6    let a = Matrix::from((&device, (2, 3), [1., 2., 3., 4., 5., 6.]));
7    let b = Matrix::from((&device, (3, 2), [6., 5., 4., 3., 2., 1.]));
8
9    let c = a.gemm(&b);
10    println!("c: {c:?}");
11
12    assert_eq!(c.read(), vec![20., 14., 56., 41.,]);
13}
examples/cpu_cache.rs (line 7)
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}

Trait Implementations§

Source§

impl<T: Float, D: MainMemory, S: Shape> ActivationOps<T, S, D> for CPU

Available on crate feature cpu only.
Source§

fn sigmoid(&self, x: &Matrix<'_, T, D, S>) -> Matrix<'_, T, Self, S>

Source§

fn sigmoid_grad( &self, activated: &Matrix<'_, T, D, S>, ) -> Matrix<'_, T, Self, S>

Source§

fn tanh(&self, x: &Matrix<'_, T, D, S>) -> Matrix<'_, T, Self, S>

Source§

fn tanh_grad(&self, x: &Matrix<'_, T, D, S>) -> Matrix<'_, T, Self, S>

Source§

fn relu(&self, x: &Matrix<'_, T, D, S>) -> Matrix<'_, T, Self, S>

Source§

fn relu_mut(&self, x: &mut Matrix<'_, T, D, S>)

inplace
Source§

fn relu_grad(&self, x: &Matrix<'_, T, D, S>) -> Matrix<'_, T, Self, S>

Source§

fn relu_grad_mut(&self, x: &mut Matrix<'_, T, D, S>)

inplace
Source§

impl<T: Number, D: MainMemory, S: Shape> AdditionalOps<T, S, D> for CPU

Available on crate feature cpu only.
Source§

fn adds(&self, lhs: &Matrix<'_, T, D, S>, rhs: T) -> Matrix<'_, T, Self, S>

Source§

fn subs(&self, lhs: &Matrix<'_, T, D, S>, rhs: T) -> Matrix<'_, T, Self, S>

Source§

fn muls(&self, lhs: &Matrix<'_, T, D, S>, rhs: T) -> Matrix<'_, T, Self, S>

Source§

fn divs(&self, lhs: &Matrix<'_, T, D, S>, rhs: T) -> Matrix<'_, T, Self, S>

Source§

impl<T, S> Alloc<'_, T, S> for CPU
where S: Shape,

Source§

fn alloc(&self, len: usize, flag: AllocFlag) -> CPUPtr<T>

Allocate memory on the implemented device. Read more
Source§

fn with_slice(&self, data: &[T]) -> CPUPtr<T>
where T: Clone,

Allocate new memory with data Read more
Source§

fn alloc_with_vec(&self, vec: Vec<T>) -> CPUPtr<T>

If the vector vec was allocated previously, this function can be used in order to reduce the amount of allocations, which may be faster than using a slice of vec.
Source§

fn with_array(&'a self, array: <S as Shape>::ARR<T>) -> Self::Ptr<T, S>
where T: Clone,

Source§

impl<T: Number, D: MainMemory, S: Shape> AssignOps<T, S, D> for CPU

Available on crate feature cpu only.
Source§

fn add_assign( &self, lhs: &mut Buffer<'_, T, Self, S>, rhs: &Buffer<'_, T, D, S>, )

Add assign Read more
Source§

fn sub_assign( &self, lhs: &mut Buffer<'_, T, Self, S>, rhs: &Buffer<'_, T, D, S>, )

Source§

fn mul_assign( &self, lhs: &mut Buffer<'_, T, Self, S>, rhs: &Buffer<'_, T, D, S>, )

Source§

impl<T, S, D> BaseOps<T, S, D> for CPU
where T: Number, S: Shape, D: MainMemory,

Available on crate feature cpu only.
Source§

fn add( &self, lhs: &Matrix<'_, T, D, S>, rhs: &Matrix<'_, T, D, S>, ) -> Matrix<'_, T, Self, S>

Element-wise addition Read more
Source§

fn sub( &self, lhs: &Matrix<'_, T, D, S>, rhs: &Matrix<'_, T, D, S>, ) -> Matrix<'_, T, Self, S>

Element-wise subtraction Read more
Source§

fn mul( &self, lhs: &Matrix<'_, T, D, S>, rhs: &Matrix<'_, T, D, S>, ) -> Matrix<'_, T, Self, S>

Element-wise multiplication Read more
Source§

fn div( &self, lhs: &Matrix<'_, T, D, S>, rhs: &Matrix<'_, T, D, S>, ) -> Matrix<'_, T, Self, S>

Element-wise division Read more
Source§

impl<'a, T> CacheBuf<'a, T> for CPU

Source§

fn cached(&'a self, len: usize) -> Buffer<'a, T>

Adds a buffer to the cache. Following calls will return this buffer, if the corresponding internal count matches with the id used in the cache. Read more
Source§

impl CacheReturn for CPU

Source§

type CT = RawCpuBuf

Source§

fn cache(&self) -> RefMut<'_, Cache<CPU>>

Returns a device specific Cache.
Source§

impl<T, D> ClearBuf<T, D> for CPU
where T: Default, D: MainMemory,

Source§

fn clear(&self, buf: &mut Buffer<'_, T, D>)

Sets all elements of the matrix to zero. Read more
Source§

impl<T: Number, D: MainMemory, S: Shape> ClipOp<T, S, D> for CPU

Available on crate feature cpu only.
Source§

fn clip( &self, x: &Matrix<'_, T, D, S>, min: T, max: T, ) -> Matrix<'_, T, Self, S>

Source§

impl<'a, T, S> CloneBuf<'a, T, S> for CPU
where T: Clone, S: Shape,

Source§

fn clone_buf(&'a self, buf: &Buffer<'a, T, CPU, S>) -> Buffer<'a, T, CPU, S>

Creates a deep copy of the specified buffer. Read more
Source§

impl<T: Number, D: MainMemory> ColOp<T, D> for CPU

Available on crate feature cpu only.
Source§

fn add_col( &self, lhs: &Matrix<'_, T, D>, rhs: &Matrix<'_, T, D>, ) -> Matrix<'_, T>

Source§

fn sub_col( &self, lhs: &Matrix<'_, T, D>, rhs: &Matrix<'_, T, D>, ) -> Matrix<'_, T>

Source§

fn div_col( &self, lhs: &Matrix<'_, T, D>, rhs: &Matrix<'_, T, D>, ) -> Matrix<'_, T>

Source§

impl<T, R, D> CopySlice<T, R, D> for CPU
where T: Copy, R: RangeBounds<usize>, D: MainMemory, [T]: Index<R, Output = [T]>,

Source§

fn copy_slice(&self, buf: &Buffer<'_, T, D>, range: R) -> Buffer<'_, T>

Copy a slice of the given buffer into a new buffer. Read more
Source§

impl Debug for CPU

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for CPU

Source§

fn default() -> CPU

Returns the “default value” for a type. Read more
Source§

impl Device for CPU

Source§

type Ptr<U, S: Shape> = CPUPtr<U>

Source§

type Cache = Cache<CPU>

Source§

fn new() -> Result<CPU, Box<dyn Error + Sync + Send>>

Source§

fn retrieve<T, S>( &self, len: usize, add_node: impl AddGraph, ) -> Buffer<'_, T, Self, S>
where S: Shape, Self: for<'a> Alloc<'a, T, S>,

Source§

impl<T: Default + Copy, D: MainMemory> DiagflatOp<T, D> for CPU

Available on crate feature cpu only.
Source§

fn diagflat(&self, x: &Matrix<'_, T, D>) -> Matrix<'_, T>

Source§

impl<T, D, S> FnsOps<T, S, D> for CPU
where T: Float, D: MainMemory, S: Shape,

Available on crate feature cpu only.
Source§

fn exp(&self, x: &Matrix<'_, T, D, S>) -> Matrix<'_, T, Self, S>

Source§

fn ln(&self, x: &Matrix<'_, T, D, S>) -> Matrix<'_, T, Self, S>

Source§

fn neg(&self, x: &Matrix<'_, T, D, S>) -> Matrix<'_, T, Self, S>

Source§

fn powf(&self, x: &Matrix<'_, T, D, S>, rhs: T) -> Matrix<'_, T, Self, S>

Source§

fn powi(&self, x: &Matrix<'_, T, D, S>, rhs: i32) -> Matrix<'_, T, Self, S>

Source§

impl<T, D, LS, RS, OS> Gemm<T, LS, RS, OS, D> for CPU
where T: Default + Copy + Mul<Output = T> + AddAssign, D: MainMemory, LS: Shape, RS: Shape, OS: Shape,

Available on crate feature cpu only.
Source§

fn gemm( &self, lhs: &Matrix<'_, T, D, LS>, rhs: &Matrix<'_, T, D, RS>, ) -> Matrix<'_, T, Self, OS>

Source§

impl GraphReturn for CPU

Source§

fn graph(&self) -> RefMut<'_, Graph>

Source§

impl MainMemory for CPU

Source§

fn as_ptr<T, S>(ptr: &<CPU as Device>::Ptr<T, S>) -> *const T
where S: Shape,

Source§

fn as_ptr_mut<T, S>(ptr: &mut <CPU as Device>::Ptr<T, S>) -> *mut T
where S: Shape,

Source§

impl<T: Copy + PartialOrd, D: MainMemory> MaxOps<T, D> for CPU

Available on crate feature cpu only.
Source§

fn max(&self, x: &Matrix<'_, T, D>) -> T

Source§

fn max_rows(&self, x: &Matrix<'_, T, D>) -> Matrix<'_, T>

Source§

fn max_cols(&self, x: &Matrix<'_, T, D>) -> Matrix<'_, T>

Source§

impl RawConv for CPU

Source§

fn construct<T, S>( ptr: &<CPU as Device>::Ptr<T, S>, len: usize, node: Node, ) -> <CPU as CacheReturn>::CT
where S: Shape,

Source§

fn destruct<T, S>( ct: &<CPU as CacheReturn>::CT, flag: AllocFlag, ) -> (<CPU as Device>::Ptr<T, S>, Node)
where S: Shape,

Source§

impl<T, D, S> Read<T, D, S> for CPU
where D: MainMemory, S: Shape,

Source§

type Read<'a> = &'a [T] where T: 'a, D: 'a, S: 'a

Source§

fn read<'a>( &self, buf: &'a Buffer<'_, T, D, S>, ) -> <CPU as Read<T, D, S>>::Read<'a>

Read the data of the Buffer as type Read. Read more
Source§

fn read_to_vec<'a>(&self, buf: &Buffer<'_, T, D, S>) -> Vec<T>
where T: Default + Clone,

Read the data of a buffer into a vector Read more
Source§

impl<T: Number, D: MainMemory, LS: Shape, RS: Shape> RowOp<T, LS, RS, D> for CPU

Available on crate feature cpu only.
Source§

fn add_row( &self, lhs: &Matrix<'_, T, D, LS>, rhs: &Matrix<'_, T, D, RS>, ) -> Matrix<'_, T, Self, LS>

Source§

fn add_row_mut( &self, lhs: &mut Matrix<'_, T, D, LS>, rhs: &Matrix<'_, T, D, RS>, )

Source§

impl<T, D: MainMemory, S: Shape> ScalarAssign<T, S, D> for CPU

Available on crate feature cpu only.
Source§

fn adds_assign(&self, lhs: &mut Matrix<'_, T, D, S>, rhs: T)

Source§

fn muls_assign(&self, lhs: &mut Matrix<'_, T, D, S>, rhs: T)

Source§

fn divs_assign(&self, lhs: &mut Matrix<'_, T, D, S>, rhs: T)

Source§

fn subs_assign(&self, lhs: &mut Matrix<'_, T, D, S>, rhs: T)

Source§

impl<T: Float + GenericBlas + MatrixMultiply> SoftmaxOps<T> for CPU
where CPU: ColOp<T>,

Available on crate feature cpu only.
Source§

fn softmax(&self, inputs: &Matrix<'_, T>) -> Matrix<'_, T>

Source§

fn softmax_grad( &self, activated: &Matrix<'_, T>, grads: &Matrix<'_, T>, ) -> Matrix<'_, T>

Source§

impl<T: Number, D: MainMemory, IS: Shape> SumOps<T, IS, D> for CPU

Available on crate feature cpu only.
Source§

fn sum(&self, x: &Matrix<'_, T, D, IS>) -> T

Source§

fn mean(&self, x: &Matrix<'_, T, D, IS>) -> T

Source§

impl<T: Copy + Default + AddAssign, D: MainMemory, IS: Shape, OS: Shape> SumOverOps<T, IS, OS, D> for CPU

Available on crate feature cpu only.
Source§

fn sum_rows(&self, x: &Matrix<'_, T, D, IS>) -> Matrix<'_, T, Self, OS>

Source§

fn sum_cols(&self, x: &Matrix<'_, T, D, IS>) -> Matrix<'_, T, Self, OS>

Source§

impl<T: Default + Copy, D: MainMemory, IS: Shape, OS: Shape> TransposeOp<T, IS, OS, D> for CPU

Available on crate feature cpu only.
Source§

fn transpose(&self, x: &Matrix<'_, T, D, IS>) -> Matrix<'_, T, Self, OS>

Source§

impl<T, D> WriteBuf<T, D> for CPU
where T: Copy, D: MainMemory,

Source§

fn write(&self, buf: &mut Buffer<'_, T, D>, data: &[T])

Write data to the buffer. Read more
Source§

fn write_buf( &self, _dst: &mut Buffer<'_, T, Self, S>, _src: &Buffer<'_, T, Self, S>, )

Writes data from Buffer to other Buffer.
Source§

impl<'a, T> DevicelessAble<'a, T> for CPU

Auto Trait Implementations§

§

impl !Freeze for CPU

§

impl !RefUnwindSafe for CPU

§

impl !Send for CPU

§

impl !Sync for CPU

§

impl Unpin for CPU

§

impl UnwindSafe for CPU

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, D, IS> CCEOp<T, IS> for D
where IS: Shape, T: Float, D: FnsOps<T> + ClipOp<T, IS> + BaseOps<T, IS> + SumOps<T> + SumOverOps<T, IS> + AdditionalOps<T, IS> + FnsOps<T, IS>,

Source§

fn cce_loss( &self, preds: &Matrix<'_, T, D, IS>, targets: &Matrix<'_, T, D, IS>, ) -> T

Source§

fn cce_grad<'a>( &self, preds: &Matrix<'a, T, D, IS>, targets: &Matrix<'a, T, D, IS>, ) -> Matrix<'a, T, D, IS>

Source§

fn cce<'a>( &self, preds: &Matrix<'a, T, D, S>, targets: &Matrix<'a, T, D, S>, ) -> (T, Matrix<'a, T, Self, S>)

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, 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.
Source§

impl<D> IsShapeIndep for D
where D: RawConv,