drop isl_map_drop_redundant_divs
[isl.git] / pip.c
blob002bc704b7d135d1a74a67b918ae99532b56070f
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT 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_map_private.h>
13 #include <isl/aff.h>
14 #include <isl/set.h>
15 #include "isl_tab.h"
16 #include "isl_sample.h"
17 #include "isl_scan.h"
18 #include <isl_seq.h>
19 #include <isl_ilp_private.h>
20 #include <isl/printer.h>
21 #include <isl_point_private.h>
22 #include <isl_vec_private.h>
23 #include <isl/options.h>
24 #include <isl_config.h>
26 /* The input of this program is the same as that of the "example" program
27 * from the PipLib distribution, except that the "big parameter column"
28 * should always be -1.
30 * Context constraints in PolyLib format
31 * -1
32 * Problem constraints in PolyLib format
33 * Optional list of options
35 * The options are
36 * Maximize compute maximum instead of minimum
37 * Rational compute rational optimum instead of integer optimum
38 * Urs_parms don't assume parameters are non-negative
39 * Urs_unknowns don't assume unknowns are non-negative
42 struct options {
43 struct isl_options *isl;
44 unsigned verify;
45 unsigned format;
48 #define FORMAT_SET 0
49 #define FORMAT_AFF 1
51 struct isl_arg_choice pip_format[] = {
52 {"set", FORMAT_SET},
53 {"affine", FORMAT_AFF},
54 {0}
57 ISL_ARGS_START(struct options, options_args)
58 ISL_ARG_CHILD(struct options, isl, "isl", &isl_options_args, "isl options")
59 ISL_ARG_BOOL(struct options, verify, 'T', "verify", 0, NULL)
60 ISL_ARG_CHOICE(struct options, format, 0, "format",
61 pip_format, FORMAT_SET, "output format")
62 ISL_ARGS_END
64 ISL_ARG_DEF(options, struct options, options_args)
66 static __isl_give isl_basic_set *set_bounds(__isl_take isl_basic_set *bset)
68 unsigned nparam;
69 int i, r;
70 isl_point *pt, *pt2;
71 isl_basic_set *box;
73 nparam = isl_basic_set_dim(bset, isl_dim_param);
74 r = nparam >= 8 ? 4 : nparam >= 5 ? 6 : 30;
76 pt = isl_basic_set_sample_point(isl_basic_set_copy(bset));
77 pt2 = isl_point_copy(pt);
79 for (i = 0; i < nparam; ++i) {
80 pt = isl_point_add_ui(pt, isl_dim_param, i, r);
81 pt2 = isl_point_sub_ui(pt2, isl_dim_param, i, r);
84 box = isl_basic_set_box_from_points(pt, pt2);
86 return isl_basic_set_intersect(bset, box);
89 static struct isl_basic_set *to_parameter_domain(struct isl_basic_set *context)
91 context = isl_basic_set_move_dims(context, isl_dim_param, 0,
92 isl_dim_set, 0, isl_basic_set_dim(context, isl_dim_set));
93 context = isl_basic_set_params(context);
94 return context;
97 /* Plug in the initial values of "params" for the parameters in "bset" and
98 * return the result. The remaining entries in "params", if any,
99 * correspond to the existentially quantified variables in the description
100 * of the original context and can be ignored.
102 static __isl_give isl_basic_set *plug_in_parameters(
103 __isl_take isl_basic_set *bset, __isl_take isl_vec *params)
105 int i, n;
107 n = isl_basic_set_dim(bset, isl_dim_param);
108 for (i = 0; i < n; ++i)
109 bset = isl_basic_set_fix(bset,
110 isl_dim_param, i, params->el[1 + i]);
112 bset = isl_basic_set_remove_dims(bset, isl_dim_param, 0, n);
114 isl_vec_free(params);
116 return bset;
119 /* Plug in the initial values of "params" for the parameters in "set" and
120 * return the result. The remaining entries in "params", if any,
121 * correspond to the existentially quantified variables in the description
122 * of the original context and can be ignored.
124 static __isl_give isl_set *set_plug_in_parameters(__isl_take isl_set *set,
125 __isl_take isl_vec *params)
127 int i, n;
129 n = isl_set_dim(set, isl_dim_param);
130 for (i = 0; i < n; ++i)
131 set = isl_set_fix(set, isl_dim_param, i, params->el[1 + i]);
133 set = isl_set_remove_dims(set, isl_dim_param, 0, n);
135 isl_vec_free(params);
137 return set;
140 /* Compute the lexicographically minimal (or maximal if max is set)
141 * element of bset for the given values of the parameters, by
142 * successively solving an ilp problem in each direction.
144 static __isl_give isl_vec *opt_at(__isl_take isl_basic_set *bset,
145 __isl_take isl_vec *params, int max)
147 unsigned dim;
148 isl_ctx *ctx;
149 struct isl_vec *opt;
150 struct isl_vec *obj;
151 int i;
153 dim = isl_basic_set_dim(bset, isl_dim_set);
155 bset = plug_in_parameters(bset, params);
157 ctx = isl_basic_set_get_ctx(bset);
158 if (isl_basic_set_plain_is_empty(bset)) {
159 opt = isl_vec_alloc(ctx, 0);
160 isl_basic_set_free(bset);
161 return opt;
164 opt = isl_vec_alloc(ctx, 1 + dim);
165 assert(opt);
167 obj = isl_vec_alloc(ctx, 1 + dim);
168 assert(obj);
170 isl_int_set_si(opt->el[0], 1);
171 isl_int_set_si(obj->el[0], 0);
173 for (i = 0; i < dim; ++i) {
174 enum isl_lp_result res;
176 isl_seq_clr(obj->el + 1, dim);
177 isl_int_set_si(obj->el[1 + i], 1);
178 res = isl_basic_set_solve_ilp(bset, max, obj->el,
179 &opt->el[1 + i], NULL);
180 if (res == isl_lp_empty)
181 goto empty;
182 assert(res == isl_lp_ok);
183 bset = isl_basic_set_fix(bset, isl_dim_set, i, opt->el[1 + i]);
186 isl_basic_set_free(bset);
187 isl_vec_free(obj);
189 return opt;
190 empty:
191 isl_vec_free(opt);
192 opt = isl_vec_alloc(ctx, 0);
193 isl_basic_set_free(bset);
194 isl_vec_free(obj);
196 return opt;
199 struct isl_scan_pip {
200 struct isl_scan_callback callback;
201 isl_basic_set *bset;
202 isl_set *sol;
203 isl_set *empty;
204 int stride;
205 int n;
206 int max;
209 /* Check if the "manually" computed optimum of bset at the "sample"
210 * values of the parameters agrees with the solution of pilp problem
211 * represented by the pair (sol, empty).
212 * In particular, if there is no solution for this value of the parameters,
213 * then it should be an element of the parameter domain "empty".
214 * Otherwise, the optimal solution, should be equal to the result of
215 * plugging in the value of the parameters in "sol".
217 static isl_stat scan_one(struct isl_scan_callback *callback,
218 __isl_take isl_vec *sample)
220 struct isl_scan_pip *sp = (struct isl_scan_pip *)callback;
221 struct isl_vec *opt;
223 sp->n--;
225 opt = opt_at(isl_basic_set_copy(sp->bset), isl_vec_copy(sample), sp->max);
226 assert(opt);
228 if (opt->size == 0) {
229 isl_point *sample_pnt;
230 sample_pnt = isl_point_alloc(isl_set_get_space(sp->empty), sample);
231 assert(isl_set_contains_point(sp->empty, sample_pnt));
232 isl_point_free(sample_pnt);
233 isl_vec_free(opt);
234 } else {
235 isl_set *sol;
236 isl_set *opt_set;
237 opt_set = isl_set_from_basic_set(isl_basic_set_from_vec(opt));
238 sol = set_plug_in_parameters(isl_set_copy(sp->sol), sample);
239 assert(isl_set_is_equal(opt_set, sol));
240 isl_set_free(sol);
241 isl_set_free(opt_set);
244 if (!(sp->n % sp->stride)) {
245 printf("o");
246 fflush(stdout);
249 return sp->n >= 1 ? isl_stat_ok : isl_stat_error;
252 static void check_solution(isl_basic_set *bset, isl_basic_set *context,
253 isl_set *sol, isl_set *empty, int max)
255 struct isl_scan_pip sp;
256 isl_int count, count_max;
257 int i, n;
258 int r;
260 context = set_bounds(context);
261 context = isl_basic_set_underlying_set(context);
263 isl_int_init(count);
264 isl_int_init(count_max);
266 isl_int_set_si(count_max, 2000);
267 r = isl_basic_set_count_upto(context, count_max, &count);
268 assert(r >= 0);
269 n = isl_int_get_si(count);
271 isl_int_clear(count_max);
272 isl_int_clear(count);
274 sp.callback.add = scan_one;
275 sp.bset = bset;
276 sp.sol = sol;
277 sp.empty = empty;
278 sp.n = n;
279 sp.stride = n > 70 ? 1 + (n + 1)/70 : 1;
280 sp.max = max;
282 for (i = 0; i < n; i += sp.stride)
283 printf(".");
284 printf("\r");
285 fflush(stdout);
287 isl_basic_set_scan(context, &sp.callback);
289 printf("\n");
291 isl_basic_set_free(bset);
294 int main(int argc, char **argv)
296 struct isl_ctx *ctx;
297 struct isl_basic_set *context, *bset, *copy, *context_copy;
298 struct isl_set *set = NULL;
299 struct isl_set *empty;
300 isl_pw_multi_aff *pma = NULL;
301 int neg_one;
302 char s[1024];
303 int urs_parms = 0;
304 int urs_unknowns = 0;
305 int max = 0;
306 int rational = 0;
307 int n;
308 int nparam;
309 struct options *options;
311 options = options_new_with_defaults();
312 assert(options);
313 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
315 ctx = isl_ctx_alloc_with_options(&options_args, options);
317 context = isl_basic_set_read_from_file(ctx, stdin);
318 assert(context);
319 n = fscanf(stdin, "%d", &neg_one);
320 assert(n == 1);
321 assert(neg_one == -1);
322 bset = isl_basic_set_read_from_file(ctx, stdin);
324 while (fgets(s, sizeof(s), stdin)) {
325 if (strncasecmp(s, "Maximize", 8) == 0)
326 max = 1;
327 if (strncasecmp(s, "Rational", 8) == 0) {
328 rational = 1;
329 bset = isl_basic_set_set_rational(bset);
331 if (strncasecmp(s, "Urs_parms", 9) == 0)
332 urs_parms = 1;
333 if (strncasecmp(s, "Urs_unknowns", 12) == 0)
334 urs_unknowns = 1;
336 if (!urs_parms)
337 context = isl_basic_set_intersect(context,
338 isl_basic_set_positive_orthant(isl_basic_set_get_space(context)));
339 context = to_parameter_domain(context);
340 nparam = isl_basic_set_dim(context, isl_dim_param);
341 if (nparam != isl_basic_set_dim(bset, isl_dim_param)) {
342 int dim = isl_basic_set_dim(bset, isl_dim_set);
343 bset = isl_basic_set_move_dims(bset, isl_dim_param, 0,
344 isl_dim_set, dim - nparam, nparam);
346 if (!urs_unknowns)
347 bset = isl_basic_set_intersect(bset,
348 isl_basic_set_positive_orthant(isl_basic_set_get_space(bset)));
350 if (options->verify) {
351 copy = isl_basic_set_copy(bset);
352 context_copy = isl_basic_set_copy(context);
355 if (options->format == FORMAT_AFF) {
356 if (max)
357 pma = isl_basic_set_partial_lexmax_pw_multi_aff(bset,
358 context, &empty);
359 else
360 pma = isl_basic_set_partial_lexmin_pw_multi_aff(bset,
361 context, &empty);
362 } else {
363 if (max)
364 set = isl_basic_set_partial_lexmax(bset,
365 context, &empty);
366 else
367 set = isl_basic_set_partial_lexmin(bset,
368 context, &empty);
371 if (options->verify) {
372 assert(!rational);
373 if (options->format == FORMAT_AFF)
374 set = isl_set_from_pw_multi_aff(pma);
375 check_solution(copy, context_copy, set, empty, max);
376 isl_set_free(set);
377 } else {
378 isl_printer *p;
379 p = isl_printer_to_file(ctx, stdout);
380 if (options->format == FORMAT_AFF)
381 p = isl_printer_print_pw_multi_aff(p, pma);
382 else
383 p = isl_printer_print_set(p, set);
384 p = isl_printer_end_line(p);
385 p = isl_printer_print_str(p, "no solution: ");
386 p = isl_printer_print_set(p, empty);
387 p = isl_printer_end_line(p);
388 isl_printer_free(p);
389 isl_set_free(set);
390 isl_pw_multi_aff_free(pma);
393 isl_set_free(empty);
394 isl_ctx_free(ctx);
396 return 0;