1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::{cpu::scalar_apply, Matrix};
use custos::{cpu::CPU, get_device, number::Number, CDatatype};
#[cfg(feature = "opencl")]
use crate::opencl::cl_scalar_op_mat;
#[cfg(feature = "opencl")]
use custos::CLDevice;
#[cfg(feature = "cuda")]
use crate::cuda::cu_scalar_op;
#[cfg(feature = "cuda")]
use custos::CudaDevice;
impl<'a, T: CDatatype> Matrix<'a, T> {
pub fn adds(&self, rhs: T) -> Matrix<'a, T> {
get_device!(self.device(), AdditionalOps<T>).adds(self, rhs)
}
pub fn muls(&self, rhs: T) -> Matrix<'a, T> {
get_device!(self.device(), AdditionalOps<T>).muls(self, rhs)
}
pub fn divs(&self, rhs: T) -> Matrix<'a, T> {
get_device!(self.device(), AdditionalOps<T>).divs(self, rhs)
}
}
pub trait AdditionalOps<T> {
fn adds(&self, lhs: &Matrix<T>, rhs: T) -> Matrix<T>;
fn muls(&self, lhs: &Matrix<T>, rhs: T) -> Matrix<T>;
fn divs(&self, lhs: &Matrix<T>, rhs: T) -> Matrix<T>;
}
#[cfg(feature = "cuda")]
impl<T: CDatatype> AdditionalOps<T> for CudaDevice {
fn adds(&self, lhs: &Matrix<T>, rhs: T) -> Matrix<T> {
(cu_scalar_op(self, lhs, rhs, "+").unwrap(), lhs.dims()).into()
}
fn muls(&self, lhs: &Matrix<T>, rhs: T) -> Matrix<T> {
(cu_scalar_op(self, lhs, rhs, "*").unwrap(), lhs.dims()).into()
}
fn divs(&self, lhs: &Matrix<T>, rhs: T) -> Matrix<T> {
(cu_scalar_op(self, lhs, rhs, "/").unwrap(), lhs.dims()).into()
}
}
#[cfg(feature = "opencl")]
impl<T: CDatatype> AdditionalOps<T> for CLDevice {
fn adds(&self, lhs: &Matrix<T>, rhs: T) -> Matrix<T> {
cl_scalar_op_mat(self, lhs, rhs, "+").unwrap()
}
fn muls(&self, lhs: &Matrix<T>, rhs: T) -> Matrix<T> {
cl_scalar_op_mat(self, lhs, rhs, "*").unwrap()
}
fn divs(&self, lhs: &Matrix<T>, rhs: T) -> Matrix<T> {
cl_scalar_op_mat(self, lhs, rhs, "/").unwrap()
}
}
impl<T: Number> AdditionalOps<T> for CPU {
fn adds(&self, lhs: &Matrix<T>, rhs: T) -> Matrix<T> {
scalar_apply(self, lhs, rhs, |c, a, b| *c = a + b)
}
fn muls(&self, lhs: &Matrix<T>, rhs: T) -> Matrix<T> {
scalar_apply(self, lhs, rhs, |c, a, b| *c = a * b)
}
fn divs(&self, lhs: &Matrix<T>, rhs: T) -> Matrix<T> {
scalar_apply(self, lhs, rhs, |c, a, b| *c = a / b)
}
}