From c548499e68e473452592c9b0bf860f76bd8b94b8 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Fri, 30 Aug 2024 07:52:53 +0200 Subject: Added tool for checking new tables (and they are wrong) --- tools/001_gendata_h48h0k4/gendata_h48h0k4.c | 4 +- tools/002_gendata_h48h0k2/gendata_h48h0k2.c | 62 ++++++++++++++++ tools/002_stats_tables_h48/stats_tables_h48.c | 100 -------------------------- tools/003_solve_small/solve_small.c | 51 ------------- tools/010_stats_tables_h48/stats_tables_h48.c | 100 ++++++++++++++++++++++++++ tools/020_solve_small/solve_small.c | 51 +++++++++++++ 6 files changed, 215 insertions(+), 153 deletions(-) create mode 100644 tools/002_gendata_h48h0k2/gendata_h48h0k2.c delete mode 100644 tools/002_stats_tables_h48/stats_tables_h48.c delete mode 100644 tools/003_solve_small/solve_small.c create mode 100644 tools/010_stats_tables_h48/stats_tables_h48.c create mode 100644 tools/020_solve_small/solve_small.c (limited to 'tools') diff --git a/tools/001_gendata_h48h0k4/gendata_h48h0k4.c b/tools/001_gendata_h48h0k4/gendata_h48h0k4.c index 0926a34..a2c57ca 100644 --- a/tools/001_gendata_h48h0k4/gendata_h48h0k4.c +++ b/tools/001_gendata_h48h0k4/gendata_h48h0k4.c @@ -37,12 +37,12 @@ void run(void) { printf("Error generating table\n"); } else { printf("Succesfully generated %" PRId64 " bytes. Table:\n", s); - h48info = (uint32_t *)buf + (ETABLESIZE(HVALUE) + COCSEPSIZE) / 4; + h48info = (uint32_t *)buf + 1 + (ETABLESIZE(HVALUE) + COCSEPSIZE) / 4; for (i = 0; i < MAXDEPTH+1 && h48info[i+1]; i++) { x = h48info[i+1]; printf("%d:\t%" PRIu32, i, x); if (x != expected[i]) - printf(" <--- Error! Expected: %" PRIu32 "\n", + printf(" <--- Error! Expected: %" PRIu32, expected[i]); printf("\n"); } diff --git a/tools/002_gendata_h48h0k2/gendata_h48h0k2.c b/tools/002_gendata_h48h0k2/gendata_h48h0k2.c new file mode 100644 index 0000000..2754f80 --- /dev/null +++ b/tools/002_gendata_h48h0k2/gendata_h48h0k2.c @@ -0,0 +1,62 @@ +#include "../tool.h" + +#define MAXDEPTH 20 +#define HVALUE 0 +#define OPTIONS "0;2;20" +#define LONGOPTIONS "h = 0, k = 2, max depth = 20" + +#define COCSEPSIZE 1119792 +#define ETABLESIZE(h) (((3393 * 495 * 70) >> 2) << (size_t)(h)) + +uint32_t expected[21] = { + /* Base value is 8 */ + [0] = 5473562, + [1] = 34776317, + [2] = 68566704, + [3] = 8750867, +}; + +char *buf; + +void run(void) { + uint32_t *h48info, x; + int i; + int64_t s; + + s = nissy_gendata("h48", OPTIONS, buf); + + if (s == -1) { + printf("Error generating table\n"); + } else { + printf("Succesfully generated %" PRId64 " bytes. Table:\n", s); + h48info = (uint32_t *)buf + 1 + (ETABLESIZE(HVALUE) + COCSEPSIZE) / 4; + for (i = 0; i < 4; i++) { + x = h48info[i+1]; + printf("%d:\t%" PRIu32, i, x); + if (x != expected[i]) + printf(" <--- Error! Expected: %" PRIu32, + expected[i]); + printf("\n"); + } + } +} + +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); + + return 0; +} diff --git a/tools/002_stats_tables_h48/stats_tables_h48.c b/tools/002_stats_tables_h48/stats_tables_h48.c deleted file mode 100644 index 8b65862..0000000 --- a/tools/002_stats_tables_h48/stats_tables_h48.c +++ /dev/null @@ -1,100 +0,0 @@ -#include - -#include "../tool.h" - -#define MAXMOVES 20 -#define NTHREADS 32 -#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[NTHREADS]; - thread_arg_t arg[NTHREADS]; - - for (i = 0; i < NTHREADS; 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 < NTHREADS; 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 < NTHREADS; i++) - tot += arg[i].v[j][k]; - printf("%" PRId64 "\t%" PRId64 "\n", k, tot); - avg += tot * k; - } - avg /= (double)(NCUBES_PER_THREAD * NTHREADS); - 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/003_solve_small/solve_small.c b/tools/003_solve_small/solve_small.c deleted file mode 100644 index dee2b55..0000000 --- a/tools/003_solve_small/solve_small.c +++ /dev/null @@ -1,51 +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("%s\n", scrambles[i]); - fprintf(stderr, "Solving scramble %s\n", scrambles[i]); - if (nissy_frommoves(scrambles[i], cube) == -1) { - fprintf(stderr, "Invalid scramble, " - "continuing with next scramble\n"); - continue; - } - n = nissy_solve( - cube, "h48", options, "", 0, 20, 1, -1, buf, sol); - if (n == 0) - fprintf(stderr, "No solution found, " - "continuing with next scramble\n"); - } - printf("\n"); -} - -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/010_stats_tables_h48/stats_tables_h48.c b/tools/010_stats_tables_h48/stats_tables_h48.c new file mode 100644 index 0000000..8b65862 --- /dev/null +++ b/tools/010_stats_tables_h48/stats_tables_h48.c @@ -0,0 +1,100 @@ +#include + +#include "../tool.h" + +#define MAXMOVES 20 +#define NTHREADS 32 +#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[NTHREADS]; + thread_arg_t arg[NTHREADS]; + + for (i = 0; i < NTHREADS; 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 < NTHREADS; 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 < NTHREADS; i++) + tot += arg[i].v[j][k]; + printf("%" PRId64 "\t%" PRId64 "\n", k, tot); + avg += tot * k; + } + avg /= (double)(NCUBES_PER_THREAD * NTHREADS); + 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 new file mode 100644 index 0000000..dee2b55 --- /dev/null +++ b/tools/020_solve_small/solve_small.c @@ -0,0 +1,51 @@ +#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("%s\n", scrambles[i]); + fprintf(stderr, "Solving scramble %s\n", scrambles[i]); + if (nissy_frommoves(scrambles[i], cube) == -1) { + fprintf(stderr, "Invalid scramble, " + "continuing with next scramble\n"); + continue; + } + n = nissy_solve( + cube, "h48", options, "", 0, 20, 1, -1, buf, sol); + if (n == 0) + fprintf(stderr, "No solution found, " + "continuing with next scramble\n"); + } + printf("\n"); +} + +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; +} -- cgit v1.3