isl_sample.c: plug_in: add memory management annotations
[isl.git] / bound.c
blob00b643d8fa09503cca0a6ea1250c6819c03e4825
1 #include <assert.h>
2 #include <isl/stream.h>
3 #include <isl_map_private.h>
4 #include <isl_polynomial_private.h>
5 #include <isl_scan.h>
6 #include <isl/options.h>
7 #include <isl/deprecated/point_int.h>
8 #include <isl/deprecated/polynomial_int.h>
10 struct bound_options {
11 struct isl_options *isl;
12 unsigned verify;
13 int print_all;
14 int continue_on_error;
17 ISL_ARGS_START(struct bound_options, bound_options_args)
18 ISL_ARG_CHILD(struct bound_options, isl, "isl", &isl_options_args,
19 "isl options")
20 ISL_ARG_BOOL(struct bound_options, verify, 'T', "verify", 0, NULL)
21 ISL_ARG_BOOL(struct bound_options, print_all, 'A', "print-all", 0, NULL)
22 ISL_ARG_BOOL(struct bound_options, continue_on_error, '\0', "continue-on-error", 0, NULL)
23 ISL_ARGS_END
25 ISL_ARG_DEF(bound_options, struct bound_options, bound_options_args)
27 static __isl_give isl_set *set_bounds(__isl_take isl_set *set)
29 unsigned nparam;
30 int i, r;
31 isl_point *pt, *pt2;
32 isl_set *box;
34 nparam = isl_set_dim(set, isl_dim_param);
35 r = nparam >= 8 ? 5 : nparam >= 5 ? 15 : 50;
37 pt = isl_set_sample_point(isl_set_copy(set));
38 pt2 = isl_point_copy(pt);
40 for (i = 0; i < nparam; ++i) {
41 pt = isl_point_add_ui(pt, isl_dim_param, i, r);
42 pt2 = isl_point_sub_ui(pt2, isl_dim_param, i, r);
45 box = isl_set_box_from_points(pt, pt2);
47 return isl_set_intersect(set, box);
50 struct verify_point_bound {
51 struct bound_options *options;
52 int stride;
53 int n;
54 int exact;
55 int error;
57 isl_pw_qpolynomial_fold *pwf;
58 isl_pw_qpolynomial_fold *bound;
61 static isl_stat verify_point(__isl_take isl_point *pnt, void *user)
63 int i;
64 unsigned nparam;
65 struct verify_point_bound *vpb = (struct verify_point_bound *) user;
66 isl_int t;
67 isl_ctx *ctx;
68 isl_pw_qpolynomial_fold *pwf;
69 isl_val *bound = NULL;
70 isl_val *opt = NULL;
71 isl_set *dom = NULL;
72 isl_printer *p;
73 const char *minmax;
74 isl_bool bounded;
75 int sign;
76 int ok;
77 FILE *out = vpb->options->print_all ? stdout : stderr;
79 vpb->n--;
81 if (1) {
82 minmax = "ub";
83 sign = 1;
84 } else {
85 minmax = "lb";
86 sign = -1;
89 ctx = isl_point_get_ctx(pnt);
90 p = isl_printer_to_file(ctx, out);
92 isl_int_init(t);
94 pwf = isl_pw_qpolynomial_fold_copy(vpb->pwf);
96 nparam = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_param);
97 for (i = 0; i < nparam; ++i) {
98 isl_point_get_coordinate(pnt, isl_dim_param, i, &t);
99 pwf = isl_pw_qpolynomial_fold_fix_dim(pwf, isl_dim_param, i, t);
102 bound = isl_pw_qpolynomial_fold_eval(
103 isl_pw_qpolynomial_fold_copy(vpb->bound),
104 isl_point_copy(pnt));
106 dom = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf));
107 bounded = isl_set_is_bounded(dom);
109 if (bounded < 0)
110 goto error;
112 if (!bounded)
113 opt = isl_pw_qpolynomial_fold_eval(
114 isl_pw_qpolynomial_fold_copy(pwf),
115 isl_set_sample_point(isl_set_copy(dom)));
116 else if (sign > 0)
117 opt = isl_pw_qpolynomial_fold_max(isl_pw_qpolynomial_fold_copy(pwf));
118 else
119 opt = isl_pw_qpolynomial_fold_min(isl_pw_qpolynomial_fold_copy(pwf));
121 if (vpb->exact && bounded)
122 ok = isl_val_eq(opt, bound);
123 else if (sign > 0)
124 ok = isl_val_le(opt, bound);
125 else
126 ok = isl_val_le(bound, opt);
127 if (ok < 0)
128 goto error;
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) {
134 if (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_val(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_val(p, opt);
145 if (ok)
146 p = isl_printer_print_str(p, ". OK");
147 else
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);
155 if (0) {
156 error:
157 ok = 0;
160 isl_pw_qpolynomial_fold_free(pwf);
161 isl_val_free(bound);
162 isl_val_free(opt);
163 isl_point_free(pnt);
164 isl_set_free(dom);
166 isl_int_clear(t);
168 isl_printer_free(p);
170 if (!ok)
171 vpb->error = 1;
173 if (vpb->options->continue_on_error)
174 ok = 1;
176 return (vpb->n >= 1 && ok) ? isl_stat_ok : isl_stat_error;
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;
184 isl_int count, max;
185 isl_set *dom;
186 isl_set *context;
187 int i, r, n;
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);
194 isl_int_init(count);
195 isl_int_init(max);
197 isl_int_set_si(max, 200);
198 r = isl_set_count_upto(context, max, &count);
199 assert(r >= 0);
200 n = isl_int_get_si(count);
202 isl_int_clear(max);
203 isl_int_clear(count);
205 vpb.options = options;
206 vpb.pwf = pwf;
207 vpb.bound = bound;
208 vpb.n = n;
209 vpb.stride = n > 70 ? 1 + (n + 1)/70 : 1;
210 vpb.error = 0;
211 vpb.exact = exact;
213 if (!options->print_all) {
214 for (i = 0; i < vpb.n; i += vpb.stride)
215 printf(".");
216 printf("\r");
217 fflush(stdout);
220 isl_set_foreach_point(context, verify_point, &vpb);
222 isl_set_free(context);
223 isl_set_free(dom);
224 isl_pw_qpolynomial_fold_free(pwf);
225 isl_pw_qpolynomial_fold_free(bound);
227 if (!options->print_all)
228 printf("\n");
230 if (vpb.error) {
231 fprintf(stderr, "Check failed !\n");
232 return -1;
235 return 0;
238 int main(int argc, char **argv)
240 isl_ctx *ctx;
241 isl_pw_qpolynomial_fold *copy;
242 isl_pw_qpolynomial_fold *pwf;
243 isl_stream *s;
244 struct isl_obj obj;
245 struct bound_options *options;
246 int exact;
247 int r = 0;
249 options = bound_options_new_with_defaults();
250 assert(options);
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,
259 obj.v);
260 else if (obj.type == isl_obj_pw_qpolynomial_fold)
261 pwf = obj.v;
262 else {
263 obj.type->free(obj.v);
264 isl_die(ctx, isl_error_invalid, "invalid input", goto error);
267 if (options->verify)
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);
275 } else {
276 if (!exact)
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);
283 error:
284 isl_stream_free(s);
286 isl_ctx_free(ctx);
288 return r;