aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-09-12 09:58:04 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2024-09-12 09:58:04 +0200
commit76cc82994c1fbb969ec61d3aede6bd42fda09005 (patch)
tree653723d48b48d5fc9be20181b1f6720f624176d5 /tools
parent10b65003102675bd19c0e5174ff249be2a163bc7 (diff)
downloadnissy-core-76cc82994c1fbb969ec61d3aede6bd42fda09005.tar.gz
nissy-core-76cc82994c1fbb969ec61d3aede6bd42fda09005.zip
Cleanup gendata tools
Diffstat (limited to '')
-rw-r--r--tools/001_gendata_h48h0k4/gendata_h48h0k4.c41
-rw-r--r--tools/002_gendata_h48h0k2/gendata_h48h0k2.c38
-rw-r--r--tools/tool.h117
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 3uint64_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
11uint32_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
27char *buf;
28
29void run(void) { 19void 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
46int main(void) { 23int 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 3uint64_t expected[21] = {
4#define HVALUE 0
5#define OPTIONS "0;2;20"
6#define LONGOPTIONS "h = 0, k = 2, max depth = 20"
7
8uint32_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
16char *buf;
17
18void run(void) { 11void 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
35int main(void) { 15int 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
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