4 #include <isl/stream.h>
5 #include <barvinok/options.h>
6 #include <barvinok/util.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
);
211 return vpb
->vpd
.n
>= 1 ? 0 : -1;
214 static void test(__isl_keep isl_pw_qpolynomial
*pwqp
,
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
};
224 context
= isl_pw_qpolynomial_domain(isl_pw_qpolynomial_copy(vpb
.pwqp
));
225 context
= isl_set_params(context
);
226 context
= verify_context_set_bounds(context
, options
);
228 r
= verify_point_data_init(&vpb
.vpd
, context
);
230 isl_int_set_si(result
->n
, vpb
.vpd
.n
);
232 isl_set_foreach_point(context
, verify_point
, &vpb
);
233 assert(!vpb
.vpd
.error
);
235 isl_set_free(context
);
237 verify_point_data_fini(&vpb
.vpd
);
240 void handle(FILE *in
, struct result_data
*result
, struct verify_options
*options
)
243 isl_ctx
*ctx
= isl_ctx_alloc();
244 isl_pw_qpolynomial
*pwqp
, *upper
, *lower
;
245 isl_pw_qpolynomial_fold
*pwf
[2*nr_methods
];
246 struct isl_stream
*s
;
248 s
= isl_stream_new_file(ctx
, in
);
249 pwqp
= isl_stream_read_pw_qpolynomial(s
);
251 upper
= isl_pw_qpolynomial_to_polynomial(isl_pw_qpolynomial_copy(pwqp
), 1);
252 lower
= isl_pw_qpolynomial_to_polynomial(isl_pw_qpolynomial_copy(pwqp
), -1);
254 for (i
= 0; i
< nr_methods
; ++i
) {
260 for (j
= 0; j
< 2; ++j
) {
261 isl_pw_qpolynomial
*poly
= j
== 0 ? upper
: lower
;
262 enum isl_fold type
= j
== 0 ? isl_fold_max
: isl_fold_min
;
263 options
->barvinok
->isl
->bound
= methods
[i
].method
;
264 poly
= isl_pw_qpolynomial_copy(poly
);
265 pwf
[2*i
+j
] = isl_pw_qpolynomial_bound(poly
, type
, NULL
);
268 result
->ticks
[i
] = time_diff(&en_cpu
, &st_cpu
);
269 result
->size
[i
] = isl_pw_qpolynomial_fold_size(pwf
[2*i
]);
270 result
->size
[i
] = isl_pw_qpolynomial_fold_size(pwf
[2*i
+1]);
271 if (options
->barvinok
->verbose
) {
272 isl_printer
*p
= isl_printer_to_file(ctx
, stdout
);
273 for (j
= 0; j
< 2; ++j
) {
274 p
= isl_printer_print_pw_qpolynomial_fold(p
, pwf
[2*i
+j
]);
275 p
= isl_printer_end_line(p
);
280 test(pwqp
, pwf
, result
, options
);
282 for (i
= 0; i
< 2*nr_methods
; ++i
)
283 isl_pw_qpolynomial_fold_free(pwf
[i
]);
284 isl_pw_qpolynomial_free(pwqp
);
285 isl_pw_qpolynomial_free(lower
);
286 isl_pw_qpolynomial_free(upper
);
292 int main(int argc
, char **argv
)
294 char path
[PATH_MAX
+1];
295 struct result_data all_result
;
297 struct options
*options
= options_new_with_defaults();
299 argc
= options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
301 if (options
->verify
->M
== INT_MIN
)
302 options
->verify
->M
= 100;
303 if (options
->verify
->m
== INT_MAX
)
304 options
->verify
->m
= -100;
306 result_data_init(&all_result
);
308 while (fgets(path
, sizeof(path
), stdin
)) {
309 struct result_data result
;
314 result_data_init(&result
);
315 fprintf(stderr
, "%s", path
);
316 *strchr(path
, '\n') = '\0';
317 in
= fopen(path
, "r");
319 handle(in
, &result
, options
->verify
);
323 result_data_print(&result
, 1);
325 isl_int_add(all_result
.n
, all_result
.n
, result
.n
);
326 for (i
= 0; i
< nr_methods
; ++i
) {
327 all_result
.RE_sum
[i
] += result
.RE_sum
[i
];
328 all_result
.ticks
[i
] += result
.ticks
[i
];
329 all_result
.size
[i
] += result
.size
[i
];
332 result_data_clear(&result
);
334 if (!options
->quiet
) {
335 fprintf(stderr
, "average\n");
336 result_data_print(&all_result
, n
);
340 result_data_clear(&all_result
);
342 options_free(options
);