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 isl_stat
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
;
44 FILE *out
= vpb
->vpd
.options
->print_all
? stdout
: stderr
;
48 if (vpb
->type
== isl_fold_max
) {
56 pwqp
= isl_pw_qpolynomial_copy(vpb
->pwqp
);
58 nparam
= isl_pw_qpolynomial_dim(pwqp
, isl_dim_param
);
59 for (i
= 0; i
< nparam
; ++i
) {
60 t
= isl_point_get_coordinate_val(pnt
, isl_dim_param
, i
);
61 pwqp
= isl_pw_qpolynomial_fix_val(pwqp
, isl_dim_param
, i
, t
);
64 b
= isl_pw_qpolynomial_fold_eval(isl_pw_qpolynomial_fold_copy(vpb
->pwf
),
68 v
= isl_pw_qpolynomial_max(pwqp
);
70 v
= isl_pw_qpolynomial_min(pwqp
);
84 ok
= isl_val_ge(b
, v
);
86 ok
= isl_val_le(b
, v
);
88 if (vpb
->vpd
.options
->print_all
|| !ok
) {
89 isl_ctx
*ctx
= isl_point_get_ctx(pnt
);
91 p
= isl_printer_to_file(ctx
, out
);
92 p
= isl_printer_print_str(p
, minmax
);
93 p
= isl_printer_print_str(p
, "(");
94 for (i
= 0; i
< nparam
; ++i
) {
96 p
= isl_printer_print_str(p
, ", ");
97 t
= isl_point_get_coordinate_val(pnt
, isl_dim_param
, i
);
98 p
= isl_printer_print_val(p
, t
);
101 p
= isl_printer_print_str(p
, ") = ");
102 p
= isl_printer_print_val(p
, q
);
103 if (!isl_val_is_int(q
)) {
104 p
= isl_printer_print_str(p
, " (");
105 p
= isl_printer_print_val(p
, b
);
106 p
= isl_printer_print_str(p
, ")");
108 p
= isl_printer_print_str(p
, ", ");
109 p
= isl_printer_print_str(p
, minmax
);
110 p
= isl_printer_print_str(p
, "(EP) = ");
111 p
= isl_printer_print_val(p
, v
);
113 p
= isl_printer_print_str(p
, ". OK");
115 p
= isl_printer_print_str(p
, ". NOT OK");
116 p
= isl_printer_end_line(p
);
118 } else if ((vpb
->vpd
.n
% vpb
->vpd
.s
) == 0) {
137 if (vpb
->vpd
.options
->continue_on_error
)
141 return isl_stat_error
;
142 if (ok
!= isl_bool_true
)
143 return isl_stat_error
;
147 static int verify(__isl_keep isl_pw_qpolynomial_fold
*pwf
,
148 __isl_take isl_pw_qpolynomial
*pwqp
, enum isl_fold type
,
149 struct verify_options
*options
)
151 struct verify_point_bound vpb
= { { options
} };
158 context
= isl_pw_qpolynomial_fold_domain(
159 isl_pw_qpolynomial_fold_copy(vpb
.pwf
));
160 context
= verify_context_set_bounds(context
, options
);
162 r
= verify_point_data_init(&vpb
.vpd
, context
);
165 isl_set_foreach_point(context
, verify_point
, &vpb
);
169 isl_set_free(context
);
170 isl_pw_qpolynomial_free(pwqp
);
172 verify_point_data_fini(&vpb
.vpd
);
177 static __isl_give isl_pw_qpolynomial_fold
*iterate(
178 __isl_take isl_pw_qpolynomial
*pwqp
, enum isl_fold type
)
180 isl_space
*dim
= isl_pw_qpolynomial_get_space(pwqp
);
184 isl_qpolynomial_fold
*fold
;
187 assert(isl_space_dim(dim
, isl_dim_param
) == 0);
188 nvar
= isl_space_dim(dim
, isl_dim_set
);
190 if (type
== isl_fold_min
)
191 v
= isl_pw_qpolynomial_min(pwqp
);
193 v
= isl_pw_qpolynomial_max(pwqp
);
195 dim
= isl_space_drop_dims(dim
, isl_dim_set
, 0, nvar
);
196 qp
= isl_qpolynomial_val_on_domain(isl_space_copy(dim
), v
);
197 fold
= isl_qpolynomial_fold_alloc(type
, qp
);
198 set
= isl_set_universe(dim
);
200 return isl_pw_qpolynomial_fold_alloc(type
, set
, fold
);
203 struct bv_split_data
{
205 __isl_give isl_pw_qpolynomial
*pwqp_less
;
206 __isl_give isl_pw_qpolynomial
*pwqp_more
;
209 static isl_stat
split_on_size(__isl_take isl_set
*set
,
210 __isl_take isl_qpolynomial
*qp
, void *user
)
212 struct bv_split_data
*data
= (struct bv_split_data
*)user
;
215 isl_pw_qpolynomial
*pwqp
;
218 nparam
= isl_set_dim(set
, isl_dim_param
);
219 set_np
= isl_set_move_dims(isl_set_copy(set
), isl_dim_set
, 0,
220 isl_dim_param
, 0, nparam
);
221 bounded
= isl_set_is_bounded(set_np
);
222 assert(bounded
>= 0);
224 isl_pw_qpolynomial
*pwqp
;
227 pwqp
= isl_set_card(set_np
);
228 m
= isl_pw_qpolynomial_max(pwqp
);
229 bounded
= isl_val_cmp_si(m
, data
->size
) <= 0;
232 isl_set_free(set_np
);
234 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
236 data
->pwqp_less
= isl_pw_qpolynomial_add_disjoint(
237 data
->pwqp_less
, pwqp
);
239 data
->pwqp_more
= isl_pw_qpolynomial_add_disjoint(
240 data
->pwqp_more
, pwqp
);
246 * Split (partition) pwpq into a partition with (sub)domains containing
247 * size integer points or less and a partition with (sub)domains
248 * containing more integer points.
250 static int split_on_domain_size(__isl_take isl_pw_qpolynomial
*pwqp
,
251 __isl_give isl_pw_qpolynomial
**pwqp_less
,
252 __isl_give isl_pw_qpolynomial
**pwqp_more
,
256 struct bv_split_data data
= { size
};
259 dim
= isl_pw_qpolynomial_get_space(pwqp
);
260 data
.pwqp_less
= isl_pw_qpolynomial_zero(isl_space_copy(dim
));
261 data
.pwqp_more
= isl_pw_qpolynomial_zero(dim
);
262 r
= isl_pw_qpolynomial_foreach_piece(pwqp
, &split_on_size
, &data
);
263 *pwqp_less
= data
.pwqp_less
;
264 *pwqp_more
= data
.pwqp_more
;
265 isl_pw_qpolynomial_free(pwqp
);
270 static __isl_give isl_pw_qpolynomial_fold
*optimize(
271 __isl_take isl_pw_qpolynomial
*pwqp
, enum isl_fold type
,
272 struct options
*options
)
274 isl_pw_qpolynomial_fold
*pwf
;
276 if (options
->iterate
> 0) {
277 isl_pw_qpolynomial
*pwqp_less
, *pwqp_more
;
278 isl_pw_qpolynomial_fold
*pwf_less
, *pwf_more
;
279 split_on_domain_size(pwqp
, &pwqp_less
, &pwqp_more
, options
->iterate
);
280 pwf_less
= iterate(pwqp_less
, type
);
281 pwf_more
= isl_pw_qpolynomial_bound(pwqp_more
, type
, NULL
);
282 pwf
= isl_pw_qpolynomial_fold_fold(pwf_less
, pwf_more
);
283 } else if (options
->iterate
)
284 pwf
= iterate(pwqp
, type
);
286 pwf
= isl_pw_qpolynomial_bound(pwqp
, type
, NULL
);
290 static int optimize_and_print(__isl_take isl_pw_qpolynomial
*pwqp
,
291 struct options
*options
)
293 int print_solution
= 1;
295 isl_pw_qpolynomial_fold
*pwf
;
296 enum isl_fold type
= options
->lower
? isl_fold_min
: isl_fold_max
;
298 if (options
->verify
->verify
) {
299 isl_space
*dim
= isl_pw_qpolynomial_get_space(pwqp
);
300 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
302 verify_options_set_range(options
->verify
, total
);
303 if (!options
->verify
->barvinok
->verbose
)
307 pwf
= optimize(isl_pw_qpolynomial_copy(pwqp
), type
, options
);
309 if (print_solution
) {
310 isl_ctx
*ctx
= isl_pw_qpolynomial_get_ctx(pwqp
);
311 isl_printer
*p
= isl_printer_to_file(ctx
, stdout
);
312 p
= isl_printer_print_pw_qpolynomial_fold(p
, pwf
);
313 p
= isl_printer_end_line(p
);
316 if (options
->verify
->verify
) {
317 enum isl_fold type
= options
->lower
? isl_fold_min
: isl_fold_max
;
318 result
= verify(pwf
, pwqp
, type
, options
->verify
);
320 isl_pw_qpolynomial_free(pwqp
);
321 isl_pw_qpolynomial_fold_free(pwf
);
326 int main(int argc
, char **argv
)
328 struct options
*options
= options_new_with_defaults();
330 isl_pw_qpolynomial
*pwqp
;
331 struct isl_stream
*s
;
334 argc
= options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
335 ctx
= isl_ctx_alloc_with_options(&options_args
, options
);
337 s
= isl_stream_new_file(ctx
, stdin
);
338 pwqp
= isl_stream_read_pw_qpolynomial(s
);
341 pwqp
= isl_pw_qpolynomial_split_periods(pwqp
, options
->split
);
343 result
= optimize_and_print(pwqp
, options
);