From b5efa2c7bfa259f1b9f5afa68349e2742427de80 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Tue, 27 Aug 2024 17:44:17 +0200 Subject: New tool for benchmarking small solver --- tools/001_gendata_h48/gendata_h48.c | 71 ---------------------- tools/001_gendata_h48h0k4/gendata_h48h0k4.c | 71 ++++++++++++++++++++++ tools/003_solve_small/solve_small.c | 94 +++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+), 71 deletions(-) delete mode 100644 tools/001_gendata_h48/gendata_h48.c create mode 100644 tools/001_gendata_h48h0k4/gendata_h48h0k4.c create mode 100644 tools/003_solve_small/solve_small.c diff --git a/tools/001_gendata_h48/gendata_h48.c b/tools/001_gendata_h48/gendata_h48.c deleted file mode 100644 index a73f818..0000000 --- a/tools/001_gendata_h48/gendata_h48.c +++ /dev/null @@ -1,71 +0,0 @@ -#include "../timerun.h" -#include "../../src/nissy.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] = { - [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, -}; - -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 + (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", - 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/001_gendata_h48h0k4/gendata_h48h0k4.c b/tools/001_gendata_h48h0k4/gendata_h48h0k4.c new file mode 100644 index 0000000..a73f818 --- /dev/null +++ b/tools/001_gendata_h48h0k4/gendata_h48h0k4.c @@ -0,0 +1,71 @@ +#include "../timerun.h" +#include "../../src/nissy.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] = { + [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, +}; + +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 + (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", + 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/003_solve_small/solve_small.c b/tools/003_solve_small/solve_small.c new file mode 100644 index 0000000..406e255 --- /dev/null +++ b/tools/003_solve_small/solve_small.c @@ -0,0 +1,94 @@ +#include +#include +#include "../timerun.h" +#include "../../src/nissy.h" + +#define 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 getdata(int64_t size) { + int64_t s; + FILE *f; + + 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("h48", OPTIONS, buf); + if (s != size) { + fprintf(stderr, "Error generating table"); + if (s != -1) + fprintf(stderr, " (got %" PRId64 " bytes)", s); + fprintf(stderr, "\n"); + return 1; + } + 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); + } + } else { + fprintf(stderr, "Reading tables from file %s\n", filename); + fread(buf, size, 1, f); + fclose(f); + } + + return 0; +} + +int main(void) { + int64_t size; + + srand(time(NULL)); + + nissy_setlogger(log_stderr); + size = nissy_datasize("h48", OPTIONS); + if (size == -1) { + printf("h48 stats: error in datasize\n"); + return 1; + } + + if (getdata(size) != 0) { + printf("Error getting table, stopping\n"); + free(buf); + return 1; + } + + timerun(run, "small solver benchmark"); + + free(buf); + return 0; +} -- cgit v1.3