update isl for support for recent clangs
[barvinok.git] / test_approx.c
blob8e8ae5f1fe2d6c300cc3301ca43f359837d142aa
1 #include <assert.h>
2 #include <ctype.h>
3 #include <limits.h>
4 #include <barvinok/barvinok.h>
5 #include "verify.h"
6 #include "config.h"
8 #ifdef HAVE_SYS_TIMES_H
10 #include <sys/times.h>
12 typedef clock_t my_clock_t;
14 static my_clock_t time_diff(struct tms *before, struct tms *after)
16 return after->tms_utime - before->tms_utime;
19 #else
21 typedef int my_clock_t;
23 struct tms { int dummy; };
24 static void times(struct tms* time)
27 static my_clock_t time_diff(struct tms *before, struct tms *after)
29 return 0;
32 #endif
34 struct {
35 int sign;
36 int method;
37 int flags;
38 } methods[] = {
39 { BV_APPROX_SIGN_NONE, BV_APPROX_NONE, 0 },
40 { BV_APPROX_SIGN_APPROX, BV_APPROX_BERNOULLI, 0 },
41 { BV_APPROX_SIGN_APPROX, BV_APPROX_DROP, 0 },
42 { BV_APPROX_SIGN_APPROX, BV_APPROX_VOLUME, BV_VOL_LIFT },
43 { BV_APPROX_SIGN_APPROX, BV_APPROX_VOLUME, BV_VOL_VERTEX },
44 { BV_APPROX_SIGN_APPROX, BV_APPROX_VOLUME, BV_VOL_BARYCENTER },
45 { BV_APPROX_SIGN_APPROX, BV_APPROX_SCALE, 0 },
46 { BV_APPROX_SIGN_APPROX, BV_APPROX_SCALE, BV_APPROX_SCALE_CHAMBER },
47 { BV_APPROX_SIGN_APPROX, BV_APPROX_SCALE, BV_APPROX_SCALE_FAST },
48 { BV_APPROX_SIGN_APPROX, BV_APPROX_SCALE, BV_APPROX_SCALE_FAST | BV_APPROX_SCALE_CHAMBER },
49 { BV_APPROX_SIGN_LOWER, BV_APPROX_DROP, 0 },
50 { BV_APPROX_SIGN_LOWER, BV_APPROX_VOLUME, BV_VOL_LIFT },
51 { BV_APPROX_SIGN_LOWER, BV_APPROX_VOLUME, BV_VOL_VERTEX },
52 { BV_APPROX_SIGN_LOWER, BV_APPROX_VOLUME, BV_VOL_BARYCENTER },
53 { BV_APPROX_SIGN_LOWER, BV_APPROX_SCALE, 0 },
54 { BV_APPROX_SIGN_LOWER, BV_APPROX_SCALE, BV_APPROX_SCALE_CHAMBER },
55 { BV_APPROX_SIGN_LOWER, BV_APPROX_SCALE, BV_APPROX_SCALE_FAST },
56 { BV_APPROX_SIGN_LOWER, BV_APPROX_SCALE, BV_APPROX_SCALE_FAST | BV_APPROX_SCALE_CHAMBER },
57 { BV_APPROX_SIGN_LOWER, BV_APPROX_SCALE, BV_APPROX_SCALE_NARROW },
58 { BV_APPROX_SIGN_LOWER, BV_APPROX_SCALE, BV_APPROX_SCALE_NARROW | BV_APPROX_SCALE_CHAMBER},
59 { BV_APPROX_SIGN_LOWER, BV_APPROX_SCALE, BV_APPROX_SCALE_FAST | BV_APPROX_SCALE_NARROW },
60 { BV_APPROX_SIGN_LOWER, BV_APPROX_SCALE, BV_APPROX_SCALE_FAST | BV_APPROX_SCALE_NARROW | BV_APPROX_SCALE_CHAMBER },
61 { BV_APPROX_SIGN_LOWER, BV_APPROX_SCALE, BV_APPROX_SCALE_NARROW2 },
62 { BV_APPROX_SIGN_LOWER, BV_APPROX_SCALE, BV_APPROX_SCALE_NARROW2 | BV_APPROX_SCALE_CHAMBER },
63 { BV_APPROX_SIGN_LOWER, BV_APPROX_SCALE, BV_APPROX_SCALE_FAST | BV_APPROX_SCALE_NARROW2 },
64 { BV_APPROX_SIGN_LOWER, BV_APPROX_SCALE, BV_APPROX_SCALE_FAST | BV_APPROX_SCALE_NARROW2 | BV_APPROX_SCALE_CHAMBER },
65 { BV_APPROX_SIGN_UPPER, BV_APPROX_DROP, 0 },
66 { BV_APPROX_SIGN_UPPER, BV_APPROX_VOLUME, BV_VOL_LIFT },
67 { BV_APPROX_SIGN_UPPER, BV_APPROX_VOLUME, BV_VOL_VERTEX },
68 { BV_APPROX_SIGN_UPPER, BV_APPROX_VOLUME, BV_VOL_BARYCENTER },
69 { BV_APPROX_SIGN_UPPER, BV_APPROX_SCALE, 0 },
70 { BV_APPROX_SIGN_UPPER, BV_APPROX_SCALE, BV_APPROX_SCALE_CHAMBER },
71 { BV_APPROX_SIGN_UPPER, BV_APPROX_SCALE, BV_APPROX_SCALE_FAST },
72 { BV_APPROX_SIGN_UPPER, BV_APPROX_SCALE, BV_APPROX_SCALE_FAST | BV_APPROX_SCALE_CHAMBER },
73 { BV_APPROX_SIGN_UPPER, BV_APPROX_SCALE, BV_APPROX_SCALE_NARROW },
74 { BV_APPROX_SIGN_UPPER, BV_APPROX_SCALE, BV_APPROX_SCALE_NARROW | BV_APPROX_SCALE_CHAMBER },
75 { BV_APPROX_SIGN_UPPER, BV_APPROX_SCALE, BV_APPROX_SCALE_FAST | BV_APPROX_SCALE_NARROW },
76 { BV_APPROX_SIGN_UPPER, BV_APPROX_SCALE, BV_APPROX_SCALE_FAST | BV_APPROX_SCALE_NARROW | BV_APPROX_SCALE_CHAMBER },
77 { BV_APPROX_SIGN_UPPER, BV_APPROX_SCALE, BV_APPROX_SCALE_NARROW2 },
78 { BV_APPROX_SIGN_UPPER, BV_APPROX_SCALE, BV_APPROX_SCALE_NARROW2 | BV_APPROX_SCALE_CHAMBER },
79 { BV_APPROX_SIGN_UPPER, BV_APPROX_SCALE, BV_APPROX_SCALE_FAST | BV_APPROX_SCALE_NARROW2 },
80 { BV_APPROX_SIGN_UPPER, BV_APPROX_SCALE, BV_APPROX_SCALE_FAST | BV_APPROX_SCALE_NARROW2 | BV_APPROX_SCALE_CHAMBER },
83 #define nr_methods (sizeof(methods)/sizeof(*methods))
85 struct options {
86 int quiet;
87 struct verify_options *verify;
90 struct isl_arg options_arg[] = {
91 ISL_ARG_CHILD(struct options, verify, NULL, verify_options_arg, NULL)
92 ISL_ARG_BOOL(struct options, quiet, 'q', "quiet", 0, NULL)
93 ISL_ARG_END
96 ISL_ARG_DEF(options, struct options, options_arg)
98 struct result_data {
99 Value n;
100 double RE_sum[nr_methods];
102 my_clock_t ticks[nr_methods];
103 size_t size[nr_methods];
106 void result_data_init(struct result_data *result)
108 int i;
109 for (i = 0; i < nr_methods; ++i) {
110 result->RE_sum[i] = 0;
111 result->ticks[i] = 0;
112 result->size[i] = 0;
114 value_init(result->n);
117 void result_data_clear(struct result_data *result)
119 int i;
120 value_clear(result->n);
123 void result_data_print(struct result_data *result, int n)
125 int i;
127 fprintf(stderr, "%g", (double)result->ticks[0]/n);
128 for (i = 1; i < nr_methods; ++i)
129 fprintf(stderr, ", %g", (double)result->ticks[i]/n);
130 fprintf(stderr, "\n");
132 fprintf(stderr, "%zd", result->size[0]/n);
133 for (i = 1; i < nr_methods; ++i)
134 fprintf(stderr, ", %zd", result->size[i]/n);
135 fprintf(stderr, "\n");
137 fprintf(stderr, "%g\n", VALUE_TO_DOUBLE(result->n));
138 fprintf(stderr, "%g", result->RE_sum[0]/VALUE_TO_DOUBLE(result->n));
139 for (i = 1; i < nr_methods; ++i)
140 fprintf(stderr, ", %g", result->RE_sum[i]/VALUE_TO_DOUBLE(result->n));
141 fprintf(stderr, "\n");
144 struct test_approx_data {
145 struct check_poly_data cp;
146 evalue **EP;
147 struct result_data *result;
150 static void eval(const evalue *EP, Value *z, int sign, Value *v)
152 evalue *res;
154 res = evalue_eval(EP, z);
155 if (sign == BV_APPROX_SIGN_LOWER)
156 mpz_cdiv_q(*v, res->x.n, res->d);
157 else if (sign == BV_APPROX_SIGN_UPPER)
158 mpz_fdiv_q(*v, res->x.n, res->d);
159 else if (sign == BV_APPROX_SIGN_APPROX)
160 mpz_tdiv_q(*v, res->x.n, res->d);
161 else {
162 assert(value_one_p(res->d));
163 value_assign(*v, res->x.n);
165 evalue_free(res);
168 static int test_approx(const struct check_poly_data *data, int nparam, Value *z,
169 const struct verify_options *options)
171 struct test_approx_data* ta_data = (struct test_approx_data*) data;
172 Value exact, approx;
173 int i;
175 value_init(exact);
176 value_init(approx);
178 eval(ta_data->EP[0], z, BV_APPROX_SIGN_NONE, &exact);
181 value_print(stderr, VALUE_FMT, exact);
184 value_increment(ta_data->result->n, ta_data->result->n);
185 for (i = 1; i < nr_methods; ++i) {
186 double error;
187 eval(ta_data->EP[i], z, methods[i].sign, &approx);
189 fprintf(stderr, ", ");
190 value_print(stderr, VALUE_FMT, approx);
192 if (methods[i].sign == BV_APPROX_SIGN_LOWER)
193 assert(value_le(approx, exact));
194 if (methods[i].sign == BV_APPROX_SIGN_UPPER)
195 assert(value_ge(approx, exact));
196 value_subtract(approx, approx, exact);
197 if (value_zero_p(exact))
198 error = abs(VALUE_TO_DOUBLE(approx));
199 else
200 error = abs(VALUE_TO_DOUBLE(approx)) / VALUE_TO_DOUBLE(exact);
201 ta_data->result->RE_sum[i] += error;
205 fprintf(stderr, "\n");
208 value_clear(exact);
209 value_clear(approx);
210 return 1;
213 static void test(Polyhedron *P, Polyhedron *C, evalue **EP,
214 struct result_data *result,
215 struct verify_options *options)
217 Polyhedron *CS;
218 Vector *p;
219 unsigned nparam = C->Dimension;
220 struct test_approx_data data;
222 CS = check_poly_context_scan(P, &C, C->Dimension, options);
224 p = Vector_Alloc(P->Dimension+2);
225 value_set_si(p->p[P->Dimension+1], 1);
227 check_poly_init(C, options);
229 data.cp.z = p->p;
230 data.cp.check = test_approx;
231 data.EP = EP;
232 data.result = result;
233 check_poly(CS, &data.cp, nparam, 0, p->p+P->Dimension-nparam+1,
234 options);
235 if (!options->print_all)
236 printf("\n");
238 Vector_Free(p);
239 if (CS) {
240 Domain_Free(CS);
241 Domain_Free(C);
245 void Matrix_File_Read_Input(FILE *in, Matrix *Mat)
247 Value *p;
248 int i,j,n;
249 char *c, s[1024],str[1024];
251 p = Mat->p_Init;
252 for (i=0;i<Mat->NbRows;i++) {
253 do {
254 c = fgets(s, 1024, in);
255 while(isspace(*c) && *c!='\n')
256 ++c;
257 } while(c && (*c =='#' || *c== '\n'));
259 if (!c) {
260 errormsg1( "Matrix_Read", "baddim", "not enough rows" );
261 break;
263 for (j=0;j<Mat->NbColumns;j++) {
264 if(!c || *c=='\n' || *c=='#') {
265 errormsg1("Matrix_Read", "baddim", "not enough columns");
266 break;
268 if (sscanf(c,"%s%n",str,&n) == 0) {
269 errormsg1( "Matrix_Read", "baddim", "not enough columns" );
270 break;
272 value_read(*(p++),str);
273 c += n;
276 } /* Matrix_Read_Input */
279 * Read the contents of the matrix 'Mat' from standard input.
280 * A '#' in the first column is a comment line
282 Matrix *Matrix_File_Read(FILE *in)
284 Matrix *Mat;
285 unsigned NbRows, NbColumns;
286 char s[1024];
288 fgets(s, 1024, in);
289 while ((*s=='#' || *s=='\n') ||
290 (sscanf(s, "%d %d", &NbRows, &NbColumns)<2))
291 fgets(s, 1024, in);
292 Mat = Matrix_Alloc(NbRows,NbColumns);
293 if(!Mat) {
294 errormsg1("Matrix_Read", "outofmem", "out of memory space");
295 return(NULL);
297 Matrix_File_Read_Input(in, Mat);
298 return Mat;
299 } /* Matrix_Read */
301 void handle(FILE *in, struct result_data *result, struct verify_options *options)
303 int i;
304 Polyhedron *A, *C;
305 Matrix *M;
306 const char **param_name;
307 evalue *EP[nr_methods];
309 M = Matrix_File_Read(in);
310 A = Constraints2Polyhedron(M, options->barvinok->MaxRays);
311 Matrix_Free(M);
312 M = Matrix_File_Read(in);
313 C = Constraints2Polyhedron(M, options->barvinok->MaxRays);
314 Matrix_Free(M);
315 param_name = Read_ParamNames(in, C->Dimension);
317 for (i = 0; i < nr_methods; ++i) {
318 struct tms st_cpu;
319 struct tms en_cpu;
320 options->barvinok->approx->approximation = methods[i].sign;
321 options->barvinok->approx->method = methods[i].method;
322 if (methods[i].method == BV_APPROX_SCALE)
323 options->barvinok->approx->scale_flags = methods[i].flags;
324 else if (methods[i].method == BV_APPROX_VOLUME)
325 options->barvinok->approx->volume_triangulate = methods[i].flags;
327 times(&st_cpu);
328 EP[i] = barvinok_enumerate_with_options(A, C, options->barvinok);
329 times(&en_cpu);
330 result->ticks[i] = time_diff(&en_cpu, &st_cpu);
332 print_evalue(stdout, EP[i], param_name);
335 for (i = 0; i < nr_methods; ++i)
336 result->size[i] = evalue_size(EP[i])/4;
337 test(A, C, EP, result, options);
338 for (i = 0; i < nr_methods; ++i)
339 evalue_free(EP[i]);
341 Free_ParamNames(param_name, C->Dimension);
342 Polyhedron_Free(A);
343 Polyhedron_Free(C);
346 int main(int argc, char **argv)
348 char path[PATH_MAX+1];
349 struct result_data all_result;
350 int n = 0;
351 struct options *options = options_new_with_defaults();
353 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
355 if (options->verify->M == INT_MIN)
356 options->verify->M = 100;
357 if (options->verify->m == INT_MAX)
358 options->verify->m = -100;
360 result_data_init(&all_result);
362 while (fgets(path, sizeof(path), stdin)) {
363 struct result_data result;
364 FILE *in;
365 int i;
367 ++n;
368 result_data_init(&result);
369 fprintf(stderr, "%s", path);
370 *strchr(path, '\n') = '\0';
371 in = fopen(path, "r");
372 assert(in);
373 handle(in, &result, options->verify);
374 fclose(in);
376 if (!options->quiet)
377 result_data_print(&result, 1);
379 value_addto(all_result.n, all_result.n, result.n);
380 for (i = 0; i < nr_methods; ++i) {
381 all_result.RE_sum[i] += result.RE_sum[i];
382 all_result.ticks[i] += result.ticks[i];
383 all_result.size[i] += result.size[i];
386 result_data_clear(&result);
388 if (!options->quiet) {
389 fprintf(stderr, "average\n");
390 result_data_print(&all_result, n);
394 result_data_clear(&all_result);
396 options_free(options);
398 return 0;