-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZmodF_mul-tune.c
337 lines (262 loc) · 8.46 KB
/
ZmodF_mul-tune.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
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
/*============================================================================
This file is part of FLINT.
FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===============================================================================*/
/*
ZmodF_mul-tune
Program for tuning the ZmodF_mul module.
This program writes to standard output an automatically tuned version of
ZmodF_mul-tuning.c.
(If DEBUG is set, it also writes logging info to standard error.)
(C) 2007 David Harvey and William Hart
*/
#include <stdio.h>
#include <math.h>
#include "flint.h"
#include "test-support.h"
#include "profiler.h"
#include "ZmodF_mul.h"
#include "ZmodF_mul-tuning.h"
#define DEBUG 1
typedef struct {
int algo;
int squaring;
unsigned long n;
unsigned long depth, m, k;
} sample_info_t;
// arg should point to a sample_info_t
void sample_mul(void* arg, unsigned long count)
{
ZmodF_mul_info_t info;
sample_info_t* z = (sample_info_t*) arg;
switch (z->algo)
{
case ZMODF_MUL_ALGO_PLAIN:
ZmodF_mul_info_init_plain(info, z->n, z->squaring);
break;
case ZMODF_MUL_ALGO_THREEWAY:
ZmodF_mul_info_init_threeway(info, z->n, z->squaring);
break;
case ZMODF_MUL_ALGO_FFT:
ZmodF_mul_info_init_fft(info, z->n, z->depth, z->m, z->k, z->squaring);
break;
}
mp_limb_t* in1 = (mp_limb_t*) flint_stack_alloc(z->n + 1);
mp_limb_t* in2 = (mp_limb_t*) flint_stack_alloc(z->n + 1);
mp_limb_t* out = (mp_limb_t*) flint_stack_alloc(z->n + 1);
urandom_limbs(in1, z->n + 1);
urandom_limbs(in2, z->n + 1);
if (z->squaring)
in2 = in1;
// warm up
unsigned long i;
for (i = 0; i < count/4; i++)
ZmodF_mul_info_mul(info, out, in1, in2);
// time it
start_clock(0);
unsigned long i;
for (i = 0; i < count; i++)
ZmodF_mul_info_mul(info, out, in1, in2);
stop_clock(0);
flint_stack_release();
flint_stack_release();
flint_stack_release();
ZmodF_mul_info_clear(info);
}
/*
Compares two ZmodF_mul algorithms for a specific n.
algo1/algo2 are:
0 for plain algorithm
1 for threeway algorithm
> 2 indicates FFT of given depth
Returns nonzero if algo2 is more efficient than algo1 for given n.
(n will be rounded up automatically to satisfy whatever divisibility
conditions are required by the requested algorithms.)
*/
int algo_compare(unsigned long n, unsigned long squaring,
unsigned long algo1, unsigned long algo2,
FILE* f)
{
sample_info_t info1, info2;
info1.squaring = info2.squaring = squaring;
info1.n = info2.n = n;
if (algo1 == 0)
info1.algo = ZMODF_MUL_ALGO_PLAIN;
else if (algo1 == 1)
info1.algo = ZMODF_MUL_ALGO_THREEWAY;
else
{
info1.algo = ZMODF_MUL_ALGO_FFT;
info1.depth = algo1;
info1.m = info1.k = 0;
}
if (algo2 == 0)
info2.algo = ZMODF_MUL_ALGO_PLAIN;
else if (algo2 == 1)
info2.algo = ZMODF_MUL_ALGO_THREEWAY;
else
{
info2.algo = ZMODF_MUL_ALGO_FFT;
info2.depth = algo2;
info2.m = info1.k = 0;
}
// round up n appropriately
unsigned long round = 1;
if (algo1 == 1 || algo2 == 1)
round = 3;
if (algo1 > FLINT_LG_BITS_PER_LIMB || algo2 > FLINT_LG_BITS_PER_LIMB)
round <<= (FLINT_MAX(algo1, algo2) - FLINT_LG_BITS_PER_LIMB);
n = (((n-1) / round) + 1) * round;
double time1, time2;
prof_repeat(&time1, NULL, sample_mul, &info1);
prof_repeat(&time2, NULL, sample_mul, &info2);
#if DEBUG
fprintf(f, "n = %ld, ", n);
if (algo1 == 0)
fprintf(f, "plain");
else if (algo1 == 1)
fprintf(f, "threeway");
else
fprintf(f, "FFT %ld", algo1);
fprintf(f, " vs ");
if (algo2 == 0)
fprintf(f, "plain");
else if (algo2 == 1)
fprintf(f, "threeway");
else
fprintf(f, "FFT %ld", algo2);
if (time2 < time1)
fprintf(f, ", 2nd wins");
else
fprintf(f, ", 1st wins");
fprintf(f, " (%lf vs %lf)\n", time1, time2);
#endif
return time2 < time1;
}
/*
Finds crossover value of n to get from algo1 to algo2.
If start != 0, then it's a starting estimate.
*/
unsigned long algo_threshold(unsigned long algo1, unsigned long algo2,
unsigned long squaring, unsigned long start,
FILE* f)
{
// find upper bound
unsigned long hi = start ? start : 100;
while (!algo_compare(hi, squaring, algo1, algo2, f))
hi *= 2;
hi *= 2;
#if DEBUG
fprintf(f, "upper bound is %ld\n\n", hi);
#endif
// find lower bound
unsigned long lo = hi / 2;
while (algo_compare(lo, squaring, algo1, algo2, f))
lo /= 2;
lo /= 2;
#if DEBUG
fprintf(f, "lower bound is %ld\n\n", lo);
#endif
// shrink interval until we reach tolerance of 10%
while (hi > 1.1 * lo)
{
unsigned long mid = (unsigned long) sqrt(1.0 * hi * lo);
double range = 1.0 * hi / lo;
if (algo_compare(mid, squaring, algo1, algo2, f))
{
lo = (unsigned long) (pow(range, -0.15) * lo);
hi = (unsigned long) (pow(range, -0.3) * hi);
}
else
{
lo = (unsigned long) (pow(range, 0.3) * lo);
hi = (unsigned long) (pow(range, 0.15) * hi);
}
#if DEBUG
fprintf(f, "interval is [%ld, %ld], ratio = %lf\n",
lo, hi, 1.0 * hi / lo);
#endif
}
return (unsigned long) sqrt(1.0 * hi * lo);
}
int main(int argc, char* argv[])
{
FILE* fout = stdout;
FILE* flog = stderr;
test_support_init();
fprintf(fout, "/*\n");
fprintf(fout, " Tuning values for ZmodF_mul module\n");
fprintf(fout, "\n");
fprintf(fout, " Automatically generated by ZmodF_mul-tune program\n");
fprintf(fout, "*/\n\n");
fprintf(fout, "#include \"ZmodF_mul-tuning.h\"\n");
fprintf(fout, "#include \"ZmodF_mul.h\"\n");
fprintf(fout, "\n");
fflush(fout);
int squaring;
for (squaring = 0; squaring <= 1; squaring++)
{
char* type = squaring ? "sqr" : "mul";
// plain/threeway threshold
unsigned long n;
for (n = 3; algo_compare(n, squaring, 1, 0, flog); n += 3);
fprintf(fout, "unsigned long ZmodF_%s_plain_threeway_threshold = %ld;\n",
type, n);
fflush(fout);
if (!squaring)
ZmodF_mul_plain_threeway_threshold = n;
else
ZmodF_sqr_plain_threeway_threshold = n;
// plain/fft threshold
n = algo_threshold(0, 3, squaring, 0, flog);
fprintf(fout, "unsigned long ZmodF_%s_plain_fft_threshold = %ld;\n",
type, n);
fflush(fout);
if (!squaring)
ZmodF_mul_plain_fft_threshold = n;
else
ZmodF_sqr_plain_fft_threshold = n;
// threeway/fft threshold
n = algo_threshold(1, 4, squaring, 0, flog);
fprintf(fout, "unsigned long ZmodF_%s_threeway_fft_threshold = %ld;\n",
type, n);
fflush(fout);
if (!squaring)
ZmodF_mul_threeway_fft_threshold = n;
else
ZmodF_sqr_threeway_fft_threshold = n;
// fft thresholds between different depths
fprintf(fout, "unsigned long ZmodF_%s_fft_table[20] =\n {", type);
unsigned long depth;
for (depth = 3; depth < 10; depth++)
{
n = algo_threshold(depth, depth+1, squaring, 0, flog);
if (!squaring)
ZmodF_mul_fft_table[depth - 3] = n;
else
ZmodF_sqr_fft_table[depth - 3] = n;
fprintf(fout, "%ld, ", n);
fflush(fout);
}
fprintf(fout, "0};\n\n");
if (!squaring)
ZmodF_mul_fft_table[depth - 3] = 0;
else
ZmodF_sqr_fft_table[depth - 3] = 0;
}
fprintf(fout, "\n");
fprintf(fout, "// end of file *********************************\n");
test_support_cleanup();
return 0;
}
// end of file ****************************************************************