4 #include <barvinok/barvinok.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
;
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
)
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))
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
)
96 ISL_ARG_DEF(options
, struct options
, options_arg
)
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
)
109 for (i
= 0; i
< nr_methods
; ++i
) {
110 result
->RE_sum
[i
] = 0;
111 result
->ticks
[i
] = 0;
114 value_init(result
->n
);
117 void result_data_clear(struct result_data
*result
)
120 value_clear(result
->n
);
123 void result_data_print(struct result_data
*result
, int n
)
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
;
147 struct result_data
*result
;
150 static void eval(const evalue
*EP
, Value
*z
, int sign
, Value
*v
)
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
);
162 assert(value_one_p(res
->d
));
163 value_assign(*v
, res
->x
.n
);
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
;
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
) {
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
));
200 error
= abs(VALUE_TO_DOUBLE(approx
)) / VALUE_TO_DOUBLE(exact
);
201 ta_data
->result
->RE_sum
[i
] += error
;
205 fprintf(stderr, "\n");
213 static void test(Polyhedron
*P
, Polyhedron
*C
, evalue
**EP
,
214 struct result_data
*result
,
215 struct verify_options
*options
)
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
);
230 data
.cp
.check
= test_approx
;
232 data
.result
= result
;
233 check_poly(CS
, &data
.cp
, nparam
, 0, p
->p
+P
->Dimension
-nparam
+1,
235 if (!options
->print_all
)
245 void Matrix_File_Read_Input(FILE *in
, Matrix
*Mat
)
249 char *c
, s
[1024],str
[1024];
252 for (i
=0;i
<Mat
->NbRows
;i
++) {
254 c
= fgets(s
, 1024, in
);
255 while(isspace(*c
) && *c
!='\n')
257 } while(c
&& (*c
=='#' || *c
== '\n'));
260 errormsg1( "Matrix_Read", "baddim", "not enough rows" );
263 for (j
=0;j
<Mat
->NbColumns
;j
++) {
264 if(!c
|| *c
=='\n' || *c
=='#') {
265 errormsg1("Matrix_Read", "baddim", "not enough columns");
268 if (sscanf(c
,"%s%n",str
,&n
) == 0) {
269 errormsg1( "Matrix_Read", "baddim", "not enough columns" );
272 value_read(*(p
++),str
);
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
)
285 unsigned NbRows
, NbColumns
;
289 while ((*s
=='#' || *s
=='\n') ||
290 (sscanf(s
, "%d %d", &NbRows
, &NbColumns
)<2))
292 Mat
= Matrix_Alloc(NbRows
,NbColumns
);
294 errormsg1("Matrix_Read", "outofmem", "out of memory space");
297 Matrix_File_Read_Input(in
, Mat
);
301 void handle(FILE *in
, struct result_data
*result
, struct verify_options
*options
)
306 const char **param_name
;
307 evalue
*EP
[nr_methods
];
309 M
= Matrix_File_Read(in
);
310 A
= Constraints2Polyhedron(M
, options
->barvinok
->MaxRays
);
312 M
= Matrix_File_Read(in
);
313 C
= Constraints2Polyhedron(M
, options
->barvinok
->MaxRays
);
315 param_name
= Read_ParamNames(in
, C
->Dimension
);
317 for (i
= 0; i
< nr_methods
; ++i
) {
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
;
328 EP
[i
] = barvinok_enumerate_with_options(A
, C
, options
->barvinok
);
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
)
341 Free_ParamNames(param_name
, C
->Dimension
);
346 int main(int argc
, char **argv
)
348 char path
[PATH_MAX
+1];
349 struct result_data all_result
;
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
;
368 result_data_init(&result
);
369 fprintf(stderr
, "%s", path
);
370 *strchr(path
, '\n') = '\0';
371 in
= fopen(path
, "r");
373 handle(in
, &result
, options
->verify
);
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
);