4 #include <barvinok/options.h>
5 #include <barvinok/util.h>
6 #include "evalue_read.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
;
23 typedef int my_clock_t
;
26 static void times(struct tms
* time
)
29 static my_clock_t
time_diff(struct tms
*before
, struct tms
*after
)
39 { ISL_BOUND_BERNSTEIN
},
43 #define nr_methods (sizeof(methods)/sizeof(*methods))
46 struct verify_options
*verify
;
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
)
56 ISL_ARG_DEF(options
, struct options
, options_arg
)
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
)
69 for (i
= 0; i
< nr_methods
; ++i
) {
70 result
->RE_sum
[i
] = 0;
74 isl_int_init(result
->n
);
77 void result_data_clear(struct result_data
*result
)
80 isl_int_clear(result
->n
);
83 void result_data_print(struct result_data
*result
, int n
)
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
;
117 isl_int max
, min
, exact
, approx
;
119 isl_qpolynomial
*opt
;
120 isl_pw_qpolynomial
*pwqp
;
128 isl_int_init(approx
);
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
);
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
);
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
) {
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
);
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
);
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) {
204 isl_int_clear(exact
);
205 isl_int_clear(approx
);
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
};
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
);
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
)
253 evalue
*EP
, *upper
, *lower
;
254 const char **all_vars
= NULL
;
257 isl_ctx
*ctx
= isl_ctx_alloc();
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
);
264 if (EVALUE_IS_ZERO(*EP
)) {
266 Free_ParamNames(all_vars
, nvar
+nparam
);
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
) {
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
;
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
);
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
);
308 test(EP
, nvar
, pwf
, result
, options
);
310 for (i
= 0; i
< 2*nr_methods
; ++i
)
311 isl_pw_qpolynomial_fold_free(pwf
[i
]);
315 Free_ParamNames(all_vars
, nvar
+nparam
);
320 int main(int argc
, char **argv
)
322 char path
[PATH_MAX
+1];
323 struct result_data all_result
;
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
;
342 result_data_init(&result
);
343 fprintf(stderr
, "%s", path
);
344 *strchr(path
, '\n') = '\0';
345 in
= fopen(path
, "r");
347 handle(in
, &result
, options
->verify
);
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
);