From 7f78e56994a824fc8364c91032c1be4ee767ec41 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Wed, 18 Sep 2024 13:58:31 +0200 Subject: Renamed tools; fixed solve tool; added gendata_h48h1k2 tool --- 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/001_gendata_h48h0k4/gendata_h48h0k4.c | 29 -------- tools/002_gendata_h48h0k2/gendata_h48h0k2.c | 21 ------ tools/010_stats_tables_h48/stats_tables_h48.c | 99 --------------------------- tools/020_solve_small/solve_small.c | 53 -------------- tools/100_stats_tables_h48/stats_tables_h48.c | 99 +++++++++++++++++++++++++++ tools/200_solve_small/solve_small.c | 53 ++++++++++++++ tools/tool.h | 2 +- 10 files changed, 224 insertions(+), 203 deletions(-) create mode 100644 tools/0002_gendata_h48h0k2/gendata_h48h0k2.c create mode 100644 tools/0004_gendata_h48h0k4/gendata_h48h0k4.c create mode 100644 tools/0012_gendata_h48h1k2/gendata_h48h1k2.c delete mode 100644 tools/001_gendata_h48h0k4/gendata_h48h0k4.c delete mode 100644 tools/002_gendata_h48h0k2/gendata_h48h0k2.c delete mode 100644 tools/010_stats_tables_h48/stats_tables_h48.c delete mode 100644 tools/020_solve_small/solve_small.c create mode 100644 tools/100_stats_tables_h48/stats_tables_h48.c create mode 100644 tools/200_solve_small/solve_small.c (limited to 'tools') diff --git a/tools/0002_gendata_h48h0k2/gendata_h48h0k2.c b/tools/0002_gendata_h48h0k2/gendata_h48h0k2.c new file mode 100644 index 0000000..14f99ee --- /dev/null +++ b/tools/0002_gendata_h48h0k2/gendata_h48h0k2.c @@ -0,0 +1,21 @@ +#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 new file mode 100644 index 0000000..ff34bb9 --- /dev/null +++ b/tools/0004_gendata_h48h0k4/gendata_h48h0k4.c @@ -0,0 +1,29 @@ +#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 new file mode 100644 index 0000000..0ebdcaa --- /dev/null +++ b/tools/0012_gendata_h48h1k2/gendata_h48h1k2.c @@ -0,0 +1,21 @@ +#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/001_gendata_h48h0k4/gendata_h48h0k4.c b/tools/001_gendata_h48h0k4/gendata_h48h0k4.c deleted file mode 100644 index ff34bb9..0000000 --- a/tools/001_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/002_gendata_h48h0k2/gendata_h48h0k2.c b/tools/002_gendata_h48h0k2/gendata_h48h0k2.c deleted file mode 100644 index 14f99ee..0000000 --- a/tools/002_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/010_stats_tables_h48/stats_tables_h48.c b/tools/010_stats_tables_h48/stats_tables_h48.c deleted file mode 100644 index adac8fa..0000000 --- a/tools/010_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/020_solve_small/solve_small.c b/tools/020_solve_small/solve_small.c deleted file mode 100644 index 4d07728..0000000 --- a/tools/020_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/100_stats_tables_h48/stats_tables_h48.c b/tools/100_stats_tables_h48/stats_tables_h48.c new file mode 100644 index 0000000..adac8fa --- /dev/null +++ b/tools/100_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/200_solve_small/solve_small.c b/tools/200_solve_small/solve_small.c new file mode 100644 index 0000000..4d07728 --- /dev/null +++ b/tools/200_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/tool.h b/tools/tool.h index eaee704..5ca1f04 100644 --- a/tools/tool.h +++ b/tools/tool.h @@ -129,7 +129,7 @@ getdata( case -2: goto getdata_error; default: - writetable(filename, size, *buf); + writetable(*buf, size, filename); break; } } else { -- cgit v1.3