-
Notifications
You must be signed in to change notification settings - Fork 2
/
arithmetic.rs
177 lines (155 loc) · 5.68 KB
/
arithmetic.rs
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use custos::{impl_stack, number::Number, Device, MainMemory, Shape, CPU};
use crate::{ew_op, Matrix};
#[cfg(feature = "stack")]
use custos::Stack;
#[cfg(any(feature = "cuda", feature = "opencl"))]
use custos::CDatatype;
#[cfg(feature = "opencl")]
use crate::cl_tew;
#[cfg(feature = "opencl")]
use custos::OpenCL;
#[cfg(feature = "cuda")]
use crate::cu_ew;
#[cfg_attr(feature = "safe", doc = "```ignore")]
/// Element-wise +, -, *, / operations for matrices.
///
/// # Examples
#[cfg_attr(feature = "cpu", doc = "```")]
#[cfg_attr(not(feature = "cpu"), doc = "```ignore")]
/// use custos::CPU;
/// use custos_math::Matrix;
///
/// let device = CPU::new();
/// let a = Matrix::from((&device, (2, 3), [2, 4, 6, 8, 10, 12]));
/// let b = Matrix::from((&device, (2, 3), [12, 4, 3, 1, -5, -3]));
///
/// let c = &a + &b;
/// assert_eq!(c.read(), vec![14, 8, 9, 9, 5, 9]);
///
/// use custos_math::BaseOps;
/// let sub = device.sub(&a, &b);
/// assert_eq!(sub.read(), vec![-10, 0, 3, 7, 15, 15]);
/// ```
pub trait BaseOps<T, S: Shape = (), D: Device = Self>: Device {
/// Element-wise addition
/// # Example
#[cfg_attr(feature = "cpu", doc = "```")]
#[cfg_attr(not(feature = "cpu"), doc = "```ignore")]
/// use custos::CPU;
/// use custos_math::Matrix;
///
/// let device = CPU::new();
/// let a = Matrix::from((&device, 2, 3, [2, 4, 6, 8, 10, 12]));
/// let b = Matrix::from((&device, 2, 3, [12, 4, 3, 1, -5, -3]));
///
/// let c = a + b;
/// assert_eq!(c.read(), vec![14, 8, 9, 9, 5, 9]);
/// ```
fn add(&self, lhs: &Matrix<T, D, S>, rhs: &Matrix<T, D, S>) -> Matrix<T, Self, S>;
/// Element-wise subtraction
/// # Example
#[cfg_attr(feature = "cpu", doc = "```")]
#[cfg_attr(not(feature = "cpu"), doc = "```ignore")]
/// use custos::CPU;
/// use custos_math::{Matrix, BaseOps};
///
/// let device = CPU::new();
/// let a = Matrix::from((&device, 2, 3, [2, 4, 6, 8, 10, 12]));
/// let b = Matrix::from((&device, 2, 3, [12, 4, 3, 1, -5, -3]));
///
/// let sub = device.sub(&a, &b);
/// assert_eq!(sub.read(), vec![-10, 0, 3, 7, 15, 15]);
/// ```
fn sub(&self, lhs: &Matrix<T, D, S>, rhs: &Matrix<T, D, S>) -> Matrix<T, Self, S>;
/// Element-wise multiplication
/// # Example
#[cfg_attr(feature = "cpu", doc = "```")]
#[cfg_attr(not(feature = "cpu"), doc = "```ignore")]
/// use custos::CPU;
/// use custos_math::{Matrix, BaseOps};
///
/// let device = CPU::new();
/// let a = Matrix::from((&device, 2, 3, [2, 4, 6, 8, 10, 12]));
/// let b = Matrix::from((&device, 2, 3, [12, 4, 3, 1, -5, -3]));
///
/// let mul = a * b;
/// assert_eq!(mul.read(), vec![24, 16, 18, 8, -50, -36]);
/// ```
fn mul(&self, lhs: &Matrix<T, D, S>, rhs: &Matrix<T, D, S>) -> Matrix<T, Self, S>;
/// Element-wise division
/// # Example
#[cfg_attr(feature = "cpu", doc = "```")]
#[cfg_attr(not(feature = "cpu"), doc = "```ignore")]
/// use custos::CPU;
/// use custos_math::{Matrix, BaseOps};
///
/// let device = CPU::new();
/// let a = Matrix::from((&device, 2, 3, [2, 4, 6, 8, 10, 12]));
/// let b = Matrix::from((&device, 2, 3, [12, 4, 3, 1, -5, -3]));
///
/// let div = device.div(&a, &b);
/// assert_eq!(div.read(), vec![0, 1, 2, 8, -2, -4]);
/// ```
fn div(&self, lhs: &Matrix<T, D, S>, rhs: &Matrix<T, D, S>) -> Matrix<T, Self, S>;
}
#[impl_stack]
impl<T, S, D> BaseOps<T, S, D> for CPU
where
T: Number,
S: Shape,
D: MainMemory,
{
fn add(&self, lhs: &Matrix<T, D, S>, rhs: &Matrix<T, D, S>) -> Matrix<T, Self, S> {
ew_op(self, lhs, rhs, |x, y| x + y)
}
fn sub(&self, lhs: &Matrix<T, D, S>, rhs: &Matrix<T, D, S>) -> Matrix<T, Self, S> {
ew_op(self, lhs, rhs, |x, y| x - y)
}
fn mul(&self, lhs: &Matrix<T, D, S>, rhs: &Matrix<T, D, S>) -> Matrix<T, Self, S> {
ew_op(self, lhs, rhs, |x, y| x * y)
}
fn div(&self, lhs: &Matrix<T, D, S>, rhs: &Matrix<T, D, S>) -> Matrix<T, Self, S> {
ew_op(self, lhs, rhs, |x, y| x / y)
}
}
#[cfg(feature = "opencl")]
impl<T: CDatatype> BaseOps<T> for OpenCL {
fn add(&self, lhs: &Matrix<T, Self>, rhs: &Matrix<T, Self>) -> Matrix<T, Self> {
let buf = cl_tew(self, lhs, rhs, "+").unwrap();
(buf, lhs.dims()).into()
}
fn sub(&self, lhs: &Matrix<T, Self>, rhs: &Matrix<T, Self>) -> Matrix<T, Self> {
let buf = cl_tew(self, lhs, rhs, "-").unwrap();
(buf, lhs.dims()).into()
}
fn mul(&self, lhs: &Matrix<T, Self>, rhs: &Matrix<T, Self>) -> Matrix<T, Self> {
let buf = cl_tew(self, lhs, rhs, "*").unwrap();
(buf, lhs.dims()).into()
}
fn div(&self, lhs: &Matrix<T, Self>, rhs: &Matrix<T, Self>) -> Matrix<T, Self> {
let buf = cl_tew(self, lhs, rhs, "/").unwrap();
(buf, lhs.dims()).into()
}
}
#[cfg(feature = "cuda")]
impl<T: CDatatype> BaseOps<T> for custos::CUDA {
fn add(&self, lhs: &Matrix<T, Self>, rhs: &Matrix<T, Self>) -> Matrix<T, Self> {
let buf = cu_ew(self, lhs, rhs, "+").unwrap();
(buf, lhs.dims()).into()
}
fn sub(&self, lhs: &Matrix<T, Self>, rhs: &Matrix<T, Self>) -> Matrix<T, Self> {
let buf = cu_ew(self, lhs, rhs, "-").unwrap();
(buf, lhs.dims()).into()
}
fn mul(&self, lhs: &Matrix<T, Self>, rhs: &Matrix<T, Self>) -> Matrix<T, Self> {
let buf = cu_ew(self, lhs, rhs, "*").unwrap();
(buf, lhs.dims()).into()
}
fn div(&self, lhs: &Matrix<T, Self>, rhs: &Matrix<T, Self>) -> Matrix<T, Self> {
let buf = cu_ew(self, lhs, rhs, "/").unwrap();
(buf, lhs.dims()).into()
}
/*fn clear(&self, buf: &mut crate::Buffer<T>) {
cu_clear(self, buf).unwrap();
}*/
}