iscc: use simplified CLooG interface
[barvinok.git] / test_bound.c
blobc01bb86a15117e0d976bca9c5a0a6877d5b30055
1 #include <assert.h>
2 #include <limits.h>
3 #include <math.h>
4 #include <barvinok/options.h>
5 #include <barvinok/util.h>
6 #include "evalue_read.h"
7 #include "verify.h"
8 #include "config.h"
10 #ifdef HAVE_SYS_TIMES_H
12 #include <sys/times.h>
14 typedef clock_t my_clock_t;
16 static my_clock_t time_diff(struct tms *before, struct tms *after)
18 return after->tms_utime - before->tms_utime;
21 #else
23 typedef int my_clock_t;
25 struct tms {};
26 static void times(struct tms* time)
29 static my_clock_t time_diff(struct tms *before, struct tms *after)
31 return 0;
34 #endif
36 static struct {
37 int method;
38 } methods[] = {
39 { ISL_BOUND_BERNSTEIN },
40 { ISL_BOUND_RANGE },
43 #define nr_methods (sizeof(methods)/sizeof(*methods))
45 struct options {
46 struct verify_options *verify;
47 int quiet;
50 struct isl_arg options_arg[] = {
51 ISL_ARG_CHILD(struct options, verify, NULL, verify_options_arg, NULL)
52 ISL_ARG_BOOL(struct options, quiet, 'q', "quiet", 0, NULL)
53 ISL_ARG_END
56 ISL_ARG_DEF(options, struct options, options_arg)
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 int i;
80 isl_int_clear(result->n);
83 void result_data_print(struct result_data *result, int n)
85 int i;
87 fprintf(stderr, "%g", (double)result->ticks[0]/n);
88 for (i = 1; i < nr_methods; ++i)
89 fprintf(stderr, ", %g", (double)result->ticks[i]/n);
90 fprintf(stderr, "\n");
92 fprintf(stderr, "%zd/%d", result->size[0], n);
93 for (i = 1; i < nr_methods; ++i)
94 fprintf(stderr, ", %zd/%d", result->size[i], n);
95 fprintf(stderr, "\n");
97 fprintf(stderr, "%g\n", isl_int_get_d(result->n));
98 fprintf(stderr, "%g", result->RE_sum[0]/isl_int_get_d(result->n));
99 for (i = 1; i < nr_methods; ++i)
100 fprintf(stderr, ", %g", result->RE_sum[i]/isl_int_get_d(result->n));
101 fprintf(stderr, "\n");
104 struct verify_point_bound {
105 struct verify_point_data vpd;
106 struct result_data *result;
107 isl_pw_qpolynomial *pwqp;
108 isl_pw_qpolynomial_fold **pwf;
111 static int verify_point(__isl_take isl_point *pnt, void *user)
113 struct verify_point_bound *vpb = (struct verify_point_bound *) user;
114 const struct verify_options *options = vpb->vpd.options;
115 int i;
116 unsigned nparam;
117 isl_int max, min, exact, approx;
118 isl_int n, d;
119 isl_qpolynomial *opt;
120 isl_pw_qpolynomial *pwqp;
121 int cst;
123 vpb->vpd.n--;
125 isl_int_init(max);
126 isl_int_init(min);
127 isl_int_init(exact);
128 isl_int_init(approx);
129 isl_int_init(n);
130 isl_int_init(d);
132 pwqp = isl_pw_qpolynomial_copy(vpb->pwqp);
134 nparam = isl_pw_qpolynomial_dim(pwqp, isl_dim_param);
135 for (i = 0; i < nparam; ++i) {
136 isl_point_get_coordinate(pnt, isl_dim_param, i, &n);
137 pwqp = isl_pw_qpolynomial_fix_dim(pwqp, isl_dim_param, i, n);
140 opt = isl_pw_qpolynomial_max(isl_pw_qpolynomial_copy(pwqp));
141 cst = isl_qpolynomial_is_cst(opt, &n, &d);
142 isl_qpolynomial_free(opt);
143 assert(cst == 1);
144 isl_int_fdiv_q(max, n, d);
146 opt = isl_pw_qpolynomial_min(pwqp);
147 cst = isl_qpolynomial_is_cst(opt, &n, &d);
148 isl_qpolynomial_free(opt);
149 assert(cst == 1);
150 isl_int_cdiv_q(min, n, d);
152 isl_int_sub(exact, max, min);
153 isl_int_add_ui(exact, exact, 1);
155 if (options->print_all) {
156 fprintf(stderr, "max: "); isl_int_print(stderr, max, 0);
157 fprintf(stderr, ", min: "); isl_int_print(stderr, min, 0);
158 fprintf(stderr, ", range: "); isl_int_print(stderr, exact, 0);
161 for (i = 0; i < nr_methods; ++i) {
162 double error;
164 opt = isl_pw_qpolynomial_fold_eval(
165 isl_pw_qpolynomial_fold_copy(vpb->pwf[2 * i]),
166 isl_point_copy(pnt));
167 cst = isl_qpolynomial_is_cst(opt, &n, &d);
168 isl_qpolynomial_free(opt);
169 assert(cst == 1);
170 isl_int_fdiv_q(max, n, d);
172 opt = isl_pw_qpolynomial_fold_eval(
173 isl_pw_qpolynomial_fold_copy(vpb->pwf[2 * i + 1]),
174 isl_point_copy(pnt));
175 cst = isl_qpolynomial_is_cst(opt, &n, &d);
176 isl_qpolynomial_free(opt);
177 assert(cst == 1);
178 isl_int_cdiv_q(min, n, d);
180 isl_int_sub(approx, max, min);
181 isl_int_add_ui(approx, approx, 1);
182 if (options->print_all) {
183 fprintf(stderr, ", "); isl_int_print(stderr, approx, 0);
186 assert(isl_int_ge(approx, exact));
187 isl_int_sub(approx, approx, exact);
189 error = fabs(isl_int_get_d(approx)) / isl_int_get_d(exact);
190 if (options->print_all)
191 fprintf(stderr, " (%g)", error);
192 vpb->result->RE_sum[i] += error;
195 if (options->print_all) {
196 fprintf(stderr, "\n");
197 } else if ((vpb->vpd.n % vpb->vpd.s) == 0) {
198 printf("o");
199 fflush(stdout);
202 isl_int_clear(max);
203 isl_int_clear(min);
204 isl_int_clear(exact);
205 isl_int_clear(approx);
206 isl_int_clear(n);
207 isl_int_clear(d);
209 isl_point_free(pnt);
211 return 0;
214 static void test(evalue *EP, unsigned nvar,
215 __isl_keep isl_pw_qpolynomial_fold **pwf, struct result_data *result,
216 struct verify_options *options)
218 struct verify_point_bound vpb = { { options }, result };
219 isl_ctx *ctx;
220 isl_dim *dim;
221 isl_set *context;
222 int r;
223 int i;
224 unsigned nparam;
226 vpb.pwf = pwf;
227 ctx = isl_pw_qpolynomial_fold_get_ctx(pwf[0]);
228 nparam = isl_pw_qpolynomial_fold_dim(pwf[0], isl_dim_param);
229 dim = isl_dim_set_alloc(ctx, nvar + nparam, 0);
230 vpb.pwqp = isl_pw_qpolynomial_from_evalue(dim, EP);
231 vpb.pwqp = isl_pw_qpolynomial_move_dims(vpb.pwqp, isl_dim_set, 0,
232 isl_dim_param, 0, nvar);
233 context = isl_pw_qpolynomial_domain(isl_pw_qpolynomial_copy(vpb.pwqp));
234 context = isl_set_remove(context, isl_dim_set, 0, nvar);
235 context = verify_context_set_bounds(context, options);
237 r = verify_point_data_init(&vpb.vpd, context);
238 assert(r == 0);
239 isl_int_set_si(result->n, vpb.vpd.n);
241 isl_set_foreach_point(context, verify_point, &vpb);
242 assert(!vpb.vpd.error);
244 isl_set_free(context);
245 isl_pw_qpolynomial_free(vpb.pwqp);
247 verify_point_data_fini(&vpb.vpd);
250 void handle(FILE *in, struct result_data *result, struct verify_options *options)
252 int i;
253 evalue *EP, *upper, *lower;
254 const char **all_vars = NULL;
255 unsigned nvar;
256 unsigned nparam;
257 isl_ctx *ctx = isl_ctx_alloc();
258 isl_dim *dim;
259 isl_pw_qpolynomial_fold *pwf[2*nr_methods];
261 EP = evalue_read_from_file(in, NULL, &all_vars,
262 &nvar, &nparam, options->barvinok->MaxRays);
263 assert(EP);
264 if (EVALUE_IS_ZERO(*EP)) {
265 evalue_free(EP);
266 Free_ParamNames(all_vars, nvar+nparam);
267 return;
270 upper = evalue_dup(EP);
271 lower = evalue_dup(EP);
272 evalue_frac2polynomial(upper, 1, options->barvinok->MaxRays);
273 evalue_frac2polynomial(lower, -1, options->barvinok->MaxRays);
275 dim = isl_dim_set_alloc(ctx, nparam, 0);
276 for (i = 0; i < nr_methods; ++i) {
277 int j;
278 struct tms st_cpu;
279 struct tms en_cpu;
281 times(&st_cpu);
282 for (j = 0; j < 2; ++j) {
283 evalue *poly = j == 0 ? upper : lower;
284 enum isl_fold type = j == 0 ? isl_fold_max : isl_fold_min;
285 isl_dim *dim_poly;
286 isl_pw_qpolynomial *pwqp;
287 dim_poly = isl_dim_insert(isl_dim_copy(dim), isl_dim_param, 0, nvar);
288 pwqp = isl_pw_qpolynomial_from_evalue(dim_poly, poly);
289 pwqp = isl_pw_qpolynomial_move_dims(pwqp, isl_dim_set, 0,
290 isl_dim_param, 0, nvar);
291 options->barvinok->isl->bound = methods[i].method;
292 pwf[2*i+j] = isl_pw_qpolynomial_bound(pwqp, type, NULL);
294 times(&en_cpu);
295 result->ticks[i] = time_diff(&en_cpu, &st_cpu);
296 result->size[i] = isl_pw_qpolynomial_fold_size(pwf[2*i]);
297 result->size[i] = isl_pw_qpolynomial_fold_size(pwf[2*i+1]);
298 if (options->barvinok->verbose) {
299 isl_printer *p = isl_printer_to_file(ctx, stdout);
300 for (j = 0; j < 2; ++j) {
301 p = isl_printer_print_pw_qpolynomial_fold(p, pwf[2*i+j]);
302 p = isl_printer_end_line(p);
304 isl_printer_free(p);
307 isl_dim_free(dim);
308 test(EP, nvar, pwf, result, options);
310 for (i = 0; i < 2*nr_methods; ++i)
311 isl_pw_qpolynomial_fold_free(pwf[i]);
312 evalue_free(EP);
313 evalue_free(lower);
314 evalue_free(upper);
315 Free_ParamNames(all_vars, nvar+nparam);
317 isl_ctx_free(ctx);
320 int main(int argc, char **argv)
322 char path[PATH_MAX+1];
323 struct result_data all_result;
324 int n = 0;
325 struct options *options = options_new_with_defaults();
327 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
329 if (options->verify->M == INT_MIN)
330 options->verify->M = 100;
331 if (options->verify->m == INT_MAX)
332 options->verify->m = -100;
334 result_data_init(&all_result);
336 while (fgets(path, sizeof(path), stdin)) {
337 struct result_data result;
338 FILE *in;
339 int i;
341 ++n;
342 result_data_init(&result);
343 fprintf(stderr, "%s", path);
344 *strchr(path, '\n') = '\0';
345 in = fopen(path, "r");
346 assert(in);
347 handle(in, &result, options->verify);
348 fclose(in);
350 if (!options->quiet)
351 result_data_print(&result, 1);
353 isl_int_add(all_result.n, all_result.n, result.n);
354 for (i = 0; i < nr_methods; ++i) {
355 all_result.RE_sum[i] += result.RE_sum[i];
356 all_result.ticks[i] += result.ticks[i];
357 all_result.size[i] += result.size[i];
360 result_data_clear(&result);
362 if (!options->quiet) {
363 fprintf(stderr, "average\n");
364 result_data_print(&all_result, n);
368 result_data_clear(&all_result);
370 options_free(options);
372 return 0;