PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/test.c

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