-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChunk.cuh
More file actions
361 lines (303 loc) · 9.05 KB
/
Chunk.cuh
File metadata and controls
361 lines (303 loc) · 9.05 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
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
/*
* Chunk.h
*
* Created on: Jan 3, 2016
* Author: matan
*/
#ifndef CHUNK_H_
#define CHUNK_H_
#include <cuda_runtime.h>
#include <iostream>
#include "utils.h"
#define BASIC_MULT_SIZE 32
#define logWarpSize 5
#ifdef __CUDACC__
extern __constant__ unsigned int pentanomialCoefficients[];
#endif
/*
* A class for GF(2^N) chunks.
* A chunk represents a set of 32 elements in the field of GF(2^k) for some k.
* This implementation currently support 32 <= k < 2048.
*/
template <unsigned int N>
class Chunk {
public:
/// A chunks with all elements set to zero.
HOST_FUNCTION Chunk();
/*
* @brief Copy c'tor.
* @param chunk - The chunk to be copied.
*/
HOST_FUNCTION Chunk(const Chunk& chunk);
static const unsigned int degree = N;
/// D'tor.
HOST_FUNCTION ~Chunk(){};
// Assignment of chunks.
HOST_DEVICE_FUNCTION void operator=(const Chunk<N>& c);
// Pretty prints a chunk, useful for debugging.
HOST_FUNCTION void print();
/*
* @brief Returns the coefficients of x^idx of all polynomials in the chunk.
* @param Idx - The power of x such that the function returns the coefficients of x^idx.
* @return The coefficients of x^idx of all polynomials in the chunk.
*/
HOST_DEVICE_FUNCTION inline unsigned int getCoefficient(unsigned int idx) const;
/*
* @brief Returns the pointer to the coefficients of x^idx of all polynomials in the chunk.
* @param Idx - The power of x such that the function returns the pointer to the coefficients of x^idx.
* @return The pointer to the coefficients of x^idx of all polynomials in the chunk.
*/
HOST_DEVICE_FUNCTION unsigned int& getCoefficientPtr(unsigned int idx) const;
/*
* @brief Sets the coefficients of x^idx of all polynomials in the chunk.
* @param Idx - The power of x such that the function sets the coefficients of x^idx.
* @param value - The value of the coefficients of x^idx.
*/
HOST_DEVICE_FUNCTION inline void setCoefficient(unsigned int idx, unsigned int value);
/*
* @brief Xors the coefficients of x^idx of all polynomials in the chunk.
* @param Idx - The power of x such that the function sets the coefficients of x^idx.
* @param value - The value of the coefficients of x^idx.
*/
HOST_DEVICE_FUNCTION inline void xorCoefficient(unsigned int idx, unsigned int value);
#ifdef __CUDACC__
HOST_FUNCTION static void setPentanomial(unsigned int coefficients[5]);
#endif
static void* operator new(std::size_t size);
static void* operator new[](std::size_t size);
static void operator delete(void * ptr, std::size_t size);
static void operator delete[] (void* ptr, std::size_t size);
/// Array of coefficients of size N, represents elements in GF(2^N).
unsigned int coefficients[N];
};
template <unsigned int N>
void Chunk<N>::print()
{
for (unsigned int i = 0 ; i < N ; ++i)
{
std::cout << this->coefficients[i] << std::endl;
}
}
#ifdef __CUDACC__
template<unsigned int N>
void Chunk<N>::setPentanomial(unsigned int pentanomial[5])
{
cudaMemcpyToSymbol(pentanomialCoefficients, pentanomial, sizeof(unsigned int)*5);
}
#endif
template <unsigned int N>
void Chunk<N>::operator=(const Chunk<N>& c)
{
#ifdef __CUDACC__ //<- GPU Version
unsigned int lindex = threadIdx.x & (warpSize-1);
for (unsigned int i = lindex ; i < N ; i += warpSize)
{
this->coefficients[i] = c.coefficients[i];
}
#else //<- CPU Version
for (unsigned int i = 0 ; i < N ; ++i)
{
this->coefficients[i] = c.coefficients[i];
}
#endif
}
template <unsigned int N>
unsigned int& Chunk<N>::getCoefficientPtr(unsigned int idx) const
{
return this->coefficients[idx];
}
template <unsigned int N>
void Chunk<N>::setCoefficient(unsigned int idx, unsigned int value)
{
this->coefficients[idx] = value;
}
template <unsigned int N>
void Chunk<N>::xorCoefficient(unsigned int idx, unsigned int value)
{
this->coefficients[idx] ^= value;
}
template <unsigned int N>
unsigned int Chunk<N>::getCoefficient(unsigned int idx) const
{
return this->coefficients[idx];
}
template <unsigned int N>
Chunk<N>::Chunk()
{
for (unsigned int i = 0 ; i < N ; ++i)
{
setCoefficient(i,0);
}
if(N > 32)
setCoefficient(32,2);
}
template <unsigned int N>
void * Chunk<N>::operator new(std::size_t size)
{
void *ptr = NULL;
cudaMallocManaged(&ptr, size);
cudaDeviceSynchronize();
return ptr;
}
template <unsigned int N>
void * Chunk<N>::operator new[](std::size_t size)
{
void *ptr = NULL;
cudaMallocManaged(&ptr, size);
cudaDeviceSynchronize();
return ptr;
}
template <unsigned int N>
void Chunk<N>::operator delete(void *ptr, std::size_t size)
{
cudaDeviceSynchronize();
cudaFree(ptr);
}
template <unsigned int N>
void Chunk<N>::operator delete[](void *ptr, std::size_t size)
{
cudaDeviceSynchronize();
cudaFree(ptr);
}
template <unsigned int N>
Chunk<N>::Chunk(const Chunk& chunk)
{
for (unsigned int i = 0 ; i < N ; ++i)
{
setCoefficient(i,chunk.getCoefficient(i));
}
}
#ifdef __CUDACC__
/**
* @brief XORs chunks a & b, stores the result in both of them.
*/
__device__ inline void chunkMutualXorBasic(
Chunk<BASIC_MULT_SIZE>& a,
Chunk<BASIC_MULT_SIZE>& b
);
/*
* @brief Multiplies a chunk by b, output is written into chunk c.
* @param a, b - The chunks being multiplied.
* @param c - The chunk into which the multiplication is written to.
*/
template <unsigned int N>
__device__ inline void chunkMultiply (Chunk<N>& a, Chunk<N>& b, Chunk<2*N>& c);
/*
* @brief Multiplies a chunk by a pentanomial stored in constant memory.
* @param a - The chunks being multiplied.
* @param c - The chunk into which the multiplication is written to.
*/
template <unsigned int N>
__device__ void chunkMultiplyPentanomial(Chunk<N>&a, Chunk<2*N>& c)
{
if (N <= 64)
{
unsigned int lindex = threadIdx.x & (warpSize -1);
unsigned int inputRegs[regsPerThread(N)];
#pragma unroll
for (int i = 0 ; i < regsPerThread(N) ; ++i)
{
inputRegs[i] = a.coefficients[lindex + i*warpSize];
}
#pragma unroll
for (int i = 0 ; i < 4 ; ++i)
{
#pragma unroll
for (int j = 0 ; j < regsPerThread(N) ; ++j)
{
c.coefficients[pentanomialCoefficients[i] + lindex + j*warpSize] ^= inputRegs[j];
}
}
}
else
{
// To be implemented....
}
}
/*
* @brief Adds rhs chunk to this chunk, output is written into lhs.
* @param rhs - The chunks to be added to this chunk.
* @param lhs - Left hand side of the addition.
*/
template <unsigned int N>
__device__ void chunkAdd (Chunk<N>& lhs, const Chunk<N>& rhs);
template <unsigned int N>
__device__ void chunkAdd (Chunk<N>& lhs, const Chunk<N>& rhs)
{
unsigned int lindex = threadIdx.x & (warpSize-1);
for (unsigned int i = lindex ; i < N ; i+=32)
{
lhs.xorCoefficient(i, rhs.getCoefficient(i));
}
}
template <>
__device__ inline void chunkMultiply<32> (Chunk<32>& a, Chunk<32>& b, Chunk<64>& c)
{
unsigned int lindex = threadIdx.x & (warpSize-1);
unsigned int aCoeff = a.getCoefficient(lindex);
unsigned int bCoeff = b.getCoefficient(lindex);
unsigned int cCoeff[2] = {0};
#pragma unroll
for (unsigned int i = 0 ; i < BASIC_MULT_SIZE ; ++i)
{
unsigned int indexAdd = (i>lindex);
cCoeff[0] ^= (1-indexAdd)*__shfl(bCoeff, lindex - i) * __shfl(aCoeff,i);
cCoeff[1] ^= indexAdd * __shfl(bCoeff, lindex + warpSize - i) * __shfl(aCoeff, i);
}
c.xorCoefficient(lindex, cCoeff[0]);
c.xorCoefficient(lindex + warpSize, cCoeff[1]);
}
// Specialization of degree 64 multiplication - optimized to be used as a building block for smaller multiplications.
template <>
__device__ inline void chunkMultiply<64> (Chunk<64>& a, Chunk<64>& b, Chunk<128>& c)
{
unsigned int lindex = threadIdx.x & (warpSize-1);
unsigned int aCoeff[2];
unsigned int cCoeff[2][2] = {0};
aCoeff[0] = a.getCoefficient(lindex);
aCoeff[1] = a.getCoefficient(lindex + warpSize);
// output of the polynomial multiplication.
unsigned int my_ans[2][2]={0};
int t;
unsigned int bCoeff;
// This loop perform the shuffle. Efficiently and in parallel multiplying the polynomials.
// Distributed in the registers.
for(unsigned int k = 0 ; k < warpSize; ++k){
bCoeff = b.coefficients[k];
t = (lindex>= k);
cCoeff[0][0] ^= (t*__shfl_up(aCoeff[0],k)) & bCoeff;
cCoeff[0][1] ^= ((1-t)*__shfl_down(aCoeff[0],warpSize-k))& bCoeff;
cCoeff[0][1] ^= (t*__shfl_up(aCoeff[1],k)) & bCoeff;
cCoeff[1][0] ^= ((1-t)*__shfl_down(aCoeff[1],warpSize-k))& bCoeff;
bCoeff=b.coefficients[k+warpSize];
cCoeff[0][1] ^= (t*__shfl_up(aCoeff[0],k)) & bCoeff;
cCoeff[1][0] ^= ((1-t)*__shfl_down(aCoeff[0],warpSize-k))& bCoeff;
cCoeff[1][0] ^= (t*__shfl_up(aCoeff[1],k)) & bCoeff;
cCoeff[1][1] ^= ((1-t)*__shfl_down(aCoeff[1],warpSize-k))& bCoeff;
}
#pragma unroll
for (unsigned int i = 0 ; i < 2 ; ++i)
{
#pragma unroll
for (unsigned int j = 0 ; j < 2 ; ++j)
{
c.xorCoefficient(lindex+i*64 + j * warpSize, cCoeff[i][j]);
}
}
}
template <unsigned int N>
__device__ inline void chunkMultiply (Chunk<N>& a, Chunk<N>& b, Chunk<2*N>& c)
{
return;
}
__device__ inline void chunkMutualXorBasic(
Chunk<BASIC_MULT_SIZE>& a,
Chunk<BASIC_MULT_SIZE>& b)
{
unsigned int lindex = threadIdx.x & (warpSize-1);
unsigned int xored = a.getCoefficient(lindex) ^ b.getCoefficient(lindex);
a.setCoefficient(lindex, xored);
b.setCoefficient(lindex, xored);
}
#endif
#endif /* CHUNK_H_ */