cholesky.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #define __heap __attribute__ ((heap_allocated))
  21. /*
  22. * code to bootstrap the factorization
  23. * and construct the DAG
  24. */
  25. static void dw_cholesky(unsigned nblocks, unsigned size, unsigned ld, float *matA[nblocks][nblocks])
  26. {
  27. struct timeval start;
  28. struct timeval end;
  29. int x, y;
  30. /* create all the DAG nodes */
  31. unsigned i,j,k;
  32. for(x = 0; x < nblocks ; x++) {
  33. for (y = 0; y < nblocks; y++) {
  34. #pragma starpu register matA[x][y] size/nblocks*size/nblocks
  35. }
  36. }
  37. gettimeofday(&start, NULL);
  38. for (k = 0; k < nblocks; k++)
  39. {
  40. #warning deal with prio and models
  41. // int prio = STARPU_DEFAULT_PRIO;
  42. // if (!noprio) prio = STARPU_MAX_PRIO;
  43. chol_codelet_update_u11(matA[k][k], size/nblocks, ld);
  44. for (j = k+1; j<nblocks; j++)
  45. {
  46. // prio = STARPU_DEFAULT_PRIO;
  47. // if (!noprio&& (j == k+1)) prio = STARPU_MAX_PRIO;
  48. chol_codelet_update_u21(matA[k][k], matA[k][j], ld, ld, size/nblocks, size/nblocks);
  49. for (i = k+1; i<nblocks; i++)
  50. {
  51. if (i <= j)
  52. {
  53. // prio = STARPU_DEFAULT_PRIO;
  54. // if (!noprio && (i == k + 1) && (j == k +1) ) prio = STARPU_MAX_PRIO;
  55. chol_codelet_update_u22(matA[k][i],
  56. matA[k][j],
  57. matA[i][j],
  58. size/nblocks, size/nblocks, size/nblocks, ld, ld, ld);
  59. }
  60. }
  61. }
  62. }
  63. #pragma starpu wait
  64. for(x = 0; x < nblocks ; x++) {
  65. for (y = 0; y < nblocks; y++) {
  66. #pragma starpu unregister matA[x][y]
  67. }
  68. }
  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. parse_args(argc, argv);
  83. #warning todo
  84. // struct starpu_conf conf;
  85. // starpu_conf_init(&conf);
  86. // conf.sched_policy_name = "heft";
  87. // conf.calibrate = 1;
  88. #pragma starpu initialize
  89. starpu_helper_cublas_init();
  90. float *bmat[nblocks][nblocks] __heap;
  91. unsigned i,j,x,y;
  92. for(x=0 ; x<nblocks ; x++)
  93. {
  94. for(y=0 ; y<nblocks ; y++)
  95. {
  96. starpu_malloc((void **)&bmat[x][y], BLOCKSIZE*BLOCKSIZE*sizeof(float));
  97. for (i = 0; i < BLOCKSIZE; i++)
  98. {
  99. for (j = 0; j < BLOCKSIZE; j++)
  100. {
  101. 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);
  102. //mat[j +i*size] = ((i == j)?1.0f*size:0.0f);
  103. }
  104. }
  105. }
  106. }
  107. if (display) {
  108. for(y=0 ; y<nblocks ; y++)
  109. {
  110. for(x=0 ; x<nblocks ; x++)
  111. {
  112. printf("Block %d,%d :\n", x, y);
  113. for (j = 0; j < BLOCKSIZE; j++)
  114. {
  115. for (i = 0; i < BLOCKSIZE; i++)
  116. {
  117. if (i <= j) {
  118. printf("%2.2f\t", bmat[y][x][j +i*BLOCKSIZE]);
  119. }
  120. else {
  121. printf(".\t");
  122. }
  123. }
  124. printf("\n");
  125. }
  126. }
  127. }
  128. }
  129. dw_cholesky(nblocks, size, size/nblocks, bmat);
  130. if (display) {
  131. printf("Results:\n");
  132. for(y=0 ; y<nblocks ; y++)
  133. {
  134. for(x=0 ; x<nblocks ; x++)
  135. {
  136. printf("Block %d,%d :\n", x, y);
  137. for (j = 0; j < BLOCKSIZE; j++)
  138. {
  139. for (i = 0; i < BLOCKSIZE; i++)
  140. {
  141. if (i <= j) {
  142. printf("%2.2f\t", bmat[y][x][j +i*BLOCKSIZE]);
  143. }
  144. else {
  145. printf(".\t");
  146. }
  147. }
  148. printf("\n");
  149. }
  150. }
  151. }
  152. }
  153. float rmat[size * size] __heap;
  154. for(x=0 ; x<nblocks ; x++) {
  155. for(y=0 ; y<nblocks ; y++) {
  156. for (i = 0; i < BLOCKSIZE; i++) {
  157. for (j = 0; j < BLOCKSIZE; j++) {
  158. rmat[j+(y*BLOCKSIZE)+(i+(x*BLOCKSIZE))*size] = bmat[x][y][j +i*BLOCKSIZE];
  159. }
  160. }
  161. }
  162. }
  163. fprintf(stderr, "compute explicit LLt ...\n");
  164. for (j = 0; j < size; j++)
  165. {
  166. for (i = 0; i < size; i++)
  167. {
  168. if (i > j) {
  169. rmat[j+i*size] = 0.0f; // debug
  170. }
  171. }
  172. }
  173. float test_mat[size * size] __heap;
  174. SSYRK("L", "N", size, size, 1.0f,
  175. rmat, size, 0.0f, test_mat, size);
  176. fprintf(stderr, "comparing results ...\n");
  177. if (display) {
  178. for (j = 0; j < size; j++)
  179. {
  180. for (i = 0; i < size; i++)
  181. {
  182. if (i <= j) {
  183. printf("%2.2f\t", test_mat[j +i*size]);
  184. }
  185. else {
  186. printf(".\t");
  187. }
  188. }
  189. printf("\n");
  190. }
  191. }
  192. int correctness = 1;
  193. for(x = 0; x < nblocks ; x++)
  194. {
  195. for (y = 0; y < nblocks; y++)
  196. {
  197. for (i = (size/nblocks)*x ; i < (size/nblocks)*x+(size/nblocks); i++)
  198. {
  199. for (j = (size/nblocks)*y ; j < (size/nblocks)*y+(size/nblocks); j++)
  200. {
  201. if (i <= j)
  202. {
  203. float orig = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  204. float err = abs(test_mat[j +i*size] - orig);
  205. if (err > 0.00001) {
  206. fprintf(stderr, "Error[%d, %d] --> %2.2f != %2.2f (err %2.2f)\n", i, j, test_mat[j +i*size], orig, err);
  207. correctness = 0;
  208. break;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. }
  215. for(x=0 ; x<nblocks ; x++)
  216. {
  217. for(y=0 ; y<nblocks ; y++)
  218. {
  219. starpu_free((void *)bmat[x][y]);
  220. }
  221. }
  222. starpu_helper_cublas_shutdown();
  223. #pragma starpu shutdown
  224. assert(correctness);
  225. return 0;
  226. }