improve demo/timing a bit * less verbose output on console * allow changing name for logs * pre-heat caches before starting the timing
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
diff --git a/demo/timing.c b/demo/timing.c
index fa5669c..8541250 100644
--- a/demo/timing.c
+++ b/demo/timing.c
@@ -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)
{
@@ -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)
@@ -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 {
@@ -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 {
@@ -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) {
@@ -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 {
@@ -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 {
@@ -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");
}
}
@@ -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);
@@ -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 {
@@ -358,8 +377,8 @@ 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);
}
@@ -367,10 +386,11 @@ int main(int argc, char **argv)
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);
@@ -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 {
@@ -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;