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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use custos::{
devices::opencl::cl_device::OpenCL,
opencl::{
api::{enqueue_write_buffer, wait_for_event},
AsClCvoidPtr,
},
prelude::CLBuffer,
CDatatype, Device, Error, GraphReturn, WriteBuf, CPU,
};
use std::fmt::Debug;
use crate::{cl_scalar_op, cl_str_op, Matrix};
#[inline]
pub fn cl_str_op_mat<'a, T: CDatatype>(
device: &'a OpenCL,
x: &Matrix<T, OpenCL>,
op: &str,
) -> Result<Matrix<'a, T, OpenCL>, Error> {
let mut out: CLBuffer<T> = device.retrieve(x.len(), x.node.idx);
cl_str_op(device, x, &mut out, op)?;
Ok((out, x.dims()).into())
}
pub fn cl_scalar_op_mat<'a, T: CDatatype>(
device: &'a OpenCL,
x: &Matrix<T, OpenCL>,
scalar: T,
op: &str,
) -> Result<Matrix<'a, T, OpenCL>, Error> {
let out = cl_scalar_op(device, x, scalar, op)?;
Ok((out, x.dims()).into())
}
pub fn cl_write<T>(device: &OpenCL, x: &mut CLBuffer<T>, data: &[T]) {
let event = unsafe { enqueue_write_buffer(&device.queue(), x.ptr.ptr, data, true).unwrap() };
wait_for_event(event).unwrap();
}
impl<'a, T> AsClCvoidPtr for Matrix<'a, T, OpenCL> {
fn as_cvoid_ptr(&self) -> *const std::ffi::c_void {
self.ptr.ptr
}
}
impl<'a, T> AsClCvoidPtr for &Matrix<'a, T, OpenCL> {
fn as_cvoid_ptr(&self) -> *const std::ffi::c_void {
self.ptr.ptr
}
}
pub fn cpu_exec<'a, 'o, T, F>(
device: &'o OpenCL,
matrix: &Matrix<'a, T, OpenCL>,
f: F,
) -> custos::Result<Matrix<'o, T, OpenCL>>
where
F: for<'b> Fn(&'b CPU, &Matrix<T>) -> Matrix<'b, T>,
T: Copy + Default + Debug,
{
#[cfg(not(feature = "realloc"))]
if device.unified_mem() {
let no_drop = f(
&device.cpu,
&Matrix::from((matrix.ptr.host_ptr, matrix.dims)),
);
let dims = no_drop.dims();
return unsafe {
custos::opencl::construct_buffer(device, no_drop.to_buf(), matrix.node.idx)
}
.map(|buf| (buf, dims).into());
}
let cpu = CPU::new();
#[cfg(feature = "realloc")]
if device.unified_mem() {
return Ok(Matrix::from((
device,
f(&cpu, &Matrix::from((matrix.ptr.host_ptr, matrix.dims))),
)));
}
let cpu_buf: Matrix<T> = Matrix::from((&cpu, matrix.dims(), matrix.read()));
let mat: Matrix<T> = f(&cpu, &cpu_buf);
let mut convert = Matrix::from((device, mat));
convert.node = device.graph().add(convert.len(), matrix.node.idx);
Ok(convert)
}
pub fn cpu_exec_mut<T, F>(
device: &OpenCL,
matrix: &mut Matrix<T, OpenCL>,
f: F,
) -> custos::Result<()>
where
F: Fn(&CPU, &mut Matrix<T>),
T: Copy + Default,
{
let cpu = CPU::new();
if device.unified_mem() {
return Ok(f(&cpu, &mut Matrix::from((matrix.ptr.host_ptr, matrix.dims))));
}
let mut cpu_matrix = Matrix::from((&cpu, matrix.dims(), matrix.read()));
f(&cpu, &mut cpu_matrix);
device.write(matrix, &cpu_matrix);
Ok(())
}
pub fn cpu_exec_lhs_rhs<'a, 'o, T, F>(
device: &'o OpenCL,
lhs: &Matrix<'a, T, OpenCL>,
rhs: &Matrix<'a, T, OpenCL>,
f: F,
) -> custos::Result<Matrix<'o, T, OpenCL>>
where
F: for<'b> Fn(&'b CPU, &Matrix<T>, &Matrix<T>) -> Matrix<'b, T>,
T: Copy + Default + Debug,
{
let cpu = CPU::new();
#[cfg(not(feature = "realloc"))]
if device.unified_mem() {
let no_drop = f(
&device.cpu,
&Matrix::from((lhs.ptr.host_ptr, lhs.dims)),
&Matrix::from((rhs.ptr.host_ptr, rhs.dims)),
);
let no_drop_dims = no_drop.dims();
return unsafe {
custos::opencl::construct_buffer(device, no_drop.to_buf(), (lhs.node.idx, rhs.node.idx))
}
.map(|buf| (buf, no_drop_dims).into());
}
#[cfg(feature = "realloc")]
if device.unified_mem() {
return Ok(Matrix::from((
device,
f(
&cpu,
&Matrix::from((lhs.ptr.host_ptr, lhs.dims)),
&Matrix::from((rhs.ptr.host_ptr, rhs.dims)),
),
)));
}
let lhs = Matrix::from((&cpu, lhs.dims(), lhs.read()));
let rhs = Matrix::from((&cpu, rhs.dims(), rhs.read()));
let mut convert = Matrix::from((device, f(&cpu, &lhs, &rhs)));
convert.node = device
.graph()
.add(convert.len(), (lhs.node.idx, rhs.node.idx));
Ok(convert)
}
pub fn cpu_exec_lhs_rhs_mut<T, F>(
device: &OpenCL,
lhs: &mut Matrix<T, OpenCL>,
rhs: &Matrix<T, OpenCL>,
f: F,
) -> custos::Result<()>
where
F: Fn(&CPU, &mut Matrix<T>, &Matrix<T>),
T: Copy + Default,
{
let cpu = CPU::new();
if device.unified_mem() {
return Ok(f(
&cpu,
&mut Matrix::from((lhs.ptr.host_ptr, lhs.dims)),
&Matrix::from((rhs.ptr.host_ptr, rhs.dims)),
));
}
let mut cpu_lhs = Matrix::from((&cpu, lhs.dims(), lhs.read()));
let cpu_rhs = Matrix::from((&cpu, rhs.dims(), rhs.read()));
f(&cpu, &mut cpu_lhs, &cpu_rhs);
device.write(lhs, &cpu_lhs);
Ok(())
}
pub fn cpu_exec_scalar<T, F>(device: &OpenCL, matrix: &Matrix<T, OpenCL>, f: F) -> T
where
F: Fn(&CPU, &Matrix<T>) -> T,
T: Copy + Default,
{
let cpu = CPU::new();
if device.unified_mem() {
return f(&cpu, &Matrix::from((matrix.ptr.host_ptr, matrix.dims)));
}
let cpu_buf = Matrix::from((&cpu, matrix.dims(), matrix.read()));
f(&cpu, &cpu_buf)
}