dataflow analysis: allow absence of "textual" order during sorting of sources
[isl.git] / pip.c
blob27ee66d676d4ea09c9f1c9e027091679634976ec
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <assert.h>
11 #include <string.h>
12 #include <isl/set.h>
13 #include "isl_tab.h"
14 #include "isl_map_private.h"
15 #include "isl_sample.h"
16 #include "isl_scan.h"
17 #include <isl/seq.h>
18 #include <isl/ilp.h>
19 #include <isl_point_private.h>
21 /* The input of this program is the same as that of the "example" program
22 * from the PipLib distribution, except that the "big parameter column"
23 * should always be -1.
25 * Context constraints in PolyLib format
26 * -1
27 * Problem constraints in PolyLib format
28 * Optional list of options
30 * The options are
31 * Maximize compute maximum instead of minimum
32 * Rational compute rational optimum instead of integer optimum
33 * Urs_parms don't assume parameters are non-negative
34 * Urs_unknowns don't assume unknowns are non-negative
37 struct options {
38 struct isl_options *isl;
39 unsigned verify;
42 struct isl_arg options_arg[] = {
43 ISL_ARG_CHILD(struct options, isl, "isl", isl_options_arg, "isl options")
44 ISL_ARG_BOOL(struct options, verify, 'T', "verify", 0, NULL)
45 ISL_ARG_END
48 ISL_ARG_DEF(options, struct options, options_arg)
50 static __isl_give isl_basic_set *set_bounds(__isl_take isl_basic_set *bset)
52 unsigned nparam;
53 int i, r;
54 isl_point *pt, *pt2;
55 isl_basic_set *box;
57 nparam = isl_basic_set_dim(bset, isl_dim_param);
58 r = nparam >= 8 ? 4 : nparam >= 5 ? 6 : 30;
60 pt = isl_basic_set_sample_point(isl_basic_set_copy(bset));
61 pt2 = isl_point_copy(pt);
63 for (i = 0; i < nparam; ++i) {
64 pt = isl_point_add_ui(pt, isl_dim_param, i, r);
65 pt2 = isl_point_sub_ui(pt2, isl_dim_param, i, r);
68 box = isl_basic_set_box_from_points(pt, pt2);
70 return isl_basic_set_intersect(bset, box);
73 static struct isl_basic_set *to_parameter_domain(struct isl_basic_set *context)
75 return isl_basic_set_move_dims(context, isl_dim_param, 0, isl_dim_set, 0,
76 isl_basic_set_dim(context, isl_dim_set));
79 isl_basic_set *plug_in_parameters(isl_basic_set *bset, struct isl_vec *params)
81 int i;
83 for (i = 0; i < params->size - 1; ++i)
84 bset = isl_basic_set_fix(bset,
85 isl_dim_param, i, params->el[1 + i]);
87 bset = isl_basic_set_remove_dims(bset,
88 isl_dim_param, 0, params->size - 1);
90 isl_vec_free(params);
92 return bset;
95 isl_set *set_plug_in_parameters(isl_set *set, struct isl_vec *params)
97 int i;
99 for (i = 0; i < params->size - 1; ++i)
100 set = isl_set_fix(set, isl_dim_param, i, params->el[1 + i]);
102 set = isl_set_remove_dims(set, isl_dim_param, 0, params->size - 1);
104 isl_vec_free(params);
106 return set;
109 /* Compute the lexicographically minimal (or maximal if max is set)
110 * element of bset for the given values of the parameters, by
111 * successively solving an ilp problem in each direction.
113 struct isl_vec *opt_at(struct isl_basic_set *bset,
114 struct isl_vec *params, int max)
116 unsigned dim;
117 struct isl_vec *opt;
118 struct isl_vec *obj;
119 int i;
121 dim = isl_basic_set_dim(bset, isl_dim_set);
123 bset = plug_in_parameters(bset, params);
125 if (isl_basic_set_fast_is_empty(bset)) {
126 opt = isl_vec_alloc(bset->ctx, 0);
127 isl_basic_set_free(bset);
128 return opt;
131 opt = isl_vec_alloc(bset->ctx, 1 + dim);
132 assert(opt);
134 obj = isl_vec_alloc(bset->ctx, 1 + dim);
135 assert(obj);
137 isl_int_set_si(opt->el[0], 1);
138 isl_int_set_si(obj->el[0], 0);
140 for (i = 0; i < dim; ++i) {
141 enum isl_lp_result res;
143 isl_seq_clr(obj->el + 1, dim);
144 isl_int_set_si(obj->el[1 + i], 1);
145 res = isl_basic_set_solve_ilp(bset, max, obj->el,
146 &opt->el[1 + i], NULL);
147 if (res == isl_lp_empty)
148 goto empty;
149 assert(res == isl_lp_ok);
150 bset = isl_basic_set_fix(bset, isl_dim_set, i, opt->el[1 + i]);
153 isl_basic_set_free(bset);
154 isl_vec_free(obj);
156 return opt;
157 empty:
158 isl_vec_free(opt);
159 opt = isl_vec_alloc(bset->ctx, 0);
160 isl_basic_set_free(bset);
161 isl_vec_free(obj);
163 return opt;
166 struct isl_scan_pip {
167 struct isl_scan_callback callback;
168 isl_basic_set *bset;
169 isl_set *sol;
170 isl_set *empty;
171 int stride;
172 int n;
173 int max;
176 /* Check if the "manually" computed optimum of bset at the "sample"
177 * values of the parameters agrees with the solution of pilp problem
178 * represented by the pair (sol, empty).
179 * In particular, if there is no solution for this value of the parameters,
180 * then it should be an element of the parameter domain "empty".
181 * Otherwise, the optimal solution, should be equal to the result of
182 * plugging in the value of the parameters in "sol".
184 static int scan_one(struct isl_scan_callback *callback,
185 __isl_take isl_vec *sample)
187 struct isl_scan_pip *sp = (struct isl_scan_pip *)callback;
188 struct isl_vec *opt;
190 opt = opt_at(isl_basic_set_copy(sp->bset), isl_vec_copy(sample), sp->max);
191 assert(opt);
193 if (opt->size == 0) {
194 isl_point *sample_pnt;
195 sample_pnt = isl_point_alloc(isl_set_get_dim(sp->empty), sample);
196 assert(isl_set_contains_point(sp->empty, sample_pnt));
197 isl_point_free(sample_pnt);
198 isl_vec_free(opt);
199 } else {
200 isl_set *sol;
201 isl_set *opt_set;
202 opt_set = isl_set_from_basic_set(isl_basic_set_from_vec(opt));
203 sol = set_plug_in_parameters(isl_set_copy(sp->sol), sample);
204 assert(isl_set_is_equal(opt_set, sol));
205 isl_set_free(sol);
206 isl_set_free(opt_set);
209 if (!(sp->n++ % sp->stride)) {
210 printf("o");
211 fflush(stdout);
214 return 0;
217 struct isl_scan_count {
218 struct isl_scan_callback callback;
219 int n;
222 static int count_one(struct isl_scan_callback *callback,
223 __isl_take isl_vec *sample)
225 struct isl_scan_count *sc = (struct isl_scan_count *)callback;
226 isl_vec_free(sample);
228 sc->n++;
230 return 0;
233 static void check_solution(isl_basic_set *bset, isl_basic_set *context,
234 isl_set *sol, isl_set *empty, int max)
236 struct isl_scan_count sc;
237 struct isl_scan_pip sp;
238 int i;
239 int r;
241 context = set_bounds(context);
242 context = isl_basic_set_underlying_set(context);
244 sc.callback.add = count_one;
245 sc.n = 0;
247 r = isl_basic_set_scan(isl_basic_set_copy(context), &sc.callback);
248 assert (r == 0);
250 sp.callback.add = scan_one;
251 sp.bset = bset;
252 sp.sol = sol;
253 sp.empty = empty;
254 sp.n = 0;
255 sp.stride = sc.n > 70 ? 1 + (sc.n + 1)/70 : 1;
256 sp.max = max;
258 for (i = 0; i < sc.n; i += sp.stride)
259 printf(".");
260 printf("\r");
261 fflush(stdout);
263 r = isl_basic_set_scan(context, &sp.callback);
264 assert(r == 0);
266 printf("\n");
268 isl_basic_set_free(bset);
271 int main(int argc, char **argv)
273 struct isl_ctx *ctx;
274 struct isl_basic_set *context, *bset, *copy, *context_copy;
275 struct isl_set *set;
276 struct isl_set *empty;
277 int neg_one;
278 char s[1024];
279 int urs_parms = 0;
280 int urs_unknowns = 0;
281 int max = 0;
282 int rational = 0;
283 int n;
284 struct options *options;
286 options = options_new_with_defaults();
287 assert(options);
288 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
290 ctx = isl_ctx_alloc_with_options(options_arg, options);
292 context = isl_basic_set_read_from_file(ctx, stdin, 0);
293 assert(context);
294 n = fscanf(stdin, "%d", &neg_one);
295 assert(n == 1);
296 assert(neg_one == -1);
297 bset = isl_basic_set_read_from_file(ctx, stdin,
298 isl_basic_set_dim(context, isl_dim_set));
300 while (fgets(s, sizeof(s), stdin)) {
301 if (strncasecmp(s, "Maximize", 8) == 0)
302 max = 1;
303 if (strncasecmp(s, "Rational", 8) == 0) {
304 rational = 1;
305 bset = isl_basic_set_set_rational(bset);
307 if (strncasecmp(s, "Urs_parms", 9) == 0)
308 urs_parms = 1;
309 if (strncasecmp(s, "Urs_unknowns", 12) == 0)
310 urs_unknowns = 1;
312 if (!urs_parms)
313 context = isl_basic_set_intersect(context,
314 isl_basic_set_positive_orthant(isl_basic_set_get_dim(context)));
315 context = to_parameter_domain(context);
316 if (!urs_unknowns)
317 bset = isl_basic_set_intersect(bset,
318 isl_basic_set_positive_orthant(isl_basic_set_get_dim(bset)));
320 if (options->verify) {
321 copy = isl_basic_set_copy(bset);
322 context_copy = isl_basic_set_copy(context);
325 if (max)
326 set = isl_basic_set_partial_lexmax(bset, context, &empty);
327 else
328 set = isl_basic_set_partial_lexmin(bset, context, &empty);
330 if (options->verify) {
331 assert(!rational);
332 check_solution(copy, context_copy, set, empty, max);
333 } else {
334 isl_set_print(set, stdout, 0, ISL_FORMAT_ISL);
335 fprintf(stdout, "\n");
336 fprintf(stdout, "no solution: ");
337 isl_set_print(empty, stdout, 0, ISL_FORMAT_ISL);
338 fprintf(stdout, "\n");
341 isl_set_free(set);
342 isl_set_free(empty);
343 isl_ctx_free(ctx);
345 return 0;