dw_cholesky_no_stride.c 6.8 KB

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