-
Notifications
You must be signed in to change notification settings - Fork 65
/
ntt.cuh
328 lines (274 loc) · 11.8 KB
/
ntt.cuh
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
// Copyright Supranational LLC
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
#ifndef __SPPARK_NTT_NTT_CUH__
#define __SPPARK_NTT_NTT_CUH__
#include <cassert>
#include <iostream>
#include <util/exception.cuh>
#include <util/rusterror.h>
#include <util/gpu_t.cuh>
#include "parameters.cuh"
#include "kernels.cu"
#ifndef __CUDA_ARCH__
class NTT {
public:
enum class InputOutputOrder { NN, NR, RN, RR };
enum class Direction { forward, inverse };
enum class Type { standard, coset };
enum class Algorithm { GS, CT };
protected:
static void bit_rev(fr_t* d_out, const fr_t* d_inp,
uint32_t lg_domain_size, stream_t& stream)
{
assert(lg_domain_size <= MAX_LG_DOMAIN_SIZE);
size_t domain_size = (size_t)1 << lg_domain_size;
// aim to read 4 cache lines of consecutive data per read
size_t z_count = 512 * 4 / (8*sizeof(fr_t));
if (domain_size <= WARP_SZ)
bit_rev_permutation
<<<1, domain_size, 0, stream>>>
(d_out, d_inp, lg_domain_size);
else if (d_out == d_inp || domain_size <= z_count * z_count)
bit_rev_permutation
<<<domain_size/WARP_SZ, WARP_SZ, 0, stream>>>
(d_out, d_inp, lg_domain_size);
else if (domain_size < 128 * z_count)
bit_rev_permutation_aux
<<<1, domain_size / z_count, domain_size * sizeof(fr_t), stream>>>
(d_out, d_inp, lg_domain_size);
else
bit_rev_permutation_aux
<<<domain_size / z_count / 128, 128, z_count * 128 * sizeof(fr_t),
stream>>>
(d_out, d_inp, lg_domain_size);
CUDA_OK(cudaGetLastError());
}
private:
static void LDE_powers(fr_t* inout, bool innt, bool bitrev,
uint32_t lg_domain_size, uint32_t lg_blowup,
stream_t& stream, bool ext_pow = false)
{
size_t domain_size = (size_t)1 << lg_domain_size;
const auto gen_powers =
NTTParameters::all(innt)[stream]->partial_group_gen_powers;
if (domain_size < WARP_SZ)
LDE_distribute_powers<<<1, domain_size, 0, stream>>>
(inout, lg_blowup, bitrev, gen_powers, ext_pow);
else if (domain_size < 512)
LDE_distribute_powers<<<domain_size / WARP_SZ, WARP_SZ, 0, stream>>>
(inout, lg_blowup, bitrev, gen_powers, ext_pow);
else
LDE_distribute_powers<<<domain_size / 512, 512, 0, stream>>>
(inout, lg_blowup, bitrev, gen_powers, ext_pow);
CUDA_OK(cudaGetLastError());
}
protected:
// coset_ext_pow is only used when NTT type is coset
static void NTT_internal(fr_t* d_inout, uint32_t lg_domain_size,
InputOutputOrder order, Direction direction,
Type type, stream_t& stream,
bool coset_ext_pow = false)
{
// Pick an NTT algorithm based on the input order and the desired output
// order of the data. In certain cases, bit reversal can be avoided which
// results in a considerable performance gain.
const bool intt = direction == Direction::inverse;
const auto& ntt_parameters = *NTTParameters::all(intt)[stream];
bool bitrev;
Algorithm algorithm;
switch (order) {
case InputOutputOrder::NN:
bit_rev(d_inout, d_inout, lg_domain_size, stream);
bitrev = true;
algorithm = Algorithm::CT;
break;
case InputOutputOrder::NR:
bitrev = false;
algorithm = Algorithm::GS;
break;
case InputOutputOrder::RN:
bitrev = true;
algorithm = Algorithm::CT;
break;
case InputOutputOrder::RR:
bitrev = true;
algorithm = Algorithm::GS;
break;
default:
assert(false);
}
if (!intt && type == Type::coset)
LDE_powers(d_inout, intt, bitrev, lg_domain_size, 0, stream,
coset_ext_pow);
switch (algorithm) {
case Algorithm::GS:
GS_NTT(d_inout, lg_domain_size, intt, ntt_parameters, stream);
break;
case Algorithm::CT:
CT_NTT(d_inout, lg_domain_size, intt, ntt_parameters, stream);
break;
}
if (intt && type == Type::coset)
LDE_powers(d_inout, intt, !bitrev, lg_domain_size, 0, stream,
coset_ext_pow);
if (order == InputOutputOrder::RR)
bit_rev(d_inout, d_inout, lg_domain_size, stream);
}
public:
static RustError Base(const gpu_t& gpu, fr_t* inout, uint32_t lg_domain_size,
InputOutputOrder order, Direction direction,
Type type, bool coset_ext_pow = false)
{
if (lg_domain_size == 0)
return RustError{cudaSuccess};
try {
gpu.select();
size_t domain_size = (size_t)1 << lg_domain_size;
dev_ptr_t<fr_t> d_inout{domain_size, gpu};
gpu.HtoD(&d_inout[0], inout, domain_size);
NTT_internal(&d_inout[0], lg_domain_size, order, direction, type, gpu,
coset_ext_pow);
gpu.DtoH(inout, &d_inout[0], domain_size);
gpu.sync();
} catch (const cuda_error& e) {
gpu.sync();
#ifdef TAKE_RESPONSIBILITY_FOR_ERROR_MESSAGE
return RustError{e.code(), e.what()};
#else
return RustError{e.code()};
#endif
}
return RustError{cudaSuccess};
}
static RustError LDE(const gpu_t& gpu, fr_t* inout,
uint32_t lg_domain_size, uint32_t lg_blowup,
bool ext_pow = false)
{
try {
gpu.select();
size_t domain_size = (size_t)1 << lg_domain_size;
size_t ext_domain_size = domain_size << lg_blowup;
dev_ptr_t<fr_t> d_ext_domain{ext_domain_size, gpu};
fr_t* d_domain = &d_ext_domain[ext_domain_size - domain_size];
gpu.HtoD(&d_domain[0], inout, domain_size);
NTT_internal(&d_domain[0], lg_domain_size,
InputOutputOrder::NR, Direction::inverse,
Type::standard, gpu);
const auto gen_powers =
NTTParameters::all()[gpu.id()]->partial_group_gen_powers;
LDE_launch(gpu, &d_ext_domain[0], &d_domain[0],
gen_powers, lg_domain_size, lg_blowup, ext_pow);
NTT_internal(&d_ext_domain[0], lg_domain_size + lg_blowup,
InputOutputOrder::RN, Direction::forward,
Type::standard, gpu);
gpu.DtoH(inout, &d_ext_domain[0], ext_domain_size);
gpu.sync();
} catch (const cuda_error& e) {
gpu.sync();
#ifdef TAKE_RESPONSIBILITY_FOR_ERROR_MESSAGE
return RustError{e.code(), e.what()};
#else
return RustError{e.code()};
#endif
}
return RustError{cudaSuccess};
}
protected:
static void LDE_launch(stream_t& stream,
fr_t* ext_domain_data, fr_t* domain_data,
const fr_t (*gen_powers)[WINDOW_SIZE],
uint32_t lg_domain_size, uint32_t lg_blowup,
bool perform_shift = true, bool ext_pow = false)
{
assert(lg_domain_size + lg_blowup <= MAX_LG_DOMAIN_SIZE);
size_t domain_size = (size_t)1 << lg_domain_size;
size_t ext_domain_size = domain_size << lg_blowup;
const cudaDeviceProp& gpu_prop = gpu_props(stream.id());
// Determine the max power of 2 SM count
size_t kernel_sms = gpu_prop.multiProcessorCount;
while (kernel_sms & (kernel_sms - 1))
kernel_sms -= (kernel_sms & (0 - kernel_sms));
size_t device_max_threads = kernel_sms * 1024;
uint32_t num_blocks, block_size;
if (device_max_threads < domain_size) {
num_blocks = kernel_sms;
block_size = 1024;
} else if (domain_size < 1024) {
num_blocks = 1;
block_size = domain_size;
} else {
num_blocks = domain_size / 1024;
block_size = 1024;
}
stream.launch_coop(LDE_spread_distribute_powers,
{dim3(num_blocks), dim3(block_size),
sizeof(fr_t) * block_size},
ext_domain_data, domain_data, gen_powers,
lg_domain_size, lg_blowup, perform_shift, ext_pow);
}
public:
static RustError LDE_aux(const gpu_t& gpu, fr_t* inout,
uint32_t lg_domain_size, uint32_t lg_blowup,
bool ext_pow = false)
{
try {
size_t domain_size = (size_t)1 << lg_domain_size;
size_t ext_domain_size = domain_size << lg_blowup;
// The 2nd to last 'domain_size' chunk will hold the original data
// The last chunk will get the bit reversed iNTT data
dev_ptr_t<fr_t> d_inout{ext_domain_size + domain_size, gpu}; // + domain_size for aux buffer
fr_t* aux_data = &d_inout[ext_domain_size];
fr_t* domain_data = &d_inout[ext_domain_size - domain_size]; // aligned to the end
fr_t* ext_domain_data = &d_inout[0];
gpu.HtoD(domain_data, inout, domain_size);
NTT_internal(domain_data, lg_domain_size,
InputOutputOrder::NR, Direction::inverse,
Type::standard, gpu);
const auto gen_powers =
NTTParameters::all()[gpu.id()]->partial_group_gen_powers;
bit_rev(aux_data, domain_data, lg_domain_size, gpu);
LDE_launch(gpu, ext_domain_data, domain_data, gen_powers,
lg_domain_size, lg_blowup, true, ext_pow);
// NTT - RN
NTT_internal(ext_domain_data, lg_domain_size + lg_blowup,
InputOutputOrder::RN, Direction::forward,
Type::standard, gpu);
gpu.DtoH(inout, ext_domain_data, domain_size << lg_blowup);
gpu.sync();
} catch (const cuda_error& e) {
gpu.sync();
#ifdef TAKE_RESPONSIBILITY_FOR_ERROR_MESSAGE
return RustError{e.code(), e.what()};
#else
return RustError{e.code()};
#endif
}
return RustError{cudaSuccess};
}
// coset_ext_pow is only used when NTT type is coset
static void Base_dev_ptr(stream_t& stream, fr_t* d_inout,
uint32_t lg_domain_size, InputOutputOrder order,
Direction direction, Type type,
bool coset_ext_pow = false)
{
size_t domain_size = (size_t)1 << lg_domain_size;
NTT_internal(&d_inout[0], lg_domain_size, order, direction, type,
stream, coset_ext_pow);
}
static void LDE_powers(stream_t& stream, fr_t* d_inout,
uint32_t lg_domain_size, bool ext_pow = false)
{
LDE_powers(d_inout, false, true, lg_domain_size, 0, stream, ext_pow);
}
// If d_out and d_in overlap, d_out is expected to encompass d_in and
// d_in is expected to be aligned to the end of d_out
// The input is expected to be in bit-reversed order
static void LDE_expand(stream_t& stream, fr_t* d_out, fr_t* d_in,
uint32_t lg_domain_size, uint32_t lg_blowup)
{
LDE_launch(stream, d_out, d_in, NULL, lg_domain_size, lg_blowup, false);
}
};
#endif
#endif