2 #include <isl/stream.h>
3 #include <barvinok/options.h>
4 #include <barvinok/util.h>
5 #include <barvinok/barvinok.h>
9 struct verify_options
*verify
;
15 ISL_ARGS_START(struct options
, options_args
)
16 ISL_ARG_CHILD(struct options
, verify
, NULL
,
17 &verify_options_args
, "verification")
18 ISL_ARG_LONG(struct options
, split
, 0, "split", 0, NULL
)
19 ISL_ARG_OPT_LONG(struct options
, iterate
, 0, "iterate", 0, -1,
20 "exact result by iterating over domain (of specified maximal size)")
21 ISL_ARG_BOOL(struct options
, lower
, 0, "lower", 0,
22 "compute lower bound instead of upper bound")
25 ISL_ARG_DEF(options
, struct options
, options_args
)
27 struct verify_point_bound
{
28 struct verify_point_data vpd
;
29 isl_pw_qpolynomial
*pwqp
;
30 isl_pw_qpolynomial_fold
*pwf
;
34 static int verify_point(__isl_take isl_point
*pnt
, void *user
)
38 struct verify_point_bound
*vpb
= (struct verify_point_bound
*) user
;
39 isl_val
*v
, *b
, *q
, *t
;
40 isl_pw_qpolynomial
*pwqp
;
45 FILE *out
= vpb
->vpd
.options
->print_all
? stdout
: stderr
;
49 if (vpb
->type
== isl_fold_max
) {
57 pwqp
= isl_pw_qpolynomial_copy(vpb
->pwqp
);
59 nparam
= isl_pw_qpolynomial_dim(pwqp
, isl_dim_param
);
60 for (i
= 0; i
< nparam
; ++i
) {
61 t
= isl_point_get_coordinate_val(pnt
, isl_dim_param
, i
);
62 pwqp
= isl_pw_qpolynomial_fix_val(pwqp
, isl_dim_param
, i
, t
);
65 b
= isl_pw_qpolynomial_fold_eval(isl_pw_qpolynomial_fold_copy(vpb
->pwf
),
69 v
= isl_pw_qpolynomial_max(pwqp
);
71 v
= isl_pw_qpolynomial_min(pwqp
);
85 ok
= isl_val_ge(b
, v
);
87 ok
= isl_val_le(b
, v
);
89 if (vpb
->vpd
.options
->print_all
|| !ok
) {
90 isl_ctx
*ctx
= isl_point_get_ctx(pnt
);
92 p
= isl_printer_to_file(ctx
, out
);
93 p
= isl_printer_print_str(p
, minmax
);
94 p
= isl_printer_print_str(p
, "(");
95 for (i
= 0; i
< nparam
; ++i
) {
97 p
= isl_printer_print_str(p
, ", ");
98 t
= isl_point_get_coordinate_val(pnt
, isl_dim_param
, i
);
99 p
= isl_printer_print_val(p
, t
);
102 p
= isl_printer_print_str(p
, ") = ");
103 p
= isl_printer_print_val(p
, q
);
104 if (!isl_val_is_int(q
)) {
105 p
= isl_printer_print_str(p
, " (");
106 p
= isl_printer_print_val(p
, b
);
107 p
= isl_printer_print_str(p
, ")");
109 p
= isl_printer_print_str(p
, ", ");
110 p
= isl_printer_print_str(p
, minmax
);
111 p
= isl_printer_print_str(p
, "(EP) = ");
112 p
= isl_printer_print_val(p
, v
);
114 p
= isl_printer_print_str(p
, ". OK");
116 p
= isl_printer_print_str(p
, ". NOT OK");
117 p
= isl_printer_end_line(p
);
119 } else if ((vpb
->vpd
.n
% vpb
->vpd
.s
) == 0) {
138 if (vpb
->vpd
.options
->continue_on_error
)
141 return (vpb
->vpd
.n
>= 1 && ok
) ? 0 : -1;
144 static int verify(__isl_keep isl_pw_qpolynomial_fold
*pwf
,
145 __isl_take isl_pw_qpolynomial
*pwqp
, enum isl_fold type
,
146 struct verify_options
*options
)
148 struct verify_point_bound vpb
= { { options
} };
155 context
= isl_pw_qpolynomial_fold_domain(
156 isl_pw_qpolynomial_fold_copy(vpb
.pwf
));
157 context
= verify_context_set_bounds(context
, options
);
159 r
= verify_point_data_init(&vpb
.vpd
, context
);
162 isl_set_foreach_point(context
, verify_point
, &vpb
);
166 isl_set_free(context
);
167 isl_pw_qpolynomial_free(pwqp
);
169 verify_point_data_fini(&vpb
.vpd
);
174 static __isl_give isl_pw_qpolynomial_fold
*iterate(
175 __isl_take isl_pw_qpolynomial
*pwqp
, enum isl_fold type
)
177 isl_space
*dim
= isl_pw_qpolynomial_get_space(pwqp
);
181 isl_qpolynomial_fold
*fold
;
184 assert(isl_space_dim(dim
, isl_dim_param
) == 0);
185 nvar
= isl_space_dim(dim
, isl_dim_set
);
187 if (type
== isl_fold_min
)
188 v
= isl_pw_qpolynomial_min(pwqp
);
190 v
= isl_pw_qpolynomial_max(pwqp
);
192 dim
= isl_space_drop_dims(dim
, isl_dim_set
, 0, nvar
);
193 qp
= isl_qpolynomial_val_on_domain(isl_space_copy(dim
), v
);
194 fold
= isl_qpolynomial_fold_alloc(type
, qp
);
195 set
= isl_set_universe(dim
);
197 return isl_pw_qpolynomial_fold_alloc(type
, set
, fold
);
200 struct bv_split_data
{
202 __isl_give isl_pw_qpolynomial
*pwqp_less
;
203 __isl_give isl_pw_qpolynomial
*pwqp_more
;
206 static int split_on_size(__isl_take isl_set
*set
,
207 __isl_take isl_qpolynomial
*qp
, void *user
)
209 struct bv_split_data
*data
= (struct bv_split_data
*)user
;
212 isl_pw_qpolynomial
*pwqp
;
215 nparam
= isl_set_dim(set
, isl_dim_param
);
216 set_np
= isl_set_move_dims(isl_set_copy(set
), isl_dim_set
, 0,
217 isl_dim_param
, 0, nparam
);
218 bounded
= isl_set_is_bounded(set_np
);
219 assert(bounded
>= 0);
221 isl_pw_qpolynomial
*pwqp
;
224 pwqp
= isl_set_card(set_np
);
225 m
= isl_pw_qpolynomial_max(pwqp
);
226 bounded
= isl_val_cmp_si(m
, data
->size
) <= 0;
229 isl_set_free(set_np
);
231 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
233 data
->pwqp_less
= isl_pw_qpolynomial_add_disjoint(
234 data
->pwqp_less
, pwqp
);
236 data
->pwqp_more
= isl_pw_qpolynomial_add_disjoint(
237 data
->pwqp_more
, pwqp
);
243 * Split (partition) pwpq into a partition with (sub)domains containing
244 * size integer points or less and a partition with (sub)domains
245 * containing more integer points.
247 static int split_on_domain_size(__isl_take isl_pw_qpolynomial
*pwqp
,
248 __isl_give isl_pw_qpolynomial
**pwqp_less
,
249 __isl_give isl_pw_qpolynomial
**pwqp_more
,
253 struct bv_split_data data
= { size
};
256 dim
= isl_pw_qpolynomial_get_space(pwqp
);
257 data
.pwqp_less
= isl_pw_qpolynomial_zero(isl_space_copy(dim
));
258 data
.pwqp_more
= isl_pw_qpolynomial_zero(dim
);
259 r
= isl_pw_qpolynomial_foreach_piece(pwqp
, &split_on_size
, &data
);
260 *pwqp_less
= data
.pwqp_less
;
261 *pwqp_more
= data
.pwqp_more
;
262 isl_pw_qpolynomial_free(pwqp
);
267 static __isl_give isl_pw_qpolynomial_fold
*optimize(
268 __isl_take isl_pw_qpolynomial
*pwqp
, enum isl_fold type
,
269 struct options
*options
)
271 isl_pw_qpolynomial_fold
*pwf
;
273 if (options
->iterate
> 0) {
274 isl_pw_qpolynomial
*pwqp_less
, *pwqp_more
;
275 isl_pw_qpolynomial_fold
*pwf_less
, *pwf_more
;
276 split_on_domain_size(pwqp
, &pwqp_less
, &pwqp_more
, options
->iterate
);
277 pwf_less
= iterate(pwqp_less
, type
);
278 pwf_more
= isl_pw_qpolynomial_bound(pwqp_more
, type
, NULL
);
279 pwf
= isl_pw_qpolynomial_fold_fold(pwf_less
, pwf_more
);
280 } else if (options
->iterate
)
281 pwf
= iterate(pwqp
, type
);
283 pwf
= isl_pw_qpolynomial_bound(pwqp
, type
, NULL
);
287 static int optimize_and_print(__isl_take isl_pw_qpolynomial
*pwqp
,
288 struct options
*options
)
290 int print_solution
= 1;
292 isl_pw_qpolynomial_fold
*pwf
;
293 enum isl_fold type
= options
->lower
? isl_fold_min
: isl_fold_max
;
295 if (options
->verify
->verify
) {
296 isl_space
*dim
= isl_pw_qpolynomial_get_space(pwqp
);
297 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
299 verify_options_set_range(options
->verify
, total
);
300 if (!options
->verify
->barvinok
->verbose
)
304 pwf
= optimize(isl_pw_qpolynomial_copy(pwqp
), type
, options
);
306 if (print_solution
) {
307 isl_ctx
*ctx
= isl_pw_qpolynomial_get_ctx(pwqp
);
308 isl_printer
*p
= isl_printer_to_file(ctx
, stdout
);
309 p
= isl_printer_print_pw_qpolynomial_fold(p
, pwf
);
310 p
= isl_printer_end_line(p
);
313 if (options
->verify
->verify
) {
314 enum isl_fold type
= options
->lower
? isl_fold_min
: isl_fold_max
;
315 result
= verify(pwf
, pwqp
, type
, options
->verify
);
317 isl_pw_qpolynomial_free(pwqp
);
318 isl_pw_qpolynomial_fold_free(pwf
);
323 int main(int argc
, char **argv
)
325 struct options
*options
= options_new_with_defaults();
327 isl_pw_qpolynomial
*pwqp
;
328 struct isl_stream
*s
;
331 argc
= options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
332 ctx
= isl_ctx_alloc_with_options(&options_args
, options
);
334 s
= isl_stream_new_file(ctx
, stdin
);
335 pwqp
= isl_stream_read_pw_qpolynomial(s
);
338 pwqp
= isl_pw_qpolynomial_split_periods(pwqp
, options
->split
);
340 result
= optimize_and_print(pwqp
, options
);