bound.c: verify_point: make sure spaces of compared qpolynomials are the same
[platform/upstream/isl.git] / bound.c
1 #include <assert.h>
2 #include <isl/stream.h>
3 #include <isl_polynomial_private.h>
4 #include <isl_scan.h>
5
6 struct bound_options {
7         struct isl_options      *isl;
8         unsigned                 verify;
9         int                      print_all;
10         int                      continue_on_error;
11 };
12
13 struct isl_arg bound_options_arg[] = {
14 ISL_ARG_CHILD(struct bound_options, isl, "isl", isl_options_arg, "isl options")
15 ISL_ARG_BOOL(struct bound_options, verify, 'T', "verify", 0, NULL)
16 ISL_ARG_BOOL(struct bound_options, print_all, 'A', "print-all", 0, NULL)
17 ISL_ARG_BOOL(struct bound_options, continue_on_error, '\0', "continue-on-error", 0, NULL)
18 ISL_ARG_END
19 };
20
21 ISL_ARG_DEF(bound_options, struct bound_options, bound_options_arg)
22
23 static __isl_give isl_set *set_bounds(__isl_take isl_set *set)
24 {
25         unsigned nparam;
26         int i, r;
27         isl_point *pt, *pt2;
28         isl_set *box;
29
30         nparam = isl_set_dim(set, isl_dim_param);
31         r = nparam >= 8 ? 5 : nparam >= 5 ? 15 : 50;
32
33         pt = isl_set_sample_point(isl_set_copy(set));
34         pt2 = isl_point_copy(pt);
35
36         for (i = 0; i < nparam; ++i) {
37                 pt = isl_point_add_ui(pt, isl_dim_param, i, r);
38                 pt2 = isl_point_sub_ui(pt2, isl_dim_param, i, r);
39         }
40
41         box = isl_set_box_from_points(pt, pt2);
42
43         return isl_set_intersect(set, box);
44 }
45
46 struct verify_point_bound {
47         struct bound_options *options;
48         int stride;
49         int n;
50         int exact;
51         int error;
52
53         isl_pw_qpolynomial_fold *pwf;
54         isl_pw_qpolynomial_fold *bound;
55 };
56
57 static int verify_point(__isl_take isl_point *pnt, void *user)
58 {
59         int i;
60         unsigned nvar;
61         unsigned nparam;
62         struct verify_point_bound *vpb = (struct verify_point_bound *) user;
63         isl_int t;
64         isl_pw_qpolynomial_fold *pwf;
65         isl_qpolynomial *bound = NULL;
66         isl_qpolynomial *opt = NULL;
67         isl_set *dom = NULL;
68         const char *minmax;
69         int bounded;
70         int sign;
71         int ok;
72         int cst;
73         FILE *out = vpb->options->print_all ? stdout : stderr;
74
75         vpb->n--;
76
77         if (1) {
78                 minmax = "ub";
79                 sign = 1;
80         } else {
81                 minmax = "lb";
82                 sign = -1;
83         }
84
85         isl_int_init(t);
86
87         pwf = isl_pw_qpolynomial_fold_copy(vpb->pwf);
88
89         nparam = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_param);
90         for (i = 0; i < nparam; ++i) {
91                 isl_point_get_coordinate(pnt, isl_dim_param, i, &t);
92                 pwf = isl_pw_qpolynomial_fold_fix_dim(pwf, isl_dim_param, i, t);
93         }
94
95         bound = isl_pw_qpolynomial_fold_eval(
96                                     isl_pw_qpolynomial_fold_copy(vpb->bound),
97                                     isl_point_copy(pnt));
98
99         dom = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf));
100         bounded = isl_set_is_bounded(dom);
101
102         if (bounded < 0)
103                 goto error;
104
105         if (!bounded)
106                 opt = isl_pw_qpolynomial_fold_eval(
107                                     isl_pw_qpolynomial_fold_copy(pwf),
108                                     isl_set_sample_point(isl_set_copy(dom)));
109         else if (sign > 0)
110                 opt = isl_pw_qpolynomial_fold_max(isl_pw_qpolynomial_fold_copy(pwf));
111         else
112                 opt = isl_pw_qpolynomial_fold_min(isl_pw_qpolynomial_fold_copy(pwf));
113
114         nvar = isl_set_dim(dom, isl_dim_set);
115         opt = isl_qpolynomial_drop_dims(opt, isl_dim_set, 0, nvar);
116         if (vpb->exact && bounded)
117                 ok = isl_qpolynomial_is_equal(opt, bound);
118         else if (sign > 0)
119                 ok = isl_qpolynomial_le_cst(opt, bound);
120         else
121                 ok = isl_qpolynomial_le_cst(bound, opt);
122         if (ok < 0)
123                 goto error;
124
125         if (vpb->options->print_all || !ok) {
126                 fprintf(out, "%s(", minmax);
127                 for (i = 0; i < nparam; ++i) {
128                         if (i)
129                                 fprintf(out, ", ");
130                         isl_point_get_coordinate(pnt, isl_dim_param, i, &t);
131                         isl_int_print(out, t, 0);
132                 }
133                 fprintf(out, ") = ");
134                 isl_qpolynomial_print(bound, out, ISL_FORMAT_ISL);
135                 fprintf(out, ", %s = ", bounded ? "opt" : "sample");
136                 isl_qpolynomial_print(opt, out, ISL_FORMAT_ISL);
137                 if (ok)
138                         fprintf(out, ". OK\n");
139                 else
140                         fprintf(out, ". NOT OK\n");
141         } else if ((vpb->n % vpb->stride) == 0) {
142                 printf("o");
143                 fflush(stdout);
144         }
145
146         if (0) {
147 error:
148                 ok = 0;
149         }
150
151         isl_pw_qpolynomial_fold_free(pwf);
152         isl_qpolynomial_free(bound);
153         isl_qpolynomial_free(opt);
154         isl_point_free(pnt);
155         isl_set_free(dom);
156
157         isl_int_clear(t);
158
159         if (!ok)
160                 vpb->error = 1;
161
162         if (vpb->options->continue_on_error)
163                 ok = 1;
164
165         return (vpb->n >= 1 && ok) ? 0 : -1;
166 }
167
168 static int check_solution(__isl_take isl_pw_qpolynomial_fold *pwf,
169         __isl_take isl_pw_qpolynomial_fold *bound, int exact,
170         struct bound_options *options)
171 {
172         struct verify_point_bound vpb;
173         isl_int count, max;
174         isl_set *dom;
175         isl_set *context;
176         int i, r, n;
177
178         dom = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf));
179         context = isl_set_remove_dims(isl_set_copy(dom), isl_dim_set,
180                                         0, isl_set_dim(dom, isl_dim_set));
181         context = isl_set_remove_divs(context);
182         context = set_bounds(context);
183
184         isl_int_init(count);
185         isl_int_init(max);
186
187         isl_int_set_si(max, 200);
188         r = isl_set_count_upto(context, max, &count);
189         assert(r >= 0);
190         n = isl_int_get_si(count);
191
192         isl_int_clear(max);
193         isl_int_clear(count);
194
195         vpb.options = options;
196         vpb.pwf = pwf;
197         vpb.bound = bound;
198         vpb.n = n;
199         vpb.stride = n > 70 ? 1 + (n + 1)/70 : 1;
200         vpb.error = 0;
201         vpb.exact = exact;
202
203         if (!options->print_all) {
204                 for (i = 0; i < vpb.n; i += vpb.stride)
205                         printf(".");
206                 printf("\r");
207                 fflush(stdout);
208         }
209
210         isl_set_foreach_point(context, verify_point, &vpb);
211
212         isl_set_free(context);
213         isl_set_free(dom);
214         isl_pw_qpolynomial_fold_free(pwf);
215         isl_pw_qpolynomial_fold_free(bound);
216
217         if (!options->print_all)
218                 printf("\n");
219
220         if (vpb.error) {
221                 fprintf(stderr, "Check failed !\n");
222                 return -1;
223         }
224
225         return 0;
226 }
227
228 int main(int argc, char **argv)
229 {
230         isl_ctx *ctx;
231         isl_pw_qpolynomial_fold *copy;
232         isl_pw_qpolynomial_fold *pwf;
233         struct isl_stream *s;
234         struct isl_obj obj;
235         struct bound_options *options;
236         int exact;
237         int r = 0;
238
239         options = bound_options_new_with_defaults();
240         assert(options);
241         argc = bound_options_parse(options, argc, argv, ISL_ARG_ALL);
242
243         ctx = isl_ctx_alloc_with_options(bound_options_arg, options);
244
245         s = isl_stream_new_file(ctx, stdin);
246         obj = isl_stream_read_obj(s);
247         if (obj.type == isl_obj_pw_qpolynomial)
248                 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
249                                                                   obj.v);
250         else if (obj.type == isl_obj_pw_qpolynomial_fold)
251                 pwf = obj.v;
252         else {
253                 obj.type->free(obj.v);
254                 isl_die(ctx, isl_error_invalid, "invalid input", goto error);
255         }
256
257         if (options->verify)
258                 copy = isl_pw_qpolynomial_fold_copy(pwf);
259
260         pwf = isl_pw_qpolynomial_fold_bound(pwf, &exact);
261         pwf = isl_pw_qpolynomial_fold_coalesce(pwf);
262
263         if (options->verify) {
264                 r = check_solution(copy, pwf, exact, options);
265         } else {
266                 if (!exact)
267                         printf("# NOT exact\n");
268                 isl_pw_qpolynomial_fold_print(pwf, stdout, 0);
269                 fprintf(stdout, "\n");
270                 isl_pw_qpolynomial_fold_free(pwf);
271         }
272
273 error:
274         isl_stream_free(s);
275
276         isl_ctx_free(ctx);
277
278         return r;
279 }