polysign_isl.c: extract_inequalities: use isl_val
[barvinok.git] / test_bound.c
blob0307d780d7e53d09ca07095d2fe2d015ba7a5fbf
1 #include <assert.h>
2 #include <limits.h>
3 #include <math.h>
4 #include <isl/stream.h>
5 #include <isl/options.h>
6 #include <barvinok/options.h>
7 #include <barvinok/util.h>
8 #include "verify.h"
9 #include "config.h"
11 #ifdef HAVE_SYS_TIMES_H
13 #include <sys/times.h>
15 typedef clock_t my_clock_t;
17 static my_clock_t time_diff(struct tms *before, struct tms *after)
19 return after->tms_utime - before->tms_utime;
22 #else
24 typedef int my_clock_t;
26 struct tms {};
27 static void times(struct tms* time)
30 static my_clock_t time_diff(struct tms *before, struct tms *after)
32 return 0;
35 #endif
37 static struct {
38 int method;
39 } methods[] = {
40 { ISL_BOUND_BERNSTEIN },
41 { ISL_BOUND_RANGE },
44 #define nr_methods (sizeof(methods)/sizeof(*methods))
46 struct options {
47 struct verify_options *verify;
48 int quiet;
51 ISL_ARGS_START(struct options, options_args)
52 ISL_ARG_CHILD(struct options, verify, NULL, &verify_options_args, NULL)
53 ISL_ARG_BOOL(struct options, quiet, 'q', "quiet", 0, NULL)
54 ISL_ARGS_END
56 ISL_ARG_DEF(options, struct options, options_args)
58 struct result_data {
59 isl_int n;
60 double RE_sum[nr_methods];
62 my_clock_t ticks[nr_methods];
63 size_t size[nr_methods];
66 void result_data_init(struct result_data *result)
68 int i;
69 for (i = 0; i < nr_methods; ++i) {
70 result->RE_sum[i] = 0;
71 result->ticks[i] = 0;
72 result->size[i] = 0;
74 isl_int_init(result->n);
77 void result_data_clear(struct result_data *result)
79 isl_int_clear(result->n);
82 void result_data_print(struct result_data *result, int n)
84 int i;
86 fprintf(stderr, "%g", (double)result->ticks[0]/n);
87 for (i = 1; i < nr_methods; ++i)
88 fprintf(stderr, ", %g", (double)result->ticks[i]/n);
89 fprintf(stderr, "\n");
91 fprintf(stderr, "%zd/%d", result->size[0], n);
92 for (i = 1; i < nr_methods; ++i)
93 fprintf(stderr, ", %zd/%d", result->size[i], n);
94 fprintf(stderr, "\n");
96 fprintf(stderr, "%g\n", isl_int_get_d(result->n));
97 fprintf(stderr, "%g", result->RE_sum[0]/isl_int_get_d(result->n));
98 for (i = 1; i < nr_methods; ++i)
99 fprintf(stderr, ", %g", result->RE_sum[i]/isl_int_get_d(result->n));
100 fprintf(stderr, "\n");
103 struct verify_point_bound {
104 struct verify_point_data vpd;
105 struct result_data *result;
106 isl_pw_qpolynomial *pwqp;
107 isl_pw_qpolynomial_fold **pwf;
110 static int verify_point(__isl_take isl_point *pnt, void *user)
112 struct verify_point_bound *vpb = (struct verify_point_bound *) user;
113 const struct verify_options *options = vpb->vpd.options;
114 int i;
115 unsigned nparam;
116 isl_int max, min, exact, approx;
117 isl_int n, d;
118 isl_qpolynomial *opt;
119 isl_pw_qpolynomial *pwqp;
120 int cst;
122 vpb->vpd.n--;
124 isl_int_init(max);
125 isl_int_init(min);
126 isl_int_init(exact);
127 isl_int_init(approx);
128 isl_int_init(n);
129 isl_int_init(d);
131 pwqp = isl_pw_qpolynomial_copy(vpb->pwqp);
133 nparam = isl_pw_qpolynomial_dim(pwqp, isl_dim_param);
134 for (i = 0; i < nparam; ++i) {
135 isl_point_get_coordinate(pnt, isl_dim_param, i, &n);
136 pwqp = isl_pw_qpolynomial_fix_dim(pwqp, isl_dim_param, i, n);
139 opt = isl_pw_qpolynomial_max(isl_pw_qpolynomial_copy(pwqp));
140 cst = isl_qpolynomial_is_cst(opt, &n, &d);
141 isl_qpolynomial_free(opt);
142 assert(cst == 1);
143 isl_int_fdiv_q(max, n, d);
145 opt = isl_pw_qpolynomial_min(pwqp);
146 cst = isl_qpolynomial_is_cst(opt, &n, &d);
147 isl_qpolynomial_free(opt);
148 assert(cst == 1);
149 isl_int_cdiv_q(min, n, d);
151 isl_int_sub(exact, max, min);
152 isl_int_add_ui(exact, exact, 1);
154 if (options->print_all) {
155 fprintf(stderr, "max: "); isl_int_print(stderr, max, 0);
156 fprintf(stderr, ", min: "); isl_int_print(stderr, min, 0);
157 fprintf(stderr, ", range: "); isl_int_print(stderr, exact, 0);
160 for (i = 0; i < nr_methods; ++i) {
161 double error;
163 opt = isl_pw_qpolynomial_fold_eval(
164 isl_pw_qpolynomial_fold_copy(vpb->pwf[2 * i]),
165 isl_point_copy(pnt));
166 cst = isl_qpolynomial_is_cst(opt, &n, &d);
167 isl_qpolynomial_free(opt);
168 assert(cst == 1);
169 isl_int_fdiv_q(max, n, d);
171 opt = isl_pw_qpolynomial_fold_eval(
172 isl_pw_qpolynomial_fold_copy(vpb->pwf[2 * i + 1]),
173 isl_point_copy(pnt));
174 cst = isl_qpolynomial_is_cst(opt, &n, &d);
175 isl_qpolynomial_free(opt);
176 assert(cst == 1);
177 isl_int_cdiv_q(min, n, d);
179 isl_int_sub(approx, max, min);
180 isl_int_add_ui(approx, approx, 1);
181 if (options->print_all) {
182 fprintf(stderr, ", "); isl_int_print(stderr, approx, 0);
185 assert(isl_int_ge(approx, exact));
186 isl_int_sub(approx, approx, exact);
188 error = fabs(isl_int_get_d(approx)) / isl_int_get_d(exact);
189 if (options->print_all)
190 fprintf(stderr, " (%g)", error);
191 vpb->result->RE_sum[i] += error;
194 if (options->print_all) {
195 fprintf(stderr, "\n");
196 } else if ((vpb->vpd.n % vpb->vpd.s) == 0) {
197 printf("o");
198 fflush(stdout);
201 isl_int_clear(max);
202 isl_int_clear(min);
203 isl_int_clear(exact);
204 isl_int_clear(approx);
205 isl_int_clear(n);
206 isl_int_clear(d);
208 isl_point_free(pnt);
210 return vpb->vpd.n >= 1 ? 0 : -1;
213 static void test(__isl_keep isl_pw_qpolynomial *pwqp,
214 __isl_keep isl_pw_qpolynomial_fold **pwf, struct result_data *result,
215 struct verify_options *options)
217 struct verify_point_bound vpb = { { options }, result };
218 isl_set *context;
219 int r;
221 vpb.pwf = pwf;
222 vpb.pwqp = pwqp;
223 context = isl_pw_qpolynomial_domain(isl_pw_qpolynomial_copy(vpb.pwqp));
224 context = isl_set_params(context);
225 context = verify_context_set_bounds(context, options);
227 r = verify_point_data_init(&vpb.vpd, context);
228 assert(r == 0);
229 isl_int_set_si(result->n, vpb.vpd.n);
231 isl_set_foreach_point(context, verify_point, &vpb);
232 assert(!vpb.vpd.error);
234 isl_set_free(context);
236 verify_point_data_fini(&vpb.vpd);
239 void handle(FILE *in, struct result_data *result, struct verify_options *options)
241 int i;
242 isl_ctx *ctx = isl_ctx_alloc();
243 isl_pw_qpolynomial *pwqp, *upper, *lower;
244 isl_pw_qpolynomial_fold *pwf[2*nr_methods];
245 struct isl_stream *s;
247 s = isl_stream_new_file(ctx, in);
248 pwqp = isl_stream_read_pw_qpolynomial(s);
250 upper = isl_pw_qpolynomial_to_polynomial(isl_pw_qpolynomial_copy(pwqp), 1);
251 lower = isl_pw_qpolynomial_to_polynomial(isl_pw_qpolynomial_copy(pwqp), -1);
253 for (i = 0; i < nr_methods; ++i) {
254 int j;
255 struct tms st_cpu;
256 struct tms en_cpu;
258 times(&st_cpu);
259 for (j = 0; j < 2; ++j) {
260 isl_pw_qpolynomial *poly = j == 0 ? upper : lower;
261 enum isl_fold type = j == 0 ? isl_fold_max : isl_fold_min;
262 isl_options_set_bound(ctx, methods[i].method);
263 poly = isl_pw_qpolynomial_copy(poly);
264 pwf[2*i+j] = isl_pw_qpolynomial_bound(poly, type, NULL);
266 times(&en_cpu);
267 result->ticks[i] = time_diff(&en_cpu, &st_cpu);
268 result->size[i] = isl_pw_qpolynomial_fold_size(pwf[2*i]);
269 result->size[i] = isl_pw_qpolynomial_fold_size(pwf[2*i+1]);
270 if (options->barvinok->verbose) {
271 isl_printer *p = isl_printer_to_file(ctx, stdout);
272 for (j = 0; j < 2; ++j) {
273 p = isl_printer_print_pw_qpolynomial_fold(p, pwf[2*i+j]);
274 p = isl_printer_end_line(p);
276 isl_printer_free(p);
279 test(pwqp, pwf, result, options);
281 for (i = 0; i < 2*nr_methods; ++i)
282 isl_pw_qpolynomial_fold_free(pwf[i]);
283 isl_pw_qpolynomial_free(pwqp);
284 isl_pw_qpolynomial_free(lower);
285 isl_pw_qpolynomial_free(upper);
287 isl_stream_free(s);
288 isl_ctx_free(ctx);
291 int main(int argc, char **argv)
293 char path[PATH_MAX+1];
294 struct result_data all_result;
295 int n = 0;
296 struct options *options = options_new_with_defaults();
298 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
300 if (options->verify->M == INT_MIN)
301 options->verify->M = 100;
302 if (options->verify->m == INT_MAX)
303 options->verify->m = -100;
305 result_data_init(&all_result);
307 while (fgets(path, sizeof(path), stdin)) {
308 struct result_data result;
309 FILE *in;
310 int i;
312 ++n;
313 result_data_init(&result);
314 fprintf(stderr, "%s", path);
315 *strchr(path, '\n') = '\0';
316 in = fopen(path, "r");
317 assert(in);
318 handle(in, &result, options->verify);
319 fclose(in);
321 if (!options->quiet)
322 result_data_print(&result, 1);
324 isl_int_add(all_result.n, all_result.n, result.n);
325 for (i = 0; i < nr_methods; ++i) {
326 all_result.RE_sum[i] += result.RE_sum[i];
327 all_result.ticks[i] += result.ticks[i];
328 all_result.size[i] += result.size[i];
331 result_data_clear(&result);
333 if (!options->quiet) {
334 fprintf(stderr, "average\n");
335 result_data_print(&all_result, n);
339 result_data_clear(&all_result);
341 options_free(options);
343 return 0;