aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/exhaustive/exhaustive.c9
-rw-r--r--src/invalid/invalid.c33
-rw-r--r--src/invalid/invalid_thread.c30
-rw-r--r--src/invalid/invalid_thread.h1
-rw-r--r--src/io/cli.c2
-rw-r--r--src/io/config.h2
-rw-r--r--src/io/output.c16
-rw-r--r--src/io/output.h8
8 files changed, 54 insertions, 47 deletions
diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c
index e76b4a1..a4ce9aa 100644
--- a/src/exhaustive/exhaustive.c
+++ b/src/exhaustive/exhaustive.c
@@ -151,17 +151,16 @@ int exhaustive_gen_retry(curve_t *curve, const config_t *cfg,
if (cfg->verbose) {
if (diff > 0) {
- fprintf(debug, "+");
+ fprintf(verbose, "+");
} else if (diff < 0) {
- fprintf(debug, "-");
+ fprintf(verbose, "-");
} else {
- fprintf(debug, ".");
+ fprintf(verbose, ".");
}
- fflush(debug);
}
}
- if (cfg->verbose) fprintf(debug, "\n");
+ if (cfg->verbose) fprintf(verbose, "\n");
return 1;
}
diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c
index 1dd4fee..4a14ad1 100644
--- a/src/invalid/invalid.c
+++ b/src/invalid/invalid.c
@@ -112,7 +112,7 @@ static size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes,
// whoo we have a new invalid curve
if (!total && cfg->verbose) {
fprintf(
- debug,
+ verbose,
"we have a new one, calculating prime order points.\n");
}
total++;
@@ -129,7 +129,7 @@ static size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes,
for (size_t i = 0; i < nprimes; ++i) {
if (curves[i] == NULL && dvdis(invalid->order, primes[i])) {
if (cfg->verbose) {
- fprintf(debug, "prime %lu divides curve order.\n",
+ fprintf(verbose, "prime %lu divides curve order.\n",
primes[i]);
}
dprimes[j++] = primes[i];
@@ -168,10 +168,11 @@ static size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes,
invalid->a = gcopy(curve->a);
if (cfg->verbose) {
- fprintf(debug,
+ fprintf(verbose,
"curve saved: %lu primes from range divide order.\n",
total);
- fprintf(debug, "curves done: %lu out of %lu needed. %.0f%% \n",
+ fprintf(verbose,
+ "curves done: %lu out of %lu needed. %.0f%% \n",
ncurves, nprimes, ((float)(ncurves) / nprimes) * 100);
}
} else {
@@ -199,12 +200,13 @@ static size_t invalid_curves_threaded(curve_t *curve, config_t *cfg,
size_t generated = 0;
state_e states[nprimes];
+ state_e old_states[nprimes];
curve_t *local_curves[nprimes];
for (size_t i = 0; i < nprimes; ++i) {
states[i] = STATE_FREE;
+ old_states[i] = STATE_FREE;
}
pthread_mutex_t state_mutex = PTHREAD_MUTEX_INITIALIZER;
- pthread_mutex_t curves_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t generated_cond = PTHREAD_COND_INITIALIZER;
for (size_t i = 0; i < cfg->threads; ++i) {
@@ -215,7 +217,6 @@ static size_t invalid_curves_threaded(curve_t *curve, config_t *cfg,
threads[i].curves = local_curves;
threads[i].generated = &generated;
threads[i].mutex_state = &state_mutex;
- threads[i].mutex_curves = &curves_mutex;
threads[i].cond_generated = &generated_cond;
threads[i].cfg = cfg;
threads[i].gens = invalid_gen;
@@ -224,11 +225,27 @@ static size_t invalid_curves_threaded(curve_t *curve, config_t *cfg,
(GEN)&threads[i]);
}
+ pthread_mutex_lock(&state_mutex);
for (size_t i = 0; i < cfg->threads; ++i) {
pthread_create(&pthreads[i], NULL, &invalid_thread,
(void *)&pari_threads[i]);
}
+ bool running = true;
+ do {
+ pthread_cond_wait(&generated_cond, &state_mutex);
+ debug("here %lu\n", generated);
+ for (size_t i = 0; i < nprimes; ++i) {
+ if (old_states[i] != states[i] && states[i] == STATE_GENERATED) {
+ output_o(local_curves[i], cfg);
+ old_states[i] = states[i];
+ }
+ }
+
+ if (generated == nprimes) running = false;
+ pthread_mutex_unlock(&state_mutex);
+ } while (running);
+
for (size_t i = 0; i < cfg->threads; ++i) {
pthread_join(pthreads[i], NULL);
}
@@ -241,6 +258,8 @@ static size_t invalid_curves_threaded(curve_t *curve, config_t *cfg,
for (size_t i = 0; i < cfg->threads; ++i) {
pari_thread_free(&pari_threads[i]);
}
+ pthread_mutex_destroy(&state_mutex);
+ pthread_cond_destroy(&generated_cond);
return generated;
}
@@ -265,7 +284,7 @@ int invalid_do(config_t *cfg) {
pari_ulong *primes;
size_t nprimes = invalid_primes(curve->order, &primes);
if (cfg->verbose) {
- fprintf(debug, "primes upto: p_max = %lu, n = %lu\n",
+ fprintf(verbose, "primes upto: p_max = %lu, n = %lu\n",
primes[nprimes - 1], nprimes);
}
diff --git a/src/invalid/invalid_thread.c b/src/invalid/invalid_thread.c
index 40b52ec..e43539c 100644
--- a/src/invalid/invalid_thread.c
+++ b/src/invalid/invalid_thread.c
@@ -31,9 +31,7 @@ void *invalid_thread(void *arg) {
ndivides++;
}
}
-#ifdef DEBUG
- printf("ndivides = %lu\n", ndivides);
-#endif
+ debug("ndivides = %lu\n", ndivides);
if (ndivides > 0) {
pthread_mutex_lock(thread->mutex_state);
size_t nfree = 0;
@@ -48,9 +46,7 @@ void *invalid_thread(void *arg) {
nfree++;
}
}
-#ifdef DEBUG
- printf("nfree = %lu\n", nfree);
-#endif
+ debug("nfree = %lu\n", nfree);
pthread_mutex_unlock(thread->mutex_state);
if (nfree > 0) {
@@ -59,33 +55,23 @@ void *invalid_thread(void *arg) {
exhaustive_gen(invalid, thread->cfg, thread->gens,
invalid_argss, OFFSET_GENERATORS, OFFSET_END);
- pthread_mutex_lock(thread->mutex_curves);
pthread_mutex_lock(thread->mutex_state);
size_t count = 0;
for (size_t i = thread->nprimes; i-- > 0;) {
if (count < nprimes && primes[count] == thread->primes[i]) {
-#ifdef DEBUG
- printf("[i] = %lu, prime = %lu\n", i, primes[count]);
- printf("state = %i\n", thread->states[i]);
-#endif
+ debug("[i] = %lu, prime = %lu\n", i, primes[count]);
+ debug("state = %i\n", thread->states[i]);
thread->states[i] = STATE_GENERATED;
thread->curves[i] = curve_new_copy(invalid);
-
- output_o(thread->curves[i], thread->cfg);
count++;
}
}
-#ifdef DEBUG
- printf("count = %lu, generated = %lu\n", count,
- *(thread->generated));
-#endif
+ debug("count = %lu, generated = %lu\n", count,
+ *(thread->generated));
*(thread->generated) += count;
-#ifdef DEBUG
- printf("generated = %lu\n", *(thread->generated));
-#endif
- // pthread_cond_signal(thread->cond_generated);
+ debug("generated = %lu\n", *(thread->generated));
+ pthread_cond_signal(thread->cond_generated);
pthread_mutex_unlock(thread->mutex_state);
- pthread_mutex_unlock(thread->mutex_curves);
invalid = curve_new();
invalid->field = gcopy(thread->original_curve->field);
diff --git a/src/invalid/invalid_thread.h b/src/invalid/invalid_thread.h
index 1ee4cf7..bc30d04 100644
--- a/src/invalid/invalid_thread.h
+++ b/src/invalid/invalid_thread.h
@@ -22,7 +22,6 @@ typedef struct {
curve_t **curves;
size_t *generated;
pthread_mutex_t *mutex_state;
- pthread_mutex_t *mutex_curves;
pthread_cond_t *cond_generated;
config_t *cfg;
gen_t *gens;
diff --git a/src/io/cli.c b/src/io/cli.c
index 060bf20..e7de595 100644
--- a/src/io/cli.c
+++ b/src/io/cli.c
@@ -149,7 +149,7 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) {
case OPT_VERBOSE:
cfg->verbose++;
if (arg) {
- cfg->debug = arg;
+ cfg->verbose_log = arg;
}
break;
case OPT_RANDOM:
diff --git a/src/io/config.h b/src/io/config.h
index 77d59b7..70018a9 100644
--- a/src/io/config.h
+++ b/src/io/config.h
@@ -49,7 +49,7 @@ typedef struct {
bool append;
long verbose;
- char *debug;
+ char *verbose_log;
unsigned long bits;
diff --git a/src/io/output.c b/src/io/output.c
index 1e1402d..fd83965 100644
--- a/src/io/output.c
+++ b/src/io/output.c
@@ -9,7 +9,7 @@
#include "math/field.h"
FILE *out;
-FILE *debug;
+FILE *verbose;
char *output_scsv(curve_t *curve, const config_t *cfg) {
pari_sp ltop = avma;
@@ -51,7 +51,6 @@ char *output_scsv(curve_t *curve, const config_t *cfg) {
void output_fcsv(FILE *out, curve_t *curve, const config_t *cfg) {
char *string = output_scsv(curve, cfg);
fprintf(out, "%s\n", string);
- fflush(out);
free(string);
}
@@ -180,7 +179,6 @@ char *output_sjson(curve_t *curve, const config_t *cfg) {
void output_fjson(FILE *out, curve_t *curve, const config_t *cfg) {
char *s = output_sjson(curve, cfg);
fprintf(out, "%s\n", s);
- fflush(out);
json_free_serialized_string(s);
}
@@ -202,16 +200,16 @@ void output_init(const config_t *cfg) {
out = stdout;
}
setvbuf(out, NULL, _IONBF, 0);
- if (cfg->debug) {
- debug = fopen(cfg->debug, "w");
- if (!debug) {
- debug = stdout;
+ if (cfg->verbose_log) {
+ verbose = fopen(cfg->verbose_log, "w");
+ if (!verbose) {
+ verbose = stdout;
perror("Failed to open verbose output file.");
}
} else {
- debug = stdout;
+ verbose = stdout;
}
- setvbuf(debug, NULL, _IONBF, 0);
+ setvbuf(verbose, NULL, _IONBF, 0);
switch (cfg->format) {
case FORMAT_JSON:
diff --git a/src/io/output.h b/src/io/output.h
index 604498a..009e318 100644
--- a/src/io/output.h
+++ b/src/io/output.h
@@ -89,7 +89,7 @@ extern FILE *out;
/**
*
*/
-extern FILE *debug;
+extern FILE *verbose;
/**
*
@@ -102,4 +102,10 @@ void output_init(const config_t *cfg);
*/
void output_quit(void);
+#ifdef DEBUG
+#define debug(fmt, ...) fprintf(out, fmt, ##__VA_ARGS__)
+#else
+#define debug(fmt, ...)
+#endif
+
#endif // ECGEN_OUTPUT_H