cholesky.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include "cholesky.h"
  19. #include "cholesky_kernels.h"
  20. /*
  21. * code to bootstrap the factorization
  22. * and construct the DAG
  23. */
  24. static void dw_cholesky(float ***matA, unsigned size, unsigned ld, unsigned nblocks)
  25. {
  26. struct timeval start;
  27. struct timeval end;
  28. int x, y;
  29. /* create all the DAG nodes */
  30. unsigned i,j,k;
  31. for(x = 0; x < nblocks ; x++) {
  32. for (y = 0; y < nblocks; y++) {
  33. #pragma starpu register matA[x][y] size/nblocks*size/nblocks
  34. }
  35. }
  36. gettimeofday(&start, NULL);
  37. for (k = 0; k < nblocks; k++)
  38. {
  39. #warning deal with prio and models
  40. // int prio = STARPU_DEFAULT_PRIO;
  41. // if (!noprio) prio = STARPU_MAX_PRIO;
  42. chol_codelet_update_u11(matA[k][k], size/nblocks, ld);
  43. for (j = k+1; j<nblocks; j++)
  44. {
  45. // prio = STARPU_DEFAULT_PRIO;
  46. // if (!noprio&& (j == k+1)) prio = STARPU_MAX_PRIO;
  47. chol_codelet_update_u21(matA[k][k], matA[k][j], ld, ld, size/nblocks, size/nblocks);
  48. for (i = k+1; i<nblocks; i++)
  49. {
  50. if (i <= j)
  51. {
  52. // prio = STARPU_DEFAULT_PRIO;
  53. // if (!noprio && (i == k + 1) && (j == k +1) ) prio = STARPU_MAX_PRIO;
  54. chol_codelet_update_u22(matA[k][i],
  55. matA[k][j],
  56. matA[i][j],
  57. size/nblocks, size/nblocks, size/nblocks, ld, ld, ld);
  58. }
  59. }
  60. }
  61. }
  62. #pragma starpu wait
  63. for(x = 0; x < nblocks ; x++) {
  64. for (y = 0; y < nblocks; y++) {
  65. #pragma starpu unregister matA[x][y]
  66. }
  67. }
  68. #pragma starpu wait
  69. gettimeofday(&end, NULL);
  70. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  71. fprintf(stderr, "Computation took (in ms)\n");
  72. fprintf(stdout, "%2.2f\n", timing/1000);
  73. double flop = (1.0f*size*size*size)/3.0f;
  74. fprintf(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  75. }
  76. int main(int argc, char **argv)
  77. {
  78. /* create a simple definite positive symetric matrix example
  79. *
  80. * Hilbert matrix : h(i,j) = 1/(i+j+1)
  81. * */
  82. float ***bmat;
  83. parse_args(argc, argv);
  84. #warning todo
  85. // struct starpu_conf conf;
  86. // starpu_conf_init(&conf);
  87. // conf.sched_policy_name = "heft";
  88. // conf.calibrate = 1;
  89. #pragma starpu initialize
  90. unsigned i,j,x,y;
  91. bmat = malloc(nblocks * sizeof(float *));
  92. for(x=0 ; x<nblocks ; x++)
  93. {
  94. bmat[x] = malloc(nblocks * sizeof(float *));
  95. for(y=0 ; y<nblocks ; y++)
  96. {
  97. starpu_malloc((void **)&bmat[x][y], BLOCKSIZE*BLOCKSIZE*sizeof(float));
  98. for (i = 0; i < BLOCKSIZE; i++)
  99. {
  100. for (j = 0; j < BLOCKSIZE; j++)
  101. {
  102. bmat[x][y][j +i*BLOCKSIZE] = (1.0f/(1.0f+(i+(x*BLOCKSIZE)+j+(y*BLOCKSIZE)))) + ((i+(x*BLOCKSIZE) == j+(y*BLOCKSIZE))?1.0f*size:0.0f);
  103. //mat[j +i*size] = ((i == j)?1.0f*size:0.0f);
  104. }
  105. }
  106. }
  107. }
  108. if (display) {
  109. for(y=0 ; y<nblocks ; y++)
  110. {
  111. for(x=0 ; x<nblocks ; x++)
  112. {
  113. printf("Block %d,%d :\n", x, y);
  114. for (j = 0; j < BLOCKSIZE; j++)
  115. {
  116. for (i = 0; i < BLOCKSIZE; i++)
  117. {
  118. if (i <= j) {
  119. printf("%2.2f\t", bmat[y][x][j +i*BLOCKSIZE]);
  120. }
  121. else {
  122. printf(".\t");
  123. }
  124. }
  125. printf("\n");
  126. }
  127. }
  128. }
  129. }
  130. dw_cholesky(bmat, size, size/nblocks, nblocks);
  131. #pragma starpu shutdown
  132. if (display) {
  133. printf("Results:\n");
  134. for(y=0 ; y<nblocks ; y++)
  135. {
  136. for(x=0 ; x<nblocks ; x++)
  137. {
  138. printf("Block %d,%d :\n", x, y);
  139. for (j = 0; j < BLOCKSIZE; j++)
  140. {
  141. for (i = 0; i < BLOCKSIZE; i++)
  142. {
  143. if (i <= j) {
  144. printf("%2.2f\t", bmat[y][x][j +i*BLOCKSIZE]);
  145. }
  146. else {
  147. printf(".\t");
  148. }
  149. }
  150. printf("\n");
  151. }
  152. }
  153. }
  154. }
  155. float *rmat = malloc(size*size*sizeof(float));
  156. for(x=0 ; x<nblocks ; x++) {
  157. for(y=0 ; y<nblocks ; y++) {
  158. for (i = 0; i < BLOCKSIZE; i++) {
  159. for (j = 0; j < BLOCKSIZE; j++) {
  160. rmat[j+(y*BLOCKSIZE)+(i+(x*BLOCKSIZE))*size] = bmat[x][y][j +i*BLOCKSIZE];
  161. }
  162. }
  163. }
  164. }
  165. fprintf(stderr, "compute explicit LLt ...\n");
  166. for (j = 0; j < size; j++)
  167. {
  168. for (i = 0; i < size; i++)
  169. {
  170. if (i > j) {
  171. rmat[j+i*size] = 0.0f; // debug
  172. }
  173. }
  174. }
  175. float *test_mat = malloc(size*size*sizeof(float));
  176. assert(test_mat);
  177. SSYRK("L", "N", size, size, 1.0f,
  178. rmat, size, 0.0f, test_mat, size);
  179. fprintf(stderr, "comparing results ...\n");
  180. if (display) {
  181. for (j = 0; j < size; j++)
  182. {
  183. for (i = 0; i < size; i++)
  184. {
  185. if (i <= j) {
  186. printf("%2.2f\t", test_mat[j +i*size]);
  187. }
  188. else {
  189. printf(".\t");
  190. }
  191. }
  192. printf("\n");
  193. }
  194. }
  195. int correctness = 1;
  196. for(x = 0; x < nblocks ; x++)
  197. {
  198. for (y = 0; y < nblocks; y++)
  199. {
  200. for (i = (size/nblocks)*x ; i < (size/nblocks)*x+(size/nblocks); i++)
  201. {
  202. for (j = (size/nblocks)*y ; j < (size/nblocks)*y+(size/nblocks); j++)
  203. {
  204. if (i <= j)
  205. {
  206. float orig = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  207. float err = abs(test_mat[j +i*size] - orig);
  208. if (err > 0.00001) {
  209. fprintf(stderr, "Error[%d, %d] --> %2.2f != %2.2f (err %2.2f)\n", i, j, test_mat[j +i*size], orig, err);
  210. correctness = 0;
  211. break;
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. for(x=0 ; x<nblocks ; x++)
  219. {
  220. for(y=0 ; y<nblocks ; y++)
  221. {
  222. starpu_free((void *)bmat[x][y]);
  223. }
  224. free(bmat[x]);
  225. }
  226. free(bmat);
  227. free(rmat);
  228. free(test_mat);
  229. starpu_helper_cublas_shutdown();
  230. #pragma starpu shutdown
  231. assert(correctness);
  232. return 0;
  233. }