dw_cholesky_no_stride.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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. static void terminal_callback(void *argcb)
  33. {
  34. sem_t *sem = argcb;
  35. sem_post(sem);
  36. }
  37. /*
  38. * Create the codelets
  39. */
  40. static starpu_codelet cl11 =
  41. {
  42. .where = STARPU_CPU|STARPU_CUDA|STARPU_GORDON,
  43. .cpu_func = chol_cpu_codelet_update_u11,
  44. #ifdef STARPU_USE_CUDA
  45. .cuda_func = chol_cublas_codelet_update_u11,
  46. #endif
  47. #ifdef STARPU_USE_GORDON
  48. #ifdef SPU_FUNC_POTRF
  49. .gordon_func = SPU_FUNC_POTRF,
  50. #else
  51. #warning SPU_FUNC_POTRF is not available
  52. #endif
  53. #endif
  54. .nbuffers = 1,
  55. .model = &chol_model_11
  56. };
  57. static struct starpu_task * create_task_11(unsigned k, unsigned nblocks, sem_t *sem)
  58. {
  59. // printf("task 11 k = %d TAG = %llx\n", k, (TAG11(k)));
  60. struct starpu_task *task = create_task(TAG11(k));
  61. task->cl = &cl11;
  62. /* which sub-data is manipulated ? */
  63. task->buffers[0].handle = A_state[k][k];
  64. task->buffers[0].mode = STARPU_RW;
  65. /* this is an important task */
  66. task->priority = STARPU_MAX_PRIO;
  67. /* enforce dependencies ... */
  68. if (k > 0) {
  69. starpu_tag_declare_deps(TAG11(k), 1, TAG22(k-1, k, k));
  70. }
  71. /* the very last task must be notified */
  72. if (k == nblocks - 1) {
  73. task->callback_func = terminal_callback;
  74. task->callback_arg = sem;
  75. }
  76. return task;
  77. }
  78. static starpu_codelet cl21 =
  79. {
  80. .where = STARPU_CPU|STARPU_CUDA|STARPU_GORDON,
  81. .cpu_func = chol_cpu_codelet_update_u21,
  82. #ifdef STARPU_USE_CUDA
  83. .cuda_func = chol_cublas_codelet_update_u21,
  84. #endif
  85. #ifdef STARPU_USE_GORDON
  86. #ifdef SPU_FUNC_STRSM
  87. .gordon_func = SPU_FUNC_STRSM,
  88. #else
  89. #warning SPU_FUNC_STRSM is not available
  90. #endif
  91. #endif
  92. .nbuffers = 2,
  93. .model = &chol_model_21
  94. };
  95. static void create_task_21(unsigned k, unsigned j)
  96. {
  97. struct starpu_task *task = create_task(TAG21(k, j));
  98. task->cl = &cl21;
  99. /* which sub-data is manipulated ? */
  100. task->buffers[0].handle = A_state[k][k];
  101. task->buffers[0].mode = STARPU_R;
  102. task->buffers[1].handle = A_state[j][k];
  103. task->buffers[1].mode = STARPU_RW;
  104. if (j == k+1) {
  105. task->priority = STARPU_MAX_PRIO;
  106. }
  107. /* enforce dependencies ... */
  108. if (k > 0) {
  109. starpu_tag_declare_deps(TAG21(k, j), 2, TAG11(k), TAG22(k-1, k, j));
  110. }
  111. else {
  112. starpu_tag_declare_deps(TAG21(k, j), 1, TAG11(k));
  113. }
  114. starpu_submit_task(task);
  115. }
  116. static starpu_codelet cl22 =
  117. {
  118. .where = STARPU_CPU|STARPU_CUDA|STARPU_GORDON,
  119. .cpu_func = chol_cpu_codelet_update_u22,
  120. #ifdef STARPU_USE_CUDA
  121. .cuda_func = chol_cublas_codelet_update_u22,
  122. #endif
  123. #ifdef STARPU_USE_GORDON
  124. #ifdef SPU_FUNC_SGEMM
  125. .gordon_func = SPU_FUNC_SGEMM,
  126. #else
  127. #warning SPU_FUNC_SGEMM is not available
  128. #endif
  129. #endif
  130. .nbuffers = 3,
  131. .model = &chol_model_22
  132. };
  133. static void create_task_22(unsigned k, unsigned i, unsigned j)
  134. {
  135. // printf("task 22 k,i,j = %d,%d,%d TAG = %llx\n", k,i,j, TAG22(k,i,j));
  136. struct starpu_task *task = create_task(TAG22(k, i, j));
  137. task->cl = &cl22;
  138. /* which sub-data is manipulated ? */
  139. task->buffers[0].handle = A_state[i][k];
  140. task->buffers[0].mode = STARPU_R;
  141. task->buffers[1].handle = A_state[j][k];
  142. task->buffers[1].mode = STARPU_R;
  143. task->buffers[2].handle = A_state[j][i];
  144. task->buffers[2].mode = STARPU_RW;
  145. if ( (i == k + 1) && (j == k +1) ) {
  146. task->priority = STARPU_MAX_PRIO;
  147. }
  148. /* enforce dependencies ... */
  149. if (k > 0) {
  150. starpu_tag_declare_deps(TAG22(k, i, j), 3, TAG22(k-1, i, j), TAG21(k, i), TAG21(k, j));
  151. }
  152. else {
  153. starpu_tag_declare_deps(TAG22(k, i, j), 2, TAG21(k, i), TAG21(k, j));
  154. }
  155. starpu_submit_task(task);
  156. }
  157. /*
  158. * code to bootstrap the factorization
  159. * and construct the DAG
  160. */
  161. static void dw_cholesky_no_stride(void)
  162. {
  163. struct timeval start;
  164. struct timeval end;
  165. /* create a new codelet */
  166. sem_t sem;
  167. sem_init(&sem, 0, 0U);
  168. struct starpu_task *entry_task = NULL;
  169. /* create all the DAG nodes */
  170. unsigned i,j,k;
  171. for (k = 0; k < nblocks; k++)
  172. {
  173. struct starpu_task *task = create_task_11(k, nblocks, &sem);
  174. /* we defer the launch of the first task */
  175. if (k == 0) {
  176. entry_task = task;
  177. }
  178. else {
  179. starpu_submit_task(task);
  180. }
  181. for (j = k+1; j<nblocks; j++)
  182. {
  183. create_task_21(k, j);
  184. for (i = k+1; i<nblocks; i++)
  185. {
  186. if (i <= j)
  187. create_task_22(k, i, j);
  188. }
  189. }
  190. }
  191. /* schedule the codelet */
  192. gettimeofday(&start, NULL);
  193. starpu_submit_task(entry_task);
  194. /* stall the application until the end of computations */
  195. sem_wait(&sem);
  196. sem_destroy(&sem);
  197. gettimeofday(&end, NULL);
  198. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  199. fprintf(stderr, "Computation took (in ms)\n");
  200. printf("%2.2f\n", timing/1000);
  201. double flop = (1.0f*size*size*size)/3.0f;
  202. fprintf(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  203. }
  204. int main(int argc, char **argv)
  205. {
  206. unsigned x, y;
  207. unsigned i, j;
  208. parse_args(argc, argv);
  209. assert(nblocks <= NMAXBLOCKS);
  210. fprintf(stderr, "BLOCK SIZE = %d\n", size / nblocks);
  211. starpu_init(NULL);
  212. starpu_helper_init_cublas();
  213. timing_init();
  214. for (y = 0; y < nblocks; y++)
  215. for (x = 0; x < nblocks; x++)
  216. {
  217. if (x <= y) {
  218. A[y][x] = malloc(BLOCKSIZE*BLOCKSIZE*sizeof(float));
  219. assert(A[y][x]);
  220. }
  221. }
  222. for (y = 0; y < nblocks; y++)
  223. for (x = 0; x < nblocks; x++)
  224. {
  225. if (x <= y) {
  226. #ifdef STARPU_HAVE_POSIX_MEMALIGN
  227. posix_memalign((void **)&A[y][x], 128, BLOCKSIZE*BLOCKSIZE*sizeof(float));
  228. #else
  229. A[y][x] = malloc(BLOCKSIZE*BLOCKSIZE*sizeof(float));
  230. #endif
  231. assert(A[y][x]);
  232. }
  233. }
  234. /* create a simple definite positive symetric matrix example
  235. *
  236. * Hilbert matrix : h(i,j) = 1/(i+j+1) ( + n In to make is stable )
  237. * */
  238. for (y = 0; y < nblocks; y++)
  239. for (x = 0; x < nblocks; x++)
  240. if (x <= y) {
  241. for (i = 0; i < BLOCKSIZE; i++)
  242. for (j = 0; j < BLOCKSIZE; j++)
  243. {
  244. A[y][x][i*BLOCKSIZE + j] =
  245. (float)(1.0f/((float) (1.0+(x*BLOCKSIZE+i)+(y*BLOCKSIZE+j))));
  246. /* make it a little more numerically stable ... ;) */
  247. if ((x == y) && (i == j))
  248. A[y][x][i*BLOCKSIZE + j] += (float)(2*size);
  249. }
  250. }
  251. for (y = 0; y < nblocks; y++)
  252. for (x = 0; x < nblocks; x++)
  253. {
  254. if (x <= y) {
  255. starpu_register_blas_data(&A_state[y][x], 0, (uintptr_t)A[y][x],
  256. BLOCKSIZE, BLOCKSIZE, BLOCKSIZE, sizeof(float));
  257. }
  258. }
  259. dw_cholesky_no_stride();
  260. starpu_helper_shutdown_cublas();
  261. starpu_shutdown();
  262. return 0;
  263. }