aboutsummaryrefslogtreecommitdiff
path: root/tools/tool.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tools/tool.h117
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
11static void log_stderr(const char *, ...);
12static void log_stdout(const char *, ...);
13static double timerun(void (*)(void), const char *);
14static void writetable(const char *, int64_t, const char *);
15static int64_t generatetable(const char *, const char *, char **);
16static int getdata(const char *, const char *, char **, const char *);
17static void gendata_run(const char *, const char *, const char *, uint64_t[static 21]);
18
11static void 19static void
12log_stderr(const char *str, ...) 20log_stderr(const char *str, ...)
13{ 21{
@@ -29,7 +37,7 @@ write_stdout(const char *str, ...)
29} 37}
30 38
31static double 39static double
32timerun(void (*run)(void), char *name) 40timerun(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
73static void
74writetable(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
88static int64_t
89generatetable(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
65static int 113static int
66getdata( 114getdata(
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:
114getdata_error_nofree: 151getdata_error_nofree:
115 return 1; 152 return 1;
116} 153}
154
155static void
156gendata_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
183gendata_run_finish:
184 free(buf);
185}

Generated with cgit - Back to sebastiano.tronto.net