update isl for sufficient schedule tree support for pet
[barvinok/uuh.git] / test.c
blob6fef3055af820b261d4b2f21b427868a4cdd984c
1 #include <assert.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <isl_set_polylib.h>
6 #include <barvinok/util.h>
7 #include <barvinok/barvinok.h>
8 #include <barvinok/sample.h>
9 #include "config.h"
10 #include "lattice_width.h"
12 #ifdef HAVE_SYS_TIMES_H
14 #include <sys/times.h>
16 static void time_diff(struct tms *before, struct tms *after)
18 long ticks = sysconf(_SC_CLK_TCK);
19 printf("User: %g; Sys: %g\n",
20 (0.0 + after->tms_utime - before->tms_utime) / ticks,
21 (0.0 + after->tms_stime - before->tms_stime) / ticks);
24 #else
26 struct tms {};
27 static void times(struct tms* time)
30 static void time_diff(struct tms *before, struct tms *after)
34 #endif
36 int main(int argc, char **argv)
38 isl_ctx *ctx;
39 int i, nbPol, nbVec, nbMat, func, j, n;
40 Polyhedron *A, *B, *C, *D, *E, *F, *G;
41 char s[128];
42 struct barvinok_options *options = barvinok_options_new_with_defaults();
44 argc = barvinok_options_parse(options, argc, argv, ISL_ARG_ALL);
45 ctx = isl_ctx_alloc_with_options(&barvinok_options_args, options);
47 nbPol = nbVec = nbMat = 0;
48 fgets(s, 128, stdin);
49 while ((*s=='#') ||
50 ((sscanf(s, "D %d", &nbPol) < 1) &&
51 (sscanf(s, "V %d", &nbVec) < 1) &&
52 (sscanf(s, "M %d", &nbMat) < 1)))
53 fgets(s, 128, stdin);
55 for (i = 0; i < nbPol; ++i) {
56 Matrix *M = Matrix_Read();
57 A = Constraints2Polyhedron(M, options->MaxRays);
58 Matrix_Free(M);
59 fgets(s, 128, stdin);
60 while ((*s=='#') || (sscanf(s, "F %d", &func)<1))
61 fgets(s, 128, stdin);
63 switch(func) {
64 case 0: {
65 Value cb, ck;
66 value_init(cb);
67 value_init(ck);
68 fgets(s, 128, stdin);
69 /* workaround for apparent bug in older gmps */
70 *strchr(s, '\n') = '\0';
71 while ((*s=='#') || (value_read(ck, s) != 0)) {
72 fgets(s, 128, stdin);
73 /* workaround for apparent bug in older gmps */
74 *strchr(s, '\n') = '\0';
76 barvinok_count_with_options(A, &cb, options);
77 if (value_ne(cb, ck))
78 return -1;
79 value_clear(cb);
80 value_clear(ck);
81 break;
83 case 1:
84 Polyhedron_Print(stdout, P_VALUE_FMT, A);
85 B = Polyhedron_Polar(A, options->MaxRays);
86 Polyhedron_Print(stdout, P_VALUE_FMT, B);
87 C = Polyhedron_Polar(B, options->MaxRays);
88 Polyhedron_Print(stdout, P_VALUE_FMT, C);
89 Polyhedron_Free(C);
90 Polyhedron_Free(B);
91 break;
92 case 2:
93 Polyhedron_Print(stdout, P_VALUE_FMT, A);
94 for (j = 0; j < A->NbRays; ++j) {
95 B = supporting_cone(A, j);
96 Polyhedron_Print(stdout, P_VALUE_FMT, B);
97 Polyhedron_Free(B);
99 break;
100 case 3:
101 Polyhedron_Print(stdout, P_VALUE_FMT, A);
102 C = B = NULL;
103 barvinok_decompose(A,&B,&C);
104 puts("Pos:");
105 Polyhedron_Print(stdout, P_VALUE_FMT, B);
106 puts("Neg:");
107 Polyhedron_Print(stdout, P_VALUE_FMT, C);
108 Domain_Free(B);
109 Domain_Free(C);
110 break;
111 case 4: {
112 Value cm, cb;
113 struct tms tms_before, tms_between, tms_after;
114 value_init(cm);
115 value_init(cb);
116 Polyhedron_Print(stdout, P_VALUE_FMT, A);
117 times(&tms_before);
118 manual_count(A, &cm);
119 times(&tms_between);
120 barvinok_count(A, &cb, 100);
121 times(&tms_after);
122 printf("manual: ");
123 value_print(stdout, P_VALUE_FMT, cm);
124 puts("");
125 time_diff(&tms_before, &tms_between);
126 printf("Barvinok: ");
127 value_print(stdout, P_VALUE_FMT, cb);
128 puts("");
129 time_diff(&tms_between, &tms_after);
130 value_clear(cm);
131 value_clear(cb);
132 break;
134 case 5:
135 Polyhedron_Print(stdout, P_VALUE_FMT, A);
136 B = triangulate_cone(A, 100);
137 Polyhedron_Print(stdout, P_VALUE_FMT, B);
138 check_triangulization(A, B);
139 Domain_Free(B);
140 break;
141 case 6:
142 Polyhedron_Print(stdout, P_VALUE_FMT, A);
143 B = remove_equalities(A, options->MaxRays);
144 Polyhedron_Print(stdout, P_VALUE_FMT, B);
145 Polyhedron_Free(B);
146 break;
147 case 8: {
148 evalue *EP;
149 Matrix *M = Matrix_Read();
150 const char **param_name;
151 C = Constraints2Polyhedron(M, options->MaxRays);
152 Matrix_Free(M);
153 Polyhedron_Print(stdout, P_VALUE_FMT, A);
154 Polyhedron_Print(stdout, P_VALUE_FMT, C);
155 EP = barvinok_enumerate_with_options(A, C, options);
156 param_name = Read_ParamNames(stdin, C->Dimension);
157 print_evalue(stdout, EP, (const char**)param_name);
158 evalue_free(EP);
159 Polyhedron_Free(C);
161 case 9:
162 Polyhedron_Print(stdout, P_VALUE_FMT, A);
163 Polyhedron_Polarize(A);
164 C = B = NULL;
165 barvinok_decompose(A,&B,&C);
166 for (D = B; D; D = D->next)
167 Polyhedron_Polarize(D);
168 for (D = C; D; D = D->next)
169 Polyhedron_Polarize(D);
170 puts("Pos:");
171 Polyhedron_Print(stdout, P_VALUE_FMT, B);
172 puts("Neg:");
173 Polyhedron_Print(stdout, P_VALUE_FMT, C);
174 Domain_Free(B);
175 Domain_Free(C);
176 break;
177 case 10: {
178 evalue *EP;
179 Value cb, ck;
181 value_init(cb);
182 value_init(ck);
183 fgets(s, 128, stdin);
184 sscanf(s, "%d", &n);
185 for (j = 0; j < n; ++j) {
186 Polyhedron *P;
187 M = Matrix_Read();
188 P = Constraints2Polyhedron(M, options->MaxRays);
189 Matrix_Free(M);
190 A = DomainConcat(P, A);
192 fgets(s, 128, stdin);
193 /* workaround for apparent bug in older gmps */
194 *strchr(s, '\n') = '\0';
195 while ((*s=='#') || (value_read(ck, s) != 0)) {
196 fgets(s, 128, stdin);
197 /* workaround for apparent bug in older gmps */
198 *strchr(s, '\n') = '\0';
200 C = Universe_Polyhedron(0);
201 EP = barvinok_enumerate_union(A, C, options->MaxRays);
202 value_set_double(cb, compute_evalue(EP, &ck)+.25);
203 if (value_ne(cb, ck))
204 return -1;
205 Domain_Free(C);
206 value_clear(cb);
207 value_clear(ck);
208 evalue_free(EP);
209 break;
211 case 11: {
212 isl_space *dim;
213 isl_basic_set *bset;
214 isl_pw_qpolynomial *expected, *computed;
215 unsigned nparam;
217 expected = isl_pw_qpolynomial_read_from_file(ctx, stdin);
218 nparam = isl_pw_qpolynomial_dim(expected, isl_dim_param);
219 dim = isl_space_set_alloc(ctx, nparam, A->Dimension - nparam);
220 bset = isl_basic_set_new_from_polylib(A, dim);
221 computed = isl_basic_set_lattice_width(bset);
222 computed = isl_pw_qpolynomial_sub(computed, expected);
223 if (!isl_pw_qpolynomial_is_zero(computed))
224 return -1;
225 isl_pw_qpolynomial_free(computed);
226 break;
228 case 12: {
229 Vector *sample;
230 int has_sample;
231 fgets(s, 128, stdin);
232 sscanf(s, "%d", &has_sample);
234 sample = Polyhedron_Sample(A, options);
235 if (!sample && has_sample)
236 return -1;
237 if (sample && !has_sample)
238 return -1;
239 if (sample && !in_domain(A, sample->p))
240 return -1;
241 Vector_Free(sample);
244 Domain_Free(A);
246 for (i = 0; i < nbVec; ++i) {
247 int ok;
248 Vector *V = Vector_Read();
249 Matrix *M = Matrix_Alloc(V->Size, V->Size);
250 Vector_Copy(V->p, M->p[0], V->Size);
251 ok = unimodular_complete(M, 1);
252 assert(ok);
253 Matrix_Print(stdout, P_VALUE_FMT, M);
254 Matrix_Free(M);
255 Vector_Free(V);
257 for (i = 0; i < nbMat; ++i) {
258 Matrix *U, *V, *S;
259 Matrix *M = Matrix_Read();
260 Smith(M, &U, &V, &S);
261 Matrix_Print(stdout, P_VALUE_FMT, U);
262 Matrix_Print(stdout, P_VALUE_FMT, V);
263 Matrix_Print(stdout, P_VALUE_FMT, S);
264 Matrix_Free(M);
265 Matrix_Free(U);
266 Matrix_Free(V);
267 Matrix_Free(S);
270 isl_ctx_free(ctx);
271 return 0;