diff options
Diffstat (limited to '')
| -rw-r--r-- | tools/tool.h | 117 |
1 files changed, 93 insertions, 24 deletions
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 | } | ||
