-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdevice_arithmetic.rs
More file actions
303 lines (271 loc) · 10.8 KB
/
device_arithmetic.rs
File metadata and controls
303 lines (271 loc) · 10.8 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use super::*;
use crate::cuda_bindings::GpuError;
use bellman::Field;
use core::ops::Range;
pub enum Operation {
AddConst,
SubConst,
MulConst,
Add,
Sub,
Mul,
AddScaled,
SubScaled,
BatchInv,
GrandProd,
SetValue,
}
impl DeviceBuf<Fr> {
pub fn async_exec_op(
&mut self,
ctx: &mut GpuContext,
other: Option<&mut DeviceBuf<Fr>>,
constant: Option<Fr>,
range: Range<usize>,
op: Operation,
) -> GpuResult<()> {
assert!(
ctx.ff,
"ff is not set up on GpuContext with id {}",
ctx.device_id()
);
set_device(ctx.device_id())?;
let length = range.len();
ctx.exec_stream.wait(self.write_event())?;
ctx.exec_stream.wait(self.read_event())?;
if let Some(other) = &other {
ctx.exec_stream.wait(other.write_event())?;
}
let result = unsafe {
match op {
Operation::AddConst => {
assert!(
other.is_none(),
"other DeviceBuf should be None in AddConst operation"
);
let constant = constant.expect("constant should be Some in AddConst operation");
ff_a_plus_x(
&constant as *const Fr as *const c_void,
self.as_ptr(range.clone()) as *const c_void,
self.as_mut_ptr(range) as *mut c_void,
length as u32,
ctx.exec_stream.inner,
)
}
Operation::SubConst => {
assert!(
other.is_none(),
"other DeviceBuf should be None in SubConst operation"
);
let constant = constant.expect("constant should be Some in SubConst operation");
let mut constant = constant;
constant.negate();
ff_a_plus_x(
&constant as *const Fr as *const c_void,
self.as_ptr(range.clone()) as *const c_void,
self.as_mut_ptr(range) as *mut c_void,
length as u32,
ctx.exec_stream.inner,
)
}
Operation::MulConst => {
assert!(
other.is_none(),
"other DeviceBuf should be None in MulConst operation"
);
let constant = constant.expect("constant should be Some in MulConst operation");
ff_ax(
&constant as *const Fr as *const c_void,
self.as_ptr(range.clone()) as *const c_void,
self.as_mut_ptr(range) as *mut c_void,
length as u32,
ctx.exec_stream.inner,
)
}
Operation::Add => {
assert!(
constant.is_none(),
"constant should be None in Add operation"
);
let other = other
.as_ref()
.expect("other DeviceBuf should be Some in Add operation");
ff_x_plus_y(
self.as_ptr(range.clone()) as *const c_void,
other.as_ptr(range.clone()) as *const c_void,
self.as_mut_ptr(range) as *mut c_void,
length as u32,
ctx.exec_stream.inner,
)
}
Operation::Sub => {
assert!(
constant.is_none(),
"constant should be None in Sub operation"
);
let other = other
.as_ref()
.expect("other DeviceBuf should be Some in Sub operation");
ff_x_minus_y(
self.as_ptr(range.clone()) as *const c_void,
other.as_ptr(range.clone()) as *const c_void,
self.as_mut_ptr(range) as *mut c_void,
length as u32,
ctx.exec_stream.inner,
)
}
Operation::Mul => {
assert!(
constant.is_none(),
"constant should be None in Mul operation"
);
let other = other
.as_ref()
.expect("other DeviceBuf should be Some in Mul operation");
ff_x_mul_y(
self.as_ptr(range.clone()) as *const c_void,
other.as_ptr(range.clone()) as *const c_void,
self.as_mut_ptr(range) as *mut c_void,
length as u32,
ctx.exec_stream.inner,
)
}
Operation::AddScaled => {
let constant =
constant.expect("constant should be Some in AddScaled operation");
let other = other
.as_ref()
.expect("other DeviceBuf should be Some in AddScaled operation");
ff_ax_plus_y(
&constant as *const Fr as *const c_void,
other.as_ptr(range.clone()) as *const c_void,
self.as_ptr(range.clone()) as *const c_void,
self.as_mut_ptr(range) as *mut c_void,
length as u32,
ctx.exec_stream.inner,
)
}
Operation::SubScaled => {
let constant =
constant.expect("constant should be Some in SubScaled operation");
let other = other
.as_ref()
.expect("other DeviceBuf should be Some in SubScaled operation");
ff_x_minus_ay(
&constant as *const Fr as *const c_void,
self.as_ptr(range.clone()) as *const c_void,
other.as_ptr(range.clone()) as *const c_void,
self.as_mut_ptr(range) as *mut c_void,
length as u32,
ctx.exec_stream.inner,
)
}
Operation::BatchInv => {
assert!(
other.is_none(),
"other DeviceBuf should be None in BatchInv operation"
);
assert!(
constant.is_none(),
"constant should be None in BatchInv operation"
);
let mem_pool = ctx
.mem_pool
.expect("mem pool should be allocated in BatchInv operation");
let cfg = ff_inverse_configuration {
mem_pool,
stream: ctx.exec_stream.inner,
inputs: self.as_mut_ptr(range.clone()) as *mut c_void,
outputs: self.as_mut_ptr(range) as *mut c_void,
count: length as u32,
};
ff_inverse(cfg)
}
Operation::GrandProd => {
assert!(
other.is_none(),
"other DeviceBuf should be None in GrandProd operation"
);
assert!(
constant.is_none(),
"constant should be None in GrandProd operation"
);
let mem_pool = ctx
.mem_pool
.expect("mem pool should be allocated in GrandProd operation");
let cfg = ff_grand_product_configuration {
mem_pool,
stream: ctx.exec_stream.inner,
inputs: self.as_mut_ptr(range.clone()) as *mut c_void,
outputs: self.as_mut_ptr(range) as *mut c_void,
count: length as u32,
};
ff_grand_product(cfg)
}
Operation::SetValue => {
assert!(
other.is_none(),
"other DeviceBuf should be None in SetValue operation"
);
let constant = constant.expect("constant should be Some in SetValue operation");
self.data_is_set = true;
ff_set_value(
self.as_mut_ptr(range) as *mut c_void,
&constant as *const Fr as *const c_void,
length as u32,
ctx.exec_stream.inner,
)
}
_ => unreachable!(),
}
};
if result != 0 {
return Err(GpuError::ArithmeticErr(result));
}
assert!(self.data_is_set, "DeviceBuf should be filled with some data");
self.write_event.record(&ctx.exec_stream)?;
if let Some(other) = other {
assert!(other.data_is_set, "DeviceBuf should be filled with some data");
other.read_event.record(&ctx.exec_stream)?;
}
Ok(())
}
// output[i] = input[i] * w^((i + offset)*shift)
// w^(2^log_degree) = 1
pub fn distribute_omega_powers(
&mut self,
ctx: &mut GpuContext,
log_degree: usize,
offset: usize,
shift: usize,
inverse: bool,
) -> GpuResult<()> {
assert!(self.data_is_set, "DeviceBuf should be filled with some data");
assert!(
ctx.ff,
"ff is not set up on GpuContext with id {}",
ctx.device_id()
);
set_device(ctx.device_id())?;
let length = self.len();
ctx.exec_stream.wait(self.write_event())?;
ctx.exec_stream.wait(self.read_event())?;
unsafe {
let result = ff_omega_shift(
self.as_ptr(0..length) as *const c_void,
self.as_mut_ptr(0..length) as *mut c_void,
log_degree as u32,
shift as u32,
offset as u32,
length as u32,
inverse,
ctx.exec_stream.inner,
);
if result != 0 {
return Err(GpuError::DistributeOmegasErr(result));
}
}
self.write_event.record(&ctx.exec_stream)?;
Ok(())
}
}