From 76cc82994c1fbb969ec61d3aede6bd42fda09005 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 12 Sep 2024 09:58:04 +0200 Subject: Cleanup gendata tools --- tools/tool.h | 117 +++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 93 insertions(+), 24 deletions(-) (limited to 'tools/tool.h') diff --git a/tools/tool.h b/tools/tool.h index 7995621..eaee704 100644 --- a/tools/tool.h +++ b/tools/tool.h @@ -8,6 +8,14 @@ #include "../src/nissy.h" +static void log_stderr(const char *, ...); +static void log_stdout(const char *, ...); +static double timerun(void (*)(void), const char *); +static void writetable(const char *, int64_t, const char *); +static int64_t generatetable(const char *, const char *, char **); +static int getdata(const char *, const char *, char **, const char *); +static void gendata_run(const char *, const char *, const char *, uint64_t[static 21]); + static void log_stderr(const char *str, ...) { @@ -29,7 +37,7 @@ write_stdout(const char *str, ...) } static double -timerun(void (*run)(void), char *name) +timerun(void (*run)(void), const char *name) { struct timespec start, end; double tdiff, tdsec, tdnano; @@ -62,6 +70,46 @@ timerun(void (*run)(void), char *name) return tdiff; } +static void +writetable(const char *buf, int64_t size, const char *filename) +{ + FILE *f; + + if ((f = fopen(filename, "wb")) == NULL) { + fprintf(stderr, "Could not write tables to file %s" + ", will be regenerated next time.\n", filename); + } else { + fwrite(buf, size, 1, f); + fclose(f); + fprintf(stderr, "Table written to %s.\n", filename); + } +} + +static int64_t +generatetable(const char *solver, const char *options, char **buf) +{ + int64_t size, gensize; + + size = nissy_datasize(solver, options); + if (size == -1) { + printf("Error getting table size.\n"); + return -1; + } + + *buf = malloc(size); + gensize = nissy_gendata(solver, options, *buf); + + if (gensize != size) { + fprintf(stderr, "Error generating table"); + if (gensize != -1) + fprintf(stderr, " (got %" PRId64 " bytes)", gensize); + fprintf(stderr, "\n"); + return -2; + } + + return gensize; +} + static int getdata( const char *solver, @@ -69,36 +117,25 @@ getdata( char **buf, const char *filename ) { - int64_t s, size, sizeread; + int64_t size, sizeread; FILE *f; - if ((size = nissy_datasize(solver, options)) == -1) { - printf("Error in datasize\n"); - goto getdata_error_nofree; - } - - *buf = malloc(size); - if ((f = fopen(filename, "rb")) == NULL) { - fprintf(stderr, "Table file not found, generating them." - " This can take a while.\n"); - s = nissy_gendata(solver, options, *buf); - if (s != size) { - fprintf(stderr, "Error generating table"); - if (s != -1) - fprintf(stderr, " (got %" PRId64 " bytes)", s); - fprintf(stderr, "\n"); + fprintf(stderr, "Table file not found, generating it.\n"); + size = generatetable(solver, options, buf); + switch (size) { + case -1: + goto getdata_error_nofree; + case -2: goto getdata_error; - } - if ((f = fopen(filename, "wb")) == NULL) { - fprintf(stderr, "Could not write tables to file %s" - ", will be regenerated next time.\n", filename); - } else { - fwrite(*buf, size, 1, f); - fclose(f); + default: + writetable(filename, size, *buf); + break; } } else { fprintf(stderr, "Reading tables from file %s\n", filename); + size = nissy_datasize(solver, options); + *buf = malloc(size); sizeread = fread(*buf, size, 1, f); fclose(f); if (sizeread != 1) { @@ -114,3 +151,35 @@ getdata_error: getdata_error_nofree: return 1; } + +static void +gendata_run( + const char *solver, + const char *options, + const char *filename, /* TODO: remove filename, use solver name */ + uint64_t expected[static 21] +) { + int64_t size; + char *buf; + + + size = generatetable(solver, options, &buf); + switch (size) { + case -1: + return; + case -2: + goto gendata_run_finish; + default: + nissy_datainfo(buf, write_stdout); + printf("\n"); + printf("Succesfully generated %" PRId64 " bytes. " + "See above for details on the tables.\n", size); + + /* TODO: check that the table is correct */ + writetable(buf, size, filename); + break; + } + +gendata_run_finish: + free(buf); +} -- cgit v1.3