util.c: drop redundant include
[barvinok.git] / bound.c
blob90b97130e1a75e4529ae0daf7b668b1d68346cff
1 #include <assert.h>
2 #include <isl/ctx.h>
3 #include <isl/val.h>
4 #include <isl/space.h>
5 #include <isl/point.h>
6 #include <isl/set.h>
7 #include <isl/polynomial.h>
8 #include <isl/stream.h>
9 #include <isl/printer.h>
10 #include <barvinok/options.h>
11 #include <barvinok/util.h>
12 #include <barvinok/isl.h>
13 #include "verify.h"
15 struct options {
16 struct verify_options *verify;
17 long split;
18 int lower;
19 long iterate;
22 ISL_ARGS_START(struct options, options_args)
23 ISL_ARG_CHILD(struct options, verify, NULL,
24 &verify_options_args, "verification")
25 ISL_ARG_LONG(struct options, split, 0, "split", 0, NULL)
26 ISL_ARG_OPT_LONG(struct options, iterate, 0, "iterate", 0, -1,
27 "exact result by iterating over domain (of specified maximal size)")
28 ISL_ARG_BOOL(struct options, lower, 0, "lower", 0,
29 "compute lower bound instead of upper bound")
30 ISL_ARGS_END
32 ISL_ARG_DEF(options, struct options, options_args)
34 struct verify_point_bound {
35 struct verify_point_data vpd;
36 isl_pw_qpolynomial *pwqp;
37 isl_pw_qpolynomial_fold *pwf;
38 enum isl_fold type;
41 static isl_stat verify_point(__isl_take isl_point *pnt, void *user)
43 int i;
44 unsigned nparam;
45 struct verify_point_bound *vpb = (struct verify_point_bound *) user;
46 isl_val *v, *b, *q, *t;
47 isl_pw_qpolynomial *pwqp;
48 const char *minmax;
49 int sign;
50 isl_bool ok;
51 FILE *out = vpb->vpd.options->print_all ? stdout : stderr;
53 vpb->vpd.n--;
55 if (vpb->type == isl_fold_max) {
56 minmax = "max";
57 sign = 1;
58 } else {
59 minmax = "min";
60 sign = -1;
63 pwqp = isl_pw_qpolynomial_copy(vpb->pwqp);
65 nparam = isl_pw_qpolynomial_dim(pwqp, isl_dim_param);
66 for (i = 0; i < nparam; ++i) {
67 t = isl_point_get_coordinate_val(pnt, isl_dim_param, i);
68 pwqp = isl_pw_qpolynomial_fix_val(pwqp, isl_dim_param, i, t);
71 b = isl_pw_qpolynomial_fold_eval(isl_pw_qpolynomial_fold_copy(vpb->pwf),
72 isl_point_copy(pnt));
74 if (sign > 0)
75 v = isl_pw_qpolynomial_max(pwqp);
76 else
77 v = isl_pw_qpolynomial_min(pwqp);
79 if (sign > 0)
80 v = isl_val_floor(v);
81 else
82 v = isl_val_ceil(v);
84 q = isl_val_copy(b);
85 if (sign > 0)
86 b = isl_val_floor(b);
87 else
88 b = isl_val_ceil(b);
90 if (sign > 0)
91 ok = isl_val_ge(b, v);
92 else
93 ok = isl_val_le(b, v);
95 if (vpb->vpd.options->print_all || !ok) {
96 isl_ctx *ctx = isl_point_get_ctx(pnt);
97 isl_printer *p;
98 p = isl_printer_to_file(ctx, out);
99 p = isl_printer_print_str(p, minmax);
100 p = isl_printer_print_str(p, "(");
101 for (i = 0; i < nparam; ++i) {
102 if (i)
103 p = isl_printer_print_str(p, ", ");
104 t = isl_point_get_coordinate_val(pnt, isl_dim_param, i);
105 p = isl_printer_print_val(p, t);
106 isl_val_free(t);
108 p = isl_printer_print_str(p, ") = ");
109 p = isl_printer_print_val(p, q);
110 if (!isl_val_is_int(q)) {
111 p = isl_printer_print_str(p, " (");
112 p = isl_printer_print_val(p, b);
113 p = isl_printer_print_str(p, ")");
115 p = isl_printer_print_str(p, ", ");
116 p = isl_printer_print_str(p, minmax);
117 p = isl_printer_print_str(p, "(EP) = ");
118 p = isl_printer_print_val(p, v);
119 if (ok)
120 p = isl_printer_print_str(p, ". OK");
121 else
122 p = isl_printer_print_str(p, ". NOT OK");
123 p = isl_printer_end_line(p);
124 isl_printer_free(p);
125 } else if ((vpb->vpd.n % vpb->vpd.s) == 0) {
126 printf("o");
127 fflush(stdout);
130 isl_point_free(pnt);
132 isl_val_free(b);
133 isl_val_free(q);
134 isl_val_free(v);
136 if (ok < 0 || !ok)
137 vpb->vpd.error = 1;
139 if (vpb->vpd.options->continue_on_error)
140 ok = isl_bool_true;
142 if (vpb->vpd.n < 1)
143 return isl_stat_error;
144 if (ok != isl_bool_true)
145 return isl_stat_error;
146 return isl_stat_ok;
149 static int verify(__isl_keep isl_pw_qpolynomial_fold *pwf,
150 __isl_take isl_pw_qpolynomial *pwqp, enum isl_fold type,
151 struct verify_options *options)
153 struct verify_point_bound vpb = { { options } };
154 isl_set *context;
155 int r;
157 vpb.pwf = pwf;
158 vpb.type = type;
159 vpb.pwqp = pwqp;
160 context = isl_pw_qpolynomial_fold_domain(
161 isl_pw_qpolynomial_fold_copy(vpb.pwf));
162 context = verify_context_set_bounds(context, options);
164 r = verify_point_data_init(&vpb.vpd, context);
166 if (r == 0)
167 isl_set_foreach_point(context, verify_point, &vpb);
168 if (vpb.vpd.error)
169 r = -1;
171 isl_set_free(context);
172 isl_pw_qpolynomial_free(pwqp);
174 verify_point_data_fini(&vpb.vpd);
176 return r;
179 static __isl_give isl_pw_qpolynomial_fold *iterate(
180 __isl_take isl_pw_qpolynomial *pwqp, enum isl_fold type)
182 isl_space *dim = isl_pw_qpolynomial_get_space(pwqp);
183 isl_set *set;
184 isl_val *v;
185 isl_qpolynomial *qp;
186 isl_qpolynomial_fold *fold;
187 unsigned nvar;
189 assert(isl_space_dim(dim, isl_dim_param) == 0);
190 nvar = isl_space_dim(dim, isl_dim_set);
192 if (type == isl_fold_min)
193 v = isl_pw_qpolynomial_min(pwqp);
194 else
195 v = isl_pw_qpolynomial_max(pwqp);
197 dim = isl_space_drop_dims(dim, isl_dim_set, 0, nvar);
198 qp = isl_qpolynomial_val_on_domain(isl_space_copy(dim), v);
199 fold = isl_qpolynomial_fold_alloc(type, qp);
200 set = isl_set_universe(dim);
202 return isl_pw_qpolynomial_fold_alloc(type, set, fold);
205 struct bv_split_data {
206 int size;
207 __isl_give isl_pw_qpolynomial *pwqp_less;
208 __isl_give isl_pw_qpolynomial *pwqp_more;
211 static isl_stat split_on_size(__isl_take isl_set *set,
212 __isl_take isl_qpolynomial *qp, void *user)
214 struct bv_split_data *data = (struct bv_split_data *)user;
215 int bounded;
216 isl_set *set_np;
217 isl_pw_qpolynomial *pwqp;
218 int nparam;
220 nparam = isl_set_dim(set, isl_dim_param);
221 set_np = isl_set_move_dims(isl_set_copy(set), isl_dim_set, 0,
222 isl_dim_param, 0, nparam);
223 bounded = isl_set_is_bounded(set_np);
224 assert(bounded >= 0);
225 if (bounded) {
226 isl_pw_qpolynomial *pwqp;
227 isl_val *m;
229 pwqp = isl_set_card(set_np);
230 m = isl_pw_qpolynomial_max(pwqp);
231 bounded = isl_val_cmp_si(m, data->size) <= 0;
232 isl_val_free(m);
233 } else
234 isl_set_free(set_np);
236 pwqp = isl_pw_qpolynomial_alloc(set, qp);
237 if (bounded)
238 data->pwqp_less = isl_pw_qpolynomial_add_disjoint(
239 data->pwqp_less, pwqp);
240 else
241 data->pwqp_more = isl_pw_qpolynomial_add_disjoint(
242 data->pwqp_more, pwqp);
244 return isl_stat_ok;
248 * Split (partition) pwpq into a partition with (sub)domains containing
249 * size integer points or less and a partition with (sub)domains
250 * containing more integer points.
252 static int split_on_domain_size(__isl_take isl_pw_qpolynomial *pwqp,
253 __isl_give isl_pw_qpolynomial **pwqp_less,
254 __isl_give isl_pw_qpolynomial **pwqp_more,
255 int size)
257 isl_space *dim;
258 struct bv_split_data data = { size };
259 int r;
261 dim = isl_pw_qpolynomial_get_space(pwqp);
262 data.pwqp_less = isl_pw_qpolynomial_zero(isl_space_copy(dim));
263 data.pwqp_more = isl_pw_qpolynomial_zero(dim);
264 r = isl_pw_qpolynomial_foreach_piece(pwqp, &split_on_size, &data);
265 *pwqp_less = data.pwqp_less;
266 *pwqp_more = data.pwqp_more;
267 isl_pw_qpolynomial_free(pwqp);
269 return r;
272 static __isl_give isl_pw_qpolynomial_fold *optimize(
273 __isl_take isl_pw_qpolynomial *pwqp, enum isl_fold type,
274 struct options *options)
276 isl_pw_qpolynomial_fold *pwf;
278 if (options->iterate > 0) {
279 isl_pw_qpolynomial *pwqp_less, *pwqp_more;
280 isl_pw_qpolynomial_fold *pwf_less, *pwf_more;
281 split_on_domain_size(pwqp, &pwqp_less, &pwqp_more, options->iterate);
282 pwf_less = iterate(pwqp_less, type);
283 pwf_more = isl_pw_qpolynomial_bound(pwqp_more, type, NULL);
284 pwf = isl_pw_qpolynomial_fold_fold(pwf_less, pwf_more);
285 } else if (options->iterate)
286 pwf = iterate(pwqp, type);
287 else
288 pwf = isl_pw_qpolynomial_bound(pwqp, type, NULL);
289 return pwf;
292 static int optimize_and_print(__isl_take isl_pw_qpolynomial *pwqp,
293 struct options *options)
295 int print_solution = 1;
296 int result = 0;
297 isl_pw_qpolynomial_fold *pwf;
298 enum isl_fold type = options->lower ? isl_fold_min : isl_fold_max;
300 if (options->verify->verify) {
301 isl_space *dim = isl_pw_qpolynomial_get_space(pwqp);
302 unsigned total = isl_space_dim(dim, isl_dim_all);
303 isl_space_free(dim);
304 verify_options_set_range(options->verify, total);
305 if (!options->verify->barvinok->verbose)
306 print_solution = 0;
309 pwf = optimize(isl_pw_qpolynomial_copy(pwqp), type, options);
310 assert(pwf);
311 if (print_solution) {
312 isl_ctx *ctx = isl_pw_qpolynomial_get_ctx(pwqp);
313 isl_printer *p = isl_printer_to_file(ctx, stdout);
314 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
315 p = isl_printer_end_line(p);
316 isl_printer_free(p);
318 if (options->verify->verify) {
319 enum isl_fold type = options->lower ? isl_fold_min : isl_fold_max;
320 result = verify(pwf, pwqp, type, options->verify);
321 } else
322 isl_pw_qpolynomial_free(pwqp);
323 isl_pw_qpolynomial_fold_free(pwf);
325 return result;
328 int main(int argc, char **argv)
330 struct options *options = options_new_with_defaults();
331 isl_ctx *ctx;
332 isl_pw_qpolynomial *pwqp;
333 struct isl_stream *s;
334 int result = 0;
336 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
337 ctx = isl_ctx_alloc_with_options(&options_args, options);
339 s = isl_stream_new_file(ctx, stdin);
340 pwqp = isl_stream_read_pw_qpolynomial(s);
342 if (options->split)
343 pwqp = isl_pw_qpolynomial_split_periods(pwqp, options->split);
345 result = optimize_and_print(pwqp, options);
347 isl_stream_free(s);
348 isl_ctx_free(ctx);
349 return result;