isl_map_card: reuse barvinok_options if available in context
[barvinok.git] / bound.cc
blob0e41cf41c887188f8270189c9ee85ada56c6b5da
1 #include <assert.h>
2 #include <iostream>
3 #include <barvinok/evalue.h>
4 #include <barvinok/options.h>
5 #include <barvinok/util.h>
6 #include <barvinok/barvinok.h>
7 #include "bound_options.h"
8 #include "evalue_convert.h"
9 #include "evalue_read.h"
10 #include "verify.h"
12 using std::cout;
13 using std::cerr;
14 using std::endl;
16 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
18 struct verify_point_bound {
19 struct verify_point_data vpd;
20 isl_pw_qpolynomial *pwqp;
21 isl_pw_qpolynomial_fold *pwf;
22 enum isl_fold type;
25 static int verify_point(__isl_take isl_point *pnt, void *user)
27 int i;
28 unsigned nparam;
29 struct verify_point_bound *vpb = (struct verify_point_bound *) user;
30 isl_int v, n, d, b, t;
31 isl_pw_qpolynomial *pwqp;
32 isl_qpolynomial *bound;
33 isl_qpolynomial *opt;
34 const char *minmax;
35 int sign;
36 int ok;
37 int cst;
38 FILE *out = vpb->vpd.options->print_all ? stdout : stderr;
40 vpb->vpd.n--;
42 if (vpb->type == isl_fold_max) {
43 minmax = "max";
44 sign = 1;
45 } else {
46 minmax = "min";
47 sign = -1;
50 isl_int_init(b);
51 isl_int_init(t);
52 isl_int_init(v);
53 isl_int_init(n);
54 isl_int_init(d);
56 pwqp = isl_pw_qpolynomial_copy(vpb->pwqp);
58 nparam = isl_pw_qpolynomial_dim(pwqp, isl_dim_param);
59 for (i = 0; i < nparam; ++i) {
60 isl_point_get_coordinate(pnt, isl_dim_param, i, &v);
61 pwqp = isl_pw_qpolynomial_fix_dim(pwqp, isl_dim_param, i, v);
64 bound = isl_pw_qpolynomial_fold_eval(isl_pw_qpolynomial_fold_copy(vpb->pwf),
65 isl_point_copy(pnt));
67 if (sign > 0)
68 opt = isl_pw_qpolynomial_max(pwqp);
69 else
70 opt = isl_pw_qpolynomial_min(pwqp);
72 cst = isl_qpolynomial_is_cst(opt, &n, &d);
73 if (cst != 1)
74 goto error;
75 if (sign > 0)
76 isl_int_fdiv_q(v, n, d);
77 else
78 isl_int_cdiv_q(v, n, d);
80 cst = isl_qpolynomial_is_cst(bound, &n, &d);
81 if (cst != 1)
82 goto error;
83 if (sign > 0)
84 isl_int_fdiv_q(b, n, d);
85 else
86 isl_int_cdiv_q(b, n, d);
88 if (sign > 0)
89 ok = value_ge(b, v);
90 else
91 ok = value_le(b, v);
93 if (vpb->vpd.options->print_all || !ok) {
94 fprintf(out, "%s(", minmax);
95 for (i = 0; i < nparam; ++i) {
96 if (i)
97 fprintf(out, ", ");
98 isl_point_get_coordinate(pnt, isl_dim_param, i, &t);
99 isl_int_print(out, t, 0);
101 fprintf(out, ") = ");
102 isl_int_print(out, n, 0);
103 if (!isl_int_is_one(d)) {
104 fprintf(out, "/");
105 isl_int_print(out, d, 0);
106 fprintf(out, " (");
107 isl_int_print(out, b, 0);
108 fprintf(out, ")");
110 fprintf(out, ", %s(EP) = ", minmax);
111 isl_int_print(out, v, 0);
112 if (ok)
113 fprintf(out, ". OK\n");
114 else
115 fprintf(out, ". NOT OK\n");
116 } else if ((vpb->vpd.n % vpb->vpd.s) == 0) {
117 printf("o");
118 fflush(stdout);
121 if (0) {
122 error:
123 ok = 0;
126 isl_qpolynomial_free(bound);
127 isl_qpolynomial_free(opt);
128 isl_point_free(pnt);
130 isl_int_clear(t);
131 isl_int_clear(d);
132 isl_int_clear(n);
133 isl_int_clear(v);
134 isl_int_clear(b);
136 if (!ok)
137 vpb->vpd.error = 1;
139 if (vpb->vpd.options->continue_on_error)
140 ok = 1;
142 return (vpb->vpd.n >= 1 && ok) ? 0 : -1;
145 static int verify(__isl_keep isl_pw_qpolynomial_fold *pwf, evalue *EP, unsigned nvar,
146 enum isl_fold type, struct verify_options *options)
148 struct verify_point_bound vpb = { { options } };
149 isl_ctx *ctx;
150 isl_dim *dim;
151 isl_set *context;
152 int r;
153 unsigned nparam;
155 ctx = isl_pw_qpolynomial_fold_get_ctx(pwf);
156 nparam = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_param);
157 vpb.pwf = pwf;
158 dim = isl_dim_set_alloc(ctx, nvar + nparam, 0);
159 vpb.type = type;
160 vpb.pwqp = isl_pw_qpolynomial_from_evalue(dim, EP);
161 vpb.pwqp = isl_pw_qpolynomial_move_dims(vpb.pwqp, isl_dim_set, 0,
162 isl_dim_param, 0, nvar);
163 context = isl_pw_qpolynomial_fold_domain(
164 isl_pw_qpolynomial_fold_copy(vpb.pwf));
165 context = verify_context_set_bounds(context, options);
167 r = verify_point_data_init(&vpb.vpd, context);
169 if (r == 0)
170 isl_set_foreach_point(context, verify_point, &vpb);
171 if (vpb.vpd.error)
172 r = -1;
174 isl_set_free(context);
175 isl_pw_qpolynomial_free(vpb.pwqp);
177 verify_point_data_fini(&vpb.vpd);
179 return r;
182 static __isl_give isl_pw_qpolynomial_fold *iterate(
183 __isl_take isl_pw_qpolynomial *pwqp, enum isl_fold type)
185 isl_dim *dim = isl_pw_qpolynomial_get_dim(pwqp);
186 isl_set *set;
187 isl_qpolynomial *qp;
188 isl_qpolynomial_fold *fold;
189 unsigned nvar;
191 assert(isl_dim_size(dim, isl_dim_param) == 0);
192 nvar = isl_dim_size(dim, isl_dim_set);
194 if (type == isl_fold_min)
195 qp = isl_pw_qpolynomial_min(pwqp);
196 else
197 qp = isl_pw_qpolynomial_max(pwqp);
199 qp = isl_qpolynomial_drop_dims(qp, isl_dim_set, 0, nvar);
200 fold = isl_qpolynomial_fold_alloc(type, qp);
201 dim = isl_dim_drop(dim, isl_dim_set, 0, nvar);
202 set = isl_set_universe(dim);
204 return isl_pw_qpolynomial_fold_alloc(type, set, fold);
208 * Split (partition) EP into a partition with (sub)domains containing
209 * size integer points or less and a partition with (sub)domains
210 * containing more integer points.
212 static void split_on_domain_size(evalue *EP, evalue **EP_less, evalue **EP_more,
213 int size, barvinok_options *options)
215 assert(value_zero_p(EP->d));
216 assert(EP->x.p->type == partition);
217 assert(EP->x.p->size >= 2);
219 struct evalue_section *s_less = new evalue_section[EP->x.p->size/2];
220 struct evalue_section *s_more = new evalue_section[EP->x.p->size/2];
222 int n_less = 0;
223 int n_more = 0;
225 Value c;
226 value_init(c);
228 for (int i = 0; i < EP->x.p->size/2; ++i) {
229 Polyhedron *D = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
230 Polyhedron *D_less = NULL;
231 Polyhedron *D_more = NULL;
232 Polyhedron **next_less = &D_less;
233 Polyhedron **next_more = &D_more;
235 for (Polyhedron *P = D; P; P = P->next) {
236 Polyhedron *next = P->next;
237 P->next = NULL;
238 barvinok_count_with_options(P, &c, options);
239 P->next = next;
241 if (value_zero_p(c))
242 continue;
244 if (value_pos_p(c) && value_cmp_si(c, size) <= 0) {
245 *next_less = Polyhedron_Copy(P);
246 next_less = &(*next_less)->next;
247 } else {
248 *next_more = Polyhedron_Copy(P);
249 next_more = &(*next_more)->next;
253 if (D_less) {
254 s_less[n_less].D = D_less;
255 s_less[n_less].E = evalue_dup(&EP->x.p->arr[2*i+1]);
256 n_less++;
257 } else {
258 s_more[n_more].D = D_more;
259 s_more[n_more].E = evalue_dup(&EP->x.p->arr[2*i+1]);
260 n_more++;
264 value_clear(c);
266 *EP_less = evalue_from_section_array(s_less, n_less);
267 *EP_more = evalue_from_section_array(s_more, n_more);
269 delete [] s_less;
270 delete [] s_more;
273 static __isl_give isl_pw_qpolynomial_fold *optimize(evalue *EP, unsigned nvar,
274 Polyhedron *C, __isl_take isl_dim *dim,
275 struct options *options)
277 isl_pw_qpolynomial_fold *pwf;
278 if (options->iterate > 0) {
279 evalue *EP_less = NULL;
280 evalue *EP_more = NULL;
281 isl_pw_qpolynomial_fold *pwf = NULL, *pwf_more = NULL;
283 split_on_domain_size(EP, &EP_less, &EP_more, options->iterate,
284 options->verify->barvinok);
285 if (!EVALUE_IS_ZERO(*EP_less)) {
286 options->iterate = -1;
287 pwf = optimize(EP_less, nvar, C, isl_dim_copy(dim), options);
289 if (!EVALUE_IS_ZERO(*EP_more)) {
290 options->iterate = 0;
291 pwf_more = optimize(EP_more, nvar, C, isl_dim_copy(dim), options);
293 isl_dim_free(dim);
294 evalue_free(EP_less);
295 evalue_free(EP_more);
296 if (!pwf)
297 return pwf_more;
298 if (!pwf_more)
299 return pwf;
300 return isl_pw_qpolynomial_fold_fold(pwf, pwf_more);
302 isl_dim *dim_EP;
303 isl_pw_qpolynomial *pwqp;
304 enum isl_fold type = options->lower ? isl_fold_min : isl_fold_max;
305 dim_EP = isl_dim_insert(dim, isl_dim_param, 0, nvar);
306 pwqp = isl_pw_qpolynomial_from_evalue(dim_EP, EP);
307 pwqp = isl_pw_qpolynomial_move_dims(pwqp, isl_dim_set, 0, isl_dim_param, 0, nvar);
308 if (options->iterate)
309 pwf = iterate(pwqp, type);
310 else
311 pwf = isl_pw_qpolynomial_bound(pwqp, type, NULL);
312 return pwf;
315 static int optimize(evalue *EP, const char **all_vars,
316 unsigned nvar, unsigned nparam, struct options *options)
318 Polyhedron *U;
319 U = Universe_Polyhedron(nparam);
320 int print_solution = 1;
321 int result = 0;
322 isl_ctx *ctx = isl_ctx_alloc_with_options(options_arg, options);
323 isl_dim *dim;
324 isl_pw_qpolynomial_fold *pwf;
326 dim = isl_dim_set_alloc(ctx, nparam, 0);
327 for (int i = 0; i < nparam; ++i)
328 dim = isl_dim_set_name(dim, isl_dim_param, i, all_vars[nvar + i]);
330 if (options->verify->verify) {
331 verify_options_set_range(options->verify, nvar+nparam);
332 if (!options->verify->barvinok->verbose)
333 print_solution = 0;
336 pwf = optimize(EP, nvar, U, dim, options);
337 assert(pwf);
338 if (print_solution) {
339 isl_printer *p = isl_printer_to_file(ctx, stdout);
340 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
341 p = isl_printer_end_line(p);
342 isl_printer_free(p);
344 if (options->verify->verify) {
345 enum isl_fold type = options->lower ? isl_fold_min : isl_fold_max;
346 result = verify(pwf, EP, nvar, type, options->verify);
348 isl_pw_qpolynomial_fold_free(pwf);
350 Polyhedron_Free(U);
352 isl_ctx_free(ctx);
354 return result;
357 int main(int argc, char **argv)
359 evalue *EP;
360 const char **all_vars = NULL;
361 unsigned nvar;
362 unsigned nparam;
363 struct options *options = options_new_with_defaults();
364 int result = 0;
366 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
368 EP = evalue_read_from_file(stdin, options->var_list, &all_vars,
369 &nvar, &nparam, options->verify->barvinok->MaxRays);
370 assert(EP);
372 if (options->split)
373 evalue_split_periods(EP, options->split, options->verify->barvinok->MaxRays);
375 evalue_convert(EP, options->convert, options->verify->barvinok->verbose,
376 nvar+nparam, all_vars);
378 if (EVALUE_IS_ZERO(*EP))
379 print_evalue(stdout, EP, all_vars);
380 else
381 result = optimize(EP, all_vars, nvar, nparam, options);
383 evalue_free(EP);
385 Free_ParamNames(all_vars, nvar+nparam);
386 return result;