diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-08-30 07:52:04 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-08-30 07:52:04 +0200 |
| commit | a7444e9ab414e1eb73451a806289a3f7c4f02047 (patch) | |
| tree | 02219807113dde108e278f38f3dd5874f7e3dc18 | |
| parent | 1983bacfbb84e2a410ebe663ac1fa7c1b22eb096 (diff) | |
| parent | c548499e68e473452592c9b0bf860f76bd8b94b8 (diff) | |
| download | nissy-core-a7444e9ab414e1eb73451a806289a3f7c4f02047.tar.gz nissy-core-a7444e9ab414e1eb73451a806289a3f7c4f02047.zip | |
Merge branch 'master' of tronto.net:h48
| -rw-r--r-- | src/solvers/h48/gendata_h48.h | 31 | ||||
| -rw-r--r-- | src/solvers/h48/map.h | 5 | ||||
| -rw-r--r-- | src/utils/math.h | 1 | ||||
| -rw-r--r-- | tools/001_gendata_h48h0k4/gendata_h48h0k4.c | 7 | ||||
| -rw-r--r-- | tools/002_gendata_h48h0k2/gendata_h48h0k2.c | 62 | ||||
| -rw-r--r-- | tools/003_solve_small/solve_small.c | 94 | ||||
| -rw-r--r-- | tools/010_stats_tables_h48/stats_tables_h48.c (renamed from tools/002_stats_tables_h48/stats_tables_h48.c) | 59 | ||||
| -rw-r--r-- | tools/020_solve_small/solve_small.c | 51 | ||||
| -rw-r--r-- | tools/timerun.h | 51 | ||||
| -rw-r--r-- | tools/tool.h | 106 |
10 files changed, 255 insertions, 212 deletions
diff --git a/src/solvers/h48/gendata_h48.h b/src/solvers/h48/gendata_h48.h index 2d222c9..fbb8b35 100644 --- a/src/solvers/h48/gendata_h48.h +++ b/src/solvers/h48/gendata_h48.h | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | #define H48_COORDMAX_NOEO ((int64_t)(COCSEP_CLASSES * _12c4 * _8c4)) | 1 | #define H48_COORDMAX_NOEO ((int64_t)(COCSEP_CLASSES * _12c4 * _8c4)) |
| 2 | #define H48_COORDMAX(h) ((int64_t)(H48_COORDMAX_NOEO << (int64_t)(h))) | 2 | #define H48_COORDMAX(h) ((int64_t)(H48_COORDMAX_NOEO << (int64_t)(h))) |
| 3 | #define H48_TABLESIZE(h, k) ((size_t)H48_COORDMAX((h)) / ((size_t)8 / (size_t)(k))) | 3 | #define H48_DIV(k) ((size_t)8 / (size_t)(k)) |
| 4 | #define H48_TABLESIZE(h, k) _div_round_up((size_t)H48_COORDMAX((h)), H48_DIV(k)) | ||
| 4 | 5 | ||
| 5 | #define H48_COEFF(k) (UINT32_C(32) / (uint32_t)(k)) | 6 | #define H48_COEFF(k) (UINT32_C(32) / (uint32_t)(k)) |
| 6 | #define H48_INDEX(i, k) ((uint32_t)(i) / H48_COEFF(k)) | 7 | #define H48_INDEX(i, k) ((uint32_t)(i) / H48_COEFF(k)) |
| @@ -140,7 +141,7 @@ gendata_h48(gendata_h48_arg_t *arg) | |||
| 140 | cocsepsize = gendata_cocsep( | 141 | cocsepsize = gendata_cocsep( |
| 141 | (void *)arg->cocsepdata, arg->selfsim, arg->crep); | 142 | (void *)arg->cocsepdata, arg->selfsim, arg->crep); |
| 142 | arg->h48data = arg->cocsepdata + (cocsepsize / sizeof(uint32_t)); | 143 | arg->h48data = arg->cocsepdata + (cocsepsize / sizeof(uint32_t)); |
| 143 | arg->info = arg->h48data + | 144 | arg->info = arg->h48data + 1 + |
| 144 | (H48_TABLESIZE(arg->h, arg->k) / sizeof(uint32_t)); | 145 | (H48_TABLESIZE(arg->h, arg->k) / sizeof(uint32_t)); |
| 145 | 146 | ||
| 146 | if (arg->buf != NULL) | 147 | if (arg->buf != NULL) |
| @@ -341,18 +342,27 @@ gendata_h48k2(gendata_h48_arg_t *arg) | |||
| 341 | }; | 342 | }; |
| 342 | 343 | ||
| 343 | i = 0; | 344 | i = 0; |
| 345 | int jj = 0; | ||
| 344 | for (kv = h48map_nextkvpair(&shortcubes, &i); | 346 | for (kv = h48map_nextkvpair(&shortcubes, &i); |
| 345 | i != shortcubes.capacity; | 347 | i != shortcubes.capacity; |
| 346 | kv = h48map_nextkvpair(&shortcubes, &i) | 348 | kv = h48map_nextkvpair(&shortcubes, &i) |
| 347 | ) { | 349 | ) { |
| 348 | /* TODO maybe over all sim? */ | ||
| 349 | dfsarg.cube = invcoord_h48(kv.key, arg->crep, 11); | 350 | dfsarg.cube = invcoord_h48(kv.key, arg->crep, 11); |
| 351 | |||
| 352 | #if 1 | ||
| 350 | gendata_h48k2_dfs(&dfsarg); | 353 | gendata_h48k2_dfs(&dfsarg); |
| 354 | #else | ||
| 355 | /* It looks like this is not necessary, we get the same result */ | ||
| 356 | _foreach_h48sim( | ||
| 357 | dfsarg.cube, arg->cocsepdata, arg->selfsim, arg->h, | ||
| 358 | gendata_h48k2_dfs(&dfsarg); | ||
| 359 | ) | ||
| 360 | #endif | ||
| 361 | if ((++jj) % 1000 == 0) LOG("Done %d distance 8 cubes\n", jj); | ||
| 351 | } | 362 | } |
| 352 | 363 | ||
| 353 | h48map_destroy(&shortcubes); | 364 | h48map_destroy(&shortcubes); |
| 354 | 365 | ||
| 355 | /* TODO: move info update to dfs? */ | ||
| 356 | memset(arg->info, 0, 5 * sizeof(arg->info[0])); | 366 | memset(arg->info, 0, 5 * sizeof(arg->info[0])); |
| 357 | arg->info[0] = base[arg->k]; | 367 | arg->info[0] = base[arg->k]; |
| 358 | for (j = 0; j < H48_COORDMAX(arg->h); j++) { | 368 | for (j = 0; j < H48_COORDMAX(arg->h); j++) { |
| @@ -367,7 +377,7 @@ gendata_h48k2_return_size: | |||
| 367 | _static void | 377 | _static void |
| 368 | gendata_h48k2_dfs(h48k2_dfs_arg_t *arg) | 378 | gendata_h48k2_dfs(h48k2_dfs_arg_t *arg) |
| 369 | { | 379 | { |
| 370 | uint8_t nmoves; | 380 | uint8_t nmoves, oldval, newval; |
| 371 | uint64_t val; | 381 | uint64_t val; |
| 372 | int64_t coord, fullcoord; | 382 | int64_t coord, fullcoord; |
| 373 | h48k2_dfs_arg_t nextarg; | 383 | h48k2_dfs_arg_t nextarg; |
| @@ -378,12 +388,13 @@ gendata_h48k2_dfs(h48k2_dfs_arg_t *arg) | |||
| 378 | 388 | ||
| 379 | val = h48map_value(arg->shortcubes, fullcoord); | 389 | val = h48map_value(arg->shortcubes, fullcoord); |
| 380 | 390 | ||
| 381 | if (arg->depth >= arg->base && arg->depth <= arg->base + 2) | 391 | if (arg->depth >= arg->base && arg->depth <= arg->base + 2) { |
| 382 | set_esep_pval( | 392 | oldval = get_esep_pval(arg->h48data, coord, arg->k); |
| 383 | arg->h48data, coord, arg->k, arg->depth - arg->base); | 393 | newval = _min(oldval, arg->depth - arg->base); |
| 394 | set_esep_pval(arg->h48data, coord, arg->k, newval); | ||
| 395 | } | ||
| 384 | 396 | ||
| 385 | if ((val < arg->shortdepth) || | 397 | if ((arg->depth > arg->shortdepth && val != MAP_UNSET_VAL) || |
| 386 | (arg->depth > arg->shortdepth && val != MAP_UNSET) || | ||
| 387 | (arg->depth >= arg->maxdepth || arg->depth >= arg->base + 2)) | 398 | (arg->depth >= arg->maxdepth || arg->depth >= arg->base + 2)) |
| 388 | return; | 399 | return; |
| 389 | 400 | ||
diff --git a/src/solvers/h48/map.h b/src/solvers/h48/map.h index 82e5a2c..0e9f926 100644 --- a/src/solvers/h48/map.h +++ b/src/solvers/h48/map.h | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | #define MAP_UNSET UINT64_C(0xFFFFFFFFFFFFFFFF) | 1 | #define MAP_UNSET UINT64_C(0xFFFFFFFFFFFFFFFF) |
| 2 | #define MAP_KEYMASK UINT64_C(0xFFFFFFFFFF) | 2 | #define MAP_KEYMASK UINT64_C(0xFFFFFFFFFF) |
| 3 | #define MAP_KEYSHIFT UINT64_C(40) | 3 | #define MAP_KEYSHIFT UINT64_C(40) |
| 4 | #define MAP_UNSET_VAL (MAP_UNSET >> MAP_KEYSHIFT) | ||
| 4 | 5 | ||
| 5 | typedef struct { | 6 | typedef struct { |
| 6 | uint64_t n; | 7 | uint64_t n; |
| @@ -84,8 +85,8 @@ h48map_nextkvpair(h48map_t *map, uint64_t *p) | |||
| 84 | kvpair_t kv; | 85 | kvpair_t kv; |
| 85 | uint64_t pair; | 86 | uint64_t pair; |
| 86 | 87 | ||
| 87 | kv.key = MAP_UNSET; | 88 | kv.key = MAP_KEYMASK; |
| 88 | kv.val = MAP_UNSET; | 89 | kv.val = MAP_UNSET_VAL; |
| 89 | 90 | ||
| 90 | DBG_ASSERT(*p < map->capacity, kv, | 91 | DBG_ASSERT(*p < map->capacity, kv, |
| 91 | "Error looping over map: given index %" PRIu64 " is out of " | 92 | "Error looping over map: given index %" PRIu64 " is out of " |
diff --git a/src/utils/math.h b/src/utils/math.h index 87402e6..a361808 100644 --- a/src/utils/math.h +++ b/src/utils/math.h | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | #define _swap(x, y) do { x ^= y; y ^= x; x ^= y; } while (0) | 1 | #define _swap(x, y) do { x ^= y; y ^= x; x ^= y; } while (0) |
| 2 | #define _min(x, y) ((x) < (y) ? (x) : (y)) | 2 | #define _min(x, y) ((x) < (y) ? (x) : (y)) |
| 3 | #define _max(x, y) ((x) > (y) ? (x) : (y)) | 3 | #define _max(x, y) ((x) > (y) ? (x) : (y)) |
| 4 | #define _div_round_up(n, d) (((n) + (d) - 1) / (d)) | ||
| 4 | 5 | ||
| 5 | _static int64_t factorial(int64_t); | 6 | _static int64_t factorial(int64_t); |
| 6 | _static bool isperm(uint8_t *, int64_t); | 7 | _static bool isperm(uint8_t *, int64_t); |
diff --git a/tools/001_gendata_h48h0k4/gendata_h48h0k4.c b/tools/001_gendata_h48h0k4/gendata_h48h0k4.c index a73f818..a2c57ca 100644 --- a/tools/001_gendata_h48h0k4/gendata_h48h0k4.c +++ b/tools/001_gendata_h48h0k4/gendata_h48h0k4.c | |||
| @@ -1,5 +1,4 @@ | |||
| 1 | #include "../timerun.h" | 1 | #include "../tool.h" |
| 2 | #include "../../src/nissy.h" | ||
| 3 | 2 | ||
| 4 | #define MAXDEPTH 20 | 3 | #define MAXDEPTH 20 |
| 5 | #define HVALUE 0 | 4 | #define HVALUE 0 |
| @@ -38,12 +37,12 @@ void run(void) { | |||
| 38 | printf("Error generating table\n"); | 37 | printf("Error generating table\n"); |
| 39 | } else { | 38 | } else { |
| 40 | printf("Succesfully generated %" PRId64 " bytes. Table:\n", s); | 39 | printf("Succesfully generated %" PRId64 " bytes. Table:\n", s); |
| 41 | h48info = (uint32_t *)buf + (ETABLESIZE(HVALUE) + COCSEPSIZE) / 4; | 40 | h48info = (uint32_t *)buf + 1 + (ETABLESIZE(HVALUE) + COCSEPSIZE) / 4; |
| 42 | for (i = 0; i < MAXDEPTH+1 && h48info[i+1]; i++) { | 41 | for (i = 0; i < MAXDEPTH+1 && h48info[i+1]; i++) { |
| 43 | x = h48info[i+1]; | 42 | x = h48info[i+1]; |
| 44 | printf("%d:\t%" PRIu32, i, x); | 43 | printf("%d:\t%" PRIu32, i, x); |
| 45 | if (x != expected[i]) | 44 | if (x != expected[i]) |
| 46 | printf(" <--- Error! Expected: %" PRIu32 "\n", | 45 | printf(" <--- Error! Expected: %" PRIu32, |
| 47 | expected[i]); | 46 | expected[i]); |
| 48 | printf("\n"); | 47 | printf("\n"); |
| 49 | } | 48 | } |
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 @@ | |||
| 1 | #include "../tool.h" | ||
| 2 | |||
| 3 | #define MAXDEPTH 20 | ||
| 4 | #define HVALUE 0 | ||
| 5 | #define OPTIONS "0;2;20" | ||
| 6 | #define LONGOPTIONS "h = 0, k = 2, max depth = 20" | ||
| 7 | |||
| 8 | #define COCSEPSIZE 1119792 | ||
| 9 | #define ETABLESIZE(h) (((3393 * 495 * 70) >> 2) << (size_t)(h)) | ||
| 10 | |||
| 11 | uint32_t expected[21] = { | ||
| 12 | /* Base value is 8 */ | ||
| 13 | [0] = 5473562, | ||
| 14 | [1] = 34776317, | ||
| 15 | [2] = 68566704, | ||
| 16 | [3] = 8750867, | ||
| 17 | }; | ||
| 18 | |||
| 19 | char *buf; | ||
| 20 | |||
| 21 | void run(void) { | ||
| 22 | uint32_t *h48info, x; | ||
| 23 | int i; | ||
| 24 | int64_t s; | ||
| 25 | |||
| 26 | s = nissy_gendata("h48", OPTIONS, buf); | ||
| 27 | |||
| 28 | if (s == -1) { | ||
| 29 | printf("Error generating table\n"); | ||
| 30 | } else { | ||
| 31 | printf("Succesfully generated %" PRId64 " bytes. Table:\n", s); | ||
| 32 | h48info = (uint32_t *)buf + 1 + (ETABLESIZE(HVALUE) + COCSEPSIZE) / 4; | ||
| 33 | for (i = 0; i < 4; i++) { | ||
| 34 | x = h48info[i+1]; | ||
| 35 | printf("%d:\t%" PRIu32, i, x); | ||
| 36 | if (x != expected[i]) | ||
| 37 | printf(" <--- Error! Expected: %" PRIu32, | ||
| 38 | expected[i]); | ||
| 39 | printf("\n"); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | int main(void) { | ||
| 45 | int64_t size; | ||
| 46 | |||
| 47 | nissy_setlogger(log_stderr); | ||
| 48 | |||
| 49 | size = nissy_datasize("h48", OPTIONS); | ||
| 50 | if (size == -1) { | ||
| 51 | printf("gendata_h48 benchmark: error in datasize\n"); | ||
| 52 | return 1; | ||
| 53 | } | ||
| 54 | |||
| 55 | buf = malloc(size); | ||
| 56 | |||
| 57 | timerun(run, "benchmark gendata_h48 " LONGOPTIONS); | ||
| 58 | |||
| 59 | free(buf); | ||
| 60 | |||
| 61 | return 0; | ||
| 62 | } | ||
diff --git a/tools/003_solve_small/solve_small.c b/tools/003_solve_small/solve_small.c deleted file mode 100644 index 406e255..0000000 --- a/tools/003_solve_small/solve_small.c +++ /dev/null | |||
| @@ -1,94 +0,0 @@ | |||
| 1 | #include <pthread.h> | ||
| 2 | #include <time.h> | ||
| 3 | #include "../timerun.h" | ||
| 4 | #include "../../src/nissy.h" | ||
| 5 | |||
| 6 | #define OPTIONS "0;4;20" | ||
| 7 | |||
| 8 | const char *filename = "tables/h48h0k4"; | ||
| 9 | char *buf; | ||
| 10 | char *scrambles[] = { | ||
| 11 | "R D' R2 D R U2 R' D' R U2 R D R'", /* 12 optimal */ | ||
| 12 | "RLUD RLUD RLUD", /* 12 optimal */ | ||
| 13 | NULL | ||
| 14 | }; | ||
| 15 | |||
| 16 | void run(void) { | ||
| 17 | int i; | ||
| 18 | int64_t n; | ||
| 19 | char sol[100], cube[22]; | ||
| 20 | |||
| 21 | printf("Solved the following scrambles:\n\n"); | ||
| 22 | for (i = 0; scrambles[i] != NULL; i++) { | ||
| 23 | printf("%s\n", scrambles[i]); | ||
| 24 | fprintf(stderr, "Solving scramble %s\n", scrambles[i]); | ||
| 25 | if (nissy_frommoves(scrambles[i], cube) == -1) { | ||
| 26 | fprintf(stderr, "Invalid scramble, " | ||
| 27 | "continuing with next scramble\n"); | ||
| 28 | continue; | ||
| 29 | } | ||
| 30 | n = nissy_solve( | ||
| 31 | cube, "h48", OPTIONS, "", 0, 20, 1, -1, buf, sol); | ||
| 32 | if (n == 0) | ||
| 33 | fprintf(stderr, "No solution found, " | ||
| 34 | "continuing with next scramble\n"); | ||
| 35 | } | ||
| 36 | printf("\n"); | ||
| 37 | } | ||
| 38 | |||
| 39 | int getdata(int64_t size) { | ||
| 40 | int64_t s; | ||
| 41 | FILE *f; | ||
| 42 | |||
| 43 | buf = malloc(size); | ||
| 44 | |||
| 45 | if ((f = fopen(filename, "rb")) == NULL) { | ||
| 46 | fprintf(stderr, "Table file not found, generating them." | ||
| 47 | " This can take a while.\n"); | ||
| 48 | s = nissy_gendata("h48", OPTIONS, buf); | ||
| 49 | if (s != size) { | ||
| 50 | fprintf(stderr, "Error generating table"); | ||
| 51 | if (s != -1) | ||
| 52 | fprintf(stderr, " (got %" PRId64 " bytes)", s); | ||
| 53 | fprintf(stderr, "\n"); | ||
| 54 | return 1; | ||
| 55 | } | ||
| 56 | if ((f = fopen(filename, "wb")) == NULL) { | ||
| 57 | fprintf(stderr, "Could not write tables to file %s" | ||
| 58 | ", will be regenerated next time.\n", filename); | ||
| 59 | } else { | ||
| 60 | fwrite(buf, size, 1, f); | ||
| 61 | fclose(f); | ||
| 62 | } | ||
| 63 | } else { | ||
| 64 | fprintf(stderr, "Reading tables from file %s\n", filename); | ||
| 65 | fread(buf, size, 1, f); | ||
| 66 | fclose(f); | ||
| 67 | } | ||
| 68 | |||
| 69 | return 0; | ||
| 70 | } | ||
| 71 | |||
| 72 | int main(void) { | ||
| 73 | int64_t size; | ||
| 74 | |||
| 75 | srand(time(NULL)); | ||
| 76 | |||
| 77 | nissy_setlogger(log_stderr); | ||
| 78 | size = nissy_datasize("h48", OPTIONS); | ||
| 79 | if (size == -1) { | ||
| 80 | printf("h48 stats: error in datasize\n"); | ||
| 81 | return 1; | ||
| 82 | } | ||
| 83 | |||
| 84 | if (getdata(size) != 0) { | ||
| 85 | printf("Error getting table, stopping\n"); | ||
| 86 | free(buf); | ||
| 87 | return 1; | ||
| 88 | } | ||
| 89 | |||
| 90 | timerun(run, "small solver benchmark"); | ||
| 91 | |||
| 92 | free(buf); | ||
| 93 | return 0; | ||
| 94 | } | ||
diff --git a/tools/002_stats_tables_h48/stats_tables_h48.c b/tools/010_stats_tables_h48/stats_tables_h48.c index 2e48497..8b65862 100644 --- a/tools/002_stats_tables_h48/stats_tables_h48.c +++ b/tools/010_stats_tables_h48/stats_tables_h48.c | |||
| @@ -1,22 +1,23 @@ | |||
| 1 | #include <pthread.h> | 1 | #include <pthread.h> |
| 2 | #include <time.h> | 2 | |
| 3 | #include "../timerun.h" | 3 | #include "../tool.h" |
| 4 | #include "../../src/nissy.h" | ||
| 5 | 4 | ||
| 6 | #define MAXMOVES 20 | 5 | #define MAXMOVES 20 |
| 7 | #define NTHREADS 32 | 6 | #define NTHREADS 32 |
| 8 | #define NCUBES_PER_THREAD 10000 | 7 | #define NCUBES_PER_THREAD 10000 |
| 9 | #define LOG_EVERY (NCUBES_PER_THREAD / 10) | 8 | #define LOG_EVERY (NCUBES_PER_THREAD / 10) |
| 10 | 9 | ||
| 10 | const char *solver = "h48stats"; | ||
| 11 | const char *options = ""; | ||
| 12 | const char *filename = "tables/h48h0k4"; | ||
| 13 | char *buf; | ||
| 14 | |||
| 11 | typedef struct { | 15 | typedef struct { |
| 12 | int n; | 16 | int n; |
| 13 | int thread_id; | 17 | int thread_id; |
| 14 | int64_t v[12][100]; | 18 | int64_t v[12][100]; |
| 15 | } thread_arg_t; | 19 | } thread_arg_t; |
| 16 | 20 | ||
| 17 | const char *filename = "tables/h48h0k4"; | ||
| 18 | char *buf; | ||
| 19 | |||
| 20 | uint64_t rand64(void) { | 21 | uint64_t rand64(void) { |
| 21 | uint64_t i, ret; | 22 | uint64_t i, ret; |
| 22 | 23 | ||
| @@ -85,56 +86,12 @@ void run(void) { | |||
| 85 | } | 86 | } |
| 86 | } | 87 | } |
| 87 | 88 | ||
| 88 | int getdata(int64_t size) { | ||
| 89 | int64_t s; | ||
| 90 | FILE *f; | ||
| 91 | |||
| 92 | buf = malloc(size); | ||
| 93 | |||
| 94 | if ((f = fopen(filename, "rb")) == NULL) { | ||
| 95 | fprintf(stderr, "Table file not found, generating them." | ||
| 96 | " This can take a while.\n"); | ||
| 97 | s = nissy_gendata("h48stats", "", buf); | ||
| 98 | if (s != size) { | ||
| 99 | fprintf(stderr, "Error generating table"); | ||
| 100 | if (s != -1) | ||
| 101 | fprintf(stderr, " (got %" PRId64 " bytes)", s); | ||
| 102 | fprintf(stderr, "\n"); | ||
| 103 | return 1; | ||
| 104 | } | ||
| 105 | if ((f = fopen(filename, "wb")) == NULL) { | ||
| 106 | fprintf(stderr, "Could not write tables to file %s" | ||
| 107 | ", will be regenerated next time.\n", filename); | ||
| 108 | } else { | ||
| 109 | fwrite(buf, size, 1, f); | ||
| 110 | fclose(f); | ||
| 111 | } | ||
| 112 | } else { | ||
| 113 | fprintf(stderr, "Reading tables from file %s\n", filename); | ||
| 114 | fread(buf, size, 1, f); | ||
| 115 | fclose(f); | ||
| 116 | } | ||
| 117 | |||
| 118 | return 0; | ||
| 119 | } | ||
| 120 | |||
| 121 | int main(void) { | 89 | int main(void) { |
| 122 | int64_t size; | ||
| 123 | |||
| 124 | srand(time(NULL)); | 90 | srand(time(NULL)); |
| 125 | |||
| 126 | nissy_setlogger(log_stderr); | 91 | nissy_setlogger(log_stderr); |
| 127 | size = nissy_datasize("h48stats", ""); | ||
| 128 | if (size == -1) { | ||
| 129 | printf("h48 stats: error in datasize\n"); | ||
| 130 | return 1; | ||
| 131 | } | ||
| 132 | 92 | ||
| 133 | if (getdata(size) != 0) { | 93 | if (getdata(solver, options, &buf, filename) != 0) |
| 134 | printf("Error getting table, stopping\n"); | ||
| 135 | free(buf); | ||
| 136 | return 1; | 94 | return 1; |
| 137 | } | ||
| 138 | 95 | ||
| 139 | timerun(run, "h48 table stats"); | 96 | timerun(run, "h48 table stats"); |
| 140 | 97 | ||
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 @@ | |||
| 1 | #include <pthread.h> | ||
| 2 | |||
| 3 | #include "../tool.h" | ||
| 4 | |||
| 5 | const char *solver = "h48"; | ||
| 6 | const char *options = "0;4;20"; | ||
| 7 | const char *filename = "tables/h48h0k4"; | ||
| 8 | char *buf; | ||
| 9 | |||
| 10 | char *scrambles[] = { | ||
| 11 | "R D' R2 D R U2 R' D' R U2 R D R'", /* 12 optimal */ | ||
| 12 | "RLUD RLUD RLUD", /* 12 optimal */ | ||
| 13 | NULL | ||
| 14 | }; | ||
| 15 | |||
| 16 | void run(void) { | ||
| 17 | int i; | ||
| 18 | int64_t n; | ||
| 19 | char sol[100], cube[22]; | ||
| 20 | |||
| 21 | printf("Solved the following scrambles:\n\n"); | ||
| 22 | for (i = 0; scrambles[i] != NULL; i++) { | ||
| 23 | printf("%s\n", scrambles[i]); | ||
| 24 | fprintf(stderr, "Solving scramble %s\n", scrambles[i]); | ||
| 25 | if (nissy_frommoves(scrambles[i], cube) == -1) { | ||
| 26 | fprintf(stderr, "Invalid scramble, " | ||
| 27 | "continuing with next scramble\n"); | ||
| 28 | continue; | ||
| 29 | } | ||
| 30 | n = nissy_solve( | ||
| 31 | cube, "h48", options, "", 0, 20, 1, -1, buf, sol); | ||
| 32 | if (n == 0) | ||
| 33 | fprintf(stderr, "No solution found, " | ||
| 34 | "continuing with next scramble\n"); | ||
| 35 | } | ||
| 36 | printf("\n"); | ||
| 37 | } | ||
| 38 | |||
| 39 | int main(void) { | ||
| 40 | |||
| 41 | srand(time(NULL)); | ||
| 42 | nissy_setlogger(log_stderr); | ||
| 43 | |||
| 44 | if (getdata(solver, options, &buf, filename) != 0) | ||
| 45 | return 1; | ||
| 46 | |||
| 47 | timerun(run, "small solver benchmark"); | ||
| 48 | |||
| 49 | free(buf); | ||
| 50 | return 0; | ||
| 51 | } | ||
diff --git a/tools/timerun.h b/tools/timerun.h deleted file mode 100644 index 58a8e7a..0000000 --- a/tools/timerun.h +++ /dev/null | |||
| @@ -1,51 +0,0 @@ | |||
| 1 | #include <stdarg.h> | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <inttypes.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <stdlib.h> | ||
| 6 | #include <time.h> | ||
| 7 | |||
| 8 | void | ||
| 9 | log_stderr(const char *str, ...) | ||
| 10 | { | ||
| 11 | va_list args; | ||
| 12 | |||
| 13 | va_start(args, str); | ||
| 14 | vfprintf(stderr, str, args); | ||
| 15 | va_end(args); | ||
| 16 | } | ||
| 17 | |||
| 18 | |||
| 19 | double | ||
| 20 | timerun(void (*run)(void), char *name) | ||
| 21 | { | ||
| 22 | struct timespec start, end; | ||
| 23 | double tdiff, tdsec, tdnano; | ||
| 24 | |||
| 25 | printf("\n"); | ||
| 26 | fflush(stdout); | ||
| 27 | |||
| 28 | if (run == NULL) { | ||
| 29 | printf("> %s: nothing to run!\n", name); | ||
| 30 | fflush(stdout); | ||
| 31 | return -1.0; | ||
| 32 | } | ||
| 33 | |||
| 34 | printf("Running tool: %s\n", name); | ||
| 35 | printf("==========\n"); | ||
| 36 | fflush(stdout); | ||
| 37 | |||
| 38 | clock_gettime(CLOCK_MONOTONIC, &start); | ||
| 39 | run(); | ||
| 40 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 41 | |||
| 42 | tdsec = end.tv_sec - start.tv_sec; | ||
| 43 | tdnano = end.tv_nsec - start.tv_nsec; | ||
| 44 | tdiff = tdsec + 1e-9 * tdnano; | ||
| 45 | |||
| 46 | printf("==========\n"); | ||
| 47 | printf("\nTotal time: %.4fs\n", tdiff); | ||
| 48 | fflush(stdout); | ||
| 49 | |||
| 50 | return tdiff; | ||
| 51 | } | ||
diff --git a/tools/tool.h b/tools/tool.h new file mode 100644 index 0000000..d75342d --- /dev/null +++ b/tools/tool.h | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | #include <time.h> | ||
| 2 | #include <stdarg.h> | ||
| 3 | #include <stdbool.h> | ||
| 4 | #include <inttypes.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <stdlib.h> | ||
| 7 | #include <time.h> | ||
| 8 | |||
| 9 | #include "../src/nissy.h" | ||
| 10 | |||
| 11 | static void | ||
| 12 | log_stderr(const char *str, ...) | ||
| 13 | { | ||
| 14 | va_list args; | ||
| 15 | |||
| 16 | va_start(args, str); | ||
| 17 | vfprintf(stderr, str, args); | ||
| 18 | va_end(args); | ||
| 19 | } | ||
| 20 | |||
| 21 | static double | ||
| 22 | timerun(void (*run)(void), char *name) | ||
| 23 | { | ||
| 24 | struct timespec start, end; | ||
| 25 | double tdiff, tdsec, tdnano; | ||
| 26 | |||
| 27 | printf("\n"); | ||
| 28 | fflush(stdout); | ||
| 29 | |||
| 30 | if (run == NULL) { | ||
| 31 | printf("> %s: nothing to run!\n", name); | ||
| 32 | fflush(stdout); | ||
| 33 | return -1.0; | ||
| 34 | } | ||
| 35 | |||
| 36 | printf("Running tool: %s\n", name); | ||
| 37 | printf("==========\n"); | ||
| 38 | fflush(stdout); | ||
| 39 | |||
| 40 | clock_gettime(CLOCK_MONOTONIC, &start); | ||
| 41 | run(); | ||
| 42 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 43 | |||
| 44 | tdsec = end.tv_sec - start.tv_sec; | ||
| 45 | tdnano = end.tv_nsec - start.tv_nsec; | ||
| 46 | tdiff = tdsec + 1e-9 * tdnano; | ||
| 47 | |||
| 48 | printf("==========\n"); | ||
| 49 | printf("\nTotal time: %.4fs\n", tdiff); | ||
| 50 | fflush(stdout); | ||
| 51 | |||
| 52 | return tdiff; | ||
| 53 | } | ||
| 54 | |||
| 55 | static int | ||
| 56 | getdata( | ||
| 57 | const char *solver, | ||
| 58 | const char *options, | ||
| 59 | char **buf, | ||
| 60 | const char *filename | ||
| 61 | ) { | ||
| 62 | int64_t s, size, sizeread; | ||
| 63 | FILE *f; | ||
| 64 | |||
| 65 | if ((size = nissy_datasize(solver, options)) == -1) { | ||
| 66 | printf("Error in datasize\n"); | ||
| 67 | goto getdata_error_nofree; | ||
| 68 | } | ||
| 69 | |||
| 70 | *buf = malloc(size); | ||
| 71 | |||
| 72 | if ((f = fopen(filename, "rb")) == NULL) { | ||
| 73 | fprintf(stderr, "Table file not found, generating them." | ||
| 74 | " This can take a while.\n"); | ||
| 75 | s = nissy_gendata(solver, options, *buf); | ||
| 76 | if (s != size) { | ||
| 77 | fprintf(stderr, "Error generating table"); | ||
| 78 | if (s != -1) | ||
| 79 | fprintf(stderr, " (got %" PRId64 " bytes)", s); | ||
| 80 | fprintf(stderr, "\n"); | ||
| 81 | goto getdata_error; | ||
| 82 | } | ||
| 83 | if ((f = fopen(filename, "wb")) == NULL) { | ||
| 84 | fprintf(stderr, "Could not write tables to file %s" | ||
| 85 | ", will be regenerated next time.\n", filename); | ||
| 86 | } else { | ||
| 87 | fwrite(*buf, size, 1, f); | ||
| 88 | fclose(f); | ||
| 89 | } | ||
| 90 | } else { | ||
| 91 | fprintf(stderr, "Reading tables from file %s\n", filename); | ||
| 92 | sizeread = fread(*buf, size, 1, f); | ||
| 93 | fclose(f); | ||
| 94 | if (sizeread != 1) { | ||
| 95 | fprintf(stderr, "Error reading table, stopping\n"); | ||
| 96 | goto getdata_error; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | return 0; | ||
| 101 | |||
| 102 | getdata_error: | ||
| 103 | free(*buf); | ||
| 104 | getdata_error_nofree: | ||
| 105 | return 1; | ||
| 106 | } | ||
