-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
384 lines (315 loc) · 8.43 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <math.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include "error.h"
#define N (10) /* number of tests. */
char* progname; /* name of this program. */
unsigned test_time = 1; /* default one second for fast. */
volatile bool proceed; /* stop after test_time seconds. */
uint64_t ref[N]; /* reference counts by /opt/fm/bin/fast */
double ratio[N]; /* (your count) / (ref count) */
bool generate_ref; /* used when new ref file should be made. */
size_t input_count; /* counts number of input files tested. will end at N above. */
bool fm(size_t rows, size_t cols, signed char a[rows][cols], signed char c[cols]);
//void bp(){};
static void timeout(int s)
{
proceed = s-s; /* set proceed to false and use s so that gcc does not warn about unused s. */
}
static void ctrl_c(int s)
{
printf("\n\npassed %zu tests (no failures)\n", input_count);
exit(s-s);
}
void cd(char* dir)
{
if (chdir(dir) < 0)
error("cd to \"%s\" failed", dir);
}
static void check(char* wd, char* input, int seconds)
{
bool result;
bool differ;
int n;
int x;
size_t index; /* with input Ax.y index is y, e.g. 123 in A4.123 */
size_t rows;
size_t rows0;
size_t cols;
size_t i;
size_t j;
char* s;
char c[100];
char output[100];
FILE* afile;
FILE* cfile;
FILE* ofile;
uint64_t count;
index = 0;
count = 0;
errno = 0;
cols = strtol(input+1, &s, 10);
if (errno != 0)
error("strtol failed for %s", input+1);
s = strchr(input, '.');
if (s != NULL) {
s += 1;
n = sscanf(s, "%zu", &index);
if (n != 1)
error("reading system number failed in %s, n = %d", s, n);
}
strncpy(c, input, sizeof c);
c[0] = 'c';
afile = fopen(input, "r");
if (afile == NULL)
error("fopen %s failed", input);
cfile = fopen(c, "r");
if (cfile == NULL) {
fclose(afile);
return;
}
n = fscanf(afile, "%zu %zu", &rows, &cols);
if (n != 2)
error("reading matrix size failed");
{
signed char a[rows][cols];
signed char c[rows];
memset(a, 0, sizeof a);
memset(c, 0, sizeof c);
for (i = 0; i < rows; ++i) {
for (j = 0; j < cols; ++j) {
n = fscanf(afile, "%d", &x);
if (n != 1)
error("reading a[%zu][%zu] of %s/%s failed", i, j, wd, a);
a[i][j] = x;
}
}
n = fscanf(cfile, "%zu", &rows0);
if (n != 1)
error("reading vector size of %s/%s failed", wd, c);
for (i = 0; i < rows; ++i) {
fscanf(cfile, "%d", &x);
c[i] = x;
}
fclose(afile);
fclose(cfile);
result = fm(rows, cols, a, c);
if (seconds > 0) {
signal(SIGALRM, timeout);
alarm(seconds);
proceed = 1;
while (proceed) {
if (result != fm(rows, cols, a, c))
error("inconsistent return value from fm after %llu iterations", count);
count += 1;
}
if (generate_ref) {
ref[index] = count;
if (input_count == 0)
printf("%12s %12s\n", "input", "ref");
printf("%12s %12" PRIu64 "\n", input, ref[index]);//%12llu
}
}
}
snprintf(output, sizeof output, "output%zu.%zu.txt", cols, index);
if (access(output, R_OK) != 0) {
/* this happens when mathematica timed out. */
unlink(input);
unlink(c);
return;
}
ofile = fopen(output, "r");
if (ofile == NULL)
error("fopen %s/%s failed", wd, output);
fgets(output, sizeof output, ofile);
fclose(ofile);
//bp();//--------------------------------------------------------------<<<<<<<<<<<
differ = (strncmp(output, "False", 5) != 0) ^ result;
if (differ)
error("failed for %s/%s", wd, input);
if (seconds > 0 && !generate_ref) {
ratio[index] = count / (double)ref[index];
if (input_count == 0)
printf("%12s %12s %12s %12s\n", "input", "ref", "count", "ratio");
printf("%12s %12" PRIu64" %12" PRIu64" %12.3lf\n", input, ref[index], count, ratio[index]);
}
input_count += 1;
}
static void search(void)
{
DIR* dir;
struct dirent* entry;
char wd[BUFSIZ];
dir = opendir(".");
if (dir == NULL)
error("cannot open .");
while ((entry = readdir(dir)) != NULL) {
if (getcwd(wd, sizeof wd) == NULL)
error("getcwd failed");
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
else if (entry->d_name[0] == 'A'){
//bp();//--------------------------------------<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
check(wd, entry->d_name, 0);
}
else if (isdigit(entry->d_name[0])) {
if (chdir(entry->d_name) < 0)
error("cd to \"%s\"", entry->d_name);
else
search();
}
}
cd("..");
closedir(dir);
}
static void eval(unsigned seconds, size_t cols)
{
size_t i;
char input[100];
char wd[BUFSIZ];
cd("eval");
if (getcwd(wd, sizeof wd) == NULL)
error("getcwd failed");
for (i = 0; i < N; i += 1) {
snprintf(input, sizeof input, "A%zu.%zu", cols, i);
if (access(input, R_OK) != 0)
error("cannot read file \"%s/\"%s", wd, input);
check(wd, input, seconds);
}
cd("..");
}
static void read_ref()
{
int i;
FILE* fp;
char filename[100];
char cmd[100];
if (generate_ref)
return;
snprintf(filename, sizeof filename, "ref.%u", test_time);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("no reference counts available for test_time = %u\n", test_time);
printf("runnning the reference implementation to create them...\n");
snprintf(cmd, sizeof cmd, "/opt/fm/bin/fm -g -t %u", test_time);
system(cmd);
fp = fopen(filename, "r");
if (fp == NULL)
error("could not produce reference counts for test_time = %u", test_time);
}
for (i = 0; i < N; i += 1)
fscanf(fp, "%" PRIu64 "\n", &ref[i]);
fclose(fp);
printf("will use reference count file \"%s\"\n", filename);
}
static void save_ref()
{
int i;
FILE* fp;
char filename[100];
char cwd[100];
snprintf(filename, sizeof filename, "ref.%u", test_time);
fp = fopen(filename, "w");
if (fp == NULL)
error("cannot open \"%s\" for writing", filename);
if (getcwd(cwd, sizeof cwd) == NULL)
error("getcwd failed");
printf("saving reference counts in %s/%s\n", cwd, filename);
for (i = 0; i < N; i += 1)
fprintf(fp, "%" PRIu64 "\n", ref[i]);
fclose(fp);
}
static void usage(char* arg)
{
if (arg != NULL)
fprintf(stderr, "%s: illegal option: %s\n", progname, arg);
fprintf(stderr, "%s: usage: %s [-h] [-i inputdir] [-t time]\n", progname, progname);
fprintf(stderr, "%s: where time is in seconds and >= 0\n", progname);
fprintf(stderr, "%s: by default \"input\" in current directory is used\n", progname);
fprintf(stderr, "%s: with -i inputdir fm recursively searches for all input files in that directory\n", progname);
fprintf(stderr, "%s: to try a really huge number of tests (for weeks), use fm -i /opt/fm/input\n", progname);
fprintf(stderr, "%s: or more reasonable, fm -i /opt/fm/input/0/0\n", progname);
fprintf(stderr, "%s: timing tests are still performed using \"eval\" in the current directory\n", progname);
exit(1);
}
int main(int argc, char** argv)
{
int i;
double prod;
char cwd[BUFSIZ];
char* input;
FILE* fp;
progname = argv[0];
input = "input";
signal(SIGINT, ctrl_c);
if (getcwd(cwd, sizeof cwd) == NULL)
error("getcwd failed");
for (i = 1; i < argc; i += 1) {
if (argv[i][0] != '-')
usage(argv[i]);
else {
switch (argv[i][1]) {
case 'g':
/* only used by fm itself. */
generate_ref = 1;
break;
case 'h':
usage(NULL);
case 'i':
if (argv[i+1] == NULL)
usage(argv[i]);
input = argv[i+1];
i += 1;
break;
case 't':
if (argv[i+1] == NULL || sscanf(argv[i+1], "%u", &test_time) != 1)
usage(argv[i]);
i += 1;
break;
default:
usage(argv[i]);
}
}
}
if (!generate_ref)
printf("welcome to the fourier-motzkin test program. use fm -h for help\n\n");
if (test_time > 0)
read_ref();
cd(input);
input_count = 0;
search();
printf("passed %zu tests (no failures)\n", input_count);
cd(cwd);
if (test_time == 0)
exit(0);
input_count = 0;
printf("now timing %d tests...please wait %u s\n", N, N*test_time);
eval(test_time, 3);
if (input_count != N)
error("expected input_count = %d\n", N);
if (ratio[0] == 0)
save_ref();
else {
prod = 1.0;
for (i = 0; i < N; i += 1)
prod *= ratio[i];
prod = pow(prod, 1.0/N);
printf("geometric mean of ratios = %1.2lf (higher is better)\n", prod);
fp = fopen("score", "w");
if (fp == NULL)
error("cannot open \"score\" for writing");
fprintf(fp, "%1.0lf\n", prod * 100); /* print as integer. */
fclose(fp);
}
return 0;
}