-
Notifications
You must be signed in to change notification settings - Fork 0
/
poly_mult.cu
315 lines (252 loc) · 8.8 KB
/
poly_mult.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
#include <cstdio>
#include <cassert>
#include <iostream>
#include <string>
/************************* NOTES *****************************
- The two input polynomials must have same degree, namely n-1
- The integer n must be a power of 2
**************************************************************/
using namespace std;
/*********** CUDA Helper functions from examples *******************/
struct cuda_exception {
explicit cuda_exception(const char *err) : error_info(err) {}
explicit cuda_exception(const string &err) : error_info(err) {}
string what() const throw() { return error_info; }
private:
string error_info;
};
void checkCudaError(const char *msg) {
cudaError_t err = cudaGetLastError();
if (cudaSuccess != err) {
string error_info(msg);
error_info += " : ";
error_info += cudaGetErrorString(err);
throw cuda_exception(error_info);
}
}
/*******************************************************************/
__global__ void poly_mult_ker(int *M, int n, int p) {
int id = blockIdx.x * blockDim.x + threadIdx.x;
int i = id % n;
int j = id / n;
int d = i + j;
M[(2 * n) + (d * n) + i] = (M[i] * M[n + j]) % p;
}
__global__ void reduce_terms_ker(int *M, int n, int p) {
int id = blockIdx.x * blockDim.x + threadIdx.x;
for (int k = 1; k < n; k *= 2) {
if ( id % (2*k) == 0)
M[(2*n) + id] = (M[(2*n) + id] + M[(2*n) + id + k]) % p;
// Sync to ensure values are ready for this step
__syncthreads();
}
// Sync to ensure the result is ready
__syncthreads();
if ( id % n == 0)
M[(2*n) + (2*n - 1) * n + (id / n)] = (M[(2*n) + (2*n - 1) * n + (id / n)] + M[(2*n) + id]) % p;
for (int k = 1; k < n; k *= 2) {
if ( id % (2*k) == 0 && id < ( n * (n-1)))
M[(2*n) + (n*n) + id] = (M[(2*n) + (n*n) + id] + M[(2*n) + (n*n) + id + k]) % p;
// Sync to ensure values are ready for this step
__syncthreads();
}
// Sync to ensure the result is ready
__syncthreads();
if ( id % n == 0 && id < ( n * (n-1)) )
M[(2*n) + (2*n - 1) * n + n + (id / n)] = (M[(2*n) + (2*n - 1) * n + n + (id / n)] + M[(2*n) + (n*n) + id]) % p;
}
__global__ void reduce_terms_ker_q2(int *M, int n, int p) {
int id = blockIdx.x * blockDim.x + threadIdx.x;
for (int k = 1; k < n; k *= 2) {
if ( id % (2*k) == 0)
M[(2*n) + id] = (M[(2*n) + id] + M[(2*n) + id + k]) % p;
// Sync to ensure values are ready for this step
__syncthreads();
}
// Sync to ensure the result is ready
__syncthreads();
if ( id % n == 0)
M[(2*n) + (2*n - 1) * n + (id / n)] = (M[(2*n) + (2*n - 1) * n + (id / n)] + M[(2*n) + id]) % p;
for (int k = 1; k < n; k *= 2) {
if ( id % (2*k) == 0 && id < ( n * (n-1)))
M[(2*n) + (n*n) + id] = (M[(2*n) + (n*n) + id] + M[(2*n) + (n*n) + id + k]) % p;
// Sync to ensure values are ready for this step
__syncthreads();
}
// Sync to ensure the result is ready
__syncthreads();
if ( id % n == 0 && id < ( n * (n-1)) )
M[(2*n) + (2*n - 1) * n + n + (id / n)] = (M[(2*n) + (2*n - 1) * n + n + (id / n)] + M[(2*n) + (n*n) + id]) % p;
}
void random_polynomials(int *M, size_t n, int p) {
for ( int i = 0; i < 2*n; i++ ){
int num = (int) rand() % p;
M[i] = num;
}
}
// Run kernel with coefficients of all 1 for simple verification
int poly_mult_test(int n_terms, int modulo_p, int question_id, int n_b, int n_t) {
const int n = n_terms;
const int p = modulo_p;
// size n for a, size n for b, size (2*n-1) * n for coefficients of each term, size 2*n-1 for final summed coefficients
const int worksp_size = (2 * n + ((2 * n - 1) * n) + (2*n)-1 );
// Set coefficients to be 1 to easily verify the result
int M[worksp_size] = {0};
for (int i = 0; i < 2*n; i++) {
M[i] = 1;
}
// Display input polynomials
printf("\n============== INPUT & RESULTS ==========\n\n");
printf("Input - Polynomial A Coefficients:\n");
for (int i=0; i<n; i++) {
printf("%d ", M[i]);
}
printf("\n\nInput - Polynomial B Coefficients:\n");
for (int i=0; i<n; i++) {
printf("%d ", M[i+n]);
}
printf("\n\n");
// Allocate GPU memory for the workspace
int *Md;
cudaMalloc((void **)&Md, sizeof(int)*worksp_size);
checkCudaError("allocate GPU memory for the workspace");
cudaMemcpy(Md, M, sizeof(int)*worksp_size, cudaMemcpyHostToDevice);
poly_mult_ker<<<n_b, n_t>>>(Md, n, p);
if (question_id == 1) {
reduce_terms_ker<<<n_b, n_t>>>(Md, n, p);
} else if (question_id == 2) {
reduce_terms_ker_q2<<<n_b, n_t>>>(Md, n, p);
}
// Copy GPU memory for the workspace back to host
cudaMemcpy(M, Md, sizeof(int)*worksp_size, cudaMemcpyDeviceToHost);
// Display resulting polynomial
int result_start = 2 * n + ((2 * n - 1) * n);
int result_length = (2 * n) - 1;
printf("Result - Polynomial A*B Coefficients:\n");
for (int i = result_start; i < result_start + result_length; i++)
printf("%d ", M[i]);
printf("\n");
int isCorrect = 1;
for (int i = result_start; i < result_start + result_length - 1; i++) {
int diff = abs(M[i] - M[i+1]);
if ( !(diff == 1 || diff == (p - 1)) ) isCorrect = 0;
}
if (isCorrect) {
printf("\nResult is correct!\n");
} else {
printf("\nResult is INCORRECT!\n");
}
cudaFree(Md);
return 0;
}
// Run kernel with random polynomial input
int poly_mult(int n_terms, int modulo_p, int q, int n_b, int n_t, int is_dev_mode) {
const int n = n_terms;
const int p = modulo_p;
// size n for a, size n for b, size (2*n-1) * n for coefficients of each term, size 2*n-1 for final summed coefficients
const int worksp_size = (2 * n + ((2 * n - 1) * n) + (2*n)-1 );
int M[worksp_size] = {0};
random_polynomials(M, n, p);
// Display input polynomials
printf("\n============== INPUT & RESULTS ==========\n\n");
printf("Input - Polynomial A Coefficients:\n");
for (int i=0; i<n; i++) {
printf("%d ", M[i]);
}
printf("\n\nInput - Polynomial B Coefficients:\n");
for (int i=0; i<n; i++) {
printf("%d ", M[i+n]);
}
printf("\n\n");
// Allocate GPU memory for the workspace
int *Md;
cudaMalloc((void **)&Md, sizeof(int)*worksp_size);
checkCudaError("allocate GPU memory for the workspace");
cudaMemcpy(Md, M, sizeof(int)*worksp_size, cudaMemcpyHostToDevice);
poly_mult_ker<<<n_b, n_t>>>(Md, n, p);
if (q == 1) {
reduce_terms_ker<<<n_b, n_t>>>(Md, n, p);
} else if (q == 2) {
reduce_terms_ker_q2<<<n_b, n_t>>>(Md, n, p);
}
// Copy GPU memory for the workspace back to host
cudaMemcpy(M, Md, sizeof(int)*worksp_size, cudaMemcpyDeviceToHost);
// Display workspace values if dev mode is true
if (is_dev_mode) {
//Debug workspace
printf("\nDEBUG WORKSPACE:\n");
for (int i = 2*n; i < worksp_size; ++i) {
if (i % n == 0)
printf(" . ");
printf("%d ", M[i]);
}
printf("\n\n\n");
}
// Display resulting polynomial
int result_start = 2 * n + ((2 * n - 1) * n);
int result_length = (2 * n) - 1;
printf("Result - Polynomial A*B Coefficients:\n");
for (int i = result_start; i < result_start + result_length; i++)
printf("%d ", M[i]);
printf("\n");
cudaFree(Md);
return 0;
}
void print_usage() {
printf("Usage: ./poly_mult [MODE: run, dev, test] [QUESTION ID: (integer) 1 OR 2] [ N_TERMS: integer power of 2 ] [ MODULO_P: integer small prime (e.g. 103) ]\n");
}
int validate_args(int argc, char **argv) {
if ( argc < 3 )
return 1;
string mode = argv[1];
int question_id = atoi(argv[2]);
if ( !(mode.compare("run") == 0) && !(mode.compare("dev") == 0) && !(mode.compare("test") == 0) ) {
printf("Invalid Mode!\n");
return 1;
}
if ( ! (question_id == 1 || question_id == 2) ) {
printf("Invalid Question Id!\n");
return 1;
}
if ( argc < 5 ) {
printf("Please provide values for both: [N_TERMS] [MODULO_P]\n");
return 1;
}
return 0;
}
int main(int argc, char **argv) {
if (validate_args(argc, argv)) {
print_usage();
exit(1);
}
string mode = argv[1];
int question_id = atoi(argv[2]);
int n_terms = atoi(argv[3]);
int modulo_p = atoi(argv[4]);
int n_blocks, n_threads;
if (question_id == 1) {
n_threads = n_terms;
n_blocks = n_terms;
} else if (question_id == 2) {
n_threads = 64;
n_blocks = (n_terms*n_terms) / n_threads;
}
printf("\n======= ARGUMENTS =======\n");
cout << "MODE: " << argv[1] << "\n";
printf("question_id: %d\n", question_id);
printf("n_terms: %d\n", n_terms);
printf("modulo_p: %d\n", modulo_p);
printf("n_blocks: %d\n", n_blocks);
printf("n_threads: %d\n", n_threads);
printf("=========================\n");
if ( mode.compare("run") == 0 ) {
poly_mult(n_terms, modulo_p, question_id, n_blocks, n_threads, 0);
}
if ( mode.compare("dev") == 0 ) {
poly_mult(n_terms, modulo_p, question_id, n_blocks, n_threads, 1);
}
if ( mode.compare("test") == 0 ) {
poly_mult_test(n_terms, modulo_p, question_id, n_blocks, n_threads);
}
return 0;
}