dw_cholesky_no_stride.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 = ANY,
  43. .core_func = chol_core_codelet_update_u11,
  44. #ifdef USE_CUDA
  45. .cublas_func = chol_cublas_codelet_update_u11,
  46. #endif
  47. #ifdef USE_GORDON
  48. .gordon_func = SPU_FUNC_POTRF,
  49. #endif
  50. .nbuffers = 1,
  51. .model = &chol_model_11
  52. };
  53. static struct starpu_task * create_task_11(unsigned k, unsigned nblocks, sem_t *sem)
  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].state = A_state[k][k];
  60. task->buffers[0].mode = RW;
  61. /* this is an important task */
  62. task->priority = MAX_PRIO;
  63. /* enforce dependencies ... */
  64. if (k > 0) {
  65. starpu_tag_declare_deps(TAG11(k), 1, TAG22(k-1, k, k));
  66. }
  67. /* the very last task must be notified */
  68. if (k == nblocks - 1) {
  69. task->callback_func = terminal_callback;
  70. task->callback_arg = sem;
  71. }
  72. return task;
  73. }
  74. static starpu_codelet cl21 =
  75. {
  76. .where = ANY,
  77. .core_func = chol_core_codelet_update_u21,
  78. #ifdef USE_CUDA
  79. .cublas_func = chol_cublas_codelet_update_u21,
  80. #endif
  81. #ifdef USE_GORDON
  82. .gordon_func = SPU_FUNC_STRSM,
  83. #endif
  84. .nbuffers = 2,
  85. .model = &chol_model_21
  86. };
  87. static void create_task_21(unsigned k, unsigned j)
  88. {
  89. struct starpu_task *task = create_task(TAG21(k, j));
  90. task->cl = &cl21;
  91. /* which sub-data is manipulated ? */
  92. task->buffers[0].state = A_state[k][k];
  93. task->buffers[0].mode = R;
  94. task->buffers[1].state = A_state[j][k];
  95. task->buffers[1].mode = RW;
  96. if (j == k+1) {
  97. task->priority = MAX_PRIO;
  98. }
  99. /* enforce dependencies ... */
  100. if (k > 0) {
  101. starpu_tag_declare_deps(TAG21(k, j), 2, TAG11(k), TAG22(k-1, k, j));
  102. }
  103. else {
  104. starpu_tag_declare_deps(TAG21(k, j), 1, TAG11(k));
  105. }
  106. starpu_submit_task(task);
  107. }
  108. static starpu_codelet cl22 =
  109. {
  110. .where = ANY,
  111. .core_func = chol_core_codelet_update_u22,
  112. #ifdef USE_CUDA
  113. .cublas_func = chol_cublas_codelet_update_u22,
  114. #endif
  115. #ifdef USE_GORDON
  116. .gordon_func = SPU_FUNC_SGEMM,
  117. #endif
  118. .nbuffers = 3,
  119. .model = &chol_model_22
  120. };
  121. static void create_task_22(unsigned k, unsigned i, unsigned j)
  122. {
  123. // printf("task 22 k,i,j = %d,%d,%d TAG = %llx\n", k,i,j, TAG22(k,i,j));
  124. struct starpu_task *task = create_task(TAG22(k, i, j));
  125. task->cl = &cl22;
  126. /* which sub-data is manipulated ? */
  127. task->buffers[0].state = A_state[i][k];
  128. task->buffers[0].mode = R;
  129. task->buffers[1].state = A_state[j][k];
  130. task->buffers[1].mode = R;
  131. task->buffers[2].state = A_state[j][i];
  132. task->buffers[2].mode = RW;
  133. if ( (i == k + 1) && (j == k +1) ) {
  134. task->priority = MAX_PRIO;
  135. }
  136. /* enforce dependencies ... */
  137. if (k > 0) {
  138. starpu_tag_declare_deps(TAG22(k, i, j), 3, TAG22(k-1, i, j), TAG21(k, i), TAG21(k, j));
  139. }
  140. else {
  141. starpu_tag_declare_deps(TAG22(k, i, j), 2, TAG21(k, i), TAG21(k, j));
  142. }
  143. starpu_submit_task(task);
  144. }
  145. /*
  146. * code to bootstrap the factorization
  147. * and construct the DAG
  148. */
  149. static void dw_cholesky_no_stride(void)
  150. {
  151. struct timeval start;
  152. struct timeval end;
  153. /* create a new codelet */
  154. sem_t sem;
  155. sem_init(&sem, 0, 0U);
  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, &sem);
  162. /* we defer the launch of the first task */
  163. if (k == 0) {
  164. entry_task = task;
  165. }
  166. else {
  167. starpu_submit_task(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_submit_task(entry_task);
  182. /* stall the application until the end of computations */
  183. sem_wait(&sem);
  184. sem_destroy(&sem);
  185. gettimeofday(&end, NULL);
  186. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  187. fprintf(stderr, "Computation took (in ms)\n");
  188. printf("%2.2f\n", timing/1000);
  189. double flop = (1.0f*size*size*size)/3.0f;
  190. fprintf(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  191. }
  192. int main(int argc, char **argv)
  193. {
  194. unsigned x, y;
  195. unsigned i, j;
  196. parse_args(argc, argv);
  197. assert(nblocks <= NMAXBLOCKS);
  198. fprintf(stderr, "BLOCK SIZE = %d\n", size / nblocks);
  199. starpu_init();
  200. timing_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. posix_memalign((void **)&A[y][x], 128, BLOCKSIZE*BLOCKSIZE*sizeof(float));
  214. assert(A[y][x]);
  215. }
  216. }
  217. /* create a simple definite positive symetric matrix example
  218. *
  219. * Hilbert matrix : h(i,j) = 1/(i+j+1) ( + n In to make is stable )
  220. * */
  221. for (y = 0; y < nblocks; y++)
  222. for (x = 0; x < nblocks; x++)
  223. if (x <= y) {
  224. for (i = 0; i < BLOCKSIZE; i++)
  225. for (j = 0; j < BLOCKSIZE; j++)
  226. {
  227. A[y][x][i*BLOCKSIZE + j] =
  228. (float)(1.0f/((float) (1.0+(x*BLOCKSIZE+i)+(y*BLOCKSIZE+j))));
  229. /* make it a little more numerically stable ... ;) */
  230. if ((x == y) && (i == j))
  231. A[y][x][i*BLOCKSIZE + j] += (float)(2*size);
  232. }
  233. }
  234. for (y = 0; y < nblocks; y++)
  235. for (x = 0; x < nblocks; x++)
  236. {
  237. if (x <= y) {
  238. starpu_monitor_blas_data(&A_state[y][x], 0, (uintptr_t)A[y][x],
  239. BLOCKSIZE, BLOCKSIZE, BLOCKSIZE, sizeof(float));
  240. }
  241. }
  242. dw_cholesky_no_stride();
  243. starpu_shutdown();
  244. return 0;
  245. }