diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-09-12 09:58:04 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-09-12 09:58:04 +0200 |
| commit | 76cc82994c1fbb969ec61d3aede6bd42fda09005 (patch) | |
| tree | 653723d48b48d5fc9be20181b1f6720f624176d5 /tools | |
| parent | 10b65003102675bd19c0e5174ff249be2a163bc7 (diff) | |
| download | nissy-core-76cc82994c1fbb969ec61d3aede6bd42fda09005.tar.gz nissy-core-76cc82994c1fbb969ec61d3aede6bd42fda09005.zip | |
Cleanup gendata tools
Diffstat (limited to '')
| -rw-r--r-- | tools/001_gendata_h48h0k4/gendata_h48h0k4.c | 41 | ||||
| -rw-r--r-- | tools/002_gendata_h48h0k2/gendata_h48h0k2.c | 38 | ||||
| -rw-r--r-- | tools/tool.h | 117 |
3 files changed, 99 insertions, 97 deletions
diff --git a/tools/001_gendata_h48h0k4/gendata_h48h0k4.c b/tools/001_gendata_h48h0k4/gendata_h48h0k4.c index a9993b8..ff34bb9 100644 --- a/tools/001_gendata_h48h0k4/gendata_h48h0k4.c +++ b/tools/001_gendata_h48h0k4/gendata_h48h0k4.c | |||
| @@ -1,14 +1,6 @@ | |||
| 1 | #include "../tool.h" | 1 | #include "../tool.h" |
| 2 | 2 | ||
| 3 | #define MAXDEPTH 20 | 3 | uint64_t expected[21] = { |
| 4 | #define HVALUE 0 | ||
| 5 | #define OPTIONS "0;4;20" | ||
| 6 | #define LONGOPTIONS "h = 0, k = 4, max depth = 20" | ||
| 7 | |||
| 8 | #define COCSEPSIZE 1119792 | ||
| 9 | #define ETABLESIZE(h) (((3393 * 495 * 70) >> 1) << (size_t)(h)) | ||
| 10 | |||
| 11 | uint32_t expected[21] = { | ||
| 12 | [0] = 1, | 4 | [0] = 1, |
| 13 | [1] = 1, | 5 | [1] = 1, |
| 14 | [2] = 4, | 6 | [2] = 4, |
| @@ -24,41 +16,14 @@ uint32_t expected[21] = { | |||
| 24 | [12] = 1673, | 16 | [12] = 1673, |
| 25 | }; | 17 | }; |
| 26 | 18 | ||
| 27 | char *buf; | ||
| 28 | |||
| 29 | void run(void) { | 19 | void run(void) { |
| 30 | int64_t s; | 20 | gendata_run("h48", "0;4;20", "tables/h48h0k4", expected); |
| 31 | |||
| 32 | s = nissy_gendata("h48", OPTIONS, buf); | ||
| 33 | |||
| 34 | if (s == -1) { | ||
| 35 | printf("Error generating table\n"); | ||
| 36 | } else { | ||
| 37 | nissy_datainfo(buf, write_stdout); | ||
| 38 | printf("\n"); | ||
| 39 | printf("Succesfully generated %" PRId64 " bytes. " | ||
| 40 | "See above for details on the tables.\n", s); | ||
| 41 | |||
| 42 | /* TODO: check that the table is correct */ | ||
| 43 | } | ||
| 44 | } | 21 | } |
| 45 | 22 | ||
| 46 | int main(void) { | 23 | int main(void) { |
| 47 | int64_t size; | ||
| 48 | |||
| 49 | nissy_setlogger(log_stderr); | 24 | nissy_setlogger(log_stderr); |
| 50 | 25 | ||
| 51 | size = nissy_datasize("h48", OPTIONS); | 26 | timerun(run, "benchmark gendata_h48 h = 0, k = 4"); |
| 52 | if (size == -1) { | ||
| 53 | printf("gendata_h48 benchmark: error in datasize\n"); | ||
| 54 | return 1; | ||
| 55 | } | ||
| 56 | |||
| 57 | buf = malloc(size); | ||
| 58 | |||
| 59 | timerun(run, "benchmark gendata_h48 " LONGOPTIONS); | ||
| 60 | |||
| 61 | free(buf); | ||
| 62 | 27 | ||
| 63 | return 0; | 28 | return 0; |
| 64 | } | 29 | } |
diff --git a/tools/002_gendata_h48h0k2/gendata_h48h0k2.c b/tools/002_gendata_h48h0k2/gendata_h48h0k2.c index 271d339..14f99ee 100644 --- a/tools/002_gendata_h48h0k2/gendata_h48h0k2.c +++ b/tools/002_gendata_h48h0k2/gendata_h48h0k2.c | |||
| @@ -1,11 +1,6 @@ | |||
| 1 | #include "../tool.h" | 1 | #include "../tool.h" |
| 2 | 2 | ||
| 3 | #define MAXDEPTH 20 | 3 | uint64_t expected[21] = { |
| 4 | #define HVALUE 0 | ||
| 5 | #define OPTIONS "0;2;20" | ||
| 6 | #define LONGOPTIONS "h = 0, k = 2, max depth = 20" | ||
| 7 | |||
| 8 | uint32_t expected[21] = { | ||
| 9 | /* Base value is 8 */ | 4 | /* Base value is 8 */ |
| 10 | [0] = 5473562, | 5 | [0] = 5473562, |
| 11 | [1] = 34776317, | 6 | [1] = 34776317, |
| @@ -13,41 +8,14 @@ uint32_t expected[21] = { | |||
| 13 | [3] = 8750867, | 8 | [3] = 8750867, |
| 14 | }; | 9 | }; |
| 15 | 10 | ||
| 16 | char *buf; | ||
| 17 | |||
| 18 | void run(void) { | 11 | void run(void) { |
| 19 | int64_t s; | 12 | gendata_run("h48", "0;2;20", "tables/h48h0k2", expected); |
| 20 | |||
| 21 | s = nissy_gendata("h48", OPTIONS, buf); | ||
| 22 | |||
| 23 | if (s == -1) { | ||
| 24 | printf("Error generating table\n"); | ||
| 25 | } else { | ||
| 26 | nissy_datainfo(buf, write_stdout); | ||
| 27 | printf("\n"); | ||
| 28 | printf("Succesfully generated %" PRId64 " bytes. " | ||
| 29 | "See above for details on the tables.\n", s); | ||
| 30 | |||
| 31 | /* TODO: check that the table is correct */ | ||
| 32 | } | ||
| 33 | } | 13 | } |
| 34 | 14 | ||
| 35 | int main(void) { | 15 | int main(void) { |
| 36 | int64_t size; | ||
| 37 | |||
| 38 | nissy_setlogger(log_stderr); | 16 | nissy_setlogger(log_stderr); |
| 39 | 17 | ||
| 40 | size = nissy_datasize("h48", OPTIONS); | 18 | timerun(run, "benchmark gendata_h48 h = 0, k = 2"); |
| 41 | if (size == -1) { | ||
| 42 | printf("gendata_h48 benchmark: error in datasize\n"); | ||
| 43 | return 1; | ||
| 44 | } | ||
| 45 | |||
| 46 | buf = malloc(size); | ||
| 47 | |||
| 48 | timerun(run, "benchmark gendata_h48 " LONGOPTIONS); | ||
| 49 | |||
| 50 | free(buf); | ||
| 51 | 19 | ||
| 52 | return 0; | 20 | return 0; |
| 53 | } | 21 | } |
diff --git a/tools/tool.h b/tools/tool.h index 7995621..eaee704 100644 --- a/tools/tool.h +++ b/tools/tool.h | |||
| @@ -8,6 +8,14 @@ | |||
| 8 | 8 | ||
| 9 | #include "../src/nissy.h" | 9 | #include "../src/nissy.h" |
| 10 | 10 | ||
| 11 | static void log_stderr(const char *, ...); | ||
| 12 | static void log_stdout(const char *, ...); | ||
| 13 | static double timerun(void (*)(void), const char *); | ||
| 14 | static void writetable(const char *, int64_t, const char *); | ||
| 15 | static int64_t generatetable(const char *, const char *, char **); | ||
| 16 | static int getdata(const char *, const char *, char **, const char *); | ||
| 17 | static void gendata_run(const char *, const char *, const char *, uint64_t[static 21]); | ||
| 18 | |||
| 11 | static void | 19 | static void |
| 12 | log_stderr(const char *str, ...) | 20 | log_stderr(const char *str, ...) |
| 13 | { | 21 | { |
| @@ -29,7 +37,7 @@ write_stdout(const char *str, ...) | |||
| 29 | } | 37 | } |
| 30 | 38 | ||
| 31 | static double | 39 | static double |
| 32 | timerun(void (*run)(void), char *name) | 40 | timerun(void (*run)(void), const char *name) |
| 33 | { | 41 | { |
| 34 | struct timespec start, end; | 42 | struct timespec start, end; |
| 35 | double tdiff, tdsec, tdnano; | 43 | double tdiff, tdsec, tdnano; |
| @@ -62,6 +70,46 @@ timerun(void (*run)(void), char *name) | |||
| 62 | return tdiff; | 70 | return tdiff; |
| 63 | } | 71 | } |
| 64 | 72 | ||
| 73 | static void | ||
| 74 | writetable(const char *buf, int64_t size, const char *filename) | ||
| 75 | { | ||
| 76 | FILE *f; | ||
| 77 | |||
| 78 | if ((f = fopen(filename, "wb")) == NULL) { | ||
| 79 | fprintf(stderr, "Could not write tables to file %s" | ||
| 80 | ", will be regenerated next time.\n", filename); | ||
| 81 | } else { | ||
| 82 | fwrite(buf, size, 1, f); | ||
| 83 | fclose(f); | ||
| 84 | fprintf(stderr, "Table written to %s.\n", filename); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | static int64_t | ||
| 89 | generatetable(const char *solver, const char *options, char **buf) | ||
| 90 | { | ||
| 91 | int64_t size, gensize; | ||
| 92 | |||
| 93 | size = nissy_datasize(solver, options); | ||
| 94 | if (size == -1) { | ||
| 95 | printf("Error getting table size.\n"); | ||
| 96 | return -1; | ||
| 97 | } | ||
| 98 | |||
| 99 | *buf = malloc(size); | ||
| 100 | gensize = nissy_gendata(solver, options, *buf); | ||
| 101 | |||
| 102 | if (gensize != size) { | ||
| 103 | fprintf(stderr, "Error generating table"); | ||
| 104 | if (gensize != -1) | ||
| 105 | fprintf(stderr, " (got %" PRId64 " bytes)", gensize); | ||
| 106 | fprintf(stderr, "\n"); | ||
| 107 | return -2; | ||
| 108 | } | ||
| 109 | |||
| 110 | return gensize; | ||
| 111 | } | ||
| 112 | |||
| 65 | static int | 113 | static int |
| 66 | getdata( | 114 | getdata( |
| 67 | const char *solver, | 115 | const char *solver, |
| @@ -69,36 +117,25 @@ getdata( | |||
| 69 | char **buf, | 117 | char **buf, |
| 70 | const char *filename | 118 | const char *filename |
| 71 | ) { | 119 | ) { |
| 72 | int64_t s, size, sizeread; | 120 | int64_t size, sizeread; |
| 73 | FILE *f; | 121 | FILE *f; |
| 74 | 122 | ||
| 75 | if ((size = nissy_datasize(solver, options)) == -1) { | ||
| 76 | printf("Error in datasize\n"); | ||
| 77 | goto getdata_error_nofree; | ||
| 78 | } | ||
| 79 | |||
| 80 | *buf = malloc(size); | ||
| 81 | |||
| 82 | if ((f = fopen(filename, "rb")) == NULL) { | 123 | if ((f = fopen(filename, "rb")) == NULL) { |
| 83 | fprintf(stderr, "Table file not found, generating them." | 124 | fprintf(stderr, "Table file not found, generating it.\n"); |
| 84 | " This can take a while.\n"); | 125 | size = generatetable(solver, options, buf); |
| 85 | s = nissy_gendata(solver, options, *buf); | 126 | switch (size) { |
| 86 | if (s != size) { | 127 | case -1: |
| 87 | fprintf(stderr, "Error generating table"); | 128 | goto getdata_error_nofree; |
| 88 | if (s != -1) | 129 | case -2: |
| 89 | fprintf(stderr, " (got %" PRId64 " bytes)", s); | ||
| 90 | fprintf(stderr, "\n"); | ||
| 91 | goto getdata_error; | 130 | goto getdata_error; |
| 92 | } | 131 | default: |
| 93 | if ((f = fopen(filename, "wb")) == NULL) { | 132 | writetable(filename, size, *buf); |
| 94 | fprintf(stderr, "Could not write tables to file %s" | 133 | break; |
| 95 | ", will be regenerated next time.\n", filename); | ||
| 96 | } else { | ||
| 97 | fwrite(*buf, size, 1, f); | ||
| 98 | fclose(f); | ||
| 99 | } | 134 | } |
| 100 | } else { | 135 | } else { |
| 101 | fprintf(stderr, "Reading tables from file %s\n", filename); | 136 | fprintf(stderr, "Reading tables from file %s\n", filename); |
| 137 | size = nissy_datasize(solver, options); | ||
| 138 | *buf = malloc(size); | ||
| 102 | sizeread = fread(*buf, size, 1, f); | 139 | sizeread = fread(*buf, size, 1, f); |
| 103 | fclose(f); | 140 | fclose(f); |
| 104 | if (sizeread != 1) { | 141 | if (sizeread != 1) { |
| @@ -114,3 +151,35 @@ getdata_error: | |||
| 114 | getdata_error_nofree: | 151 | getdata_error_nofree: |
| 115 | return 1; | 152 | return 1; |
| 116 | } | 153 | } |
| 154 | |||
| 155 | static void | ||
| 156 | gendata_run( | ||
| 157 | const char *solver, | ||
| 158 | const char *options, | ||
| 159 | const char *filename, /* TODO: remove filename, use solver name */ | ||
| 160 | uint64_t expected[static 21] | ||
| 161 | ) { | ||
| 162 | int64_t size; | ||
| 163 | char *buf; | ||
| 164 | |||
| 165 | |||
| 166 | size = generatetable(solver, options, &buf); | ||
| 167 | switch (size) { | ||
| 168 | case -1: | ||
| 169 | return; | ||
| 170 | case -2: | ||
| 171 | goto gendata_run_finish; | ||
| 172 | default: | ||
| 173 | nissy_datainfo(buf, write_stdout); | ||
| 174 | printf("\n"); | ||
| 175 | printf("Succesfully generated %" PRId64 " bytes. " | ||
| 176 | "See above for details on the tables.\n", size); | ||
| 177 | |||
| 178 | /* TODO: check that the table is correct */ | ||
| 179 | writetable(buf, size, filename); | ||
| 180 | break; | ||
| 181 | } | ||
| 182 | |||
| 183 | gendata_run_finish: | ||
| 184 | free(buf); | ||
| 185 | } | ||
