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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use crate::{cpu::scalar_apply, Matrix};
use custos::{impl_stack, number::Number, CDatatype, Device, MainMemory, Shape};
#[cfg(feature = "cpu")]
use custos::CPU;
#[cfg(feature = "stack")]
use custos::Stack;
#[cfg(feature = "opencl")]
use crate::opencl::cl_scalar_op_mat;
#[cfg(feature = "opencl")]
use custos::OpenCL;
#[cfg(feature = "cuda")]
use crate::cuda::cu_scalar_op;
#[cfg(feature = "cuda")]
use custos::CUDA;
impl<'a, T, D, S> Matrix<'a, T, D, S>
where
D: AdditionalOps<T, S>,
S: Shape,
{
#[inline]
pub fn adds(&self, rhs: T) -> Self {
self.device().adds(self, rhs)
}
#[inline]
pub fn subs(&self, rhs: T) -> Self {
self.device().subs(self, rhs)
}
#[inline]
pub fn muls(&self, rhs: T) -> Self {
self.device().muls(self, rhs)
}
#[inline]
pub fn divs(&self, rhs: T) -> Self {
self.device().divs(self, rhs)
}
}
pub trait AdditionalOps<T, S: Shape = (), D: Device = Self>: Device {
fn adds(&self, lhs: &Matrix<T, D, S>, rhs: T) -> Matrix<T, Self, S>;
fn subs(&self, lhs: &Matrix<T, D, S>, rhs: T) -> Matrix<T, Self, S>;
fn muls(&self, lhs: &Matrix<T, D, S>, rhs: T) -> Matrix<T, Self, S>;
fn divs(&self, lhs: &Matrix<T, D, S>, rhs: T) -> Matrix<T, Self, S>;
}
#[cfg(feature = "cuda")]
impl<T: CDatatype> AdditionalOps<T> for CUDA {
#[inline]
fn adds(&self, lhs: &Matrix<T, CUDA>, rhs: T) -> Matrix<T, CUDA> {
(cu_scalar_op(self, lhs, rhs, "+").unwrap(), lhs.dims()).into()
}
#[inline]
fn muls(&self, lhs: &Matrix<T, CUDA>, rhs: T) -> Matrix<T, CUDA> {
(cu_scalar_op(self, lhs, rhs, "*").unwrap(), lhs.dims()).into()
}
#[inline]
fn divs(&self, lhs: &Matrix<T, CUDA>, rhs: T) -> Matrix<T, CUDA> {
(cu_scalar_op(self, lhs, rhs, "/").unwrap(), lhs.dims()).into()
}
fn subs(&self, lhs: &Matrix<T, Self>, rhs: T) -> Matrix<T, Self, ()> {
(cu_scalar_op(self, lhs, rhs, "-").unwrap(), lhs.dims()).into()
}
}
#[cfg(feature = "opencl")]
impl<T: CDatatype> AdditionalOps<T> for OpenCL {
#[inline]
fn adds(&self, lhs: &Matrix<T, Self>, rhs: T) -> Matrix<T, Self> {
cl_scalar_op_mat(self, lhs, rhs, "+").unwrap()
}
#[inline]
fn subs(&self, lhs: &Matrix<T, Self, ()>, rhs: T) -> Matrix<T, Self, ()> {
cl_scalar_op_mat(self, lhs, rhs, "-").unwrap()
}
#[inline]
fn muls(&self, lhs: &Matrix<T, Self>, rhs: T) -> Matrix<T, Self> {
cl_scalar_op_mat(self, lhs, rhs, "*").unwrap()
}
#[inline]
fn divs(&self, lhs: &Matrix<T, Self>, rhs: T) -> Matrix<T, Self> {
cl_scalar_op_mat(self, lhs, rhs, "/").unwrap()
}
}
#[impl_stack]
impl<T: Number, D: MainMemory, S: Shape> AdditionalOps<T, S, D> for CPU {
#[inline]
fn adds(&self, lhs: &Matrix<T, D, S>, rhs: T) -> Matrix<T, Self, S> {
scalar_apply(self, lhs, rhs, |c, a, b| *c = a + b)
}
#[inline]
fn subs(&self, lhs: &Matrix<T, D, S>, rhs: T) -> Matrix<T, Self, S> {
scalar_apply(self, lhs, rhs, |c, a, b| *c = a - b)
}
#[inline]
fn muls(&self, lhs: &Matrix<T, D, S>, rhs: T) -> Matrix<T, Self, S> {
scalar_apply(self, lhs, rhs, |c, a, b| *c = a * b)
}
#[inline]
fn divs(&self, lhs: &Matrix<T, D, S>, rhs: T) -> Matrix<T, Self, S> {
scalar_apply(self, lhs, rhs, |c, a, b| *c = a / b)
}
}