isl_basic_set_total_dim: implement in terms of isl_basic_map_total_dim
[isl.git] / pip.c
bloba4395bd6361b1e86aa18cf77f24d129db9f6909b
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 __isl_give isl_basic_set *to_parameter_domain(
90 __isl_take isl_basic_set *context)
92 context = isl_basic_set_move_dims(context, isl_dim_param, 0,
93 isl_dim_set, 0, isl_basic_set_dim(context, isl_dim_set));
94 context = isl_basic_set_params(context);
95 return context;
98 /* Plug in the initial values of "params" for the parameters in "bset" and
99 * return the result. The remaining entries in "params", if any,
100 * correspond to the existentially quantified variables in the description
101 * of the original context and can be ignored.
103 static __isl_give isl_basic_set *plug_in_parameters(
104 __isl_take isl_basic_set *bset, __isl_take isl_vec *params)
106 int i, n;
108 n = isl_basic_set_dim(bset, isl_dim_param);
109 for (i = 0; i < n; ++i)
110 bset = isl_basic_set_fix(bset,
111 isl_dim_param, i, params->el[1 + i]);
113 bset = isl_basic_set_remove_dims(bset, isl_dim_param, 0, n);
115 isl_vec_free(params);
117 return bset;
120 /* Plug in the initial values of "params" for the parameters in "set" and
121 * return the result. The remaining entries in "params", if any,
122 * correspond to the existentially quantified variables in the description
123 * of the original context and can be ignored.
125 static __isl_give isl_set *set_plug_in_parameters(__isl_take isl_set *set,
126 __isl_take isl_vec *params)
128 int i, n;
130 n = isl_set_dim(set, isl_dim_param);
131 for (i = 0; i < n; ++i)
132 set = isl_set_fix(set, isl_dim_param, i, params->el[1 + i]);
134 set = isl_set_remove_dims(set, isl_dim_param, 0, n);
136 isl_vec_free(params);
138 return set;
141 /* Compute the lexicographically minimal (or maximal if max is set)
142 * element of bset for the given values of the parameters, by
143 * successively solving an ilp problem in each direction.
145 static __isl_give isl_vec *opt_at(__isl_take isl_basic_set *bset,
146 __isl_take isl_vec *params, int max)
148 unsigned dim;
149 isl_ctx *ctx;
150 struct isl_vec *opt;
151 struct isl_vec *obj;
152 int i;
154 dim = isl_basic_set_dim(bset, isl_dim_set);
156 bset = plug_in_parameters(bset, params);
158 ctx = isl_basic_set_get_ctx(bset);
159 if (isl_basic_set_plain_is_empty(bset)) {
160 opt = isl_vec_alloc(ctx, 0);
161 isl_basic_set_free(bset);
162 return opt;
165 opt = isl_vec_alloc(ctx, 1 + dim);
166 assert(opt);
168 obj = isl_vec_alloc(ctx, 1 + dim);
169 assert(obj);
171 isl_int_set_si(opt->el[0], 1);
172 isl_int_set_si(obj->el[0], 0);
174 for (i = 0; i < dim; ++i) {
175 enum isl_lp_result res;
177 isl_seq_clr(obj->el + 1, dim);
178 isl_int_set_si(obj->el[1 + i], 1);
179 res = isl_basic_set_solve_ilp(bset, max, obj->el,
180 &opt->el[1 + i], NULL);
181 if (res == isl_lp_empty)
182 goto empty;
183 assert(res == isl_lp_ok);
184 bset = isl_basic_set_fix(bset, isl_dim_set, i, opt->el[1 + i]);
187 isl_basic_set_free(bset);
188 isl_vec_free(obj);
190 return opt;
191 empty:
192 isl_vec_free(opt);
193 opt = isl_vec_alloc(ctx, 0);
194 isl_basic_set_free(bset);
195 isl_vec_free(obj);
197 return opt;
200 struct isl_scan_pip {
201 struct isl_scan_callback callback;
202 isl_basic_set *bset;
203 isl_set *sol;
204 isl_set *empty;
205 int stride;
206 int n;
207 int max;
210 /* Check if the "manually" computed optimum of bset at the "sample"
211 * values of the parameters agrees with the solution of pilp problem
212 * represented by the pair (sol, empty).
213 * In particular, if there is no solution for this value of the parameters,
214 * then it should be an element of the parameter domain "empty".
215 * Otherwise, the optimal solution, should be equal to the result of
216 * plugging in the value of the parameters in "sol".
218 static isl_stat scan_one(struct isl_scan_callback *callback,
219 __isl_take isl_vec *sample)
221 struct isl_scan_pip *sp = (struct isl_scan_pip *)callback;
222 struct isl_vec *opt;
224 sp->n--;
226 opt = opt_at(isl_basic_set_copy(sp->bset), isl_vec_copy(sample), sp->max);
227 assert(opt);
229 if (opt->size == 0) {
230 isl_point *sample_pnt;
231 sample_pnt = isl_point_alloc(isl_set_get_space(sp->empty), sample);
232 assert(isl_set_contains_point(sp->empty, sample_pnt));
233 isl_point_free(sample_pnt);
234 isl_vec_free(opt);
235 } else {
236 isl_set *sol;
237 isl_set *opt_set;
238 opt_set = isl_set_from_basic_set(isl_basic_set_from_vec(opt));
239 sol = set_plug_in_parameters(isl_set_copy(sp->sol), sample);
240 assert(isl_set_is_equal(opt_set, sol));
241 isl_set_free(sol);
242 isl_set_free(opt_set);
245 if (!(sp->n % sp->stride)) {
246 printf("o");
247 fflush(stdout);
250 return sp->n >= 1 ? isl_stat_ok : isl_stat_error;
253 static void check_solution(isl_basic_set *bset, isl_basic_set *context,
254 isl_set *sol, isl_set *empty, int max)
256 struct isl_scan_pip sp;
257 isl_int count, count_max;
258 int i, n;
259 int r;
261 context = set_bounds(context);
262 context = isl_basic_set_underlying_set(context);
264 isl_int_init(count);
265 isl_int_init(count_max);
267 isl_int_set_si(count_max, 2000);
268 r = isl_basic_set_count_upto(context, count_max, &count);
269 assert(r >= 0);
270 n = isl_int_get_si(count);
272 isl_int_clear(count_max);
273 isl_int_clear(count);
275 sp.callback.add = scan_one;
276 sp.bset = bset;
277 sp.sol = sol;
278 sp.empty = empty;
279 sp.n = n;
280 sp.stride = n > 70 ? 1 + (n + 1)/70 : 1;
281 sp.max = max;
283 for (i = 0; i < n; i += sp.stride)
284 printf(".");
285 printf("\r");
286 fflush(stdout);
288 isl_basic_set_scan(context, &sp.callback);
290 printf("\n");
292 isl_basic_set_free(bset);
295 int main(int argc, char **argv)
297 struct isl_ctx *ctx;
298 struct isl_basic_set *context, *bset, *copy, *context_copy;
299 struct isl_set *set = NULL;
300 struct isl_set *empty;
301 isl_pw_multi_aff *pma = NULL;
302 int neg_one;
303 char s[1024];
304 int urs_parms = 0;
305 int urs_unknowns = 0;
306 int max = 0;
307 int rational = 0;
308 int n;
309 int nparam;
310 struct options *options;
312 options = options_new_with_defaults();
313 assert(options);
314 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
316 ctx = isl_ctx_alloc_with_options(&options_args, options);
318 context = isl_basic_set_read_from_file(ctx, stdin);
319 assert(context);
320 n = fscanf(stdin, "%d", &neg_one);
321 assert(n == 1);
322 assert(neg_one == -1);
323 bset = isl_basic_set_read_from_file(ctx, stdin);
325 while (fgets(s, sizeof(s), stdin)) {
326 if (strncasecmp(s, "Maximize", 8) == 0)
327 max = 1;
328 if (strncasecmp(s, "Rational", 8) == 0) {
329 rational = 1;
330 bset = isl_basic_set_set_rational(bset);
332 if (strncasecmp(s, "Urs_parms", 9) == 0)
333 urs_parms = 1;
334 if (strncasecmp(s, "Urs_unknowns", 12) == 0)
335 urs_unknowns = 1;
337 if (!urs_parms)
338 context = isl_basic_set_intersect(context,
339 isl_basic_set_positive_orthant(isl_basic_set_get_space(context)));
340 context = to_parameter_domain(context);
341 nparam = isl_basic_set_dim(context, isl_dim_param);
342 if (nparam != isl_basic_set_dim(bset, isl_dim_param)) {
343 int dim = isl_basic_set_dim(bset, isl_dim_set);
344 bset = isl_basic_set_move_dims(bset, isl_dim_param, 0,
345 isl_dim_set, dim - nparam, nparam);
347 if (!urs_unknowns)
348 bset = isl_basic_set_intersect(bset,
349 isl_basic_set_positive_orthant(isl_basic_set_get_space(bset)));
351 if (options->verify) {
352 copy = isl_basic_set_copy(bset);
353 context_copy = isl_basic_set_copy(context);
356 if (options->format == FORMAT_AFF) {
357 if (max)
358 pma = isl_basic_set_partial_lexmax_pw_multi_aff(bset,
359 context, &empty);
360 else
361 pma = isl_basic_set_partial_lexmin_pw_multi_aff(bset,
362 context, &empty);
363 } else {
364 if (max)
365 set = isl_basic_set_partial_lexmax(bset,
366 context, &empty);
367 else
368 set = isl_basic_set_partial_lexmin(bset,
369 context, &empty);
372 if (options->verify) {
373 assert(!rational);
374 if (options->format == FORMAT_AFF)
375 set = isl_set_from_pw_multi_aff(pma);
376 check_solution(copy, context_copy, set, empty, max);
377 isl_set_free(set);
378 } else {
379 isl_printer *p;
380 p = isl_printer_to_file(ctx, stdout);
381 if (options->format == FORMAT_AFF)
382 p = isl_printer_print_pw_multi_aff(p, pma);
383 else
384 p = isl_printer_print_set(p, set);
385 p = isl_printer_end_line(p);
386 p = isl_printer_print_str(p, "no solution: ");
387 p = isl_printer_print_set(p, empty);
388 p = isl_printer_end_line(p);
389 isl_printer_free(p);
390 isl_set_free(set);
391 isl_pw_multi_aff_free(pma);
394 isl_set_free(empty);
395 isl_ctx_free(ctx);
397 return 0;