/test.c

https://repo.or.cz/barvinok.git · C · 254 lines · 236 code · 14 blank · 4 comment · 25 complexity · ee51dd61419bdfa7d9d611b0ef863966 MD5 · raw file

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