dw_cholesky_no_stride.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include "dw_cholesky.h"
  18. #include "dw_cholesky_models.h"
  19. /* A [ y ] [ x ] */
  20. float *A[NMAXBLOCKS][NMAXBLOCKS];
  21. starpu_data_handle A_state[NMAXBLOCKS][NMAXBLOCKS];
  22. /*
  23. * Some useful functions
  24. */
  25. static struct starpu_task *create_task(starpu_tag_t id)
  26. {
  27. struct starpu_task *task = starpu_task_create();
  28. task->cl_arg = NULL;
  29. task->use_tag = 1;
  30. task->tag_id = id;
  31. return task;
  32. }
  33. /*
  34. * Create the codelets
  35. */
  36. static starpu_codelet cl11 =
  37. {
  38. .where = STARPU_CPU|STARPU_CUDA|STARPU_GORDON,
  39. .cpu_func = chol_cpu_codelet_update_u11,
  40. #ifdef STARPU_USE_CUDA
  41. .cuda_func = chol_cublas_codelet_update_u11,
  42. #endif
  43. #ifdef STARPU_USE_GORDON
  44. #ifdef SPU_FUNC_POTRF
  45. .gordon_func = SPU_FUNC_POTRF,
  46. #else
  47. #warning SPU_FUNC_POTRF is not available
  48. #endif
  49. #endif
  50. .nbuffers = 1,
  51. .model = &chol_model_11
  52. };
  53. static struct starpu_task * create_task_11(unsigned k, unsigned nblocks)
  54. {
  55. // printf("task 11 k = %d TAG = %llx\n", k, (TAG11(k)));
  56. struct starpu_task *task = create_task(TAG11(k));
  57. task->cl = &cl11;
  58. /* which sub-data is manipulated ? */
  59. task->buffers[0].handle = A_state[k][k];
  60. task->buffers[0].mode = STARPU_RW;
  61. /* this is an important task */
  62. task->priority = STARPU_MAX_PRIO;
  63. /* enforce dependencies ... */
  64. if (k > 0) {
  65. starpu_tag_declare_deps(TAG11(k), 1, TAG22(k-1, k, k));
  66. }
  67. return task;
  68. }
  69. static starpu_codelet cl21 =
  70. {
  71. .where = STARPU_CPU|STARPU_CUDA|STARPU_GORDON,
  72. .cpu_func = chol_cpu_codelet_update_u21,
  73. #ifdef STARPU_USE_CUDA
  74. .cuda_func = chol_cublas_codelet_update_u21,
  75. #endif
  76. #ifdef STARPU_USE_GORDON
  77. #ifdef SPU_FUNC_STRSM
  78. .gordon_func = SPU_FUNC_STRSM,
  79. #else
  80. #warning SPU_FUNC_STRSM is not available
  81. #endif
  82. #endif
  83. .nbuffers = 2,
  84. .model = &chol_model_21
  85. };
  86. static void create_task_21(unsigned k, unsigned j)
  87. {
  88. struct starpu_task *task = create_task(TAG21(k, j));
  89. task->cl = &cl21;
  90. /* which sub-data is manipulated ? */
  91. task->buffers[0].handle = A_state[k][k];
  92. task->buffers[0].mode = STARPU_R;
  93. task->buffers[1].handle = A_state[j][k];
  94. task->buffers[1].mode = STARPU_RW;
  95. if (j == k+1) {
  96. task->priority = STARPU_MAX_PRIO;
  97. }
  98. /* enforce dependencies ... */
  99. if (k > 0) {
  100. starpu_tag_declare_deps(TAG21(k, j), 2, TAG11(k), TAG22(k-1, k, j));
  101. }
  102. else {
  103. starpu_tag_declare_deps(TAG21(k, j), 1, TAG11(k));
  104. }
  105. starpu_task_submit(task);
  106. }
  107. static starpu_codelet cl22 =
  108. {
  109. .where = STARPU_CPU|STARPU_CUDA|STARPU_GORDON,
  110. .cpu_func = chol_cpu_codelet_update_u22,
  111. #ifdef STARPU_USE_CUDA
  112. .cuda_func = chol_cublas_codelet_update_u22,
  113. #endif
  114. #ifdef STARPU_USE_GORDON
  115. #ifdef SPU_FUNC_SGEMM
  116. .gordon_func = SPU_FUNC_SGEMM,
  117. #else
  118. #warning SPU_FUNC_SGEMM is not available
  119. #endif
  120. #endif
  121. .nbuffers = 3,
  122. .model = &chol_model_22
  123. };
  124. static void create_task_22(unsigned k, unsigned i, unsigned j)
  125. {
  126. // printf("task 22 k,i,j = %d,%d,%d TAG = %llx\n", k,i,j, TAG22(k,i,j));
  127. struct starpu_task *task = create_task(TAG22(k, i, j));
  128. task->cl = &cl22;
  129. /* which sub-data is manipulated ? */
  130. task->buffers[0].handle = A_state[i][k];
  131. task->buffers[0].mode = STARPU_R;
  132. task->buffers[1].handle = A_state[j][k];
  133. task->buffers[1].mode = STARPU_R;
  134. task->buffers[2].handle = A_state[j][i];
  135. task->buffers[2].mode = STARPU_RW;
  136. if ( (i == k + 1) && (j == k +1) ) {
  137. task->priority = STARPU_MAX_PRIO;
  138. }
  139. /* enforce dependencies ... */
  140. if (k > 0) {
  141. starpu_tag_declare_deps(TAG22(k, i, j), 3, TAG22(k-1, i, j), TAG21(k, i), TAG21(k, j));
  142. }
  143. else {
  144. starpu_tag_declare_deps(TAG22(k, i, j), 2, TAG21(k, i), TAG21(k, j));
  145. }
  146. starpu_task_submit(task);
  147. }
  148. /*
  149. * code to bootstrap the factorization
  150. * and construct the DAG
  151. */
  152. static void dw_cholesky_no_stride(void)
  153. {
  154. struct timeval start;
  155. struct timeval end;
  156. struct starpu_task *entry_task = NULL;
  157. /* create all the DAG nodes */
  158. unsigned i,j,k;
  159. for (k = 0; k < nblocks; k++)
  160. {
  161. struct starpu_task *task = create_task_11(k, nblocks);
  162. /* we defer the launch of the first task */
  163. if (k == 0) {
  164. entry_task = task;
  165. }
  166. else {
  167. starpu_task_submit(task);
  168. }
  169. for (j = k+1; j<nblocks; j++)
  170. {
  171. create_task_21(k, j);
  172. for (i = k+1; i<nblocks; i++)
  173. {
  174. if (i <= j)
  175. create_task_22(k, i, j);
  176. }
  177. }
  178. }
  179. /* schedule the codelet */
  180. gettimeofday(&start, NULL);
  181. starpu_task_submit(entry_task);
  182. /* stall the application until the end of computations */
  183. starpu_tag_wait(TAG11(nblocks-1));
  184. gettimeofday(&end, NULL);
  185. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  186. fprintf(stderr, "Computation took (in ms)\n");
  187. printf("%2.2f\n", timing/1000);
  188. double flop = (1.0f*size*size*size)/3.0f;
  189. fprintf(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  190. }
  191. int main(int argc, char **argv)
  192. {
  193. unsigned x, y;
  194. unsigned i, j;
  195. parse_args(argc, argv);
  196. assert(nblocks <= NMAXBLOCKS);
  197. fprintf(stderr, "BLOCK SIZE = %d\n", size / nblocks);
  198. starpu_init(NULL);
  199. /* Disable sequential consistency */
  200. starpu_data_set_default_sequential_consistency_flag(0);
  201. starpu_helper_cublas_init();
  202. for (y = 0; y < nblocks; y++)
  203. for (x = 0; x < nblocks; x++)
  204. {
  205. if (x <= y) {
  206. A[y][x] = malloc(BLOCKSIZE*BLOCKSIZE*sizeof(float));
  207. assert(A[y][x]);
  208. }
  209. }
  210. for (y = 0; y < nblocks; y++)
  211. for (x = 0; x < nblocks; x++)
  212. {
  213. if (x <= y) {
  214. #ifdef STARPU_HAVE_POSIX_MEMALIGN
  215. posix_memalign((void **)&A[y][x], 128, BLOCKSIZE*BLOCKSIZE*sizeof(float));
  216. #else
  217. A[y][x] = malloc(BLOCKSIZE*BLOCKSIZE*sizeof(float));
  218. #endif
  219. assert(A[y][x]);
  220. }
  221. }
  222. /* create a simple definite positive symetric matrix example
  223. *
  224. * Hilbert matrix : h(i,j) = 1/(i+j+1) ( + n In to make is stable )
  225. * */
  226. for (y = 0; y < nblocks; y++)
  227. for (x = 0; x < nblocks; x++)
  228. if (x <= y) {
  229. for (i = 0; i < BLOCKSIZE; i++)
  230. for (j = 0; j < BLOCKSIZE; j++)
  231. {
  232. A[y][x][i*BLOCKSIZE + j] =
  233. (float)(1.0f/((float) (1.0+(x*BLOCKSIZE+i)+(y*BLOCKSIZE+j))));
  234. /* make it a little more numerically stable ... ;) */
  235. if ((x == y) && (i == j))
  236. A[y][x][i*BLOCKSIZE + j] += (float)(2*size);
  237. }
  238. }
  239. for (y = 0; y < nblocks; y++)
  240. for (x = 0; x < nblocks; x++)
  241. {
  242. if (x <= y) {
  243. starpu_matrix_data_register(&A_state[y][x], 0, (uintptr_t)A[y][x],
  244. BLOCKSIZE, BLOCKSIZE, BLOCKSIZE, sizeof(float));
  245. }
  246. }
  247. dw_cholesky_no_stride();
  248. starpu_helper_cublas_shutdown();
  249. starpu_shutdown();
  250. return 0;
  251. }