OpenCL

Struct OpenCL 

Source
pub struct OpenCL {
    pub kernel_cache: RefCell<KernelCacheCL>,
    pub cache: RefCell<Cache<OpenCL>>,
    pub inner: RefCell<CLDevice>,
    pub graph: RefCell<Graph>,
    pub cpu: CPU,
}
Expand description

Used to perform calculations with an OpenCL capable device. To make new calculations invocable, a trait providing new operations should be implemented for CLDevice.

§Example

use custos::{OpenCL, Read, Buffer, Error};

fn main() -> Result<(), Error> {
    let device = OpenCL::new(0)?;
     
    let a = Buffer::from((&device, [1.3; 25]));
    let out = device.read(&a);
     
    assert_eq!(out, vec![1.3; 5*5]);
    Ok(())
}

Fields§

§kernel_cache: RefCell<KernelCacheCL>§cache: RefCell<Cache<OpenCL>>§inner: RefCell<CLDevice>§graph: RefCell<Graph>§cpu: CPU

Implementations§

Source§

impl OpenCL

Source

pub fn new(device_idx: usize) -> Result<OpenCL, Box<dyn Error + Sync + Send>>

Returns an OpenCL at the specified device index.

§Errors
  • No device is found at the given device index
  • some other OpenCL related errors
Examples found in repository?
examples/simple.rs (line 36)
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}
Source

pub fn reset(&'static mut self)

Sets the values of the attributes cache, kernel cache, graph and CPU to their default. This cleans up any accumulated allocations.

Source

pub fn ctx(&self) -> Ref<'_, Context>

Source

pub fn queue(&self) -> Ref<'_, CommandQueue>

Source

pub fn device(&self) -> CLIntDevice

Source

pub fn global_mem_size_in_gb(&self) -> Result<f64, Box<dyn Error + Sync + Send>>

Source

pub fn max_mem_alloc_in_gb(&self) -> Result<f64, Box<dyn Error + Sync + Send>>

Source

pub fn name(&self) -> Result<String, Box<dyn Error + Sync + Send>>

Source

pub fn version(&self) -> Result<String, Box<dyn Error + Sync + Send>>

Source

pub fn unified_mem(&self) -> bool

Checks whether the device supports unified memory.

Source

pub fn set_unified_mem(&self, unified_mem: bool)

👎Deprecated since 0.6.0: Use the environment variable ‘CUSTOS_USE_UNIFIED’ set to ‘true’, ‘false’ or ‘default’[=hardware dependent] instead.

Trait Implementations§

Source§

impl<T: CDatatype + Float> ActivationOps<T> for OpenCL

Available on crate feature opencl only.
Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

inplace
Source§

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

Source§

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

inplace
Source§

impl<T: CDatatype> AdditionalOps<T> for OpenCL

Available on crate feature opencl only.
Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Allocate memory on the implemented device. Read more
Source§

fn with_slice(&self, data: &[T]) -> CLPtr<T>

Allocate new memory with data Read more
Source§

fn alloc_with_vec(&'a self, vec: Vec<T>) -> Self::Ptr<T, S>
where T: Clone,

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: CDatatype> AssignOps<T> for OpenCL

Available on crate feature opencl only.
Source§

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

Add assign Read more
Source§

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

Source§

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

Source§

impl<T: CDatatype> BaseOps<T> for OpenCL

Available on crate feature opencl only.
Source§

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

Element-wise addition Read more
Source§

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

Element-wise subtraction Read more
Source§

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

Element-wise multiplication Read more
Source§

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

Element-wise division Read more
Source§

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

Source§

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

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 OpenCL

Source§

type CT = RawCL

Source§

fn cache(&self) -> RefMut<'_, Cache<OpenCL>>
where OpenCL: RawConv,

Returns a device specific Cache.
Source§

impl<T> ClearBuf<T> for OpenCL
where T: CDatatype,

Source§

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

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

impl<T: CDatatype> ClipOp<T> for OpenCL

Available on crate feature opencl only.
Source§

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

Source§

impl<'a, T> CloneBuf<'a, T> for OpenCL

Source§

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

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

impl<T: CDatatype> ColOp<T> for OpenCL

Available on crate feature opencl only.
Source§

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

Source§

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

Source§

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

Source§

impl<T, R> CopySlice<T, R> for OpenCL
where R: RangeBounds<usize>,

Source§

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

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

impl Debug for OpenCL

Source§

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

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

impl Device for OpenCL

Source§

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

Source§

type Cache = Cache<OpenCL>

Source§

fn new() -> Result<OpenCL, 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: CDatatype> DiagflatOp<T> for OpenCL

Available on crate feature opencl only.
Source§

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

Source§

impl<T: CDatatype> FnsOps<T> for OpenCL

Available on crate feature opencl only.
Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

impl<T: CDatatype> Gemm<T> for OpenCL

Available on crate feature opencl only.
Source§

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

Source§

impl GraphReturn for OpenCL

Source§

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

Source§

impl<T: CDatatype> MaxOps<T> for OpenCL

Available on crate feature opencl only.
Source§

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

Source§

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

Source§

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

Source§

impl RawConv for OpenCL

Source§

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

Source§

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

Source§

impl<T> Read<T> for OpenCL
where T: Clone + Default,

Source§

type Read<'a> = Vec<T> where T: 'a

Source§

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

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

fn read_to_vec(&self, buf: &Buffer<'_, T, OpenCL>) -> Vec<T>

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

impl<T: CDatatype> RowOp<T> for OpenCL

Available on crate feature opencl only.
Source§

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

Source§

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

Source§

impl<T: CDatatype> ScalarAssign<T> for OpenCL

Available on crate feature opencl only.
Source§

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

Source§

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

Source§

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

Source§

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

Source§

impl<T: GenericBlas + MatrixMultiply + Float> SoftmaxOps<T> for OpenCL

Available on crate feature opencl only.
Source§

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

Source§

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

Source§

impl<T: Number> SumOps<T> for OpenCL

Available on crate feature opencl only.
Source§

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

Source§

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

Source§

impl<T: CDatatype> SumOverOps<T> for OpenCL

Available on crate feature opencl only.
Source§

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

Source§

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

Source§

impl<T: CDatatype> TransposeOp<T> for OpenCL

Available on crate feature opencl only.
Source§

fn transpose(&self, x: &Matrix<'_, T, OpenCL>) -> Matrix<'_, T, OpenCL>

Source§

impl<T> WriteBuf<T> for OpenCL

Source§

fn write(&self, buf: &mut Buffer<'_, T, OpenCL>, 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.

Auto Trait Implementations§

§

impl !Freeze for OpenCL

§

impl !RefUnwindSafe for OpenCL

§

impl !Send for OpenCL

§

impl !Sync for OpenCL

§

impl Unpin for OpenCL

§

impl UnwindSafe for OpenCL

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,