2 #include <isl/stream.h>
3 #include <isl_polynomial_private.h>
5 #include <isl/options.h>
8 struct isl_options *isl;
11 int continue_on_error;
14 ISL_ARGS_START(struct bound_options, bound_options_args)
15 ISL_ARG_CHILD(struct bound_options, isl, "isl", &isl_options_args,
17 ISL_ARG_BOOL(struct bound_options, verify, 'T', "verify", 0, NULL)
18 ISL_ARG_BOOL(struct bound_options, print_all, 'A', "print-all", 0, NULL)
19 ISL_ARG_BOOL(struct bound_options, continue_on_error, '\0', "continue-on-error", 0, NULL)
22 ISL_ARG_DEF(bound_options, struct bound_options, bound_options_args)
24 static __isl_give isl_set *set_bounds(__isl_take isl_set *set)
31 nparam = isl_set_dim(set, isl_dim_param);
32 r = nparam >= 8 ? 5 : nparam >= 5 ? 15 : 50;
34 pt = isl_set_sample_point(isl_set_copy(set));
35 pt2 = isl_point_copy(pt);
37 for (i = 0; i < nparam; ++i) {
38 pt = isl_point_add_ui(pt, isl_dim_param, i, r);
39 pt2 = isl_point_sub_ui(pt2, isl_dim_param, i, r);
42 box = isl_set_box_from_points(pt, pt2);
44 return isl_set_intersect(set, box);
47 struct verify_point_bound {
48 struct bound_options *options;
54 isl_pw_qpolynomial_fold *pwf;
55 isl_pw_qpolynomial_fold *bound;
58 static int verify_point(__isl_take isl_point *pnt, void *user)
63 struct verify_point_bound *vpb = (struct verify_point_bound *) user;
66 isl_pw_qpolynomial_fold *pwf;
67 isl_qpolynomial *bound = NULL;
68 isl_qpolynomial *opt = NULL;
75 FILE *out = vpb->options->print_all ? stdout : stderr;
87 ctx = isl_point_get_ctx(pnt);
88 p = isl_printer_to_file(ctx, out);
92 pwf = isl_pw_qpolynomial_fold_copy(vpb->pwf);
94 nparam = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_param);
95 for (i = 0; i < nparam; ++i) {
96 isl_point_get_coordinate(pnt, isl_dim_param, i, &t);
97 pwf = isl_pw_qpolynomial_fold_fix_dim(pwf, isl_dim_param, i, t);
100 bound = isl_pw_qpolynomial_fold_eval(
101 isl_pw_qpolynomial_fold_copy(vpb->bound),
102 isl_point_copy(pnt));
104 dom = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf));
105 bounded = isl_set_is_bounded(dom);
111 opt = isl_pw_qpolynomial_fold_eval(
112 isl_pw_qpolynomial_fold_copy(pwf),
113 isl_set_sample_point(isl_set_copy(dom)));
115 opt = isl_pw_qpolynomial_fold_max(isl_pw_qpolynomial_fold_copy(pwf));
117 opt = isl_pw_qpolynomial_fold_min(isl_pw_qpolynomial_fold_copy(pwf));
119 nvar = isl_set_dim(dom, isl_dim_set);
120 opt = isl_qpolynomial_project_domain_on_params(opt);
121 if (vpb->exact && bounded)
122 ok = isl_qpolynomial_plain_is_equal(opt, bound);
124 ok = isl_qpolynomial_le_cst(opt, bound);
126 ok = isl_qpolynomial_le_cst(bound, opt);
130 if (vpb->options->print_all || !ok) {
131 p = isl_printer_print_str(p, minmax);
132 p = isl_printer_print_str(p, "(");
133 for (i = 0; i < nparam; ++i) {
135 p = isl_printer_print_str(p, ", ");
136 isl_point_get_coordinate(pnt, isl_dim_param, i, &t);
137 p = isl_printer_print_isl_int(p, t);
139 p = isl_printer_print_str(p, ") = ");
140 p = isl_printer_print_qpolynomial(p, bound);
141 p = isl_printer_print_str(p, ", ");
142 p = isl_printer_print_str(p, bounded ? "opt" : "sample");
143 p = isl_printer_print_str(p, " = ");
144 p = isl_printer_print_qpolynomial(p, opt);
146 p = isl_printer_print_str(p, ". OK");
148 p = isl_printer_print_str(p, ". NOT OK");
149 p = isl_printer_end_line(p);
150 } else if ((vpb->n % vpb->stride) == 0) {
151 p = isl_printer_print_str(p, "o");
152 p = isl_printer_flush(p);
160 isl_pw_qpolynomial_fold_free(pwf);
161 isl_qpolynomial_free(bound);
162 isl_qpolynomial_free(opt);
173 if (vpb->options->continue_on_error)
176 return (vpb->n >= 1 && ok) ? 0 : -1;
179 static int check_solution(__isl_take isl_pw_qpolynomial_fold *pwf,
180 __isl_take isl_pw_qpolynomial_fold *bound, int exact,
181 struct bound_options *options)
183 struct verify_point_bound vpb;
189 dom = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf));
190 context = isl_set_params(isl_set_copy(dom));
191 context = isl_set_remove_divs(context);
192 context = set_bounds(context);
197 isl_int_set_si(max, 200);
198 r = isl_set_count_upto(context, max, &count);
200 n = isl_int_get_si(count);
203 isl_int_clear(count);
205 vpb.options = options;
209 vpb.stride = n > 70 ? 1 + (n + 1)/70 : 1;
213 if (!options->print_all) {
214 for (i = 0; i < vpb.n; i += vpb.stride)
220 isl_set_foreach_point(context, verify_point, &vpb);
222 isl_set_free(context);
224 isl_pw_qpolynomial_fold_free(pwf);
225 isl_pw_qpolynomial_fold_free(bound);
227 if (!options->print_all)
231 fprintf(stderr, "Check failed !\n");
238 int main(int argc, char **argv)
241 isl_pw_qpolynomial_fold *copy;
242 isl_pw_qpolynomial_fold *pwf;
243 struct isl_stream *s;
245 struct bound_options *options;
249 options = bound_options_new_with_defaults();
251 argc = bound_options_parse(options, argc, argv, ISL_ARG_ALL);
253 ctx = isl_ctx_alloc_with_options(&bound_options_args, options);
255 s = isl_stream_new_file(ctx, stdin);
256 obj = isl_stream_read_obj(s);
257 if (obj.type == isl_obj_pw_qpolynomial)
258 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
260 else if (obj.type == isl_obj_pw_qpolynomial_fold)
263 obj.type->free(obj.v);
264 isl_die(ctx, isl_error_invalid, "invalid input", goto error);
268 copy = isl_pw_qpolynomial_fold_copy(pwf);
270 pwf = isl_pw_qpolynomial_fold_bound(pwf, &exact);
271 pwf = isl_pw_qpolynomial_fold_coalesce(pwf);
273 if (options->verify) {
274 r = check_solution(copy, pwf, exact, options);
277 printf("# NOT exact\n");
278 isl_pw_qpolynomial_fold_print(pwf, stdout, 0);
279 fprintf(stdout, "\n");
280 isl_pw_qpolynomial_fold_free(pwf);