From 818186b480d7c05e0c58ff89da2180f5b2476434 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 26 Sep 2024 14:59:11 +0200 Subject: Added TOOLARGS and simplified gendata tool(s) --- src/nissy.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nissy.c') diff --git a/src/nissy.c b/src/nissy.c index 6bc4aea..318cff1 100644 --- a/src/nissy.c +++ b/src/nissy.c @@ -11,7 +11,7 @@ #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]); /* TODO: add option to get DR, maybe C-only, E-only, eo... */ @@ -24,7 +24,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; -- cgit v1.3 From bc2cba8529a163d129dec91b5ec448a29a14591a Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 26 Sep 2024 15:57:25 +0200 Subject: Added checkdata function --- src/nissy.c | 63 +++++++++++++++++++++++++++++++ src/nissy.h | 6 +++ src/solvers/h48/gendata_cocsep.h | 12 ++++++ src/solvers/h48/gendata_h48.h | 24 +++++++++++- tools/000_gendata/gendata.c | 80 ++++++++++++++++++++++++++++++++++++++++ tools/100_gendata/gendata.c | 80 ---------------------------------------- 6 files changed, 184 insertions(+), 81 deletions(-) create mode 100644 tools/000_gendata/gendata.c delete mode 100644 tools/100_gendata/gendata.c (limited to 'src/nissy.c') diff --git a/src/nissy.c b/src/nissy.c index 318cff1..27e2835 100644 --- a/src/nissy.c +++ b/src/nissy.c @@ -13,6 +13,9 @@ 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 21], const uint64_t [static 21], 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 } @@ -62,6 +65,49 @@ parse_h48_options_error: return -1; } +STATIC bool +checkdata(const void *buf, const tableinfo_t *info) +{ + uint64_t distr[21]; + + 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 21], + const uint64_t actual[static 21], + 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("chekdata: %d wrong values\n", wrong); + + return wrong > 0; +} + STATIC int64_t write_result(cube_t cube, char result[static 22]) { @@ -274,6 +320,23 @@ nissy_gendata( return ret; } +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; + + return 0; +} + int64_t nissy_solve( const char cube[static 22], diff --git a/src/nissy.h b/src/nissy.h index 336130c..e8c35c3 100644 --- a/src/nissy.h +++ b/src/nissy.h @@ -86,6 +86,12 @@ int64_t nissy_gendata( 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 index 2189145..262793b 100644 --- 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 index 3445570..6e7b0ca 100644 --- a/src/solvers/h48/gendata_h48.h +++ b/src/solvers/h48/gendata_h48.h @@ -104,13 +104,16 @@ 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 void getdistribution_h48( + const uint8_t *, uint64_t [static 21], 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 *); STATIC uint64_t gendata_h48short(gendata_h48short_arg_t *arg) @@ -662,6 +665,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 21], + uint8_t h, + uint8_t k +) { + uint8_t val; + int64_t i, h48max; + + memset(distr, 0, 21 * 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) { diff --git a/tools/000_gendata/gendata.c b/tools/000_gendata/gendata.c new file mode 100644 index 0000000..78f81ab --- /dev/null +++ b/tools/000_gendata/gendata.c @@ -0,0 +1,80 @@ +#include "../tool.h" + +char *solver, *options; +uint64_t *expected; + +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, + }, + }, +}; + +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/100_gendata/gendata.c b/tools/100_gendata/gendata.c deleted file mode 100644 index 78f81ab..0000000 --- a/tools/100_gendata/gendata.c +++ /dev/null @@ -1,80 +0,0 @@ -#include "../tool.h" - -char *solver, *options; -uint64_t *expected; - -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, - }, - }, -}; - -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; -} -- cgit v1.3 From 91f912176045fd2f95fd45a0e12354e390dff219 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 26 Sep 2024 17:07:02 +0200 Subject: Added checkdata tool --- src/nissy.c | 7 ++++-- tools/000_gendata/gendata.c | 29 ++--------------------- tools/100_checkdata/checkdata.c | 52 +++++++++++++++++++++++++++++++++++++++++ tools/expected_distributions.h | 25 ++++++++++++++++++++ 4 files changed, 84 insertions(+), 29 deletions(-) create mode 100644 tools/100_checkdata/checkdata.c create mode 100644 tools/expected_distributions.h (limited to 'src/nissy.c') diff --git a/src/nissy.c b/src/nissy.c index 27e2835..953471f 100644 --- a/src/nissy.c +++ b/src/nissy.c @@ -102,8 +102,11 @@ distribution_equal( } } - if (wrong > 0) - LOG("chekdata: %d wrong values\n", wrong); + if (wrong > 0) { + LOG("checkdata: %d wrong values\n", wrong); + } else { + LOG("checkdata: table is consistent with info\n"); + } return wrong > 0; } diff --git a/tools/000_gendata/gendata.c b/tools/000_gendata/gendata.c index 78f81ab..90166dd 100644 --- a/tools/000_gendata/gendata.c +++ b/tools/000_gendata/gendata.c @@ -1,34 +1,9 @@ #include "../tool.h" +#include "../expected_distributions.h" char *solver, *options; uint64_t *expected; -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, - }, - }, -}; - static void run(void) { int64_t size; @@ -60,7 +35,7 @@ int main(int argc, char **argv) { char description[256]; if (argc < 3) { - fprintf(stderr, "Error: not enough arguments." + fprintf(stderr, "Error: not enough arguments. " "A solver and its options must be given.\n"); return 1; } diff --git a/tools/100_checkdata/checkdata.c b/tools/100_checkdata/checkdata.c new file mode 100644 index 0000000..05961c1 --- /dev/null +++ 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/expected_distributions.h b/tools/expected_distributions.h new file mode 100644 index 0000000..27bd64a --- /dev/null +++ b/tools/expected_distributions.h @@ -0,0 +1,25 @@ +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, + }, + }, +}; -- cgit v1.3 From 388b55b4ea644aa1b6074a70652b8f62e7755280 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Fri, 27 Sep 2024 09:11:00 +0200 Subject: Use constant instead of magic number --- src/nissy.c | 10 +++++----- src/solvers/h48/gendata_h48.h | 20 ++++++++------------ tools/001_derive_h48h0k2/derive_h48h0k2.c | 21 +++++++++++++++++++++ tools/1002_derive_h48h0k2/derive_h48h0k2.c | 21 --------------------- tools/tool.h | 2 +- 5 files changed, 35 insertions(+), 39 deletions(-) create mode 100644 tools/001_derive_h48h0k2/derive_h48h0k2.c delete mode 100644 tools/1002_derive_h48h0k2/derive_h48h0k2.c (limited to 'src/nissy.c') diff --git a/src/nissy.c b/src/nissy.c index 953471f..2eed1b5 100644 --- a/src/nissy.c +++ b/src/nissy.c @@ -13,8 +13,8 @@ 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 21], const uint64_t [static 21], uint8_t); +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... */ @@ -68,7 +68,7 @@ parse_h48_options_error: STATIC bool checkdata(const void *buf, const tableinfo_t *info) { - uint64_t distr[21]; + uint64_t distr[INFO_DISTRIBUTION_LEN]; if (!strncmp(info->solver, "cocsep", 6)) { getdistribution_cocsep( @@ -86,8 +86,8 @@ checkdata(const void *buf, const tableinfo_t *info) STATIC bool distribution_equal( - const uint64_t expected[static 21], - const uint64_t actual[static 21], + const uint64_t expected[static INFO_DISTRIBUTION_LEN], + const uint64_t actual[static INFO_DISTRIBUTION_LEN], uint8_t maxvalue ) { diff --git a/src/solvers/h48/gendata_h48.h b/src/solvers/h48/gendata_h48.h index 48d5582..b1ae38c 100644 --- a/src/solvers/h48/gendata_h48.h +++ b/src/solvers/h48/gendata_h48.h @@ -104,8 +104,8 @@ 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 *); -STATIC void getdistribution_h48( - const uint8_t *, uint64_t [static 21], uint8_t, uint8_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 *); @@ -671,14 +671,14 @@ makeinfo_h48k2(gendata_h48_arg_t *arg) STATIC void getdistribution_h48( const uint8_t *table, - uint64_t distr[static 21], + uint64_t distr[static INFO_DISTRIBUTION_LEN], uint8_t h, uint8_t k ) { uint8_t val; int64_t i, h48max; - memset(distr, 0, 21 * sizeof(uint64_t)); + memset(distr, 0, INFO_DISTRIBUTION_LEN * sizeof(uint64_t)); h48max = H48_COORDMAX(h); for (i = 0; i < h48max; i++) { @@ -725,12 +725,13 @@ size_t gendata_h48_derive(uint8_t h, const void *fulltable, void *buf) { size_t cocsepsize, h48size; - uint8_t val_full, val_derive, val_new, *h48full, *h48derive; + 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; @@ -777,15 +778,10 @@ gendata_h48_derive(uint8_t h, const void *fulltable, void *buf) j = i >> (int64_t)(11-h); val_full = get_h48_pval(h48full, i, arg.k); val_derive = get_h48_pval(h48derive, j, arg.k); - val_new = MIN(val_full, val_derive); - set_h48_pval(h48derive, j, arg.k, val_new); + set_h48_pval(h48derive, j, arg.k, MIN(val_full, val_derive)); } - h48max = H48_COORDMAX(h); - for (i = 0; i < h48max; i++) { - val_derive = get_h48_pval(h48derive, i, arg.k); - arg.info.distribution[val_derive]++; - } + getdistribution_h48(h48derive, arg.info.distribution, h, arg.k); if (!writetableinfo(&arg.info, buf)) { LOG("gendata_h48_derive: could not write info for table\n"); diff --git a/tools/001_derive_h48h0k2/derive_h48h0k2.c b/tools/001_derive_h48h0k2/derive_h48h0k2.c new file mode 100644 index 0000000..73fab2e --- /dev/null +++ b/tools/001_derive_h48h0k2/derive_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) { + derivedata_run(0, "tables/h48h0k2_derived", expected); +} + +int main(void) { + nissy_setlogger(log_stderr); + + timerun(run, "benchmark derivedata_h48 h = 0, k = 2"); + + return 0; +} diff --git a/tools/1002_derive_h48h0k2/derive_h48h0k2.c b/tools/1002_derive_h48h0k2/derive_h48h0k2.c deleted file mode 100644 index 73fab2e..0000000 --- a/tools/1002_derive_h48h0k2/derive_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) { - derivedata_run(0, "tables/h48h0k2_derived", expected); -} - -int main(void) { - nissy_setlogger(log_stderr); - - timerun(run, "benchmark derivedata_h48 h = 0, k = 2"); - - return 0; -} diff --git a/tools/tool.h b/tools/tool.h index 5eec318..d9071fb 100644 --- a/tools/tool.h +++ b/tools/tool.h @@ -130,7 +130,7 @@ derivetable(uint8_t h, char **buf) int64_t size, gensize; char *fulltable; - char options[20] = " ;2;20"; /* Fixed for k = 2 for now */ + char options[20] = " ;2;20"; /* Only for k = 2 for now */ options[0] = (char)(h + '0'); /* h = 10 not supported for now */ /* Support only b8 for now */ -- cgit v1.3 From 5c22df4e8600d49983f008f22e942a2dd120ad9a Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Fri, 27 Sep 2024 09:11:55 +0200 Subject: Fixed checkdata --- src/nissy.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/nissy.c') diff --git a/src/nissy.c b/src/nissy.c index 953471f..08cacf0 100644 --- a/src/nissy.c +++ b/src/nissy.c @@ -333,9 +333,12 @@ nissy_checkdata( char *buf; tableinfo_t info; - for (buf = (char *)data; readtableinfo(buf, &info); buf += info.next) + for (buf = (char *)data; readtableinfo(buf, &info); buf += info.next) { if (!checkdata(buf, &info)) return 1; + if (info.next == 0) + break; + } return 0; } -- cgit v1.3