h48

A prototype for an optimal Rubik's cube solver, work in progress.
git clone https://git.tronto.net/h48
Download | Log | Files | Refs | README | LICENSE

commit 7a52b4cd50a40e4bce919b6bd51203d0e9867141
parent 8fcfb3a33fe053ed2032d58ecc0b5d640c155931
Author: enricotenuti <tenutz_27@outlook.it>
Date:   Sun, 29 Sep 2024 12:42:31 +0200

Merge remote-tracking branch 'upstream/master'

Merge upstream

Diffstat:
M.gitignore | 1+
MREADME.md | 22+++++++++++++++++-----
Mconfigure.sh | 17+++++------------
Msrc/nissy.c | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Msrc/nissy.h | 13+++++++++++++
Msrc/solvers/h48/gendata_cocsep.h | 12++++++++++++
Msrc/solvers/h48/gendata_h48.h | 120+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
Msrc/solvers/tables.h | 50+++++++++++++++++++++++++++-----------------------
Mtest/090_tables_readwrite/tables_readwrite_tests.c | 6+++---
Mtest/100_gendata_cocsep/gendata_cocsep_tests.c | 6+++---
Mtest/120_gendata_h48h0k4/gendata_h48h0k4_tests.c | 7++++---
Dtools/0002_gendata_h48h0k2/gendata_h48h0k2.c | 21---------------------
Dtools/0004_gendata_h48h0k4/gendata_h48h0k4.c | 29-----------------------------
Atools/000_gendata/gendata.c | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dtools/0012_gendata_h48h1k2/gendata_h48h1k2.c | 21---------------------
Atools/001_derive_h48/derive_h48.c | 33+++++++++++++++++++++++++++++++++
Dtools/0022_gendata_h48h2k2/gendata_h48h2k2.c | 21---------------------
Dtools/0032_gendata_h48h3k2/gendata_h48h3k2.c | 21---------------------
Dtools/0042_gendata_h48h4k2/gendata_h48h4k2.c | 21---------------------
Dtools/0052_gendata_h48h5k2/gendata_h48h5k2.c | 21---------------------
Dtools/0062_gendata_h48h6k2/gendata_h48h6k2.c | 21---------------------
Dtools/0072_gendata_h48h7k2/gendata_h48h7k2.c | 21---------------------
Dtools/0082_gendata_h48h8k2/gendata_h48h8k2.c | 21---------------------
Dtools/0092_gendata_h48h9k2/gendata_h48h9k2.c | 21---------------------
Dtools/0102_gendata_h48h10k2/gendata_h48h10k2.c | 21---------------------
Dtools/0112_gendata_h48h11k2/gendata_h48h11k2.c | 21---------------------
Atools/100_checkdata/checkdata.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Rtools/100_stats_tables_h48/stats_tables_h48.c -> tools/200_stats_tables_h48/stats_tables_h48.c | 0
Rtools/200_solve_small/solve_small.c -> tools/300_solve_small/solve_small.c | 0
Atools/expected_distributions.h | 33+++++++++++++++++++++++++++++++++
Atools/nissy_extra.h | 7+++++++
Mtools/run_tool.sh | 5+++--
Mtools/tool.h | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
33 files changed, 538 insertions(+), 347 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -14,6 +14,7 @@ test/run test/run.DSYM tools/.DS_Store run.DSYM +run.core test/last.* tools/results .vscode diff --git a/README.md b/README.md @@ -61,7 +61,7 @@ The results of the last test case run is saved in test/last.out (standard output, the results compared with the .out files) and test/last.err (standard error). -Tests are always run in "debug mode": this means that optimizations are +Tests are always run in debug mode: this means that optimizations are disabled and some extra logging is enabled. See the test folder and test/test.sh for details. @@ -70,7 +70,7 @@ See the test folder and test/test.sh for details. In the tools folder there are some small programs that test various functionality of the H48 library. They work similarly to test, but they -are not run in debug mode. +are not run in debug mode by default. To run a tool you must select it with the environment variable `TOOL`. For example the command: @@ -79,14 +79,26 @@ For example the command: TOOL=stats make tool ``` -Will run the stats_tables_h48 tool. Like for tests, the value of the -`TOOL` variable can be any regular expression matching the name of the -tool. Unlike tests, one and only one tool will be selected for each run. +Will run the stats_tables_h48 tool. + +To pass some arguments to a tool, use the `TOOLARGS` variable: + +``` +TOOL=gendata TOOLARGS="h48 0;2;20" make tool +``` + +Like for tests, the value of the `TOOL` variable can be any regular +expression matching the name of the tool. Unlike tests, one and +only one tool will be selected for each run. The content of the +`TOOLARGS` variable is used directly as command line arguments for +the chosen tool. Each tool run is automatically timed, so these tools can be used as benchmark. The output as well as the time of the run are saved to a file in the tools/results folder. +To build and run a tool in debug mode, use `make debugtool`. + ## Running commands manually This project also includes a rudimentary shell that can be used to run diff --git a/configure.sh b/configure.sh @@ -22,10 +22,10 @@ # SANITIZE="option1,option2,..." # Add the options "-fsanitize=option1", "-fsanitize=option2", ... to the # compilation command when compiling in debug mode. -# By default, "-fsanitize=address" and "-fsanitize=undefined" will be used, -# if available. If this variable is set, the default is overridden. -# No check is performed on the given sanitizers, make sure that the ones you -# choose are available on your system and compatible with each other. +# By default, "-fsanitize=address" and "-fsanitize=undefined" will be used. +# If this variable is set, the default is overridden. No check is performed +# on the availability of any sanitizer used, make sure the ones you use are +# available on your system. # # Examples # @@ -39,10 +39,6 @@ greparch() { $CC -march=native -dM -E - </dev/null 2>/dev/null | grep "$1" } -grepsan() { - $CC -fsanitize="$1" -dM -E -x c - </dev/null 2>/dev/null | grep "SANITIZE" -} - detectthreads() { echo 16 # TODO: choose based on system } @@ -100,10 +96,7 @@ if [ -n "$SANITIZE" ]; then SAN="$SAN -fsanitize=$san" done else - # No sanitizer specified, use "address" and "undefined" if present - [ -n "$(grepsan address)" ] && ADDR="-fsanitize=address" - [ -n "$(grepsan undefined)" ] && UNDEF="-fsanitize=undefined" - SAN="$ADDR $UNDEF" + SAN="-fsanitize=address -fsanitize=undefined" fi LIBS="-lpthread" diff --git a/src/nissy.c b/src/nissy.c @@ -12,8 +12,11 @@ #include "nissy.h" -STATIC int parse_h48_options(const char *, uint8_t *, uint8_t *, uint8_t *); +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 INFO_DISTRIBUTION_LEN], + const uint64_t [static INFO_DISTRIBUTION_LEN], 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 } @@ -25,7 +28,7 @@ struct { GETCUBE_OPTIONS(NULL, NULL) }; -STATIC int +int parse_h48_options(const char *buf, uint8_t *h, uint8_t *k, uint8_t *maxdepth) { bool h_valid, k_valid, maxdepth_valid; @@ -63,6 +66,52 @@ parse_h48_options_error: return -1; } +STATIC bool +checkdata(const void *buf, const tableinfo_t *info) +{ + uint64_t distr[INFO_DISTRIBUTION_LEN]; + + 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 INFO_DISTRIBUTION_LEN], + const uint64_t actual[static INFO_DISTRIBUTION_LEN], + 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("checkdata: %d wrong values\n", wrong); + } else { + LOG("checkdata: table is consistent with info\n"); + } + + return wrong > 0; +} + STATIC int64_t write_result(cube_t cube, char result[static 22]) { @@ -276,6 +325,26 @@ nissy_gendata( } 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; + if (info.next == 0) + break; + } + + return 0; +} + +int64_t nissy_solve( const char cube[static 22], const char *solver, diff --git a/src/nissy.h b/src/nissy.h @@ -86,6 +86,19 @@ int64_t nissy_gendata( void *generated_data ); +/* Temporarily added to test h48 intermediate tables */ +int64_t nissy_derivedata( + const char *options, + const void *fulltable, + 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 @@ -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 @@ -103,14 +103,19 @@ STATIC_INLINE bool gendata_h48k2_dfs_stop(cube_t, int8_t, h48k2_dfs_arg_t *); 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 tableinfo_t makeinfo_h48k2(gendata_h48_arg_t *); +STATIC void getdistribution_h48(const uint8_t *, + uint64_t [static INFO_DISTRIBUTION_LEN], 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 *); + +size_t gendata_h48_derive(uint8_t, const void *, void *); STATIC uint64_t gendata_h48short(gendata_h48short_arg_t *arg) @@ -387,7 +392,7 @@ gendata_h48k2(gendata_h48_arg_t *arg) [11] = 10 }; - uint8_t t, selectedbase, *table; + uint8_t t, *table; int64_t j; uint64_t i, ii, inext, count; h48map_t shortcubes; @@ -414,8 +419,9 @@ gendata_h48k2(gendata_h48_arg_t *arg) }; gendata_h48short(&shortarg); - selectedbase = arg->base < 20 ? arg->base : base[arg->h]; - arg->info = makeinfo_h48k2(arg, selectedbase); + if (arg->base >= 20) + arg->base = base[arg->h]; + arg->info = makeinfo_h48k2(arg); inext = count = 0; pthread_mutex_init(&shortcubes_mutex, NULL); @@ -425,7 +431,7 @@ gendata_h48k2(gendata_h48_arg_t *arg) dfsarg[i] = (h48k2_dfs_arg_t){ .h = arg->h, .k = arg->k, - .base = selectedbase, + .base = arg->base, .shortdepth = shortdepth, .cocsepdata = arg->cocsepdata, .table = table, @@ -603,14 +609,17 @@ STATIC_INLINE bool gendata_h48k2_dfs_stop(cube_t cube, int8_t depth, h48k2_dfs_arg_t *arg) { uint64_t val; - int64_t coord; + int64_t coord, mutex; int8_t oldval; if (arg->h == 0 || arg->h == 11) { /* We are in the "real coordinate" case, we can stop if this coordinate has already been visited */ coord = coord_h48(cube, arg->cocsepdata, arg->h); + mutex = H48_INDEX(coord, arg->k) % CHUNKS; + pthread_mutex_lock(arg->table_mutex[mutex]); oldval = get_h48_pval(arg->table, coord, arg->k); + pthread_mutex_unlock(arg->table_mutex[mutex]); return oldval <= depth; } else { /* With 0 < k < 11 we do not have a "real coordinate". @@ -637,7 +646,7 @@ gendata_h48k2_realcoord_runthread(void *arg) } STATIC tableinfo_t -makeinfo_h48k2(gendata_h48_arg_t *arg, uint8_t base) +makeinfo_h48k2(gendata_h48_arg_t *arg) { tableinfo_t info; @@ -651,7 +660,7 @@ makeinfo_h48k2(gendata_h48_arg_t *arg, uint8_t base) .classes = 0, .h48h = arg->h, .bits = 2, - .base = base, + .base = arg->base, .maxvalue = 3, .next = 0, }; @@ -662,6 +671,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 INFO_DISTRIBUTION_LEN], + uint8_t h, + uint8_t k +) { + uint8_t val; + int64_t i, h48max; + + memset(distr, 0, INFO_DISTRIBUTION_LEN * 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) { @@ -695,3 +723,77 @@ get_h48_bound(cube_t cube, uint32_t cdata, uint8_t h, uint8_t k, uint8_t *table) coord = coord_h48_edges(cube, COCLASS(cdata), TTREP(cdata), h); return get_h48_pval(table, coord, k); } + +size_t +gendata_h48_derive(uint8_t h, const void *fulltable, void *buf) +{ + size_t cocsepsize, h48size; + uint8_t val_full, val_derive, *h48full, *h48derive; + int64_t i, j, h48max; + gendata_h48_arg_t arg; + tableinfo_t cocsepinfo, fulltableinfo; + + /* Initializing values in case of error */ + /* TODO cleanup this */ + fulltableinfo.bits = 2; + fulltableinfo.base = 8; + + readtableinfo_n(fulltable, 2, &fulltableinfo); + arg.h = h; + arg.k = fulltableinfo.bits; + arg.maxdepth = 20; + arg.buf = buf; + arg.cocsepdata = (uint32_t *)((char *)buf + INFOSIZE); + arg.base = fulltableinfo.base; + arg.info = makeinfo_h48k2(&arg); + + /* Technically this step is redundant, except that we + need selfsim and crep */ + cocsepsize = gendata_cocsep(buf, arg.selfsim, arg.crep); + arg.h48buf = (uint8_t *)buf + cocsepsize; + h48size = H48_TABLESIZE(h, arg.k) + INFOSIZE; + + if (buf == NULL) + goto gendata_h48_derive_return_size; + + if (!readtableinfo(buf, &cocsepinfo)) { + LOG("gendata_h48: could not read info for cocsep table\n"); + goto gendata_h48_derive_error; + } + + cocsepinfo.next = cocsepsize; + if (!writetableinfo(&cocsepinfo, buf)) { + LOG("gendata_h48_derive: could not write info for cocsep table" + " with updated 'next' value\n"); + goto gendata_h48_derive_error; + } + + h48full = (uint8_t *)fulltable + cocsepsize + INFOSIZE; + h48derive = (uint8_t *)arg.h48buf + INFOSIZE; + memset(h48derive, 0xFF, H48_TABLESIZE(h, arg.k)); + memset(arg.info.distribution, 0, + INFO_DISTRIBUTION_LEN * sizeof(uint64_t)); + + h48max = H48_COORDMAX(fulltableinfo.h48h); + for (i = 0; i < h48max; i++) { + if (i % INT64_C(1000000000) == 0 && i > 0) + LOG("Processing %" PRId64 "th coordinate\n", i); + j = i >> (int64_t)(fulltableinfo.h48h - h); + val_full = get_h48_pval(h48full, i, arg.k); + val_derive = get_h48_pval(h48derive, j, arg.k); + set_h48_pval(h48derive, j, arg.k, MIN(val_full, val_derive)); + } + + getdistribution_h48(h48derive, arg.info.distribution, h, arg.k); + + if (!writetableinfo(&arg.info, arg.h48buf)) { + LOG("gendata_h48_derive: could not write info for table\n"); + goto gendata_h48_derive_error; + } + +gendata_h48_derive_return_size: + return cocsepsize + h48size; + +gendata_h48_derive_error: + return 0; +} diff --git a/src/solvers/tables.h b/src/solvers/tables.h @@ -7,34 +7,34 @@ #define TABLETYPE_PRUNING 0 #define TABLETYPE_SPECIAL 1 -#define INFO_OFFSET_SOLVER 0 -#define INFO_OFFSET_TYPE INFO_SOLVER_STRLEN +#define INFO_OFFSET_DISTRIBUTION 0 +#define INFO_OFFSET_TYPE (INFO_DISTRIBUTION_LEN * sizeof(uint64_t)) #define INFO_OFFSET_INFOSIZE (INFO_OFFSET_TYPE + sizeof(uint64_t)) #define INFO_OFFSET_FULLSIZE (INFO_OFFSET_INFOSIZE + sizeof(uint64_t)) #define INFO_OFFSET_HASH (INFO_OFFSET_FULLSIZE + sizeof(uint64_t)) #define INFO_OFFSET_ENTRIES (INFO_OFFSET_HASH + sizeof(uint64_t)) #define INFO_OFFSET_CLASSES (INFO_OFFSET_ENTRIES + sizeof(uint64_t)) -#define INFO_OFFSET_H48H (INFO_OFFSET_CLASSES + sizeof(uint64_t)) +#define INFO_OFFSET_NEXT (INFO_OFFSET_CLASSES + sizeof(uint64_t)) +#define INFO_OFFSET_SOLVER (INFO_OFFSET_NEXT + sizeof(uint64_t)) +#define INFO_OFFSET_H48H (INFO_OFFSET_SOLVER + INFO_SOLVER_STRLEN) #define INFO_OFFSET_BITS (INFO_OFFSET_H48H + sizeof(uint8_t)) #define INFO_OFFSET_BASE (INFO_OFFSET_BITS + sizeof(uint8_t)) #define INFO_OFFSET_MAXVALUE (INFO_OFFSET_BASE + sizeof(uint8_t)) -#define INFO_OFFSET_NEXT (INFO_OFFSET_MAXVALUE + sizeof(uint8_t)) -#define INFO_OFFSET_DISTRIBUTION (INFO_OFFSET_NEXT + sizeof(uint64_t)) typedef struct { - char solver[INFO_SOLVER_STRLEN]; + uint64_t distribution[INFO_DISTRIBUTION_LEN]; uint64_t type; uint64_t infosize; uint64_t fullsize; uint64_t hash; uint64_t entries; uint64_t classes; /* Used only by cocsepdata, for now */ + uint64_t next; + char solver[INFO_SOLVER_STRLEN]; uint8_t h48h; /* Specific to H48 tables */ uint8_t bits; uint8_t base; uint8_t maxvalue; - uint64_t next; - uint64_t distribution[INFO_DISTRIBUTION_LEN]; } tableinfo_t; STATIC bool readtableinfo(const void *, tableinfo_t *); @@ -54,21 +54,24 @@ readtableinfo(const void *buf, tableinfo_t *info) return false; } - memcpy(info->solver, OFFSET(buf, INFO_OFFSET_SOLVER), - INFO_SOLVER_STRLEN); + memcpy(info->distribution, OFFSET(buf, INFO_OFFSET_DISTRIBUTION), + INFO_DISTRIBUTION_LEN * sizeof(uint64_t)); + info->type = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_TYPE); info->infosize = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_INFOSIZE); info->fullsize = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_FULLSIZE); info->hash = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_HASH); info->entries = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_ENTRIES); info->classes = *(const uint64_t *)OFFSET(buf, INFO_OFFSET_CLASSES); + info->next = *(const uint64_t* )OFFSET(buf, INFO_OFFSET_NEXT); + + memcpy(info->solver, OFFSET(buf, INFO_OFFSET_SOLVER), + INFO_SOLVER_STRLEN); + info->h48h = *OFFSET(buf, INFO_OFFSET_H48H); info->bits = *OFFSET(buf, INFO_OFFSET_BITS); info->base = *OFFSET(buf, INFO_OFFSET_BASE); info->maxvalue = *OFFSET(buf, INFO_OFFSET_MAXVALUE); - info->next = *(const uint64_t* )OFFSET(buf, INFO_OFFSET_NEXT); - memcpy(info->distribution, OFFSET(buf, INFO_OFFSET_DISTRIBUTION), - INFO_DISTRIBUTION_LEN * sizeof(uint64_t)); return true; } @@ -98,6 +101,17 @@ writetableinfo(const tableinfo_t *info, void *buf) return false; } + memcpy(OFFSET(buf, INFO_OFFSET_DISTRIBUTION), info->distribution, + INFO_DISTRIBUTION_LEN * sizeof(uint64_t)); + + *(uint64_t *)OFFSET(buf, INFO_OFFSET_TYPE) = info->type; + *(uint64_t *)OFFSET(buf, INFO_OFFSET_INFOSIZE) = info->infosize; + *(uint64_t *)OFFSET(buf, INFO_OFFSET_FULLSIZE) = info->fullsize; + *(uint64_t *)OFFSET(buf, INFO_OFFSET_HASH) = info->hash; + *(uint64_t *)OFFSET(buf, INFO_OFFSET_ENTRIES) = info->entries; + *(uint64_t *)OFFSET(buf, INFO_OFFSET_CLASSES) = info->classes; + *(uint64_t *)OFFSET(buf, INFO_OFFSET_NEXT) = info->next; + memcpy(OFFSET(buf, INFO_OFFSET_SOLVER), info->solver, INFO_SOLVER_STRLEN); @@ -106,20 +120,10 @@ writetableinfo(const tableinfo_t *info, void *buf) if (*OFFSET(buf, i) == 0) *OFFSET(buf, i) = 0; - *(uint64_t *)OFFSET(buf, INFO_OFFSET_TYPE) = info->type; - *(uint64_t *)OFFSET(buf, INFO_OFFSET_INFOSIZE) = info->infosize; - *(uint64_t *)OFFSET(buf, INFO_OFFSET_FULLSIZE) = info->fullsize; - *(uint64_t *)OFFSET(buf, INFO_OFFSET_HASH) = info->hash; - *(uint64_t *)OFFSET(buf, INFO_OFFSET_ENTRIES) = info->entries; - *(uint64_t *)OFFSET(buf, INFO_OFFSET_CLASSES) = info->classes; *OFFSET(buf, INFO_OFFSET_H48H) = info->h48h; *OFFSET(buf, INFO_OFFSET_BITS) = info->bits; *OFFSET(buf, INFO_OFFSET_BASE) = info->base; *OFFSET(buf, INFO_OFFSET_MAXVALUE) = info->maxvalue; - *(uint64_t *)OFFSET(buf, INFO_OFFSET_NEXT) = info->next; - - memcpy(OFFSET(buf, INFO_OFFSET_DISTRIBUTION), info->distribution, - INFO_DISTRIBUTION_LEN * sizeof(uint64_t)); return true; } diff --git a/test/090_tables_readwrite/tables_readwrite_tests.c b/test/090_tables_readwrite/tables_readwrite_tests.c @@ -5,19 +5,19 @@ #define INFO_DISTRIBUTION_LEN 21 typedef struct { - char solver[INFO_SOLVER_STRLEN]; + uint64_t distribution[INFO_DISTRIBUTION_LEN]; uint64_t type; uint64_t infosize; uint64_t fullsize; uint64_t hash; uint64_t entries; uint64_t classes; + uint64_t next; + char solver[INFO_SOLVER_STRLEN]; uint8_t h48h; uint8_t bits; uint8_t base; uint8_t maxvalue; - uint64_t next; - uint64_t distribution[INFO_DISTRIBUTION_LEN]; } tableinfo_t; bool readtableinfo(const void *, tableinfo_t *); diff --git a/test/100_gendata_cocsep/gendata_cocsep_tests.c b/test/100_gendata_cocsep/gendata_cocsep_tests.c @@ -6,19 +6,19 @@ #define COCSEP_CLASSES 3393 typedef struct { - char solver[INFO_SOLVER_STRLEN]; + uint64_t distribution[INFO_DISTRIBUTION_LEN]; uint64_t type; uint64_t infosize; uint64_t fullsize; uint64_t hash; uint64_t entries; uint64_t classes; /* Used only by cocsepdata, for now */ + uint64_t next; + char solver[INFO_SOLVER_STRLEN]; uint8_t h48h; uint8_t bits; uint8_t base; uint8_t maxvalue; - uint64_t next; - uint64_t distribution[INFO_DISTRIBUTION_LEN]; } tableinfo_t; size_t gendata_cocsep(void *, uint64_t *, cube_t *); diff --git a/test/120_gendata_h48h0k4/gendata_h48h0k4_tests.c b/test/120_gendata_h48h0k4/gendata_h48h0k4_tests.c @@ -6,24 +6,25 @@ #define INFO_DISTRIBUTION_LEN 21 typedef struct { - char solver[INFO_SOLVER_STRLEN]; + uint64_t distribution[INFO_DISTRIBUTION_LEN]; uint64_t type; uint64_t infosize; uint64_t fullsize; uint64_t hash; uint64_t entries; uint64_t classes; /* Used only by cocsepdata, for now */ + uint64_t next; + char solver[INFO_SOLVER_STRLEN]; uint8_t h48h; uint8_t bits; uint8_t base; uint8_t maxvalue; - uint64_t next; - uint64_t distribution[INFO_DISTRIBUTION_LEN]; } tableinfo_t; typedef struct { uint8_t h; uint8_t k; + uint8_t base; uint8_t maxdepth; tableinfo_t info; void *buf; diff --git a/tools/0002_gendata_h48h0k2/gendata_h48h0k2.c b/tools/0002_gendata_h48h0k2/gendata_h48h0k2.c @@ -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 @@ -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/000_gendata/gendata.c b/tools/000_gendata/gendata.c @@ -0,0 +1,55 @@ +#include "../tool.h" +#include "../expected_distributions.h" + +char *solver, *options; +uint64_t *expected; + +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/0012_gendata_h48h1k2/gendata_h48h1k2.c b/tools/0012_gendata_h48h1k2/gendata_h48h1k2.c @@ -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/001_derive_h48/derive_h48.c b/tools/001_derive_h48/derive_h48.c @@ -0,0 +1,33 @@ +#include "../tool.h" + +char *opts_large, *opts_small, *filename_large, *filename_small; + +void run(void) { + derivedata_run(opts_large, opts_small, filename_large, filename_small); +} + +int main(int argc, char **argv) { + char description[256]; + + if (argc < 5) { + fprintf(stderr, + "Error: not enough arguments. Required:\n" + "1. Options for large table\n" + "2. Options for derived table\n" + "3. Filename containing large table\n" + "4. Filename for saving derived table\n"); + return 1; + } + + opts_large = argv[1]; + opts_small = argv[2]; + filename_large = argv[3]; + filename_small = argv[4]; + sprintf(description, "deriving %s from %s\n", opts_small, opts_large); + + nissy_setlogger(log_stderr); + + timerun(run, description); + + return 0; +} diff --git a/tools/0022_gendata_h48h2k2/gendata_h48h2k2.c b/tools/0022_gendata_h48h2k2/gendata_h48h2k2.c @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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_checkdata/checkdata.c 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/100_stats_tables_h48/stats_tables_h48.c b/tools/200_stats_tables_h48/stats_tables_h48.c diff --git a/tools/200_solve_small/solve_small.c b/tools/300_solve_small/solve_small.c diff --git a/tools/expected_distributions.h b/tools/expected_distributions.h @@ -0,0 +1,33 @@ +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, + }, + }, + [1] = { + [2] = { + [0] = 6012079, + [1] = 45822302, + [2] = 142018732, + [3] = 41281787, + }, + }, +}; diff --git a/tools/nissy_extra.h b/tools/nissy_extra.h @@ -0,0 +1,7 @@ +/* +This header file exposes certain functions that are meant to be used +for testing purposes only. +*/ + +size_t gendata_h48_derive(uint8_t, const void *, void *); +int parse_h48_options(const char *, uint8_t *, uint8_t *, uint8_t *); diff --git 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 @@ -6,14 +6,19 @@ #include <stdlib.h> #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 int64_t derivetable(const char *, 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 21]); +static void derivedata_run( + const char *, const char *, const char *, const char *); static void log_stderr(const char *str, ...) @@ -70,6 +75,17 @@ timerun(void (*run)(void), const char *name) } 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) { FILE *f; @@ -109,6 +125,44 @@ generatetable(const char *solver, const char *options, char **buf) return gensize; } +static int64_t +derivetable( + const char *opts_large, + const char *opts_small, + const char *filename_large, + char **buf +) +{ + uint8_t h; + int64_t size, gensize; + char *fulltable; + + if (getdata("h48", opts_large, &fulltable, filename_large) != 0) { + printf("Error reading full table.\n"); + return -1; + } + + size = nissy_datasize("h48", opts_small); + if (size == -1) { + printf("Error getting table size.\n"); + free(fulltable); + return -1; + } + + h = atoi(opts_small); /* TODO: use option parser */ + *buf = malloc(size); + gensize = gendata_h48_derive(h, fulltable, *buf); + + if (gensize != size) { + fprintf(stderr, "Error deriving table\n"); + free(fulltable); + return -2; + } + + free(fulltable); + return gensize; +} + static int getdata( const char *solver, @@ -155,13 +209,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: @@ -182,3 +235,34 @@ gendata_run( gendata_run_finish: free(buf); } + +static void +derivedata_run( + const char *opts_large, + const char *opts_small, + const char *filename_large, + const char *filename_small +) +{ + int64_t size; + char *buf; + + size = derivetable(opts_large, opts_small, filename_large, &buf); + switch (size) { + case -1: + return; + case -2: + goto derivedata_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_small); + break; + } + +derivedata_run_finish: + free(buf); +}