Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ UpgradeLog*.htm
perf.data
perf.data.old

# ignore mpi.c generated by make
mpi.c
# ignore tommath_amalgam.c generated by make
tommath_amalgam.c

# ignore file generated by make tune
tuning_list
Expand Down Expand Up @@ -97,4 +97,7 @@ test_*.txt
doc/pics/*.ps
doc/*.bak*

logs/*.png
logs/*-*.dem

callgraph.txt
90 changes: 56 additions & 34 deletions demo/timing.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
#define LTM_TIMING_RAND_SEED 23
#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)
{
Expand Down Expand Up @@ -85,25 +90,28 @@ static uint64_t TIMFUNC(void)
#endif
}

#define DO2(x) x; x
#define DO4(x) DO2(x); DO2(x)
#define DO8(x) DO4(x); DO4(x)

#if 1
#define DO(x) x; x;
#define DO(x) DO2(x)
#else
#define DO2(x) x; x;
#define DO4(x) DO2(x); DO2(x);
#define DO8(x) DO4(x); DO4(x);
#define DO(x) DO8(x); DO8(x);
#define DO(x) DO8(x); DO8(x)
#endif

#ifdef TIMING_NO_LOGS
#define FOPEN(a, b) NULL
#define FOPEN(a, b) NULL
#define FPRINTF(a,b,c,d)
#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
#define FOPEN(a,b) fopen(a,b)
#define FPRINTF(a,b,c,d) fprintf(a,b,c,d)
#define FFLUSH(a) fflush(a)
#define FCLOSE(a) fclose(a)
#define FOPEN(a,b) fopen(a,b)
#define FPRINTF(a,b,c,d) fprintf(a,b,c,d)
#define FFLUSH(a) fflush(a)
#define FCLOSE(a) fclose(a)
#define PRINTLN(fm,b,n,m) do { printf("\r" fm, b, n, m); fflush(stdout); }while(0)
#endif

static int should_test(const char *test, int argc, char **argv)
Expand Down Expand Up @@ -178,19 +186,20 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}
} while (++rr < 100u);
printf("Prime-check\t%s(%2d) => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n",
name, cnt, CLK_PER_SEC / tt, tt);
PRINTLN("Prime-check\t%s(%2d) => %9" PRIu64 "/sec, %9" PRIu64 " cycles",
name, cnt, CLK_PER_SEC / tt, tt);
}
}
}
#endif

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) {
SLEEP;
mp_rand(&a, cnt);
mp_rand(&b, cnt);
DO8(mp_add(&a, &b, &c));
rr = 0u;
tt = UINT64_MAX;
do {
Expand All @@ -200,20 +209,22 @@ int main(int argc, char **argv)
if (tt > gg)
tt = gg;
} while (++rr < 100000u);
printf("Adding\t\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
PRINTLN("Adding\t\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
FPRINTF(log, "%6d %9" PRIu64 "\n", cnt * MP_DIGIT_BIT, tt);
FFLUSH(log);
}
FCLOSE(log);
printf("\n");
}

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) {
SLEEP;
mp_rand(&a, cnt);
mp_rand(&b, cnt);
DO8(mp_sub(&a, &b, &c));
rr = 0u;
tt = UINT64_MAX;
do {
Expand All @@ -224,12 +235,13 @@ int main(int argc, char **argv)
tt = gg;
} while (++rr < 100000u);

printf("Subtracting\t\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
PRINTLN("Subtracting\t\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
FPRINTF(log, "%6d %9" PRIu64 "\n", cnt * MP_DIGIT_BIT, tt);
FFLUSH(log);
}
FCLOSE(log);
printf("\n\n");
}

if (should_test("mulsqr", argc, argv) != 0) {
Expand All @@ -247,11 +259,13 @@ int main(int argc, char **argv)
MP_TOOM_MUL_CUTOFF = (ix == 2) ? old_toom_m : 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) {
SLEEP;
mp_rand(&a, cnt);
mp_rand(&b, cnt);
DO8(mp_mul(&a, &b, &c));
rr = 0u;
tt = UINT64_MAX;
do {
Expand All @@ -261,17 +275,20 @@ int main(int argc, char **argv)
if (tt > gg)
tt = gg;
} while (++rr < 100u);
printf("Multiplying\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
PRINTLN("Multiplying\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
FPRINTF(log, "%6d %9" PRIu64 "\n", mp_count_bits(&a), tt);
FFLUSH(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) {
SLEEP;
mp_rand(&a, cnt);
DO8(mp_sqr(&a, &b));
rr = 0u;
tt = UINT64_MAX;
do {
Expand All @@ -281,12 +298,13 @@ int main(int argc, char **argv)
if (tt > gg)
tt = gg;
} while (++rr < 100u);
printf("Squaring\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
PRINTLN("Squaring\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
FPRINTF(log, "%6d %9" PRIu64 "\n", mp_count_bits(&a), tt);
FFLUSH(log);
}
FCLOSE(log);
printf("\n\n");

}
}
Expand Down Expand Up @@ -324,10 +342,10 @@ int main(int argc, char **argv)
"1214855636816562637502584060163403830270705000634713483015101384881871978446801224798536155406895823305035467591632531067547890948695117172076954220727075688048751022421198712032848890056357845974246560748347918630050853933697792254955890439720297560693579400297062396904306270145886830719309296352765295712183040773146419022875165382778007040109957609739589875590885701126197906063620133954893216612678838507540777138437797705602453719559017633986486649523611975865005712371194067612263330335590526176087004421363598470302731349138773205901447704682181517904064735636518462452242791676541725292378925568296858010151852326316777511935037531017413910506921922450666933202278489024521263798482237150056835746454842662048692127173834433089016107854491097456725016327709663199738238442164843147132789153725513257167915555162094970853584447993125488607696008169807374736711297007473812256272245489405898470297178738029484459690836250560495461579533254473316340608217876781986188705928270735695752830825527963838355419762516246028680280988020401914551825487349990306976304093109384451438813251211051597392127491464898797406789175453067960072008590614886532333015881171367104445044718144312416815712216611576221546455968770801413440778423979",
NULL
};
log = FOPEN("logs/expt.log", "w");
logb = FOPEN("logs/expt_dr.log", "w");
logc = FOPEN("logs/expt_2k.log", "w");
logd = FOPEN("logs/expt_2kl.log", "w");
log = FOPEN("logs/expt" MP_TIMING_VERSION ".log", "w");
logb = FOPEN("logs/expt_dr" MP_TIMING_VERSION ".log", "w");
logc = FOPEN("logs/expt_2k" MP_TIMING_VERSION ".log", "w");
logd = FOPEN("logs/expt_2kl" MP_TIMING_VERSION ".log", "w");
for (n = 0; primes[n] != NULL; n++) {
SLEEP;
mp_read_radix(&a, primes[n], 10);
Expand All @@ -340,6 +358,7 @@ int main(int argc, char **argv)
mp_sub_d(&a, 1uL, &c);
mp_mod(&b, &c, &b);
mp_set(&c, 3uL);
DO8(mp_exptmod(&c, &b, &a, &d));
rr = 0u;
tt = UINT64_MAX;
do {
Expand All @@ -358,19 +377,20 @@ int main(int argc, char **argv)
draw(&d);
exit(0);
}
printf("Exponentiating\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
PRINTLN("Exponentiating\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
FPRINTF((n < 3) ? logd : (n < 9) ? logc : (n < 16) ? logb : log,
"%6d %9" PRIu64 "\n", mp_count_bits(&a), tt);
}
FCLOSE(log);
FCLOSE(logb);
FCLOSE(logc);
FCLOSE(logd);
printf("\n");
}

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) {
SLEEP;
mp_rand(&a, cnt);
Expand All @@ -381,6 +401,7 @@ int main(int argc, char **argv)
mp_gcd(&a, &b, &c);
} while (mp_cmp_d(&c, 1uL) != MP_EQ);

DO2(mp_invmod(&b, &a, &c));
rr = 0u;
tt = UINT64_MAX;
do {
Expand All @@ -395,11 +416,12 @@ int main(int argc, char **argv)
printf("Failed to invert\n");
return 0;
}
printf("Inverting mod\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles\n",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
PRINTLN("Inverting mod\t%4d-bit => %9" PRIu64 "/sec, %9" PRIu64 " cycles",
mp_count_bits(&a), CLK_PER_SEC / tt, tt);
FPRINTF(log, "%6d %9" PRIu64 "\n", cnt * MP_DIGIT_BIT, tt);
}
FCLOSE(log);
printf("\n");
}

return 0;
Expand Down
20 changes: 0 additions & 20 deletions gen.pl

This file was deleted.

9 changes: 8 additions & 1 deletion logs/Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
all:
gnuplot graphs.dem
sed -e 's@\.log@-$(VERSION)\.log@g' graphs.dem > graphs-$(VERSION).dem
gnuplot graphs-$(VERSION).dem

cmp:
gnuplot before_after.dem

clean:
rm -f *-*.log *.png graphs-*.dem
16 changes: 0 additions & 16 deletions logs/add.log

This file was deleted.

Binary file removed logs/addsub.png
Binary file not shown.
36 changes: 36 additions & 0 deletions logs/before_after.dem
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
set terminal png
set ylabel "Cycles per Operation"
set xlabel "Operand size (bits)"

set output "addsub-ba.png"
plot 'add-before.log' smooth bezier title "Addition (before)", \
'add-after.log' smooth bezier title "Addition (after)", \
'sub-before.log' smooth bezier title "Subtraction (before)", \
'sub-after.log' smooth bezier title "Subtraction (after)"

set output "mult-ba.png"
plot 'mult-before.log' smooth bezier title "Multiplication (without Karatsuba) (before)", \
'mult-after.log' smooth bezier title "Multiplication (without Karatsuba) (after)", \
'mult_kara-before.log' smooth bezier title "Multiplication (Karatsuba) (before)", \
'mult_kara-after.log' smooth bezier title "Multiplication (Karatsuba) (after)"

set output "sqr-ba.png"
plot 'sqr-before.log' smooth bezier title "Squaring (without Karatsuba) (before)", \
'sqr-after.log' smooth bezier title "Squaring (without Karatsuba) (after)", \
'sqr_kara-before.log' smooth bezier title "Squaring (Karatsuba) (before)", \
'sqr_kara-after.log' smooth bezier title "Squaring (Karatsuba) (after)"

set output "expt-ba.png"
plot 'expt-before.log' smooth bezier title "Exptmod (Montgomery) (before)", \
'expt-after.log' smooth bezier title "Exptmod (Montgomery) (after)", \
'expt_dr-before.log' smooth bezier title "Exptmod (Dimminished Radix) (before)", \
'expt_dr-after.log' smooth bezier title "Exptmod (Dimminished Radix) (after)", \
'expt_2k-before.log' smooth bezier title "Exptmod (2k Reduction) (before)", \
'expt_2k-after.log' smooth bezier title "Exptmod (2k Reduction) (after)", \
'expt_2kl-before.log' smooth bezier title "Exptmod (2k-l Reduction) (before)", \
'expt_2kl-after.log' smooth bezier title "Exptmod (2k-l Reduction) (after)"

set output "invmod-ba.png"
plot 'invmod-before.log' smooth bezier title "Modular Inverse (before)", \
'invmod-after.log' smooth bezier title "Modular Inverse (after)"

28 changes: 28 additions & 0 deletions logs/before_after.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<html>
<head>
<title>LibTomMath Log Plots</title>
</head>
<body>

<h1>Addition and Subtraction</h1>
<center><img src=addsub-ba.png></center>
<hr>

<h1>Multiplication</h1>
<center><img src=mult-ba.png></center>
<hr>

<h1>Squaring</h1>
<center><img src=sqr-ba.png></center>
<hr>

<h1>Exptmod</h1>
<center><img src=expt-ba.png></center>
<hr>

<h1>Modular Inverse</h1>
<center><img src=invmod-ba.png></center>
<hr>

</body>
</html>
7 changes: 0 additions & 7 deletions logs/expt.log

This file was deleted.

Binary file removed logs/expt.png
Binary file not shown.
6 changes: 0 additions & 6 deletions logs/expt_2k.log

This file was deleted.

3 changes: 0 additions & 3 deletions logs/expt_2kl.log

This file was deleted.

Loading