-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
312 lines (295 loc) · 6.71 KB
/
common.h
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
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <omp.h>
#include <cuda.h>
#include <gmp.h>
#include "cgbn/cgbn.h"
#include <assert.h>
#include "gpu_support.h"
/*
##################################
Different Compute Type
*/
typedef enum {
test_all,
test_unknown,
xt_add,
xt_addui,
xt_mul,
xt_sub,
xt_accumulate,
xt_div_qr,
xt_sqrt,
xt_powm_odd,
xt_mont_reduce,
xt_gcd,
xt_modinv,
} Compute_Type;
#define XT_FIRST xt_add
#define XT_LAST xt_modinv
#define XT_CURRENT xt_mul
/*
##################################
parse compute mode
*/
Compute_Type parse_compute_type(const char *name) {
if(strcmp(name, "add")==0)
return xt_add;
else if (strcmp(name, "addui"))
return xt_addui;
else if(strcmp(name, "sub")==0)
return xt_sub;
else if(strcmp(name, "accumulate")==0)
return xt_accumulate;
else if(strcmp(name, "mul")==0)
return xt_mul;
else if(strcmp(name, "div_qr")==0)
return xt_div_qr;
else if(strcmp(name, "sqrt")==0)
return xt_sqrt;
else if(strcmp(name, "powm_odd")==0)
return xt_powm_odd;
else if(strcmp(name, "mont_reduce")==0)
return xt_mont_reduce;
else if(strcmp(name, "gcd")==0)
return xt_gcd;
else if(strcmp(name, "modinv")==0)
return xt_modinv;
return test_unknown;
}
/*
##################################
return compute mode
*/
const char *actual_compute_name(Compute_Type test) {
switch(test) {
case xt_add:
return "add";
case xt_addui:
return "addui";
case xt_sub:
return "sub";
case xt_accumulate:
return "accumulate";
case xt_mul:
return "mul";
case xt_div_qr:
return "div_qr";
case xt_sqrt:
return "sqrt";
case xt_powm_odd:
return "powm_odd";
case xt_mont_reduce:
return "mont_reduce";
case xt_gcd:
return "gcd";
case xt_modinv:
return "modinv";
}
return "unknown";
}
/*
##################################
Record time
*/
struct Timer{
double t1;
Timer(): t1(omp_get_wtime()) {}
double stop(){return omp_get_wtime() - t1;}
};
/*
###################################
Data Base
* x0: scalar
* x1: scalar
* num: Big Number
*/
template<uint32_t bits>
class DataBase{
public:
cgbn_mem_t<bits> *x0;
cgbn_mem_t<bits> *x1;
cgbn_mem_t<bits> *num;
DataBase(uint32_t count){}
virtual ~DataBase(){}
};
/*
##################################
GPU_Data
* x0: scalar
* x1: scalar
* num: Big Number
*/
template<uint32_t bits>
class GPU_Data : public DataBase<bits>{
public:
GPU_Data(int count):DataBase<bits>(count){
CUDA_CHECK(cudaMalloc((void **)&this->x0, sizeof(cgbn_mem_t<bits>)*count));
CUDA_CHECK(cudaMalloc((void **)&this->x1, sizeof(cgbn_mem_t<bits>)*count));
CUDA_CHECK(cudaMalloc((void **)&this->num, sizeof(cgbn_mem_t<bits>)));
}
~GPU_Data(){
CUDA_CHECK(cudaFree(this->x0));
CUDA_CHECK(cudaFree(this->x1));
CUDA_CHECK(cudaFree(this->num));
}
};
/*
##################################
CPU_data
* x0: scalar
* x1: scalar
* num: Big Number
*/
template<uint32_t bits>
class CPU_Data : public DataBase<bits>{
public:
CPU_Data(int count):DataBase<bits>(count){
this->x0 = (cgbn_mem_t<bits> *)malloc(sizeof(cgbn_mem_t<bits>)*count);
this->x1 = (cgbn_mem_t<bits> *)malloc(sizeof(cgbn_mem_t<bits>)*count);
this->num = (cgbn_mem_t<bits> *)malloc(sizeof(cgbn_mem_t<bits>));
}
~CPU_Data(){
free(this->x0);
free(this->x1);
free(this->num);
}
};
/*
###################################
Result Base
* r: scalar
*/
template<uint32_t bits>
class ResultBase{
public:
cgbn_mem_t<bits> *r;
ResultBase(uint32_t count){}
virtual ~ResultBase(){}
};
/*
##################################
GPU_result
* r: scalar
*/
template<uint32_t bits>
class GPU_result : public ResultBase<bits>{
public:
GPU_result(int count):ResultBase<bits>(count){
CUDA_CHECK(cudaMalloc((void **)&this->r, sizeof(cgbn_mem_t<bits>)*count));
}
~GPU_result(){
CUDA_CHECK(cudaFree(this->r));
}
};
/*
##################################
CPU_result
* r: scalar
*/
template<uint32_t bits>
class CPU_result : public ResultBase<bits>{
public:
CPU_result(int count):ResultBase<bits>(count){
this->r = (cgbn_mem_t<bits> *)malloc(sizeof(cgbn_mem_t<bits>)*count);
}
~CPU_result(){
free(this->r);
}
};
/*
##################################
supported_size
* size: size of each instance
*/
bool supported_size(uint32_t size) {
return size==128 || size==256 || size==512 ||
size==1024 || size==2048 || size==3072 || size==4096 ||
size==5120 || size==6144 || size==7168 || size==8192;
}
/*
##################################
supported_tpi_size
* tpi: threads per instance
* size: size of each instance
*/
bool supported_tpi_size(uint32_t tpi, uint32_t size) {
if(size==128 && tpi==4)
return true;
else if(size==256 && (tpi==4 || tpi==8))
return true;
else if(size==512 && (tpi==4 || tpi==8 || tpi==16))
return true;
else if(size==1024 && (tpi==8 || tpi==16 || tpi==32))
return true;
else if(size==2048 && (tpi==8 || tpi==16 || tpi==32))
return true;
else if(size==3072 && (tpi==16 || tpi==32))
return true;
else if(size==4096 && (tpi==16 || tpi==32))
return true;
else if(size==5120 && tpi==32)
return true;
else if(size==6144 && tpi==32)
return true;
else if(size==7168 && tpi==32)
return true;
else if(size==8192 && tpi==32)
return true;
return false;
}
/*
* Function: from_mpz
* Description: load value from mpz object into cgbn_mem_t
* Para:
* words: target room
* count: number of words transferred
* value: mpz value
*/
void from_mpz(uint32_t *words, uint32_t count, mpz_t value) {
size_t written;
if(mpz_sizeinbase(value, 2)>count*32) {
fprintf(stderr, "from_mpz failed -- result does not fit\n");
exit(1);
}
mpz_export(words, &written, -1, sizeof(uint32_t), 0, 0, value);
while(written<count) words[written++]=0;
}
/*
##################################
print_words
* x: words array
* count: (bits + 31) / 32, number of words in array
*/
void print_words(uint32_t *x, uint32_t count){
int index;
for (index=count-1; index>=0; index--){
//little endian, so we print in reverse order
printf("%08X", x[index]);
}
printf("\n");
}
/*
##################################
compare_words
* para:
* x: words array
* y: words array
* count: (bits + 31) / 32, number of words in array
* return:
* 1 : x > y
* -1: x < y
* 0 : x = y
*/
int compare_words(uint32_t *x, uint32_t *y, uint32_t count){
int index;
for (index = count-1; index >= 0; index ++){
if (x[index] > y[index]) return 1;
else if (x[index] < y[index]) return -1;
else continue;
}
return 0;
}