From 818186b480d7c05e0c58ff89da2180f5b2476434 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 26 Sep 2024 14:59:11 +0200 Subject: Added TOOLARGS and simplified gendata tool(s) --- tools/0002_gendata_h48h0k2/gendata_h48h0k2.c | 21 ------ tools/0004_gendata_h48h0k4/gendata_h48h0k4.c | 29 -------- tools/0012_gendata_h48h1k2/gendata_h48h1k2.c | 21 ------ tools/0022_gendata_h48h2k2/gendata_h48h2k2.c | 21 ------ tools/0032_gendata_h48h3k2/gendata_h48h3k2.c | 21 ------ tools/0042_gendata_h48h4k2/gendata_h48h4k2.c | 21 ------ tools/0052_gendata_h48h5k2/gendata_h48h5k2.c | 21 ------ tools/0062_gendata_h48h6k2/gendata_h48h6k2.c | 21 ------ tools/0072_gendata_h48h7k2/gendata_h48h7k2.c | 21 ------ tools/0082_gendata_h48h8k2/gendata_h48h8k2.c | 21 ------ tools/0092_gendata_h48h9k2/gendata_h48h9k2.c | 21 ------ tools/0102_gendata_h48h10k2/gendata_h48h10k2.c | 21 ------ tools/0112_gendata_h48h11k2/gendata_h48h11k2.c | 21 ------ tools/100_gendata/gendata.c | 80 +++++++++++++++++++++ tools/100_stats_tables_h48/stats_tables_h48.c | 99 -------------------------- tools/200_solve_small/solve_small.c | 53 -------------- tools/200_stats_tables_h48/stats_tables_h48.c | 99 ++++++++++++++++++++++++++ tools/300_solve_small/solve_small.c | 53 ++++++++++++++ tools/nissy_extra.h | 3 + tools/run_tool.sh | 5 +- tools/tool.h | 20 ++++-- 21 files changed, 254 insertions(+), 439 deletions(-) delete mode 100644 tools/0002_gendata_h48h0k2/gendata_h48h0k2.c delete mode 100644 tools/0004_gendata_h48h0k4/gendata_h48h0k4.c delete mode 100644 tools/0012_gendata_h48h1k2/gendata_h48h1k2.c delete mode 100644 tools/0022_gendata_h48h2k2/gendata_h48h2k2.c delete mode 100644 tools/0032_gendata_h48h3k2/gendata_h48h3k2.c delete mode 100644 tools/0042_gendata_h48h4k2/gendata_h48h4k2.c delete mode 100644 tools/0052_gendata_h48h5k2/gendata_h48h5k2.c delete mode 100644 tools/0062_gendata_h48h6k2/gendata_h48h6k2.c delete mode 100644 tools/0072_gendata_h48h7k2/gendata_h48h7k2.c delete mode 100644 tools/0082_gendata_h48h8k2/gendata_h48h8k2.c delete mode 100644 tools/0092_gendata_h48h9k2/gendata_h48h9k2.c delete mode 100644 tools/0102_gendata_h48h10k2/gendata_h48h10k2.c delete mode 100644 tools/0112_gendata_h48h11k2/gendata_h48h11k2.c create mode 100644 tools/100_gendata/gendata.c delete mode 100644 tools/100_stats_tables_h48/stats_tables_h48.c delete mode 100644 tools/200_solve_small/solve_small.c create mode 100644 tools/200_stats_tables_h48/stats_tables_h48.c create mode 100644 tools/300_solve_small/solve_small.c create mode 100644 tools/nissy_extra.h (limited to 'tools') diff --git a/tools/0002_gendata_h48h0k2/gendata_h48h0k2.c b/tools/0002_gendata_h48h0k2/gendata_h48h0k2.c deleted file mode 100644 index 14f99ee..0000000 --- a/tools/0002_gendata_h48h0k2/gendata_h48h0k2.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "../tool.h" - -uint64_t expected[21] = { - /* Base value is 8 */ - [0] = 5473562, - [1] = 34776317, - [2] = 68566704, - [3] = 8750867, -}; - -void run(void) { - gendata_run("h48", "0;2;20", "tables/h48h0k2", expected); -} - -int main(void) { - nissy_setlogger(log_stderr); - - timerun(run, "benchmark gendata_h48 h = 0, k = 2"); - - return 0; -} diff --git a/tools/0004_gendata_h48h0k4/gendata_h48h0k4.c b/tools/0004_gendata_h48h0k4/gendata_h48h0k4.c deleted file mode 100644 index ff34bb9..0000000 --- a/tools/0004_gendata_h48h0k4/gendata_h48h0k4.c +++ /dev/null @@ -1,29 +0,0 @@ -#include "../tool.h" - -uint64_t expected[21] = { - [0] = 1, - [1] = 1, - [2] = 4, - [3] = 34, - [4] = 331, - [5] = 3612, - [6] = 41605, - [7] = 474128, - [8] = 4953846, - [9] = 34776317, - [10] = 68566704, - [11] = 8749194, - [12] = 1673, -}; - -void run(void) { - gendata_run("h48", "0;4;20", "tables/h48h0k4", expected); -} - -int main(void) { - nissy_setlogger(log_stderr); - - timerun(run, "benchmark gendata_h48 h = 0, k = 4"); - - return 0; -} diff --git a/tools/0012_gendata_h48h1k2/gendata_h48h1k2.c b/tools/0012_gendata_h48h1k2/gendata_h48h1k2.c deleted file mode 100644 index 0ebdcaa..0000000 --- a/tools/0012_gendata_h48h1k2/gendata_h48h1k2.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "../tool.h" - -uint64_t expected[21] = { - /* Base value is 8 */ - [0] = 0, /* Unknown */ - [1] = 0, /* Unknown */ - [2] = 0, /* Unknown */ - [3] = 0, /* Unknown */ -}; - -void run(void) { - gendata_run("h48", "1;2;20", "tables/h48h1k2", expected); -} - -int main(void) { - nissy_setlogger(log_stderr); - - timerun(run, "benchmark gendata_h48 h = 1, k = 2"); - - return 0; -} diff --git a/tools/0022_gendata_h48h2k2/gendata_h48h2k2.c b/tools/0022_gendata_h48h2k2/gendata_h48h2k2.c deleted file mode 100644 index f437e98..0000000 --- a/tools/0022_gendata_h48h2k2/gendata_h48h2k2.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "../tool.h" - -uint64_t expected[21] = { - /* Base value is 8 */ - [0] = 0, /* Unknown */ - [1] = 0, /* Unknown */ - [2] = 0, /* Unknown */ - [3] = 0, /* Unknown */ -}; - -void run(void) { - gendata_run("h48", "2;2;20", "tables/h48h2k2", expected); -} - -int main(void) { - nissy_setlogger(log_stderr); - - timerun(run, "benchmark gendata_h48 h = 2, k = 2"); - - return 0; -} diff --git a/tools/0032_gendata_h48h3k2/gendata_h48h3k2.c b/tools/0032_gendata_h48h3k2/gendata_h48h3k2.c deleted file mode 100644 index 2e0eab5..0000000 --- a/tools/0032_gendata_h48h3k2/gendata_h48h3k2.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "../tool.h" - -uint64_t expected[21] = { - /* Base value is 8 */ - [0] = 0, /* Unknown */ - [1] = 0, /* Unknown */ - [2] = 0, /* Unknown */ - [3] = 0, /* Unknown */ -}; - -void run(void) { - gendata_run("h48", "3;2;20", "tables/h48h3k2", expected); -} - -int main(void) { - nissy_setlogger(log_stderr); - - timerun(run, "benchmark gendata_h48 h = 3, k = 2"); - - return 0; -} diff --git a/tools/0042_gendata_h48h4k2/gendata_h48h4k2.c b/tools/0042_gendata_h48h4k2/gendata_h48h4k2.c deleted file mode 100644 index 868c360..0000000 --- a/tools/0042_gendata_h48h4k2/gendata_h48h4k2.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "../tool.h" - -uint64_t expected[21] = { - /* Base value is 8 */ - [0] = 0, /* Unknown */ - [1] = 0, /* Unknown */ - [2] = 0, /* Unknown */ - [3] = 0, /* Unknown */ -}; - -void run(void) { - gendata_run("h48", "4;2;20", "tables/h48h4k2", expected); -} - -int main(void) { - nissy_setlogger(log_stderr); - - timerun(run, "benchmark gendata_h48 h = 4, k = 2"); - - return 0; -} diff --git a/tools/0052_gendata_h48h5k2/gendata_h48h5k2.c b/tools/0052_gendata_h48h5k2/gendata_h48h5k2.c deleted file mode 100644 index d292d49..0000000 --- a/tools/0052_gendata_h48h5k2/gendata_h48h5k2.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "../tool.h" - -uint64_t expected[21] = { - /* Base value is 8 */ - [0] = 0, /* Unknown */ - [1] = 0, /* Unknown */ - [2] = 0, /* Unknown */ - [3] = 0, /* Unknown */ -}; - -void run(void) { - gendata_run("h48", "5;2;20", "tables/h48h5k2", expected); -} - -int main(void) { - nissy_setlogger(log_stderr); - - timerun(run, "benchmark gendata_h48 h = 5, k = 2"); - - return 0; -} diff --git a/tools/0062_gendata_h48h6k2/gendata_h48h6k2.c b/tools/0062_gendata_h48h6k2/gendata_h48h6k2.c deleted file mode 100644 index 910d514..0000000 --- a/tools/0062_gendata_h48h6k2/gendata_h48h6k2.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "../tool.h" - -uint64_t expected[21] = { - /* Base value is 8 */ - [0] = 0, /* Unknown */ - [1] = 0, /* Unknown */ - [2] = 0, /* Unknown */ - [3] = 0, /* Unknown */ -}; - -void run(void) { - gendata_run("h48", "6;2;20", "tables/h48h6k2", expected); -} - -int main(void) { - nissy_setlogger(log_stderr); - - timerun(run, "benchmark gendata_h48 h = 6, k = 2"); - - return 0; -} diff --git a/tools/0072_gendata_h48h7k2/gendata_h48h7k2.c b/tools/0072_gendata_h48h7k2/gendata_h48h7k2.c deleted file mode 100644 index b803328..0000000 --- a/tools/0072_gendata_h48h7k2/gendata_h48h7k2.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "../tool.h" - -uint64_t expected[21] = { - /* Base value is 8 */ - [0] = 0, /* Unknown */ - [1] = 0, /* Unknown */ - [2] = 0, /* Unknown */ - [3] = 0, /* Unknown */ -}; - -void run(void) { - gendata_run("h48", "7;2;20", "tables/h48h7k2", expected); -} - -int main(void) { - nissy_setlogger(log_stderr); - - timerun(run, "benchmark gendata_h48 h = 7, k = 2"); - - return 0; -} diff --git a/tools/0082_gendata_h48h8k2/gendata_h48h8k2.c b/tools/0082_gendata_h48h8k2/gendata_h48h8k2.c deleted file mode 100644 index 52c377a..0000000 --- a/tools/0082_gendata_h48h8k2/gendata_h48h8k2.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "../tool.h" - -uint64_t expected[21] = { - /* Base value is 8 */ - [0] = 0, /* Unknown */ - [1] = 0, /* Unknown */ - [2] = 0, /* Unknown */ - [3] = 0, /* Unknown */ -}; - -void run(void) { - gendata_run("h48", "8;2;20", "tables/h48h8k2", expected); -} - -int main(void) { - nissy_setlogger(log_stderr); - - timerun(run, "benchmark gendata_h48 h = 8, k = 2"); - - return 0; -} diff --git a/tools/0092_gendata_h48h9k2/gendata_h48h9k2.c b/tools/0092_gendata_h48h9k2/gendata_h48h9k2.c deleted file mode 100644 index 5d3e34a..0000000 --- a/tools/0092_gendata_h48h9k2/gendata_h48h9k2.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "../tool.h" - -uint64_t expected[21] = { - /* Base value is 8 */ - [0] = 0, /* Unknown */ - [1] = 0, /* Unknown */ - [2] = 0, /* Unknown */ - [3] = 0, /* Unknown */ -}; - -void run(void) { - gendata_run("h48", "9;2;20", "tables/h48h9k2", expected); -} - -int main(void) { - nissy_setlogger(log_stderr); - - timerun(run, "benchmark gendata_h48 h = 9, k = 2"); - - return 0; -} diff --git a/tools/0102_gendata_h48h10k2/gendata_h48h10k2.c b/tools/0102_gendata_h48h10k2/gendata_h48h10k2.c deleted file mode 100644 index 5e8fbb0..0000000 --- a/tools/0102_gendata_h48h10k2/gendata_h48h10k2.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "../tool.h" - -uint64_t expected[21] = { - /* Base value is 8 */ - [0] = 0, /* Unknown */ - [1] = 0, /* Unknown */ - [2] = 0, /* Unknown */ - [3] = 0, /* Unknown */ -}; - -void run(void) { - gendata_run("h48", "10;2;20", "tables/h48h10k2", expected); -} - -int main(void) { - nissy_setlogger(log_stderr); - - timerun(run, "benchmark gendata_h48 h = 10, k = 2"); - - return 0; -} diff --git a/tools/0112_gendata_h48h11k2/gendata_h48h11k2.c b/tools/0112_gendata_h48h11k2/gendata_h48h11k2.c deleted file mode 100644 index 00c0747..0000000 --- a/tools/0112_gendata_h48h11k2/gendata_h48h11k2.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "../tool.h" - -uint64_t expected[21] = { - /* Base value is 8 */ - [0] = 0, /* Unknown */ - [1] = 0, /* Unknown */ - [2] = 0, /* Unknown */ - [3] = 0, /* Unknown */ -}; - -void run(void) { - gendata_run("h48", "11;2;20", "tables/h48h11k2", expected); -} - -int main(void) { - nissy_setlogger(log_stderr); - - timerun(run, "benchmark gendata_h48 h = 11, k = 2"); - - return 0; -} diff --git a/tools/100_gendata/gendata.c b/tools/100_gendata/gendata.c new file mode 100644 index 0000000..78f81ab --- /dev/null +++ b/tools/100_gendata/gendata.c @@ -0,0 +1,80 @@ +#include "../tool.h" + +char *solver, *options; +uint64_t *expected; + +uint64_t expected_h48[12][9][21] = { + [0] = { + [2] = { + [0] = 5473562, + [1] = 34776317, + [2] = 68566704, + [3] = 8750867, + }, + [4] = { + [0] = 1, + [1] = 1, + [2] = 4, + [3] = 34, + [4] = 331, + [5] = 3612, + [6] = 41605, + [7] = 474128, + [8] = 4953846, + [9] = 34776317, + [10] = 68566704, + [11] = 8749194, + [12] = 1673, + }, + }, +}; + +static void +run(void) { + int64_t size; + char *buf, filename[1024]; + + getfilename(solver, options, filename); + 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); + + writetable(buf, size, filename); + break; + } + +gendata_run_finish: + free(buf); +} + +int main(int argc, char **argv) { + uint8_t h, k; + char description[256]; + + if (argc < 3) { + fprintf(stderr, "Error: not enough arguments." + "A solver and its options must be given.\n"); + return 1; + } + + solver = argv[1]; + options = argv[2]; + parse_h48_options(options, &h, &k, NULL); + expected = expected_h48[h][k]; + sprintf(description, "benchmark gendata_h48 h = %" PRIu8 + ", k = %" PRIu8 "", h, k); + + nissy_setlogger(log_stderr); + + timerun(run, description); + + return 0; +} diff --git a/tools/100_stats_tables_h48/stats_tables_h48.c b/tools/100_stats_tables_h48/stats_tables_h48.c deleted file mode 100644 index adac8fa..0000000 --- a/tools/100_stats_tables_h48/stats_tables_h48.c +++ /dev/null @@ -1,99 +0,0 @@ -#include - -#include "../tool.h" - -#define MAXMOVES 20 -#define NCUBES_PER_THREAD 10000 -#define LOG_EVERY (NCUBES_PER_THREAD / 10) - -const char *solver = "h48stats"; -const char *options = ""; -const char *filename = "tables/h48h0k4"; -char *buf; - -typedef struct { - int n; - int thread_id; - int64_t v[12][100]; -} thread_arg_t; - -uint64_t rand64(void) { - uint64_t i, ret; - - for (i = 0, ret = 0; i < 64; i++) - ret |= (uint64_t)(rand() % 2) << i; - - return ret; -} - -static void * -run_thread(void *arg) -{ - char sols[12], cube[22]; - int64_t ep, eo, cp, co; - int i, j; - - thread_arg_t *a = (thread_arg_t *)arg; - - for (i = 0; i < a->n; i++) { - ep = rand64(); - eo = rand64(); - cp = rand64(); - co = rand64(); - nissy_getcube(ep, eo, cp, co, "fix", cube); - nissy_solve(cube, "h48stats", "", "", - 0, MAXMOVES, 1, -1, buf, sols); - for (j = 0; j < 12; j++) - a->v[j][(int)sols[j]]++; - if ((i+1) % LOG_EVERY == 0) - fprintf(stderr, "[thread %d] %d cubes solved...\n", - a->thread_id, i+1); - } - - return NULL; -} - -void run(void) { - int64_t i, j, k, tot; - double avg; - pthread_t thread[THREADS]; - thread_arg_t arg[THREADS]; - - for (i = 0; i < THREADS; i++) { - arg[i] = (thread_arg_t) { - .thread_id = i, - .n = NCUBES_PER_THREAD, - .v = {{0}} - }; - pthread_create(&thread[i], NULL, run_thread, &arg[i]); - } - - for (i = 0; i < THREADS; i++) - pthread_join(thread[i], NULL); - - for (j = 0; j < 12; j++) { - printf("Data for h=%" PRId64 "\n", j); - for (k = 0, avg = 0.0; k < 16; k++) { - for (i = 0, tot = 0; i < THREADS; i++) - tot += arg[i].v[j][k]; - printf("%" PRId64 "\t%" PRId64 "\n", k, tot); - avg += tot * k; - } - avg /= (double)(NCUBES_PER_THREAD * THREADS); - printf("Average: %.4lf\n", avg); - printf("\n"); - } -} - -int main(void) { - srand(time(NULL)); - nissy_setlogger(log_stderr); - - if (getdata(solver, options, &buf, filename) != 0) - return 1; - - timerun(run, "h48 table stats"); - - free(buf); - return 0; -} diff --git a/tools/200_solve_small/solve_small.c b/tools/200_solve_small/solve_small.c deleted file mode 100644 index 4d07728..0000000 --- a/tools/200_solve_small/solve_small.c +++ /dev/null @@ -1,53 +0,0 @@ -#include - -#include "../tool.h" - -const char *solver = "h48"; -const char *options = "0;4;20"; -const char *filename = "tables/h48h0k4"; -char *buf; - -char *scrambles[] = { - "R D' R2 D R U2 R' D' R U2 R D R'", /* 12 optimal */ - "RLUD RLUD RLUD", /* 12 optimal */ - NULL -}; - -void run(void) { - int i; - int64_t n; - char sol[100], cube[22]; - - printf("Solved the following scrambles:\n\n"); - for (i = 0; scrambles[i] != NULL; i++) { - printf("%d. %s\n", i+1, scrambles[i]); - fprintf(stderr, "Solving scramble %s\n", scrambles[i]); - if (nissy_frommoves(scrambles[i], cube) == -1) { - fprintf(stderr, "Invalid scramble\n"); - printf("Invalid\n"); - continue; - } - n = nissy_solve( - cube, "h48", options, "", 0, 20, 1, -1, buf, sol); - if (n == 0) { - printf("No solution\n"); - fprintf(stderr, "No solution found\n"); - } else { - printf("Solutions:\n%s\n", sol); - } - } -} - -int main(void) { - - srand(time(NULL)); - nissy_setlogger(log_stderr); - - if (getdata(solver, options, &buf, filename) != 0) - return 1; - - timerun(run, "small solver benchmark"); - - free(buf); - return 0; -} diff --git a/tools/200_stats_tables_h48/stats_tables_h48.c b/tools/200_stats_tables_h48/stats_tables_h48.c new file mode 100644 index 0000000..adac8fa --- /dev/null +++ b/tools/200_stats_tables_h48/stats_tables_h48.c @@ -0,0 +1,99 @@ +#include + +#include "../tool.h" + +#define MAXMOVES 20 +#define NCUBES_PER_THREAD 10000 +#define LOG_EVERY (NCUBES_PER_THREAD / 10) + +const char *solver = "h48stats"; +const char *options = ""; +const char *filename = "tables/h48h0k4"; +char *buf; + +typedef struct { + int n; + int thread_id; + int64_t v[12][100]; +} thread_arg_t; + +uint64_t rand64(void) { + uint64_t i, ret; + + for (i = 0, ret = 0; i < 64; i++) + ret |= (uint64_t)(rand() % 2) << i; + + return ret; +} + +static void * +run_thread(void *arg) +{ + char sols[12], cube[22]; + int64_t ep, eo, cp, co; + int i, j; + + thread_arg_t *a = (thread_arg_t *)arg; + + for (i = 0; i < a->n; i++) { + ep = rand64(); + eo = rand64(); + cp = rand64(); + co = rand64(); + nissy_getcube(ep, eo, cp, co, "fix", cube); + nissy_solve(cube, "h48stats", "", "", + 0, MAXMOVES, 1, -1, buf, sols); + for (j = 0; j < 12; j++) + a->v[j][(int)sols[j]]++; + if ((i+1) % LOG_EVERY == 0) + fprintf(stderr, "[thread %d] %d cubes solved...\n", + a->thread_id, i+1); + } + + return NULL; +} + +void run(void) { + int64_t i, j, k, tot; + double avg; + pthread_t thread[THREADS]; + thread_arg_t arg[THREADS]; + + for (i = 0; i < THREADS; i++) { + arg[i] = (thread_arg_t) { + .thread_id = i, + .n = NCUBES_PER_THREAD, + .v = {{0}} + }; + pthread_create(&thread[i], NULL, run_thread, &arg[i]); + } + + for (i = 0; i < THREADS; i++) + pthread_join(thread[i], NULL); + + for (j = 0; j < 12; j++) { + printf("Data for h=%" PRId64 "\n", j); + for (k = 0, avg = 0.0; k < 16; k++) { + for (i = 0, tot = 0; i < THREADS; i++) + tot += arg[i].v[j][k]; + printf("%" PRId64 "\t%" PRId64 "\n", k, tot); + avg += tot * k; + } + avg /= (double)(NCUBES_PER_THREAD * THREADS); + printf("Average: %.4lf\n", avg); + printf("\n"); + } +} + +int main(void) { + srand(time(NULL)); + nissy_setlogger(log_stderr); + + if (getdata(solver, options, &buf, filename) != 0) + return 1; + + timerun(run, "h48 table stats"); + + free(buf); + return 0; +} diff --git a/tools/300_solve_small/solve_small.c b/tools/300_solve_small/solve_small.c new file mode 100644 index 0000000..4d07728 --- /dev/null +++ b/tools/300_solve_small/solve_small.c @@ -0,0 +1,53 @@ +#include + +#include "../tool.h" + +const char *solver = "h48"; +const char *options = "0;4;20"; +const char *filename = "tables/h48h0k4"; +char *buf; + +char *scrambles[] = { + "R D' R2 D R U2 R' D' R U2 R D R'", /* 12 optimal */ + "RLUD RLUD RLUD", /* 12 optimal */ + NULL +}; + +void run(void) { + int i; + int64_t n; + char sol[100], cube[22]; + + printf("Solved the following scrambles:\n\n"); + for (i = 0; scrambles[i] != NULL; i++) { + printf("%d. %s\n", i+1, scrambles[i]); + fprintf(stderr, "Solving scramble %s\n", scrambles[i]); + if (nissy_frommoves(scrambles[i], cube) == -1) { + fprintf(stderr, "Invalid scramble\n"); + printf("Invalid\n"); + continue; + } + n = nissy_solve( + cube, "h48", options, "", 0, 20, 1, -1, buf, sol); + if (n == 0) { + printf("No solution\n"); + fprintf(stderr, "No solution found\n"); + } else { + printf("Solutions:\n%s\n", sol); + } + } +} + +int main(void) { + + srand(time(NULL)); + nissy_setlogger(log_stderr); + + if (getdata(solver, options, &buf, filename) != 0) + return 1; + + timerun(run, "small solver benchmark"); + + free(buf); + return 0; +} diff --git a/tools/nissy_extra.h b/tools/nissy_extra.h new file mode 100644 index 0000000..17906d4 --- /dev/null +++ b/tools/nissy_extra.h @@ -0,0 +1,3 @@ +/* Intended only for tools */ + +int parse_h48_options(const char *, uint8_t *, uint8_t *, uint8_t *); diff --git a/tools/run_tool.sh b/tools/run_tool.sh index 7e09684..2216a66 100755 --- a/tools/run_tool.sh +++ b/tools/run_tool.sh @@ -5,7 +5,7 @@ if [ -z "$TOOL" ]; then exit 1 fi -CC="$CC -D_POSIX_C_SOURCE=199309L" +CC="$CC -D_POSIX_C_SOURCE=199309L" # For timer BIN="tools/run" d="$(date +'%Y-%m-%d-%H-%M-%S')" @@ -16,7 +16,8 @@ for t in tools/*; do fi toolname="$(basename "$t" .c)" $CC -o $BIN "$t"/*.c "$CUBEOBJ" || exit 1; - $BIN | tee "tools/results/$toolname-$d.txt" "tools/results/last.out" + $BIN $TOOLARGS \ + | tee "tools/results/$toolname-$d.txt" "tools/results/last.out" break done diff --git a/tools/tool.h b/tools/tool.h index 757657b..1540f22 100644 --- a/tools/tool.h +++ b/tools/tool.h @@ -6,14 +6,16 @@ #include #include "../src/nissy.h" +#include "nissy_extra.h" static void log_stderr(const char *, ...); static void log_stdout(const char *, ...); static double timerun(void (*)(void), const char *); +static void getfilename(const char *, const char *, 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 gendata_run(const char *, const char *, uint64_t *); static void log_stderr(const char *str, ...) @@ -69,6 +71,17 @@ timerun(void (*run)(void), const char *name) return tdiff; } +static void +getfilename(const char *solver, const char *options, char *filename) +{ + uint8_t h, k; + + /* Only h48 supported for now */ + parse_h48_options(options, &h, &k, NULL); + + sprintf(filename, "tables/%sh%dk%d", solver, h, k); +} + static void writetable(const char *buf, int64_t size, const char *filename) { @@ -155,13 +168,12 @@ 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; + char *buf, filename[1024]; - + getfilename(solver, options, filename); size = generatetable(solver, options, &buf); switch (size) { case -1: -- cgit v1.3 From bc2cba8529a163d129dec91b5ec448a29a14591a Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 26 Sep 2024 15:57:25 +0200 Subject: Added checkdata function --- src/nissy.c | 63 +++++++++++++++++++++++++++++++ src/nissy.h | 6 +++ src/solvers/h48/gendata_cocsep.h | 12 ++++++ src/solvers/h48/gendata_h48.h | 24 +++++++++++- tools/000_gendata/gendata.c | 80 ++++++++++++++++++++++++++++++++++++++++ tools/100_gendata/gendata.c | 80 ---------------------------------------- 6 files changed, 184 insertions(+), 81 deletions(-) create mode 100644 tools/000_gendata/gendata.c delete mode 100644 tools/100_gendata/gendata.c (limited to 'tools') diff --git a/src/nissy.c b/src/nissy.c index 318cff1..27e2835 100644 --- a/src/nissy.c +++ b/src/nissy.c @@ -13,6 +13,9 @@ int parse_h48_options(const char *, uint8_t *, uint8_t *, uint8_t *); STATIC int64_t write_result(cube_t, char [static 22]); +STATIC bool distribution_equal( + const uint64_t [static 21], const uint64_t [static 21], uint8_t); +STATIC bool checkdata(const void *, const tableinfo_t *); /* TODO: add option to get DR, maybe C-only, E-only, eo... */ #define GETCUBE_OPTIONS(S, F) { .option = S, .fix = F } @@ -62,6 +65,49 @@ parse_h48_options_error: return -1; } +STATIC bool +checkdata(const void *buf, const tableinfo_t *info) +{ + uint64_t distr[21]; + + if (!strncmp(info->solver, "cocsep", 6)) { + getdistribution_cocsep( + (uint32_t *)((char *)buf + INFOSIZE), distr); + } else if (!strncmp(info->solver, "h48", 3)) { + getdistribution_h48((uint8_t *)buf + INFOSIZE, distr, + info->h48h, info->bits); + } else { + LOG("checkdata: unknown solver %s\n", info->solver); + return false; + } + + return distribution_equal(info->distribution, distr, info->maxvalue); +} + +STATIC bool +distribution_equal( + const uint64_t expected[static 21], + const uint64_t actual[static 21], + uint8_t maxvalue +) +{ + int wrong; + uint8_t i; + + for (i = 0, wrong = 0; i <= MAX(maxvalue, 20); i++) { + if (expected[i] != actual[i]) { + wrong++; + LOG("Value %" PRIu8 ": expected %" PRIu64 ", found %" + PRIu64 "\n", i, expected[i], actual[i]); + } + } + + if (wrong > 0) + LOG("chekdata: %d wrong values\n", wrong); + + return wrong > 0; +} + STATIC int64_t write_result(cube_t cube, char result[static 22]) { @@ -274,6 +320,23 @@ nissy_gendata( return ret; } +int64_t +nissy_checkdata( + const char *solver, + const char *options, + const void *data +) +{ + char *buf; + tableinfo_t info; + + for (buf = (char *)data; readtableinfo(buf, &info); buf += info.next) + if (!checkdata(buf, &info)) + return 1; + + return 0; +} + int64_t nissy_solve( const char cube[static 22], diff --git a/src/nissy.h b/src/nissy.h index 336130c..e8c35c3 100644 --- a/src/nissy.h +++ b/src/nissy.h @@ -86,6 +86,12 @@ int64_t nissy_gendata( void *generated_data ); +int64_t nissy_checkdata( + const char *solver, + const char *options, + const void *data +); + /* Print information on a data table via the provided callback writer */ int64_t nissy_datainfo( const void *table, diff --git a/src/solvers/h48/gendata_cocsep.h b/src/solvers/h48/gendata_cocsep.h index 2189145..262793b 100644 --- a/src/solvers/h48/gendata_cocsep.h +++ b/src/solvers/h48/gendata_cocsep.h @@ -25,6 +25,7 @@ STATIC_INLINE void set_visited(uint8_t *, int64_t); STATIC size_t gendata_cocsep(void *, uint64_t *, cube_t *); STATIC uint32_t gendata_cocsep_dfs(cocsep_dfs_arg_t *); +STATIC void getdistribution_cocsep(const uint32_t *, uint64_t [static 21]); STATIC_INLINE int8_t get_h48_cdata(cube_t, uint32_t *, uint32_t *); @@ -157,6 +158,17 @@ gendata_cocsep_dfs(cocsep_dfs_arg_t *arg) return cc; } +STATIC void +getdistribution_cocsep(const uint32_t *table, uint64_t distr[static 21]) +{ + size_t i; + + memset(distr, 0, 21 * sizeof(uint64_t)); + + for (i = 0; i < COCSEP_TABLESIZE; i++) + distr[CBOUND(table[i])]++; +} + STATIC_INLINE bool get_visited(const uint8_t *a, int64_t i) { diff --git a/src/solvers/h48/gendata_h48.h b/src/solvers/h48/gendata_h48.h index 3445570..6e7b0ca 100644 --- a/src/solvers/h48/gendata_h48.h +++ b/src/solvers/h48/gendata_h48.h @@ -104,13 +104,16 @@ STATIC size_t gendata_h48k2_realcoord(gendata_h48_arg_t *); STATIC void gendata_h48k2_dfs(h48k2_dfs_arg_t *arg); STATIC void * gendata_h48k2_runthread(void *); STATIC tableinfo_t makeinfo_h48k2(gendata_h48_arg_t *, uint8_t); +STATIC void getdistribution_h48( + const uint8_t *, uint64_t [static 21], uint8_t, uint8_t); STATIC uint32_t *get_cocsepdata_ptr(const void *); STATIC uint8_t *get_h48data_ptr(const void *); STATIC_INLINE uint8_t get_h48_pval(const uint8_t *, int64_t, uint8_t); STATIC_INLINE void set_h48_pval(uint8_t *, int64_t, uint8_t, uint8_t); -STATIC_INLINE uint8_t get_h48_bound(cube_t, uint32_t, uint8_t, uint8_t, uint8_t *); +STATIC_INLINE uint8_t get_h48_bound( + cube_t, uint32_t, uint8_t, uint8_t, uint8_t *); STATIC uint64_t gendata_h48short(gendata_h48short_arg_t *arg) @@ -662,6 +665,25 @@ makeinfo_h48k2(gendata_h48_arg_t *arg, uint8_t base) return info; } +STATIC void +getdistribution_h48( + const uint8_t *table, + uint64_t distr[static 21], + uint8_t h, + uint8_t k +) { + uint8_t val; + int64_t i, h48max; + + memset(distr, 0, 21 * sizeof(uint64_t)); + + h48max = H48_COORDMAX(h); + for (i = 0; i < h48max; i++) { + val = get_h48_pval(table, i, k); + distr[val]++; + } +} + STATIC uint32_t * get_cocsepdata_ptr(const void *data) { diff --git a/tools/000_gendata/gendata.c b/tools/000_gendata/gendata.c new file mode 100644 index 0000000..78f81ab --- /dev/null +++ b/tools/000_gendata/gendata.c @@ -0,0 +1,80 @@ +#include "../tool.h" + +char *solver, *options; +uint64_t *expected; + +uint64_t expected_h48[12][9][21] = { + [0] = { + [2] = { + [0] = 5473562, + [1] = 34776317, + [2] = 68566704, + [3] = 8750867, + }, + [4] = { + [0] = 1, + [1] = 1, + [2] = 4, + [3] = 34, + [4] = 331, + [5] = 3612, + [6] = 41605, + [7] = 474128, + [8] = 4953846, + [9] = 34776317, + [10] = 68566704, + [11] = 8749194, + [12] = 1673, + }, + }, +}; + +static void +run(void) { + int64_t size; + char *buf, filename[1024]; + + getfilename(solver, options, filename); + 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); + + writetable(buf, size, filename); + break; + } + +gendata_run_finish: + free(buf); +} + +int main(int argc, char **argv) { + uint8_t h, k; + char description[256]; + + if (argc < 3) { + fprintf(stderr, "Error: not enough arguments." + "A solver and its options must be given.\n"); + return 1; + } + + solver = argv[1]; + options = argv[2]; + parse_h48_options(options, &h, &k, NULL); + expected = expected_h48[h][k]; + sprintf(description, "benchmark gendata_h48 h = %" PRIu8 + ", k = %" PRIu8 "", h, k); + + nissy_setlogger(log_stderr); + + timerun(run, description); + + return 0; +} diff --git a/tools/100_gendata/gendata.c b/tools/100_gendata/gendata.c deleted file mode 100644 index 78f81ab..0000000 --- a/tools/100_gendata/gendata.c +++ /dev/null @@ -1,80 +0,0 @@ -#include "../tool.h" - -char *solver, *options; -uint64_t *expected; - -uint64_t expected_h48[12][9][21] = { - [0] = { - [2] = { - [0] = 5473562, - [1] = 34776317, - [2] = 68566704, - [3] = 8750867, - }, - [4] = { - [0] = 1, - [1] = 1, - [2] = 4, - [3] = 34, - [4] = 331, - [5] = 3612, - [6] = 41605, - [7] = 474128, - [8] = 4953846, - [9] = 34776317, - [10] = 68566704, - [11] = 8749194, - [12] = 1673, - }, - }, -}; - -static void -run(void) { - int64_t size; - char *buf, filename[1024]; - - getfilename(solver, options, filename); - 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); - - writetable(buf, size, filename); - break; - } - -gendata_run_finish: - free(buf); -} - -int main(int argc, char **argv) { - uint8_t h, k; - char description[256]; - - if (argc < 3) { - fprintf(stderr, "Error: not enough arguments." - "A solver and its options must be given.\n"); - return 1; - } - - solver = argv[1]; - options = argv[2]; - parse_h48_options(options, &h, &k, NULL); - expected = expected_h48[h][k]; - sprintf(description, "benchmark gendata_h48 h = %" PRIu8 - ", k = %" PRIu8 "", h, k); - - nissy_setlogger(log_stderr); - - timerun(run, description); - - return 0; -} -- cgit v1.3 From 91f912176045fd2f95fd45a0e12354e390dff219 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 26 Sep 2024 17:07:02 +0200 Subject: Added checkdata tool --- src/nissy.c | 7 ++++-- tools/000_gendata/gendata.c | 29 ++--------------------- tools/100_checkdata/checkdata.c | 52 +++++++++++++++++++++++++++++++++++++++++ tools/expected_distributions.h | 25 ++++++++++++++++++++ 4 files changed, 84 insertions(+), 29 deletions(-) create mode 100644 tools/100_checkdata/checkdata.c create mode 100644 tools/expected_distributions.h (limited to 'tools') diff --git a/src/nissy.c b/src/nissy.c index 27e2835..953471f 100644 --- a/src/nissy.c +++ b/src/nissy.c @@ -102,8 +102,11 @@ distribution_equal( } } - if (wrong > 0) - LOG("chekdata: %d wrong values\n", wrong); + if (wrong > 0) { + LOG("checkdata: %d wrong values\n", wrong); + } else { + LOG("checkdata: table is consistent with info\n"); + } return wrong > 0; } diff --git a/tools/000_gendata/gendata.c b/tools/000_gendata/gendata.c index 78f81ab..90166dd 100644 --- a/tools/000_gendata/gendata.c +++ b/tools/000_gendata/gendata.c @@ -1,34 +1,9 @@ #include "../tool.h" +#include "../expected_distributions.h" char *solver, *options; uint64_t *expected; -uint64_t expected_h48[12][9][21] = { - [0] = { - [2] = { - [0] = 5473562, - [1] = 34776317, - [2] = 68566704, - [3] = 8750867, - }, - [4] = { - [0] = 1, - [1] = 1, - [2] = 4, - [3] = 34, - [4] = 331, - [5] = 3612, - [6] = 41605, - [7] = 474128, - [8] = 4953846, - [9] = 34776317, - [10] = 68566704, - [11] = 8749194, - [12] = 1673, - }, - }, -}; - static void run(void) { int64_t size; @@ -60,7 +35,7 @@ int main(int argc, char **argv) { char description[256]; if (argc < 3) { - fprintf(stderr, "Error: not enough arguments." + fprintf(stderr, "Error: not enough arguments. " "A solver and its options must be given.\n"); return 1; } diff --git a/tools/100_checkdata/checkdata.c b/tools/100_checkdata/checkdata.c new file mode 100644 index 0000000..05961c1 --- /dev/null +++ b/tools/100_checkdata/checkdata.c @@ -0,0 +1,52 @@ +#include "../tool.h" +#include "../expected_distributions.h" + +char *solver, *options, *filename; + +static void +run(void) { + int64_t size; + char *buf; + FILE *f; + + size = nissy_datasize(solver, options); + + if (size <= 0) { + fprintf(stderr, "Error in datasize\n"); + return; + } + + if ((f = fopen(filename, "rb")) == NULL) { + fprintf(stderr, "Error reading file %s\n", filename); + return; + } + + buf = malloc(size); + fread(buf, size, 1, f); + fclose(f); + nissy_checkdata(solver, options, buf); + free(buf); + + /* TODO: cross-check with expected distributions? */ +} + +int main(int argc, char **argv) { + char description[256]; + + if (argc < 4) { + fprintf(stderr, "Error: not enough arguments. " + "A solver, its options and a file name must be given.\n"); + return 1; + } + + solver = argv[1]; + options = argv[2]; + filename = argv[3]; + sprintf(description, "checking data for solver %s" + "with options %s from file %s", solver, options, filename); + nissy_setlogger(log_stderr); + + timerun(run, description); + + return 0; +} diff --git a/tools/expected_distributions.h b/tools/expected_distributions.h new file mode 100644 index 0000000..27bd64a --- /dev/null +++ b/tools/expected_distributions.h @@ -0,0 +1,25 @@ +uint64_t expected_h48[12][9][21] = { + [0] = { + [2] = { + [0] = 5473562, + [1] = 34776317, + [2] = 68566704, + [3] = 8750867, + }, + [4] = { + [0] = 1, + [1] = 1, + [2] = 4, + [3] = 34, + [4] = 331, + [5] = 3612, + [6] = 41605, + [7] = 474128, + [8] = 4953846, + [9] = 34776317, + [10] = 68566704, + [11] = 8749194, + [12] = 1673, + }, + }, +}; -- cgit v1.3 From 79500f46632ba50b1640d16983942b940dd7955f Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 26 Sep 2024 19:07:10 +0200 Subject: small fix --- tools/tool.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/tool.h b/tools/tool.h index 1540f22..18b8ce9 100644 --- a/tools/tool.h +++ b/tools/tool.h @@ -15,7 +15,7 @@ static void getfilename(const char *, const char *, 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 *, uint64_t *); +static void gendata_run(const char *, const char *, uint64_t [static 21]); static void log_stderr(const char *str, ...) -- cgit v1.3