tommath/etc/tune.c

177 lines
4.3 KiB
C
Raw Normal View History

2003-02-28 11:08:34 -05:00
/* Tune the Karatsuba parameters
*
* Tom St Denis, tomstdenis@iahu.ca
*/
#include <tommath.h>
#include <time.h>
2003-06-06 15:35:48 -04:00
/* how many times todo each size mult. Depends on your computer. For slow computers
* this can be low like 5 or 10. For fast [re: Athlon] should be 25 - 50 or so
*/
#define TIMES 50
2003-05-17 08:33:54 -04:00
#ifndef X86_TIMER
/* generic ISO C timer */
2003-05-29 09:35:26 -04:00
ulong64 __T;
2003-05-17 08:33:54 -04:00
void t_start(void) { __T = clock(); }
2003-05-29 09:35:26 -04:00
ulong64 t_read(void) { return clock() - __T; }
2003-05-17 08:33:54 -04:00
#else
extern void t_start(void);
2003-05-29 09:35:26 -04:00
extern ulong64 t_read(void);
2003-05-17 08:33:54 -04:00
#endif
2003-05-29 09:35:26 -04:00
ulong64
time_mult (int max)
2003-02-28 11:08:34 -05:00
{
2003-03-12 21:11:11 -05:00
int x, y;
mp_int a, b, c;
2003-02-28 11:08:34 -05:00
mp_init (&a);
mp_init (&b);
mp_init (&c);
2003-05-17 08:33:54 -04:00
t_start();
2003-05-29 09:35:26 -04:00
for (x = 32; x <= max; x += 4) {
2003-03-12 21:11:11 -05:00
mp_rand (&a, x);
mp_rand (&b, x);
2003-06-06 15:35:48 -04:00
for (y = 0; y < TIMES; y++) {
2003-02-28 11:08:34 -05:00
mp_mul (&a, &b, &c);
}
}
mp_clear (&a);
mp_clear (&b);
mp_clear (&c);
2003-05-17 08:33:54 -04:00
return t_read();
2003-02-28 11:08:34 -05:00
}
2003-05-29 09:35:26 -04:00
ulong64
time_sqr (int max)
2003-02-28 11:08:34 -05:00
{
2003-03-12 21:11:11 -05:00
int x, y;
mp_int a, b;
2003-02-28 11:08:34 -05:00
mp_init (&a);
mp_init (&b);
2003-05-17 08:33:54 -04:00
t_start();
2003-05-29 09:35:26 -04:00
for (x = 32; x <= max; x += 4) {
2003-03-12 21:11:11 -05:00
mp_rand (&a, x);
2003-06-06 15:35:48 -04:00
for (y = 0; y < TIMES; y++) {
2003-02-28 11:08:34 -05:00
mp_sqr (&a, &b);
}
}
mp_clear (&a);
mp_clear (&b);
2003-05-17 08:33:54 -04:00
return t_read();
2003-03-12 21:11:11 -05:00
}
2003-02-28 11:08:34 -05:00
int
main (void)
{
2003-06-06 15:35:48 -04:00
int best_kmult, best_tmult, best_ksquare, best_tsquare, counter;
2003-05-29 09:35:26 -04:00
ulong64 best, ti;
2003-03-12 21:11:11 -05:00
FILE *log;
2003-02-28 11:08:34 -05:00
2003-05-29 09:35:26 -04:00
best_kmult = best_ksquare = best_tmult = best_tsquare = 0;
2003-02-28 11:08:34 -05:00
/* tune multiplication first */
2003-05-29 09:35:26 -04:00
/* effectively turn TOOM off */
TOOM_SQR_CUTOFF = TOOM_MUL_CUTOFF = 100000;
2003-03-12 21:11:11 -05:00
log = fopen ("mult.log", "w");
2003-05-17 08:33:54 -04:00
best = -1;
2003-06-06 15:35:48 -04:00
counter = 16;
2003-05-17 08:33:54 -04:00
for (KARATSUBA_MUL_CUTOFF = 8; KARATSUBA_MUL_CUTOFF <= 200; KARATSUBA_MUL_CUTOFF++) {
2003-05-29 09:35:26 -04:00
ti = time_mult (300);
2003-06-06 15:35:48 -04:00
printf ("%4d : %9llu \r", KARATSUBA_MUL_CUTOFF, ti);
2003-05-17 08:33:54 -04:00
fprintf (log, "%d, %llu\n", KARATSUBA_MUL_CUTOFF, ti);
2003-02-28 11:08:34 -05:00
fflush (stdout);
if (ti < best) {
2003-06-06 15:35:48 -04:00
printf ("New best: %llu, %d \r", ti, KARATSUBA_MUL_CUTOFF);
2003-02-28 11:08:34 -05:00
best = ti;
2003-05-29 09:35:26 -04:00
best_kmult = KARATSUBA_MUL_CUTOFF;
2003-06-06 15:35:48 -04:00
counter = 16;
} else if (--counter == 0) {
printf("No better found in 16 trials.\n");
break;
2003-02-28 11:08:34 -05:00
}
}
2003-03-12 21:11:11 -05:00
fclose (log);
2003-06-06 15:35:48 -04:00
printf("Karatsuba Multiplier Cutoff (KARATSUBA_MUL_CUTOFF) == %d\n", best_kmult);
2003-05-29 09:35:26 -04:00
2003-02-28 11:08:34 -05:00
/* tune squaring */
2003-03-12 21:11:11 -05:00
log = fopen ("sqr.log", "w");
2003-05-17 08:33:54 -04:00
best = -1;
2003-06-06 15:35:48 -04:00
counter = 16;
2003-05-17 08:33:54 -04:00
for (KARATSUBA_SQR_CUTOFF = 8; KARATSUBA_SQR_CUTOFF <= 200; KARATSUBA_SQR_CUTOFF++) {
2003-05-29 09:35:26 -04:00
ti = time_sqr (300);
2003-06-06 15:35:48 -04:00
printf ("%4d : %9llu \r", KARATSUBA_SQR_CUTOFF, ti);
2003-05-17 08:33:54 -04:00
fprintf (log, "%d, %llu\n", KARATSUBA_SQR_CUTOFF, ti);
2003-02-28 11:08:34 -05:00
fflush (stdout);
if (ti < best) {
2003-06-06 15:35:48 -04:00
printf ("New best: %llu, %d \r", ti, KARATSUBA_SQR_CUTOFF);
2003-02-28 11:08:34 -05:00
best = ti;
2003-05-29 09:35:26 -04:00
best_ksquare = KARATSUBA_SQR_CUTOFF;
2003-06-06 15:35:48 -04:00
counter = 16;
} else if (--counter == 0) {
printf("No better found in 16 trials.\n");
break;
2003-02-28 11:08:34 -05:00
}
}
2003-03-12 21:11:11 -05:00
fclose (log);
2003-06-06 15:35:48 -04:00
printf("Karatsuba Squaring Cutoff (KARATSUBA_SQR_CUTOFF) == %d\n", best_ksquare);
2003-05-29 09:35:26 -04:00
KARATSUBA_MUL_CUTOFF = best_kmult;
KARATSUBA_SQR_CUTOFF = best_ksquare;
/* tune TOOM mult */
2003-06-06 15:35:48 -04:00
counter = 16;
2003-05-29 09:35:26 -04:00
log = fopen ("tmult.log", "w");
best = -1;
for (TOOM_MUL_CUTOFF = best_kmult*5; TOOM_MUL_CUTOFF <= 800; TOOM_MUL_CUTOFF++) {
ti = time_mult (1200);
2003-06-06 15:35:48 -04:00
printf ("%4d : %9llu \r", TOOM_MUL_CUTOFF, ti);
2003-05-29 09:35:26 -04:00
fprintf (log, "%d, %llu\n", TOOM_MUL_CUTOFF, ti);
fflush (stdout);
if (ti < best) {
2003-06-06 15:35:48 -04:00
printf ("New best: %llu, %d \r", ti, TOOM_MUL_CUTOFF);
2003-05-29 09:35:26 -04:00
best = ti;
best_tmult = TOOM_MUL_CUTOFF;
2003-06-06 15:35:48 -04:00
counter = 16;
} else if (--counter == 0) {
printf("No better found in 16 trials.\n");
break;
2003-05-29 09:35:26 -04:00
}
}
fclose (log);
2003-06-06 15:35:48 -04:00
printf("Toom-Cook Multiplier Cutoff (TOOM_MUL_CUTOFF) == %d\n", best_tmult);
2003-05-29 09:35:26 -04:00
/* tune TOOM sqr */
log = fopen ("tsqr.log", "w");
best = -1;
2003-06-06 15:35:48 -04:00
counter = 16;
2003-05-29 09:35:26 -04:00
for (TOOM_SQR_CUTOFF = best_ksquare*3; TOOM_SQR_CUTOFF <= 800; TOOM_SQR_CUTOFF++) {
ti = time_sqr (1200);
2003-06-06 15:35:48 -04:00
printf ("%4d : %9llu \r", TOOM_SQR_CUTOFF, ti);
2003-05-29 09:35:26 -04:00
fprintf (log, "%d, %llu\n", TOOM_SQR_CUTOFF, ti);
fflush (stdout);
if (ti < best) {
2003-06-06 15:35:48 -04:00
printf ("New best: %llu, %d \r", ti, TOOM_SQR_CUTOFF);
2003-05-29 09:35:26 -04:00
best = ti;
best_tsquare = TOOM_SQR_CUTOFF;
2003-06-06 15:35:48 -04:00
counter = 16;
} else if (--counter == 0) {
printf("No better found in 16 trials.\n");
break;
2003-05-29 09:35:26 -04:00
}
}
fclose (log);
2003-06-06 15:35:48 -04:00
printf("Toom-Cook Squaring Cutoff (TOOM_SQR_CUTOFF) == %d\n", best_tsquare);
2003-03-12 21:11:11 -05:00
2003-02-28 11:08:34 -05:00
return 0;
}