-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench64bit.c
269 lines (201 loc) · 7 KB
/
bench64bit.c
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
#include <stdio.h>
#include <time.h>
#include <sys/resource.h>
#include "GF2x_Arithmetics.h"
#define BILLION 1000000000L
LIMB myRand(LIMB low, LIMB high) {
return rand() % (high - low + 1) + low;
}
struct timespec diff(struct timespec start, struct timespec end) {
struct timespec temp;
if ((end.tv_nsec - start.tv_nsec) < 0) {
temp.tv_sec = end.tv_sec - start.tv_sec - 1;
temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec - start.tv_sec;
temp.tv_nsec = end.tv_nsec - start.tv_nsec;
}
return temp;
}
void setResultArray(MPN *result, int RANDOM_NUMBERS) {
for (int m = 0; m < RANDOM_NUMBERS; ++m) {
MP_free(result[m]);
result[m] = init_null();
}
}
bool everything_is_fine(MPN a, MPN b) {
MPN r = init_null();
MPN result = init_null();
MP_CombRtoLMul(&result, a, b);
// print("\nresult: ", result);
// 1
MP_CombLtoRMul_w(&r, a, b, 4);
//print("\nr: ", r);
// printf("\n%d)\t%d", N, MP_compare(result, r));
if (!MP_compare(result, r))
return false;
MP_CombLtoRMul_w(&r, a, b, 8);
//print("\nr: ", r);
// printf("\t%d", MP_compare(result, r));
if (!MP_compare(result, r))
return false;
// 2
MP_KaratsubaMul(&r, a, b);
// print("\nr: ", r);
// printf("\t%d", MP_compare(result, r));
if (!MP_compare(result, r))
return false;
// 3
MP_Toom3(&r, a, b);
//print("\nr: ", r);
// printf("\t%d", MP_compare(result, r));
if (!MP_compare(result, r))
return false;
// 4
MP_Toom4(&r, a, b);
//print("\nr: ", r);
// printf("\t%d", MP_compare(result, r));
if (!MP_compare(result, r))
return false;
// 5
MP_CombLtoRMul(&r, a, b);
//print("\nr: ", r);
// printf("\t%d", MP_compare(result, r));
if (!MP_compare(result, r))
return false;
return true;
}
static inline void MP_free(MPN poly) {
free(poly.num);
} //end MP_free
void main(int argc, char *argv[]) {
// ---------------------------- optional -----------------------------
// changing stack size to avoid stack overflow during large number multiplications
const rlim_t stackSize = 128L * 1024L * 1024L; // min stack size = 128 Mb
struct rlimit rl;
int response;
// current stack limit
int dim = getrlimit(RLIMIT_STACK, &rl);
rl.rlim_cur = stackSize;
response = setrlimit(RLIMIT_STACK, &rl);
if (response != 0)
printf("error when changing stack limit!\n");
// ---------------------------- optional -----------------------------
setvbuf(stdout, 0, 2, 0);
int factors_size = atoi(argv[1]);
int random_numbers = atoi(argv[2]);
MPN result[random_numbers];
for (int m = 0; m < random_numbers; ++m) {
result[m] = init_null();
}
// Calculate time taken by a request
struct timespec requestStart, requestEnd;
// srand(time(NULL));
int l = factors_size;
printf("\n%d", l);
LIMB limbs[l];
MPN factors1[random_numbers], factors2[random_numbers];
for (int j = 0; j < random_numbers; ++j) {
for (int i = 0; i < l; ++i) {
limbs[i] = myRand(1, 0xffffffffffffff);
}
factors1[j] = init(limbs, l);
}
for (int j = 0; j < random_numbers; ++j) {
for (int i = 0; i < l; ++i) {
limbs[i] = myRand(1, 0xffffffffffffff);
}
factors2[j] = init(limbs, (unsigned) l);
}
double accum;
struct timespec time;
if (!everything_is_fine(factors1[0], factors2[0])) {
printf("Something is not working! Aborting...\n");
exit(EXIT_FAILURE);
}
//1
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &requestStart);
for (int k = 0; k < random_numbers; ++k) {
MP_CombRtoLMul(&result[k], factors1[k], factors2[k]);
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &requestEnd);
time = diff(requestStart, requestEnd);
accum = time.tv_nsec + time.tv_sec * BILLION;
accum /= BILLION;
// printf("\nMP_CombRtoLMul:\t\t%lf\n", accum);
printf("\t%lf", accum);
setResultArray(result, random_numbers);
//2
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &requestStart);
for (int k = 0; k < random_numbers; ++k) {
MP_CombLtoRMul(&result[k], factors1[k], factors2[k]);
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &requestEnd);
time = diff(requestStart, requestEnd);
accum = time.tv_nsec + time.tv_sec * BILLION;
accum /= BILLION;
// printf("\nMP_CombLtoRMul:\t\t%lf\n", accum);
printf("\t%lf", accum);
setResultArray(result, random_numbers);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &requestStart);
for (int k = 0; k < random_numbers; ++k) {
MP_CombLtoRMul_w(&result[k], factors1[k], factors2[k], 4);
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &requestEnd);
time = diff(requestStart, requestEnd);
accum = time.tv_nsec + time.tv_sec * BILLION;
accum /= BILLION;
// printf("\nMP_CombLtoRMul_w:\t%lf\n", accum);
printf("\t%lf", accum);
setResultArray(result, random_numbers);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &requestStart);
for (int k = 0; k < random_numbers; ++k) {
MP_CombLtoRMul_w(&result[k], factors1[k], factors2[k], 8);
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &requestEnd);
time = diff(requestStart, requestEnd);
accum = time.tv_nsec + time.tv_sec * BILLION;
accum /= BILLION;
// printf("\nMP_CombLtoRMul_w:\t%lf\n", accum);
printf("\t%lf", accum);
setResultArray(result, random_numbers);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &requestStart);
for (int k = 0; k < random_numbers; ++k) {
MP_KaratsubaMul(&result[k], factors1[k], factors2[k]);
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &requestEnd);
time = diff(requestStart, requestEnd);
accum = time.tv_nsec + time.tv_sec * BILLION;
accum /= BILLION;
// printf("\nMP_KaratsubaMul:\t%lf\n", accum);
printf("\t%lf", accum);
setResultArray(result, random_numbers);
//
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &requestStart);
for (int k = 0; k < random_numbers; ++k) {
MP_Toom3(&result[k], factors1[k], factors2[k]);
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &requestEnd);
time = diff(requestStart, requestEnd);
accum = time.tv_nsec + time.tv_sec * BILLION;
accum /= BILLION;
// printf("\nMP_Toom3:\t\t\t%lf\n", accum);
printf("\t%lf", accum);
setResultArray(result, random_numbers);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &requestStart);
for (int k = 0; k < random_numbers; ++k) {
MP_Toom4(&result[k], factors1[k], factors2[k]);
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &requestEnd);
time = diff(requestStart, requestEnd);
accum = time.tv_nsec + time.tv_sec * BILLION;
accum /= BILLION;
// printf("\nMP_Toom4:\t\t\t%lf\n", accum);
printf("\t%lf", accum);
for (int k = 0; k < random_numbers; ++k) {
MP_free(factors1[k]);
MP_free(factors2[k]);
MP_free(result[k]);
}
// }
}