h48

A prototype for an optimal Rubik's cube solver, work in progress.
git clone https://git.tronto.net/h48
Download | Log | Files | Refs | README | LICENSE

commit 76cc82994c1fbb969ec61d3aede6bd42fda09005
parent 10b65003102675bd19c0e5174ff249be2a163bc7
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Thu, 12 Sep 2024 09:58:04 +0200

Cleanup gendata tools

Diffstat:
Msrc/nissy.c | 1+
Mtools/001_gendata_h48h0k4/gendata_h48h0k4.c | 41+++--------------------------------------
Mtools/002_gendata_h48h0k2/gendata_h48h0k2.c | 38+++-----------------------------------
Mtools/tool.h | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
4 files changed, 100 insertions(+), 97 deletions(-)

diff --git a/src/nissy.c b/src/nissy.c @@ -217,6 +217,7 @@ nissy_datainfo( switch (info.type) { case TABLETYPE_PRUNING: write("\n"); + write("Table distribution"); if (info.base != 0) write(" (base value = %" PRIu8 ")", info.base); write(":\nValue\tPositions\n"); diff --git a/tools/001_gendata_h48h0k4/gendata_h48h0k4.c b/tools/001_gendata_h48h0k4/gendata_h48h0k4.c @@ -1,14 +1,6 @@ #include "../tool.h" -#define MAXDEPTH 20 -#define HVALUE 0 -#define OPTIONS "0;4;20" -#define LONGOPTIONS "h = 0, k = 4, max depth = 20" - -#define COCSEPSIZE 1119792 -#define ETABLESIZE(h) (((3393 * 495 * 70) >> 1) << (size_t)(h)) - -uint32_t expected[21] = { +uint64_t expected[21] = { [0] = 1, [1] = 1, [2] = 4, @@ -24,41 +16,14 @@ uint32_t expected[21] = { [12] = 1673, }; -char *buf; - void run(void) { - int64_t s; - - s = nissy_gendata("h48", OPTIONS, buf); - - if (s == -1) { - printf("Error generating table\n"); - } else { - nissy_datainfo(buf, write_stdout); - printf("\n"); - printf("Succesfully generated %" PRId64 " bytes. " - "See above for details on the tables.\n", s); - - /* TODO: check that the table is correct */ - } + gendata_run("h48", "0;4;20", "tables/h48h0k4", expected); } int main(void) { - int64_t size; - nissy_setlogger(log_stderr); - size = nissy_datasize("h48", OPTIONS); - if (size == -1) { - printf("gendata_h48 benchmark: error in datasize\n"); - return 1; - } - - buf = malloc(size); - - timerun(run, "benchmark gendata_h48 " LONGOPTIONS); - - free(buf); + timerun(run, "benchmark gendata_h48 h = 0, k = 4"); return 0; } diff --git a/tools/002_gendata_h48h0k2/gendata_h48h0k2.c b/tools/002_gendata_h48h0k2/gendata_h48h0k2.c @@ -1,11 +1,6 @@ #include "../tool.h" -#define MAXDEPTH 20 -#define HVALUE 0 -#define OPTIONS "0;2;20" -#define LONGOPTIONS "h = 0, k = 2, max depth = 20" - -uint32_t expected[21] = { +uint64_t expected[21] = { /* Base value is 8 */ [0] = 5473562, [1] = 34776317, @@ -13,41 +8,14 @@ uint32_t expected[21] = { [3] = 8750867, }; -char *buf; - void run(void) { - int64_t s; - - s = nissy_gendata("h48", OPTIONS, buf); - - if (s == -1) { - printf("Error generating table\n"); - } else { - nissy_datainfo(buf, write_stdout); - printf("\n"); - printf("Succesfully generated %" PRId64 " bytes. " - "See above for details on the tables.\n", s); - - /* TODO: check that the table is correct */ - } + gendata_run("h48", "0;2;20", "tables/h48h0k2", expected); } int main(void) { - int64_t size; - nissy_setlogger(log_stderr); - size = nissy_datasize("h48", OPTIONS); - if (size == -1) { - printf("gendata_h48 benchmark: error in datasize\n"); - return 1; - } - - buf = malloc(size); - - timerun(run, "benchmark gendata_h48 " LONGOPTIONS); - - free(buf); + timerun(run, "benchmark gendata_h48 h = 0, k = 2"); return 0; } diff --git 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); +}