Polly 20.0.0git
pip.c
Go to the documentation of this file.
1/*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
9
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_sample.h"
16#include "isl_scan.h"
17#include <isl_seq.h>
18#include <isl_ilp_private.h>
19#include <isl/printer.h>
20#include <isl_point_private.h>
21#include <isl_vec_private.h>
22#include <isl/options.h>
23#include <isl_config.h>
24
25/* The input of this program is the same as that of the "example" program
26 * from the PipLib distribution, except that the "big parameter column"
27 * should always be -1.
28 *
29 * Context constraints in PolyLib format
30 * -1
31 * Problem constraints in PolyLib format
32 * Optional list of options
33 *
34 * The options are
35 * Maximize compute maximum instead of minimum
36 * Rational compute rational optimum instead of integer optimum
37 * Urs_parms don't assume parameters are non-negative
38 * Urs_unknowns don't assume unknowns are non-negative
39 */
40
41struct options {
42 struct isl_options *isl;
43 unsigned verify;
44 unsigned format;
45};
46
47#define FORMAT_SET 0
48#define FORMAT_AFF 1
49
51 {"set", FORMAT_SET},
52 {"affine", FORMAT_AFF},
53 {0}
54};
55
56ISL_ARGS_START(struct options, options_args)
57ISL_ARG_CHILD(struct options, isl, "isl", &isl_options_args, "isl options")
58ISL_ARG_BOOL(struct options, verify, 'T', "verify", 0, NULL)
59ISL_ARG_CHOICE(struct options, format, 0, "format",
60 pip_format, FORMAT_SET, "output format")
62
63ISL_ARG_DEF(options, struct options, options_args)
64
66{
67 isl_size nparam;
68 int i, r;
69 isl_point *pt, *pt2;
70 isl_basic_set *box;
71
72 nparam = isl_basic_set_dim(bset, isl_dim_param);
73 if (nparam < 0)
74 return isl_basic_set_free(bset);
75 r = nparam >= 8 ? 4 : nparam >= 5 ? 6 : 30;
76
78 pt2 = isl_point_copy(pt);
79
80 for (i = 0; i < nparam; ++i) {
81 pt = isl_point_add_ui(pt, isl_dim_param, i, r);
82 pt2 = isl_point_sub_ui(pt2, isl_dim_param, i, r);
83 }
84
85 box = isl_basic_set_box_from_points(pt, pt2);
86
87 return isl_basic_set_intersect(bset, box);
88}
89
92{
93 isl_size dim;
94
96 if (dim < 0)
99 isl_dim_set, 0, dim);
101 return context;
102}
103
104/* If "context" has more parameters than "bset", then reinterpret
105 * the last dimensions of "bset" as parameters.
106 */
109{
110 isl_size nparam, nparam_bset, dim;
111
113 nparam_bset = isl_basic_set_dim(bset, isl_dim_param);
114 if (nparam < 0 || nparam_bset < 0)
115 return isl_basic_set_free(bset);
116 if (nparam == nparam_bset)
117 return bset;
118 dim = isl_basic_set_dim(bset, isl_dim_set);
119 if (dim < 0)
120 return isl_basic_set_free(bset);
122 isl_dim_set, dim - nparam, nparam);
123 return bset;
124}
125
126/* Plug in the initial values of "params" for the parameters in "bset" and
127 * return the result. The remaining entries in "params", if any,
128 * correspond to the existentially quantified variables in the description
129 * of the original context and can be ignored.
130 */
133{
134 int i;
135 isl_size n;
136
138 if (n < 0)
139 bset = isl_basic_set_free(bset);
140 for (i = 0; i < n; ++i)
141 bset = isl_basic_set_fix(bset,
142 isl_dim_param, i, params->el[1 + i]);
143
144 bset = isl_basic_set_remove_dims(bset, isl_dim_param, 0, n);
145
147
148 return bset;
149}
150
151/* Plug in the initial values of "params" for the parameters in "set" and
152 * return the result. The remaining entries in "params", if any,
153 * correspond to the existentially quantified variables in the description
154 * of the original context and can be ignored.
155 */
158{
159 int i;
160 isl_size n;
161
163 if (n < 0)
165 for (i = 0; i < n; ++i)
166 set = isl_set_fix(set, isl_dim_param, i, params->el[1 + i]);
167
169
171
172 return set;
173}
174
175/* Compute the lexicographically minimal (or maximal if max is set)
176 * element of bset for the given values of the parameters, by
177 * successively solving an ilp problem in each direction.
178 */
180 __isl_take isl_vec *params, int max)
181{
182 isl_size dim;
183 isl_ctx *ctx;
184 struct isl_vec *opt;
185 struct isl_vec *obj;
186 int i;
187
188 dim = isl_basic_set_dim(bset, isl_dim_set);
189 if (dim < 0)
190 goto error;
191
192 bset = plug_in_parameters(bset, params);
193
196 opt = isl_vec_alloc(ctx, 0);
197 isl_basic_set_free(bset);
198 return opt;
199 }
200
201 opt = isl_vec_alloc(ctx, 1 + dim);
202 assert(opt);
203
204 obj = isl_vec_alloc(ctx, 1 + dim);
205 assert(obj);
206
207 isl_int_set_si(opt->el[0], 1);
208 isl_int_set_si(obj->el[0], 0);
209
210 for (i = 0; i < dim; ++i) {
211 enum isl_lp_result res;
212
213 isl_seq_clr(obj->el + 1, dim);
214 isl_int_set_si(obj->el[1 + i], 1);
215 res = isl_basic_set_solve_ilp(bset, max, obj->el,
216 &opt->el[1 + i], NULL);
217 if (res == isl_lp_empty)
218 goto empty;
219 assert(res == isl_lp_ok);
220 bset = isl_basic_set_fix(bset, isl_dim_set, i, opt->el[1 + i]);
221 }
222
223 isl_basic_set_free(bset);
225
226 return opt;
227error:
228 isl_basic_set_free(bset);
230 return NULL;
231empty:
232 isl_vec_free(opt);
233 opt = isl_vec_alloc(ctx, 0);
234 isl_basic_set_free(bset);
236
237 return opt;
238}
239
246 int n;
247 int max;
248};
249
250/* Check if the "manually" computed optimum of bset at the "sample"
251 * values of the parameters agrees with the solution of pilp problem
252 * represented by the pair (sol, empty).
253 * In particular, if there is no solution for this value of the parameters,
254 * then it should be an element of the parameter domain "empty".
255 * Otherwise, the optimal solution, should be equal to the result of
256 * plugging in the value of the parameters in "sol".
257 */
258static isl_stat scan_one(struct isl_scan_callback *callback,
259 __isl_take isl_vec *sample)
260{
261 struct isl_scan_pip *sp = (struct isl_scan_pip *)callback;
262 struct isl_vec *opt;
263
264 sp->n--;
265
266 opt = opt_at(isl_basic_set_copy(sp->bset), isl_vec_copy(sample), sp->max);
267 assert(opt);
268
269 if (opt->size == 0) {
270 isl_point *sample_pnt;
271 sample_pnt = isl_point_alloc(isl_set_get_space(sp->empty), sample);
272 assert(isl_set_contains_point(sp->empty, sample_pnt));
273 isl_point_free(sample_pnt);
274 isl_vec_free(opt);
275 } else {
276 isl_set *sol;
277 isl_set *opt_set;
279 sol = set_plug_in_parameters(isl_set_copy(sp->sol), sample);
280 assert(isl_set_is_equal(opt_set, sol));
281 isl_set_free(sol);
282 isl_set_free(opt_set);
283 }
284
285 if (!(sp->n % sp->stride)) {
286 printf("o");
287 fflush(stdout);
288 }
289
290 return sp->n >= 1 ? isl_stat_ok : isl_stat_error;
291}
292
294 isl_set *sol, isl_set *empty, int max)
295{
296 struct isl_scan_pip sp;
297 isl_int count, count_max;
298 int i, n;
299 int r;
300
303
305 isl_int_init(count_max);
306
307 isl_int_set_si(count_max, 2000);
308 r = isl_basic_set_count_upto(context, count_max, &count);
309 assert(r >= 0);
311
312 isl_int_clear(count_max);
314
315 sp.callback.add = scan_one;
316 sp.bset = bset;
317 sp.sol = sol;
318 sp.empty = empty;
319 sp.n = n;
320 sp.stride = n > 70 ? 1 + (n + 1)/70 : 1;
321 sp.max = max;
322
323 for (i = 0; i < n; i += sp.stride)
324 printf(".");
325 printf("\r");
326 fflush(stdout);
327
329
330 printf("\n");
331
333}
334
335int main(int argc, char **argv)
336{
337 struct isl_ctx *ctx;
338 struct isl_basic_set *context, *bset, *copy, *context_copy;
339 struct isl_set *set = NULL;
340 struct isl_set *empty;
341 isl_pw_multi_aff *pma = NULL;
342 int neg_one;
343 char s[1024];
344 int urs_parms = 0;
345 int urs_unknowns = 0;
346 int max = 0;
347 int rational = 0;
348 int n;
349 struct options *options;
350
351 options = options_new_with_defaults();
353 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
354
355 ctx = isl_ctx_alloc_with_options(&options_args, options);
356
359 n = fscanf(stdin, "%d", &neg_one);
360 assert(n == 1);
361 assert(neg_one == -1);
362 bset = isl_basic_set_read_from_file(ctx, stdin);
363
364 while (fgets(s, sizeof(s), stdin)) {
365 if (strncasecmp(s, "Maximize", 8) == 0)
366 max = 1;
367 if (strncasecmp(s, "Rational", 8) == 0) {
368 rational = 1;
369 bset = isl_basic_set_set_rational(bset);
370 }
371 if (strncasecmp(s, "Urs_parms", 9) == 0)
372 urs_parms = 1;
373 if (strncasecmp(s, "Urs_unknowns", 12) == 0)
374 urs_unknowns = 1;
375 }
376 if (!urs_parms)
380 bset = move_parameters(bset, context);
381 if (!urs_unknowns)
382 bset = isl_basic_set_intersect(bset,
384
385 if (options->verify) {
386 copy = isl_basic_set_copy(bset);
387 context_copy = isl_basic_set_copy(context);
388 }
389
390 if (options->format == FORMAT_AFF) {
391 if (max)
393 context, &empty);
394 else
396 context, &empty);
397 } else {
398 if (max)
400 context, &empty);
401 else
403 context, &empty);
404 }
405
406 if (options->verify) {
407 assert(!rational);
408 if (options->format == FORMAT_AFF)
410 check_solution(copy, context_copy, set, empty, max);
412 } else {
413 isl_printer *p;
414 p = isl_printer_to_file(ctx, stdout);
415 if (options->format == FORMAT_AFF)
417 else
420 p = isl_printer_print_str(p, "no solution: ");
421 p = isl_printer_print_set(p, empty);
426 }
427
428 isl_set_free(empty);
429 isl_ctx_free(ctx);
430
431 return 0;
432}
__isl_give isl_set * isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
Definition: isl_aff_map.c:411
__isl_null isl_pw_multi_aff * isl_pw_multi_aff_free(__isl_take isl_pw_multi_aff *pma)
__isl_give isl_printer * isl_printer_print_pw_multi_aff(__isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
Definition: isl_output.c:3056
#define ISL_ARG_DEF(prefix, st, args)
Definition: arg.h:302
#define ISL_ARG_CHOICE(st, f, s, l, c, d, h)
Definition: arg.h:134
#define ISL_ARG_BOOL(st, f, s, l, d, h)
Definition: arg.h:182
#define ISL_ARG_CHILD(st, f, l, c, h)
Definition: arg.h:264
#define ISL_ARG_ALL
Definition: arg.h:288
#define ISL_ARGS_START(s, name)
Definition: arg.h:113
#define ISL_ARGS_END
Definition: arg.h:117
isl_ctx * isl_ctx_alloc_with_options(struct isl_args *args, __isl_take void *opt)
#define __isl_take
Definition: ctx.h:22
isl_stat
Definition: ctx.h:84
@ isl_stat_error
Definition: ctx.h:85
@ isl_stat_ok
Definition: ctx.h:86
#define __isl_give
Definition: ctx.h:19
#define __isl_keep
Definition: ctx.h:25
int isl_size
Definition: ctx.h:96
void isl_ctx_free(isl_ctx *ctx)
Definition: isl_ctx.c:288
static int count(int *con, unsigned len, int status)
Definition: isl_coalesce.c:152
static __isl_give isl_basic_set * set_bounds(__isl_keep isl_set *set, int dim)
enum isl_lp_result isl_basic_set_solve_ilp(__isl_keep isl_basic_set *bset, int max, isl_int *f, isl_int *opt, __isl_give isl_vec **sol_p)
Definition: isl_ilp.c:311
#define isl_int_get_si(r)
Definition: isl_int_gmp.h:18
#define isl_int_set_si(r, i)
Definition: isl_int_gmp.h:15
#define isl_int_init(i)
Definition: isl_int_gmp.h:11
#define isl_int_clear(i)
Definition: isl_int_gmp.h:12
__isl_give isl_set * isl_set_fix(__isl_take isl_set *set, enum isl_dim_type type, unsigned pos, isl_int value)
Definition: isl_map.c:6660
__isl_give isl_basic_set * isl_basic_set_fix(__isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos, isl_int value)
Definition: isl_map.c:6548
__isl_give isl_basic_set * isl_basic_set_set_rational(__isl_take isl_basic_set *bset)
Definition: isl_map.c:2233
__isl_give isl_basic_set * isl_basic_set_underlying_set(__isl_take isl_basic_set *bset)
Definition: isl_map.c:5648
int isl_basic_set_count_upto(__isl_keep isl_basic_set *bset, isl_int max, isl_int *count)
Definition: isl_scan.c:251
isl_bool isl_set_contains_point(__isl_keep isl_set *set, __isl_keep isl_point *point)
Definition: isl_point.c:668
__isl_give isl_point * isl_point_alloc(__isl_take isl_space *space, __isl_take isl_vec *vec)
Definition: isl_point.c:66
__isl_give isl_basic_set * isl_basic_set_from_vec(__isl_take isl_vec *vec)
Definition: isl_sample.c:1209
isl_stat isl_basic_set_scan(__isl_take isl_basic_set *bset, struct isl_scan_callback *callback)
Definition: isl_scan.c:102
void isl_seq_clr(isl_int *p, unsigned len)
Definition: isl_seq.c:14
const char * set
Definition: isl_test.c:1356
const char * pma
Definition: isl_test.c:3169
const char * p
Definition: isl_test.c:8643
const char * context
Definition: isl_test.c:1784
const char * obj
Definition: isl_test.c:3316
const char * res
Definition: isl_test.c:775
#define assert(exp)
isl_lp_result
Definition: lp.h:17
@ isl_lp_ok
Definition: lp.h:19
@ isl_lp_empty
Definition: lp.h:21
struct isl_set isl_set
Definition: map_type.h:26
struct isl_basic_set isl_basic_set
Definition: map_type.h:20
These are automatically generated checked C++ bindings for isl.
Definition: ISLTools.h:45
static isl_stat scan_one(struct isl_scan_callback *callback, __isl_take isl_vec *sample)
Definition: pip.c:258
#define FORMAT_AFF
Definition: pip.c:48
struct isl_arg_choice pip_format[]
Definition: pip.c:50
static __isl_give isl_set * set_plug_in_parameters(__isl_take isl_set *set, __isl_take isl_vec *params)
Definition: pip.c:156
static __isl_give isl_basic_set * move_parameters(__isl_take isl_basic_set *bset, __isl_keep isl_basic_set *context)
Definition: pip.c:107
static __isl_give isl_vec * opt_at(__isl_take isl_basic_set *bset, __isl_take isl_vec *params, int max)
Definition: pip.c:179
static __isl_give isl_basic_set * to_parameter_domain(__isl_take isl_basic_set *context)
Definition: pip.c:90
static __isl_give isl_basic_set * plug_in_parameters(__isl_take isl_basic_set *bset, __isl_take isl_vec *params)
Definition: pip.c:131
#define FORMAT_SET
Definition: pip.c:47
static void check_solution(isl_basic_set *bset, isl_basic_set *context, isl_set *sol, isl_set *empty, int max)
Definition: pip.c:293
__isl_give isl_point * isl_point_copy(__isl_keep isl_point *pnt)
Definition: isl_point.c:141
__isl_null isl_point * isl_point_free(__isl_take isl_point *pnt)
Definition: isl_point.c:150
__isl_give isl_point * isl_point_add_ui(__isl_take isl_point *pnt, enum isl_dim_type type, int pos, unsigned val)
Definition: isl_point.c:502
__isl_give isl_point * isl_point_sub_ui(__isl_take isl_point *pnt, enum isl_dim_type type, int pos, unsigned val)
Definition: isl_point.c:530
__isl_null isl_printer * isl_printer_free(__isl_take isl_printer *printer)
Definition: isl_printer.c:269
__isl_give isl_printer * isl_printer_to_file(isl_ctx *ctx, FILE *file)
Definition: isl_printer.c:217
__isl_give isl_printer * isl_printer_print_str(__isl_take isl_printer *p, const char *s)
Definition: isl_printer.c:617
__isl_give isl_printer * isl_printer_end_line(__isl_take isl_printer *p)
Definition: isl_printer.c:667
isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset, enum isl_dim_type type)
Definition: isl_map.c:201
__isl_give isl_basic_set * isl_basic_set_read_from_file(isl_ctx *ctx, FILE *input)
Definition: isl_input.c:3296
__isl_give isl_pw_multi_aff * isl_basic_set_partial_lexmax_pw_multi_aff(__isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom, __isl_give isl_set **empty)
__isl_export __isl_give isl_space * isl_set_get_space(__isl_keep isl_set *set)
Definition: isl_map.c:603
__isl_export isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
Definition: isl_map.c:9083
__isl_give isl_space * isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
Definition: isl_map.c:421
__isl_give isl_basic_set * isl_basic_set_remove_dims(__isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first, unsigned n)
Definition: isl_map.c:3319
__isl_give isl_set * isl_basic_set_partial_lexmin(__isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom, __isl_give isl_set **empty)
__isl_export __isl_give isl_basic_set * isl_basic_set_params(__isl_take isl_basic_set *bset)
Definition: isl_map.c:5912
__isl_null isl_basic_set * isl_basic_set_free(__isl_take isl_basic_set *bset)
Definition: isl_map.c:1523
isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
Definition: isl_map.c:9431
__isl_null isl_set * isl_set_free(__isl_take isl_set *set)
Definition: isl_map.c:3513
__isl_give isl_set * isl_set_copy(__isl_keep isl_set *set)
Definition: isl_map.c:1470
__isl_give isl_basic_set * isl_basic_set_box_from_points(__isl_take isl_point *pnt1, __isl_take isl_point *pnt2)
Definition: isl_point.c:723
__isl_give isl_printer * isl_printer_print_set(__isl_take isl_printer *printer, __isl_keep isl_set *map)
Definition: isl_output.c:1572
__isl_give isl_basic_set * isl_basic_set_move_dims(__isl_take isl_basic_set *bset, enum isl_dim_type dst_type, unsigned dst_pos, enum isl_dim_type src_type, unsigned src_pos, unsigned n)
Definition: isl_map.c:4311
__isl_give isl_set * isl_set_remove_dims(__isl_take isl_set *bset, enum isl_dim_type type, unsigned first, unsigned n)
Definition: isl_map.c:3353
__isl_give isl_basic_set * isl_basic_set_positive_orthant(__isl_take isl_space *space)
Definition: isl_map.c:8909
__isl_give isl_pw_multi_aff * isl_basic_set_partial_lexmin_pw_multi_aff(__isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom, __isl_give isl_set **empty)
isl_size isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
Definition: isl_map.c:129
__isl_give isl_set * isl_basic_set_partial_lexmax(__isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom, __isl_give isl_set **empty)
isl_ctx * isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
Definition: isl_map.c:386
__isl_constructor __isl_give isl_set * isl_set_from_basic_set(__isl_take isl_basic_set *bset)
Definition: isl_map.c:3482
__isl_give isl_basic_set * isl_basic_set_copy(__isl_keep isl_basic_set *bset)
Definition: isl_map.c:1465
__isl_export __isl_give isl_basic_set * isl_basic_set_intersect(__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
Definition: isl_map.c:3770
__isl_export __isl_give isl_point * isl_basic_set_sample_point(__isl_take isl_basic_set *bset)
Definition: isl_sample.c:1300
@ isl_dim_param
Definition: space_type.h:15
@ isl_dim_set
Definition: space_type.h:18
isl_stat(* add)(struct isl_scan_callback *cb, __isl_take isl_vec *sample)
Definition: isl_scan.h:17
isl_basic_set * bset
Definition: pip.c:242
isl_set * empty
Definition: pip.c:244
struct isl_scan_callback callback
Definition: pip.c:241
int n
Definition: pip.c:246
int max
Definition: pip.c:247
isl_set * sol
Definition: pip.c:243
int stride
Definition: pip.c:245
isl_int * el
struct isl_ctx * ctx
unsigned size
unsigned verify
Definition: pip.c:43
struct isl_options * isl
Definition: codegen.c:34
unsigned format
Definition: pip.c:44
static Kind params
__isl_null isl_vec * isl_vec_free(__isl_take isl_vec *vec)
Definition: isl_vec.c:234
__isl_give isl_vec * isl_vec_copy(__isl_keep isl_vec *vec)
Definition: isl_vec.c:198
__isl_give isl_vec * isl_vec_alloc(isl_ctx *ctx, unsigned size)
Definition: isl_vec.c:33
n
Definition: youcefn.c:8