3 #include <barvinok/options.h>
4 #include <barvinok/util.h>
7 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
9 /* RANGE : normal range for evalutations (-RANGE -> RANGE) */
12 /* SRANGE : small range for evalutations */
15 /* if dimension >= BIDDIM, use SRANGE */
18 /* VSRANGE : very small range for evalutations */
21 /* if dimension >= VBIDDIM, use VSRANGE */
24 static int set_m(void *opt
, long val
)
26 struct verify_options
*options
= (struct verify_options
*)opt
;
32 static int set_M(void *opt
, long val
)
34 struct verify_options
*options
= (struct verify_options
*)opt
;
40 static int set_r(void *opt
, long val
)
42 struct verify_options
*options
= (struct verify_options
*)opt
;
50 ISL_ARGS_START(struct verify_options
, verify_options_args
)
51 ISL_ARG_CHILD(struct verify_options
, barvinok
, NULL
, &barvinok_options_args
,
53 ISL_ARG_BOOL(struct verify_options
, verify
, 'T', "verify", 0, NULL
)
54 ISL_ARG_BOOL(struct verify_options
, exact
, 'E', "exact", 0, NULL
)
55 ISL_ARG_BOOL(struct verify_options
, print_all
, 'A', "print-all", 0, NULL
)
56 ISL_ARG_BOOL(struct verify_options
, continue_on_error
, 'C',
57 "continue-on-error", 0, NULL
)
58 ISL_ARG_USER_LONG(struct verify_options
, m
, 'm', "min", set_m
, INT_MAX
, NULL
)
59 ISL_ARG_USER_LONG(struct verify_options
, M
, 'M', "max", set_M
, INT_MIN
, NULL
)
60 ISL_ARG_USER_LONG(struct verify_options
, r
, 'r', "range", set_r
, -1, NULL
)
63 void verify_options_set_range(struct verify_options
*options
, int dim
)
69 else if (dim
>= BIGDIM
)
73 /* If the user didn't set m or M, then we try to adjust the window
74 * to the context in check_poly_context_scan.
76 if (options
->m
== INT_MAX
&& options
->M
== INT_MIN
)
78 if (options
->M
== INT_MIN
)
80 if (options
->m
== INT_MAX
)
83 if (options
->verify
&& options
->m
> options
->M
) {
84 fprintf(stderr
,"Nothing to do: min > max !\n");
89 static Polyhedron
*project_on(Polyhedron
*P
, int i
)
91 unsigned dim
= P
->Dimension
;
96 return Polyhedron_Copy(P
);
98 T
= Matrix_Alloc(2, dim
+1);
99 value_set_si(T
->p
[0][i
], 1);
100 value_set_si(T
->p
[1][dim
], 1);
101 I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
106 static void set_bounds(Polyhedron
*C
, Value
**rows
, int i
, unsigned nparam
,
107 const struct verify_options
*options
)
114 value_set_si(min
, options
->m
);
115 value_set_si(max
, options
->M
);
117 if (options
->r
> 0) {
118 Polyhedron
*I
= project_on(C
, i
);
119 line_minmax(I
, &min
, &max
);
120 if (value_cmp_si(min
, options
->M
) >= 0)
121 value_add_int(max
, min
, options
->r
);
122 else if (value_cmp_si(max
, options
->m
) <= 0)
123 value_sub_int(min
, max
, options
->r
);
125 value_set_si(min
, options
->m
);
126 value_set_si(max
, options
->M
);
130 value_set_si(rows
[0][0], 1);
131 value_set_si(rows
[0][1+i
], 1);
132 value_oppose(rows
[0][1+nparam
], min
);
133 value_set_si(rows
[1][0], 1);
134 value_set_si(rows
[1][1+i
], -1);
135 value_assign(rows
[1][1+nparam
], max
);
141 Polyhedron
*check_poly_context_scan(Polyhedron
*P
, Polyhedron
**C
,
143 const struct verify_options
*options
)
147 Polyhedron
*CC
, *CC2
, *CS
, *U
;
148 unsigned MaxRays
= options
->barvinok
->MaxRays
;
156 CC
= Polyhedron_Project(P
, nparam
);
158 CC2
= DomainIntersection(*C
, CC
, MaxRays
);
164 /* Intersect context with range */
165 MM
= Matrix_Alloc(2*nparam
, nparam
+2);
166 for (i
= 0; i
< nparam
; ++i
)
167 set_bounds(CC
, &MM
->p
[2*i
], i
, nparam
, options
);
168 CC2
= AddConstraints(MM
->p
[0], 2*nparam
, CC
, options
->barvinok
->MaxRays
);
172 U
= Universe_Polyhedron(0);
173 CS
= Polyhedron_Scan(CC
, U
, MaxRays
& POL_NO_DUAL
? 0 : MaxRays
);
180 void check_poly_init(Polyhedron
*C
, struct verify_options
*options
)
184 if (options
->print_all
)
186 if (C
->Dimension
<= 0)
189 d
= options
->M
- options
->m
;
191 options
->st
= 1+d
/80;
194 for (i
= options
->m
; i
<= options
->M
; i
+= options
->st
)
200 static void print_rational(FILE *out
, Value n
, Value d
)
202 value_print(out
, VALUE_FMT
, n
);
203 if (value_notone_p(d
)) {
205 value_print(out
, VALUE_FMT
, d
);
209 void check_poly_print(int ok
, int nparam
, Value
*z
,
210 Value want_n
, Value want_d
,
211 Value got_n
, Value got_d
,
212 const char *op
, const char *check
,
214 const struct verify_options
*options
)
218 if (options
->print_all
) {
220 value_print(stdout
, VALUE_FMT
, z
[0]);
221 for (k
= 1; k
< nparam
; ++k
) {
223 value_print(stdout
, VALUE_FMT
, z
[k
]);
226 print_rational(stdout
, got_n
, got_d
);
227 printf(", %s = ", check
);
228 print_rational(stdout
, want_n
, want_d
);
235 fprintf(stderr
, "Error !\n");
236 fprintf(stderr
, "%s(", op
);
237 value_print(stderr
, VALUE_FMT
, z
[0]);
238 for (k
= 1; k
< nparam
; ++k
) {
239 fprintf(stderr
,", ");
240 value_print(stderr
, VALUE_FMT
, z
[k
]);
242 fprintf(stderr
, ") should be ");
243 print_rational(stderr
, want_n
, want_d
);
244 fprintf(stderr
, ", while %s gives ", long_op
);
245 print_rational(stderr
, got_n
, got_d
);
246 fprintf(stderr
, ".\n");
247 } else if (options
->print_all
)
251 /****************************************************/
252 /* function check_poly : */
253 /* scans the parameter space from Min to Max (all */
254 /* directions). Computes the number of points in */
255 /* the polytope using both methods, and compare them*/
256 /* returns 1 on success */
257 /****************************************************/
259 int check_poly(Polyhedron
*CS
, const struct check_poly_data
*data
,
260 int nparam
, int pos
, Value
*z
,
261 const struct verify_options
*options
)
264 if (!data
->check(data
, nparam
, z
, options
) && !options
->continue_on_error
)
271 ok
= !(lower_upper_bounds(1+pos
, CS
, z
-1, &LB
, &UB
));
273 for (; value_le(LB
, UB
); value_increment(LB
, LB
)) {
274 if (!options
->print_all
) {
275 int k
= VALUE_TO_INT(LB
);
276 if (!pos
&& !(k
% options
->st
)) {
282 value_assign(z
[pos
], LB
);
283 if (!check_poly(CS
->next
, data
, nparam
, pos
+1, z
, options
)) {
289 value_set_si(z
[pos
], 0);
296 void check_EP_set_scan(struct check_EP_data
*data
, Polyhedron
*C
,
299 const evalue
*EP
= data
->EP
;
303 for (i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
304 Polyhedron
*A
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
305 for (; A
; A
= A
->next
)
310 data
->S
= ALLOCN(Polyhedron
*, n_S
);
312 for (i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
313 Polyhedron
*A
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
314 for (; A
; A
= A
->next
) {
315 Polyhedron
*next
= A
->next
;
317 data
->S
[n_S
++] = Polyhedron_Scan(A
, C
,
318 MaxRays
& POL_NO_DUAL
? 0 : MaxRays
);
324 void check_EP_clear_scan(struct check_EP_data
*data
)
328 for (i
= 0; i
< data
->n_S
; ++i
)
329 Domain_Free(data
->S
[i
]);
333 static int check_EP_on_poly(Polyhedron
*P
,
334 struct check_EP_data
*data
,
335 unsigned nvar
, unsigned nparam
,
336 struct verify_options
*options
)
339 unsigned MaxRays
= options
->barvinok
->MaxRays
;
341 const evalue
*EP
= data
->EP
;
343 CS
= check_poly_context_scan(NULL
, &P
, P
->Dimension
, options
);
345 check_poly_init(P
, options
);
347 if (!(CS
&& emptyQ2(CS
))) {
348 check_EP_set_scan(data
, P
, MaxRays
);
349 ok
= check_poly(CS
, &data
->cp
, nparam
, 0, data
->cp
.z
+1+nvar
, options
);
350 check_EP_clear_scan(data
);
353 if (!options
->print_all
)
365 * Project on final dim dimensions
367 Polyhedron
*DomainProject(Polyhedron
*D
, unsigned dim
, unsigned MaxRays
)
372 R
= Polyhedron_Project(D
, dim
);
373 for (P
= D
->next
; P
; P
= P
->next
) {
374 Polyhedron
*R2
= Polyhedron_Project(P
, dim
);
375 Polyhedron
*R3
= DomainUnion(R
, R2
, MaxRays
);
383 static Polyhedron
*evalue_parameter_domain(const evalue
*e
, unsigned nparam
,
387 Polyhedron
*U
= NULL
;
389 if (EVALUE_IS_ZERO(*e
))
390 return Universe_Polyhedron(0);
392 assert(value_zero_p(e
->d
));
393 assert(e
->x
.p
->type
== partition
);
394 assert(e
->x
.p
->size
>= 2);
396 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
397 Polyhedron
*D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
398 Polyhedron
*P
= DomainProject(D
, nparam
, MaxRays
);
403 U
= DomainUnion(U
, P
, MaxRays
);
411 int check_EP(struct check_EP_data
*data
, unsigned nvar
, unsigned nparam
,
412 struct verify_options
*options
)
419 p
= Vector_Alloc(nvar
+nparam
+2);
420 value_set_si(p
->p
[nvar
+nparam
+1], 1);
424 D
= evalue_parameter_domain(data
->EP
, nparam
, options
->barvinok
->MaxRays
);
426 for (P
= D
; P
; P
= P
->next
) {
427 ok
= check_EP_on_poly(P
, data
, nvar
, nparam
, options
);
428 if (!ok
&& !options
->continue_on_error
)
438 __isl_give isl_set
*verify_context_set_bounds(__isl_take isl_set
*set
,
439 const struct verify_options
*options
)
446 nparam
= isl_set_dim(set
, isl_dim_param
);
448 if (options
->r
> 0) {
449 pt
= isl_set_sample_point(isl_set_copy(set
));
450 pt2
= isl_point_copy(pt
);
452 for (i
= 0; i
< nparam
; ++i
) {
453 pt
= isl_point_add_ui(pt
, isl_dim_param
, i
, options
->r
);
454 pt2
= isl_point_sub_ui(pt2
, isl_dim_param
, i
, options
->r
);
460 pt
= isl_point_zero(isl_set_get_space(set
));
461 isl_int_set_si(v
, options
->m
);
462 for (i
= 0; i
< nparam
; ++i
)
463 pt
= isl_point_set_coordinate(pt
, isl_dim_param
, i
, v
);
465 pt2
= isl_point_zero(isl_set_get_space(set
));
466 isl_int_set_si(v
, options
->M
);
467 for (i
= 0; i
< nparam
; ++i
)
468 pt2
= isl_point_set_coordinate(pt2
, isl_dim_param
, i
, v
);
473 box
= isl_set_box_from_points(pt
, pt2
);
475 return isl_set_intersect(set
, box
);
478 int verify_point_data_init(struct verify_point_data
*vpd
,
479 __isl_keep isl_set
*context
)
486 r
= isl_set_count(context
, &v
);
487 vpd
->n
= isl_int_cmp_si(v
, 200) < 0 ? isl_int_get_si(v
) : 200;
490 if (!vpd
->options
->print_all
) {
491 vpd
->s
= vpd
->n
< 80 ? 1 : 1 + vpd
->n
/80;
492 for (i
= 0; i
< vpd
->n
; i
+= vpd
->s
)
498 vpd
->error
= r
< 0 ? -1 : 0;
503 void verify_point_data_fini(struct verify_point_data
*vpd
)
505 if (!vpd
->options
->print_all
)
509 fprintf(stderr
, "Check failed !\n");