-
Notifications
You must be signed in to change notification settings - Fork 1
/
fr-tensor.cu
451 lines (373 loc) · 14.9 KB
/
fr-tensor.cu
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include "fr-tensor.cuh"
using namespace std;
ostream& operator<<(ostream& os, const Fr_t& x)
{
os << "0x" << std::hex;
for (uint i = 8; i > 0; -- i)
{
os << std::setfill('0') << std::setw(8) << x.val[i - 1];
}
return os << std::dec << std::setw(0) << std::setfill(' ');
}
// define the kernels
// Elementwise addition
KERNEL void Fr_elementwise_add(GLOBAL Fr_t* arr1, GLOBAL Fr_t* arr2, GLOBAL Fr_t* arr_out, uint n)
{
const uint gid = GET_GLOBAL_ID();
if (gid >= n) return;
arr_out[gid] = blstrs__scalar__Scalar_add(arr1[gid], arr2[gid]);
}
// Broadcast addition
KERNEL void Fr_broadcast_add(GLOBAL Fr_t* arr, Fr_t x, GLOBAL Fr_t* arr_out, uint n)
{
const uint gid = GET_GLOBAL_ID();
if (gid >= n) return;
arr_out[gid] = blstrs__scalar__Scalar_add(arr[gid], x);
}
// Elementwise negation
KERNEL void Fr_elementwise_neg(GLOBAL Fr_t* arr, GLOBAL Fr_t* arr_out, uint n)
{
const uint gid = GET_GLOBAL_ID();
if (gid >= n) return;
arr_out[gid] = blstrs__scalar__Scalar_sub(blstrs__scalar__Scalar_ZERO, arr[gid]);
}
// Elementwise subtraction
KERNEL void Fr_elementwise_sub(GLOBAL Fr_t* arr1, GLOBAL Fr_t* arr2, GLOBAL Fr_t* arr_out, uint n) {
const uint gid = GET_GLOBAL_ID();
if (gid >= n) return;
arr_out[gid] = blstrs__scalar__Scalar_sub(arr1[gid], arr2[gid]);
}
// Broadcast subtraction
KERNEL void Fr_broadcast_sub(GLOBAL Fr_t* arr, Fr_t x, GLOBAL Fr_t* arr_out, uint n)
{
const uint gid = GET_GLOBAL_ID();
if (gid >= n) return;
arr_out[gid] = blstrs__scalar__Scalar_sub(arr[gid], x);
}
// To montegomery form
KERNEL void Fr_elementwise_mont(GLOBAL Fr_t* arr, GLOBAL Fr_t* arr_out, uint n)
{
const uint gid = GET_GLOBAL_ID();
if (gid >= n) return;
arr_out[gid] = blstrs__scalar__Scalar_mont(arr[gid]);
}
// From montgomery form
KERNEL void Fr_elementwise_unmont(GLOBAL Fr_t* arr, GLOBAL Fr_t* arr_out, uint n)
{
const uint gid = GET_GLOBAL_ID();
if (gid >= n) return;
arr_out[gid] = blstrs__scalar__Scalar_unmont(arr[gid]);
}
// Elementwise montegomery multiplication
KERNEL void Fr_elementwise_mont_mul(GLOBAL Fr_t* arr1, GLOBAL Fr_t* arr2, GLOBAL Fr_t* arr_out, uint n) {
const uint gid = GET_GLOBAL_ID();
if (gid >= n) return;
arr_out[gid] = blstrs__scalar__Scalar_mul(arr1[gid], arr2[gid]);
}
// Broadcast montegomery multiplication
KERNEL void Fr_broadcast_mont_mul(GLOBAL Fr_t* arr, Fr_t x, GLOBAL Fr_t* arr_out, uint n)
{
const uint gid = GET_GLOBAL_ID();
if (gid >= n) return;
arr_out[gid] = blstrs__scalar__Scalar_mul(arr[gid], x);
}
// implement the class FrTensor
FrTensor::FrTensor(uint size): size(size), gpu_data(nullptr)
{
cudaMalloc((void **)&gpu_data, sizeof(Fr_t) * size);
}
FrTensor::FrTensor(uint size, const Fr_t* cpu_data): size(size), gpu_data(nullptr)
{
cudaMalloc((void **)&gpu_data, sizeof(Fr_t) * size);
cudaMemcpy(gpu_data, cpu_data, sizeof(Fr_t) * size, cudaMemcpyHostToDevice);
}
FrTensor::FrTensor(const FrTensor& t): size(t.size), gpu_data(nullptr)
{
cudaMalloc((void **)&gpu_data, sizeof(Fr_t) * size);
cudaMemcpy(gpu_data, t.gpu_data, sizeof(Fr_t) * size, cudaMemcpyDeviceToDevice);
}
FrTensor::~FrTensor()
{
cudaFree(gpu_data);
gpu_data = nullptr;
}
Fr_t FrTensor::operator()(uint idx) const
{
Fr_t out;
cudaMemcpy(&out, gpu_data + idx, sizeof(Fr_t), cudaMemcpyDeviceToHost);
return out;
}
FrTensor FrTensor::operator+(const FrTensor& t) const
{
if (size != t.size) throw std::runtime_error("Incompatible dimensions");
FrTensor out(size);
Fr_elementwise_add<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(gpu_data, t.gpu_data, out.gpu_data, size);
cudaDeviceSynchronize();
return out;
}
FrTensor FrTensor::operator+(const Fr_t& x) const
{
FrTensor out(size);
Fr_broadcast_add<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(gpu_data, x, out.gpu_data, size);
cudaDeviceSynchronize();
return out;
}
FrTensor& FrTensor::operator+=(const FrTensor& t)
{
if (size != t.size) throw std::runtime_error("Incompatible dimensions");
Fr_elementwise_add<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(gpu_data, t.gpu_data, gpu_data, size);
cudaDeviceSynchronize();
return *this;
}
FrTensor& FrTensor::operator+=(const Fr_t& x)
{
Fr_broadcast_add<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(gpu_data, x, gpu_data, size);
cudaDeviceSynchronize();
return *this;
}
FrTensor FrTensor::operator-() const
{
FrTensor out(size);
Fr_elementwise_neg<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(gpu_data, out.gpu_data, size);
cudaDeviceSynchronize();
return out;
}
FrTensor FrTensor::operator-(const FrTensor& t) const
{
if (size != t.size) throw std::runtime_error("Incompatible dimensions");
FrTensor out(size);
Fr_elementwise_sub<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(gpu_data, t.gpu_data, out.gpu_data, size);
cudaDeviceSynchronize();
return out;
}
FrTensor FrTensor::operator-(const Fr_t& x) const
{
FrTensor out(size);
Fr_broadcast_sub<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(gpu_data, x, out.gpu_data, size);
cudaDeviceSynchronize();
return out;
}
FrTensor& FrTensor::operator-=(const FrTensor& t)
{
if (size != t.size) throw std::runtime_error("Incompatible dimensions");
Fr_elementwise_sub<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(gpu_data, t.gpu_data, gpu_data, size);
cudaDeviceSynchronize();
return *this;
}
FrTensor& FrTensor::operator-=(const Fr_t& x)
{
Fr_broadcast_sub<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(gpu_data, x, gpu_data, size);
cudaDeviceSynchronize();
return *this;
}
FrTensor& FrTensor::mont()
{
Fr_elementwise_mont<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(gpu_data, gpu_data, size);
cudaDeviceSynchronize();
return *this;
}
FrTensor& FrTensor::unmont()
{
Fr_elementwise_unmont<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(gpu_data, gpu_data, size);
cudaDeviceSynchronize();
return *this;
}
FrTensor FrTensor::operator*(const FrTensor& t) const
{
if (size != t.size) throw std::runtime_error("Incompatible dimensions");
FrTensor out(size);
Fr_elementwise_mont_mul<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(gpu_data, t.gpu_data, out.gpu_data, size);
cudaDeviceSynchronize();
return out;
}
FrTensor FrTensor::operator*(const Fr_t& x) const
{
FrTensor out(size);
Fr_broadcast_mont_mul<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(gpu_data, x, out.gpu_data, size);
cudaDeviceSynchronize();
return out;
}
FrTensor& FrTensor::operator*=(const FrTensor& t)
{
if (size != t.size) throw std::runtime_error("Incompatible dimensions");
Fr_elementwise_mont_mul<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(gpu_data, t.gpu_data, gpu_data, size);
cudaDeviceSynchronize();
return *this;
}
FrTensor& FrTensor::operator*=(const Fr_t& x)
{
Fr_broadcast_mont_mul<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(gpu_data, x, gpu_data, size);
cudaDeviceSynchronize();
return *this;
}
KERNEL void Fr_sum_reduction(GLOBAL Fr_t *arr, GLOBAL Fr_t *output, uint n) {
extern __shared__ Fr_t frsum_sdata[];
unsigned int tid = threadIdx.x;
unsigned int i = blockIdx.x * (2 * blockDim.x) + threadIdx.x;
// Load input into shared memory
frsum_sdata[tid] = (i < n) ? arr[i] : blstrs__scalar__Scalar_ZERO;
if (i + blockDim.x < n) frsum_sdata[tid] = blstrs__scalar__Scalar_add(frsum_sdata[tid], arr[i + blockDim.x]);
__syncthreads();
// Reduction in shared memory
for (unsigned int s = blockDim.x / 2; s > 0; s >>= 1) {
if (tid < s) {
frsum_sdata[tid] = blstrs__scalar__Scalar_add(frsum_sdata[tid], frsum_sdata[tid + s]);
}
__syncthreads();
}
// Write the result for this block to output
if (tid == 0) output[blockIdx.x] = frsum_sdata[0];
}
Fr_t FrTensor::sum() const
{
Fr_t *ptr_input, *ptr_output;
uint curSize = size;
cudaMalloc((void**)&ptr_input, size * sizeof(Fr_t));
cudaMalloc((void**)&ptr_output, ((size + 1)/ 2) * sizeof(Fr_t));
cudaMemcpy(ptr_input, gpu_data, size * sizeof(Fr_t), cudaMemcpyDeviceToDevice);
while(curSize > 1) {
uint gridSize = (curSize + FrNumThread - 1) / FrNumThread;
Fr_sum_reduction<<<gridSize, FrNumThread, FrSharedMemorySize>>>(ptr_input, ptr_output, curSize);
cudaDeviceSynchronize(); // Ensure kernel completion before proceeding
// Swap pointers. Use the output from this step as the input for the next step.
Fr_t *temp = ptr_input;
ptr_input = ptr_output;
ptr_output = temp;
curSize = gridSize; // The output size is equivalent to the grid size used in the kernel launch
}
Fr_t finalSum;
cudaMemcpy(&finalSum, ptr_input, sizeof(Fr_t), cudaMemcpyDeviceToHost);
cudaFree(ptr_input);
cudaFree(ptr_output);
return finalSum;
}
Fr_t FrTensor::operator()(const vector<Fr_t>& u) const
{
uint log_dim = u.size();
if (size <= ((1 << log_dim) / 2) || size > (1 << log_dim)) throw std::runtime_error("Incompatible dimensions");
return Fr_me(*this, u.begin(), u.end());
}
KERNEL void random_int_kernel(Fr_t* gpu_data, uint num_bits, uint n, unsigned long seed)
{
int tid = blockIdx.x * blockDim.x + threadIdx.x;
curandState state;
// Initialize the RNG state for this thread.
curand_init(seed, tid, 0, &state);
if (tid < n) {
gpu_data[tid] = {curand(&state) & ((1U << num_bits) - 1), 0, 0, 0, 0, 0, 0, 0};
gpu_data[tid] = blstrs__scalar__Scalar_sub(gpu_data[tid], {1U << (num_bits - 1), 0, 0, 0, 0, 0, 0, 0});
}
}
FrTensor FrTensor::random_int(uint size, uint num_bits)
{
// Create a random device
std::random_device rd;
// Initialize a 64-bit Mersenne Twister random number generator
// with a seed from the random device
std::mt19937_64 rng(rd());
// Define the range for your unsigned long numbers
std::uniform_int_distribution<unsigned long> distribution(0, ULONG_MAX);
// Generate a random unsigned long number
unsigned long seed = distribution(rng);
FrTensor out(size);
random_int_kernel<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(out.gpu_data, num_bits, size, seed);
cudaDeviceSynchronize();
return out;
}
KERNEL void random_kernel(Fr_t* gpu_data, uint n, unsigned long seed)
{
int tid = blockIdx.x * blockDim.x + threadIdx.x;
curandState state;
if (tid > n) return;
// Initialize the RNG state for this thread.
curand_init(seed, tid, 0, &state);
gpu_data[tid] = {curand(&state), curand(&state), curand(&state), curand(&state), curand(&state), curand(&state), curand(&state), curand(&state) % 1944954707};
}
FrTensor FrTensor::random(uint size)
{
// Create a random device
std::random_device rd;
// Initialize a 64-bit Mersenne Twister random number generator
// with a seed from the random device
std::mt19937_64 rng(rd());
// Define the range for your unsigned long numbers
std::uniform_int_distribution<unsigned long> distribution(0, ULONG_MAX);
// Generate a random unsigned long number
unsigned long seed = distribution(rng);
FrTensor out(size);
random_kernel<<<(size+FrNumThread-1)/FrNumThread,FrNumThread>>>(out.gpu_data, size, seed);
cudaDeviceSynchronize();
return out;
}
FrTensor FrTensor::partial_me(vector<Fr_t> u, uint window_size) const
{
if (size <= window_size * (1 << (u.size() - 1))) throw std::runtime_error("Incompatible dimensions");
return Fr_partial_me(*this, u.begin(), u.end(), window_size);
}
KERNEL void Fr_split_by_window(GLOBAL Fr_t *arr_in, GLOBAL Fr_t *arr0, GLOBAL Fr_t *arr1, uint in_size, uint out_size, uint window_size)
{
const uint gid = GET_GLOBAL_ID();
if (gid >= out_size) return;
uint window_id = gid / window_size;
uint idx_in_window = gid % window_size;
uint gid0 = 2 * window_id * window_size + idx_in_window;
uint gid1 = (2 * window_id + 1) * window_size + idx_in_window;
arr0[gid] = (gid0 < in_size) ? arr_in[gid0] : blstrs__scalar__Scalar_ZERO;
arr1[gid] = (gid1 < in_size) ? arr_in[gid1] : blstrs__scalar__Scalar_ZERO;
}
std::pair<FrTensor, FrTensor> FrTensor::split(uint window_size) const
{
if (window_size < 1 || window_size >= size) throw std::runtime_error("Invalid window size.");
uint out_size = (size + 1) / 2;
std::pair<FrTensor, FrTensor> out {out_size, out_size};
Fr_split_by_window<<<(out_size+FrNumThread-1)/FrNumThread,FrNumThread>>>(gpu_data, out.first.gpu_data, out.second.gpu_data, size, out_size, window_size);
cudaDeviceSynchronize();
return out;
}
KERNEL void Fr_me_step(GLOBAL Fr_t *arr_in, GLOBAL Fr_t *arr_out, Fr_t x, uint in_size, uint out_size)
{
const uint gid = GET_GLOBAL_ID();
if (gid >= out_size) return;
uint gid0 = 2 * gid;
uint gid1 = 2 * gid + 1;
if (gid1 < in_size) arr_out[gid] = blstrs__scalar__Scalar_add(arr_in[gid0], blstrs__scalar__Scalar_mul(x, blstrs__scalar__Scalar_sub(arr_in[gid1], arr_in[gid0])));
else if (gid0 < in_size) arr_out[gid] = blstrs__scalar__Scalar_sub(arr_in[gid0], blstrs__scalar__Scalar_mul(x, arr_in[gid0]));
else arr_out[gid] = blstrs__scalar__Scalar_ZERO;
}
Fr_t Fr_me(const FrTensor& t, vector<Fr_t>::const_iterator begin, vector<Fr_t>::const_iterator end)
{
FrTensor t_new((t.size + 1) / 2);
if (begin >= end) return t(0);
Fr_me_step<<<(t_new.size+FrNumThread-1)/FrNumThread,FrNumThread>>>(t.gpu_data, t_new.gpu_data, *begin, t.size, t_new.size);
cudaDeviceSynchronize();
return Fr_me(t_new, begin + 1, end);
}
KERNEL void Fr_partial_me_step(GLOBAL Fr_t *arr_in, GLOBAL Fr_t *arr_out, Fr_t x, uint in_size, uint out_size, uint window_size)
{
const uint gid = GET_GLOBAL_ID();
if (gid >= out_size) return;
uint window_id = gid / window_size;
uint idx_in_window = gid % window_size;
uint gid0 = 2 * window_id * window_size + idx_in_window;
uint gid1 = (2 * window_id + 1) * window_size + idx_in_window;
if (gid1 < in_size) arr_out[gid] = blstrs__scalar__Scalar_add(arr_in[gid0], blstrs__scalar__Scalar_mul(x, blstrs__scalar__Scalar_sub(arr_in[gid1], arr_in[gid0])));
else if (gid0 < in_size) arr_out[gid] = blstrs__scalar__Scalar_sub(arr_in[gid0], blstrs__scalar__Scalar_mul(x, arr_in[gid0]));
else arr_out[gid] = blstrs__scalar__Scalar_ZERO;
}
FrTensor Fr_partial_me(const FrTensor& t, vector<Fr_t>::const_iterator begin, vector<Fr_t>::const_iterator end, uint window_size)
{
if (begin >= end) return t;
uint num_windows = (t.size + 2 * window_size - 1) / (2 * window_size);
uint out_size = window_size * num_windows;
FrTensor t_new(out_size);
Fr_partial_me_step<<<(t_new.size+FrNumThread-1)/FrNumThread,FrNumThread>>>(t.gpu_data, t_new.gpu_data, *begin, t.size, t_new.size, window_size);
cudaDeviceSynchronize();
return Fr_partial_me(t_new, begin + 1, end, window_size);
}
ostream& operator<<(ostream& os, const FrTensor& A)
{
os << '[';
for (uint i = 0; i < A.size - 1; ++ i) os << A(i) << '\n';
os << A(A.size-1) << ']';
return os;
}