improve demo/timing a bit

* less verbose output on console
* allow changing name for logs
* pre-heat caches before starting the timing
This commit is contained in:
Steffen Jaeckel 2019-10-28 15:07:58 +01:00
parent 8e3a440464
commit 04ee1e75d7

View File

@ -20,6 +20,11 @@
#define LTM_TIMING_RAND_SEED 23 #define LTM_TIMING_RAND_SEED 23
#endif #endif
#ifndef MP_VERSION
#define MP_TIMING_VERSION
#else
#define MP_TIMING_VERSION "-" MP_VERSION
#endif
static void ndraw(const mp_int *a, const char *name) static void ndraw(const mp_int *a, const char *name)
{ {
@ -85,13 +90,14 @@ static uint64_t TIMFUNC(void)
#endif #endif
} }
#define DO2(x) x; x
#define DO4(x) DO2(x); DO2(x)
#define DO8(x) DO4(x); DO4(x)
#if 1 #if 1
#define DO(x) x; x; #define DO(x) DO2(x)
#else #else
#define DO2(x) x; x; #define DO(x) DO8(x); DO8(x)
#define DO4(x) DO2(x); DO2(x);
#define DO8(x) DO4(x); DO4(x);
#define DO(x) DO8(x); DO8(x);
#endif #endif
#ifdef TIMING_NO_LOGS #ifdef TIMING_NO_LOGS
@ -99,11 +105,13 @@ static uint64_t TIMFUNC(void)
#define FPRINTF(a,b,c,d) #define FPRINTF(a,b,c,d)
#define FFLUSH(a) #define FFLUSH(a)
#define FCLOSE(a) (void)(a) #define FCLOSE(a) (void)(a)
#define PRINTLN(fm,b,n,m) printf(fm "\n", b, n, m)
#else #else
#define FOPEN(a,b) fopen(a,b) #define FOPEN(a,b) fopen(a,b)
#define FPRINTF(a,b,c,d) fprintf(a,b,c,d) #define FPRINTF(a,b,c,d) fprintf(a,b,c,d)
#define FFLUSH(a) fflush(a) #define FFLUSH(a) fflush(a)
#define FCLOSE(a) fclose(a) #define FCLOSE(a) fclose(a)
#define PRINTLN(fm,b,n,m) do { printf("\r" fm, b, n, m); fflush(stdout); }while(0)
#endif #endif
static int should_test(const char *test, int argc, char **argv) static int should_test(const char *test, int argc, char **argv)
@ -178,7 +186,7 @@ int main(int argc, char **argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} while (++rr < 100u); } while (++rr < 100u);
printf("Prime-check\t%s(%2d) => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n", PRINTLN("Prime-check\t%s(%2d) => %9" PRIu64 "/sec, %9" PRIu64 " cycles",
name, cnt, CLK_PER_SEC / tt, tt); name, cnt, CLK_PER_SEC / tt, tt);
} }
} }
@ -186,11 +194,12 @@ int main(int argc, char **argv)
#endif #endif
if (should_test("add", argc, argv) != 0) { if (should_test("add", argc, argv) != 0) {
log = FOPEN("logs/add.log", "w"); log = FOPEN("logs/add" MP_TIMING_VERSION ".log", "w");
for (cnt = 8; cnt <= 128; cnt += 8) { for (cnt = 8; cnt <= 128; cnt += 8) {
SLEEP; SLEEP;
mp_rand(&a, cnt); mp_rand(&a, cnt);
mp_rand(&b, cnt); mp_rand(&b, cnt);
DO8(mp_add(&a, &b, &c));
rr = 0u; rr = 0u;
tt = UINT64_MAX; tt = UINT64_MAX;
do { do {
@ -200,20 +209,22 @@ int main(int argc, char **argv)
if (tt > gg) if (tt > gg)
tt = gg; tt = gg;
} while (++rr < 100000u); } while (++rr < 100000u);
printf("Adding\t\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n", PRINTLN("Adding\t\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles",
mp_count_bits(&a), CLK_PER_SEC / tt, tt); mp_count_bits(&a), CLK_PER_SEC / tt, tt);
FPRINTF(log, "%6d %9" PRIu64 "\n", cnt * MP_DIGIT_BIT, tt); FPRINTF(log, "%6d %9" PRIu64 "\n", cnt * MP_DIGIT_BIT, tt);
FFLUSH(log); FFLUSH(log);
} }
FCLOSE(log); FCLOSE(log);
printf("\n");
} }
if (should_test("sub", argc, argv) != 0) { if (should_test("sub", argc, argv) != 0) {
log = FOPEN("logs/sub.log", "w"); log = FOPEN("logs/sub" MP_TIMING_VERSION ".log", "w");
for (cnt = 8; cnt <= 128; cnt += 8) { for (cnt = 8; cnt <= 128; cnt += 8) {
SLEEP; SLEEP;
mp_rand(&a, cnt); mp_rand(&a, cnt);
mp_rand(&b, cnt); mp_rand(&b, cnt);
DO8(mp_sub(&a, &b, &c));
rr = 0u; rr = 0u;
tt = UINT64_MAX; tt = UINT64_MAX;
do { do {
@ -224,12 +235,13 @@ int main(int argc, char **argv)
tt = gg; tt = gg;
} while (++rr < 100000u); } while (++rr < 100000u);
printf("Subtracting\t\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n", PRINTLN("Subtracting\t\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles",
mp_count_bits(&a), CLK_PER_SEC / tt, tt); mp_count_bits(&a), CLK_PER_SEC / tt, tt);
FPRINTF(log, "%6d %9" PRIu64 "\n", cnt * MP_DIGIT_BIT, tt); FPRINTF(log, "%6d %9" PRIu64 "\n", cnt * MP_DIGIT_BIT, tt);
FFLUSH(log); FFLUSH(log);
} }
FCLOSE(log); FCLOSE(log);
printf("\n\n");
} }
if (should_test("mulsqr", argc, argv) != 0) { if (should_test("mulsqr", argc, argv) != 0) {
@ -247,11 +259,13 @@ int main(int argc, char **argv)
MP_TOOM_MUL_CUTOFF = (ix == 2) ? old_toom_m : 9999; MP_TOOM_MUL_CUTOFF = (ix == 2) ? old_toom_m : 9999;
MP_TOOM_SQR_CUTOFF = (ix == 2) ? old_toom_s : 9999; MP_TOOM_SQR_CUTOFF = (ix == 2) ? old_toom_s : 9999;
log = FOPEN((ix == 0) ? "logs/mult.log" : (ix == 1) ? "logs/mult_kara.log" : "logs/mult_toom.log", "w"); log = FOPEN((ix == 0) ? "logs/mult" MP_TIMING_VERSION ".log" : (ix == 1) ? "logs/mult_kara" MP_TIMING_VERSION ".log" :
"logs/mult_toom" MP_TIMING_VERSION ".log", "w");
for (cnt = 4; cnt <= (10240 / MP_DIGIT_BIT); cnt += 2) { for (cnt = 4; cnt <= (10240 / MP_DIGIT_BIT); cnt += 2) {
SLEEP; SLEEP;
mp_rand(&a, cnt); mp_rand(&a, cnt);
mp_rand(&b, cnt); mp_rand(&b, cnt);
DO8(mp_mul(&a, &b, &c));
rr = 0u; rr = 0u;
tt = UINT64_MAX; tt = UINT64_MAX;
do { do {
@ -261,17 +275,20 @@ int main(int argc, char **argv)
if (tt > gg) if (tt > gg)
tt = gg; tt = gg;
} while (++rr < 100u); } while (++rr < 100u);
printf("Multiplying\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n", PRINTLN("Multiplying\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles",
mp_count_bits(&a), CLK_PER_SEC / tt, tt); mp_count_bits(&a), CLK_PER_SEC / tt, tt);
FPRINTF(log, "%6d %9" PRIu64 "\n", mp_count_bits(&a), tt); FPRINTF(log, "%6d %9" PRIu64 "\n", mp_count_bits(&a), tt);
FFLUSH(log); FFLUSH(log);
} }
FCLOSE(log); FCLOSE(log);
printf("\n");
log = FOPEN((ix == 0) ? "logs/sqr.log" : (ix == 1) ? "logs/sqr_kara.log" : "logs/sqr_toom.log", "w"); log = FOPEN((ix == 0) ? "logs/sqr" MP_TIMING_VERSION ".log" : (ix == 1) ? "logs/sqr_kara" MP_TIMING_VERSION ".log" :
"logs/sqr_toom" MP_TIMING_VERSION ".log", "w");
for (cnt = 4; cnt <= (10240 / MP_DIGIT_BIT); cnt += 2) { for (cnt = 4; cnt <= (10240 / MP_DIGIT_BIT); cnt += 2) {
SLEEP; SLEEP;
mp_rand(&a, cnt); mp_rand(&a, cnt);
DO8(mp_sqr(&a, &b));
rr = 0u; rr = 0u;
tt = UINT64_MAX; tt = UINT64_MAX;
do { do {
@ -281,12 +298,13 @@ int main(int argc, char **argv)
if (tt > gg) if (tt > gg)
tt = gg; tt = gg;
} while (++rr < 100u); } while (++rr < 100u);
printf("Squaring\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n", PRINTLN("Squaring\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles",
mp_count_bits(&a), CLK_PER_SEC / tt, tt); mp_count_bits(&a), CLK_PER_SEC / tt, tt);
FPRINTF(log, "%6d %9" PRIu64 "\n", mp_count_bits(&a), tt); FPRINTF(log, "%6d %9" PRIu64 "\n", mp_count_bits(&a), tt);
FFLUSH(log); FFLUSH(log);
} }
FCLOSE(log); FCLOSE(log);
printf("\n\n");
} }
} }
@ -324,10 +342,10 @@ int main(int argc, char **argv)
"1214855636816562637502584060163403830270705000634713483015101384881871978446801224798536155406895823305035467591632531067547890948695117172076954220727075688048751022421198712032848890056357845974246560748347918630050853933697792254955890439720297560693579400297062396904306270145886830719309296352765295712183040773146419022875165382778007040109957609739589875590885701126197906063620133954893216612678838507540777138437797705602453719559017633986486649523611975865005712371194067612263330335590526176087004421363598470302731349138773205901447704682181517904064735636518462452242791676541725292378925568296858010151852326316777511935037531017413910506921922450666933202278489024521263798482237150056835746454842662048692127173834433089016107854491097456725016327709663199738238442164843147132789153725513257167915555162094970853584447993125488607696008169807374736711297007473812256272245489405898470297178738029484459690836250560495461579533254473316340608217876781986188705928270735695752830825527963838355419762516246028680280988020401914551825487349990306976304093109384451438813251211051597392127491464898797406789175453067960072008590614886532333015881171367104445044718144312416815712216611576221546455968770801413440778423979", "1214855636816562637502584060163403830270705000634713483015101384881871978446801224798536155406895823305035467591632531067547890948695117172076954220727075688048751022421198712032848890056357845974246560748347918630050853933697792254955890439720297560693579400297062396904306270145886830719309296352765295712183040773146419022875165382778007040109957609739589875590885701126197906063620133954893216612678838507540777138437797705602453719559017633986486649523611975865005712371194067612263330335590526176087004421363598470302731349138773205901447704682181517904064735636518462452242791676541725292378925568296858010151852326316777511935037531017413910506921922450666933202278489024521263798482237150056835746454842662048692127173834433089016107854491097456725016327709663199738238442164843147132789153725513257167915555162094970853584447993125488607696008169807374736711297007473812256272245489405898470297178738029484459690836250560495461579533254473316340608217876781986188705928270735695752830825527963838355419762516246028680280988020401914551825487349990306976304093109384451438813251211051597392127491464898797406789175453067960072008590614886532333015881171367104445044718144312416815712216611576221546455968770801413440778423979",
NULL NULL
}; };
log = FOPEN("logs/expt.log", "w"); log = FOPEN("logs/expt" MP_TIMING_VERSION ".log", "w");
logb = FOPEN("logs/expt_dr.log", "w"); logb = FOPEN("logs/expt_dr" MP_TIMING_VERSION ".log", "w");
logc = FOPEN("logs/expt_2k.log", "w"); logc = FOPEN("logs/expt_2k" MP_TIMING_VERSION ".log", "w");
logd = FOPEN("logs/expt_2kl.log", "w"); logd = FOPEN("logs/expt_2kl" MP_TIMING_VERSION ".log", "w");
for (n = 0; primes[n] != NULL; n++) { for (n = 0; primes[n] != NULL; n++) {
SLEEP; SLEEP;
mp_read_radix(&a, primes[n], 10); mp_read_radix(&a, primes[n], 10);
@ -340,6 +358,7 @@ int main(int argc, char **argv)
mp_sub_d(&a, 1uL, &c); mp_sub_d(&a, 1uL, &c);
mp_mod(&b, &c, &b); mp_mod(&b, &c, &b);
mp_set(&c, 3uL); mp_set(&c, 3uL);
DO8(mp_exptmod(&c, &b, &a, &d));
rr = 0u; rr = 0u;
tt = UINT64_MAX; tt = UINT64_MAX;
do { do {
@ -358,7 +377,7 @@ int main(int argc, char **argv)
draw(&d); draw(&d);
exit(0); exit(0);
} }
printf("Exponentiating\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n", PRINTLN("Exponentiating\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles",
mp_count_bits(&a), CLK_PER_SEC / tt, tt); mp_count_bits(&a), CLK_PER_SEC / tt, tt);
FPRINTF((n < 3) ? logd : (n < 9) ? logc : (n < 16) ? logb : log, FPRINTF((n < 3) ? logd : (n < 9) ? logc : (n < 16) ? logb : log,
"%6d %9" PRIu64 "\n", mp_count_bits(&a), tt); "%6d %9" PRIu64 "\n", mp_count_bits(&a), tt);
@ -367,10 +386,11 @@ int main(int argc, char **argv)
FCLOSE(logb); FCLOSE(logb);
FCLOSE(logc); FCLOSE(logc);
FCLOSE(logd); FCLOSE(logd);
printf("\n");
} }
if (should_test("invmod", argc, argv) != 0) { if (should_test("invmod", argc, argv) != 0) {
log = FOPEN("logs/invmod.log", "w"); log = FOPEN("logs/invmod" MP_TIMING_VERSION ".log", "w");
for (cnt = 4; cnt <= 32; cnt += 4) { for (cnt = 4; cnt <= 32; cnt += 4) {
SLEEP; SLEEP;
mp_rand(&a, cnt); mp_rand(&a, cnt);
@ -381,6 +401,7 @@ int main(int argc, char **argv)
mp_gcd(&a, &b, &c); mp_gcd(&a, &b, &c);
} while (mp_cmp_d(&c, 1uL) != MP_EQ); } while (mp_cmp_d(&c, 1uL) != MP_EQ);
DO2(mp_invmod(&b, &a, &c));
rr = 0u; rr = 0u;
tt = UINT64_MAX; tt = UINT64_MAX;
do { do {
@ -395,11 +416,12 @@ int main(int argc, char **argv)
printf("Failed to invert\n"); printf("Failed to invert\n");
return 0; return 0;
} }
printf("Inverting mod\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n", PRINTLN("Inverting mod\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles",
mp_count_bits(&a), CLK_PER_SEC / tt, tt); mp_count_bits(&a), CLK_PER_SEC / tt, tt);
FPRINTF(log, "%6d %9" PRIu64 "\n", cnt * MP_DIGIT_BIT, tt); FPRINTF(log, "%6d %9" PRIu64 "\n", cnt * MP_DIGIT_BIT, tt);
} }
FCLOSE(log); FCLOSE(log);
printf("\n");
} }
return 0; return 0;