cholesky_tile_tag.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013 CNRS
  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. /*
  18. * This version of the Cholesky factorization uses explicit dependency
  19. * declaration through dependency tags.
  20. * It also directly registers matrix tiles instead of using partitioning.
  21. */
  22. #include "cholesky.h"
  23. #if defined(STARPU_USE_CUDA) && defined(STARPU_HAVE_MAGMA)
  24. #include "magma.h"
  25. #endif
  26. /* A [ y ] [ x ] */
  27. float *A[NMAXBLOCKS][NMAXBLOCKS];
  28. starpu_data_handle_t A_state[NMAXBLOCKS][NMAXBLOCKS];
  29. /*
  30. * Some useful functions
  31. */
  32. static struct starpu_task *create_task(starpu_tag_t id)
  33. {
  34. struct starpu_task *task = starpu_task_create();
  35. task->cl_arg = NULL;
  36. task->use_tag = 1;
  37. task->tag_id = id;
  38. return task;
  39. }
  40. /*
  41. * Create the codelets
  42. */
  43. static struct starpu_task * create_task_11(unsigned k, unsigned nblocks)
  44. {
  45. /* FPRINTF(stdout, "task 11 k = %d TAG = %llx\n", k, (TAG11(k))); */
  46. struct starpu_task *task = create_task(TAG11(k));
  47. task->cl = &cl11;
  48. /* which sub-data is manipulated ? */
  49. task->handles[0] = A_state[k][k];
  50. /* this is an important task */
  51. task->priority = STARPU_MAX_PRIO;
  52. /* enforce dependencies ... */
  53. if (k > 0)
  54. {
  55. starpu_tag_declare_deps(TAG11(k), 1, TAG22(k-1, k, k));
  56. }
  57. int n = starpu_matrix_get_nx(task->handles[0]);
  58. task->flops = FLOPS_SPOTRF(n);
  59. return task;
  60. }
  61. static int create_task_21(unsigned k, unsigned j)
  62. {
  63. int ret;
  64. struct starpu_task *task = create_task(TAG21(k, j));
  65. task->cl = &cl21;
  66. /* which sub-data is manipulated ? */
  67. task->handles[0] = A_state[k][k];
  68. task->handles[1] = A_state[j][k];
  69. if (j == k+1)
  70. {
  71. task->priority = STARPU_MAX_PRIO;
  72. }
  73. /* enforce dependencies ... */
  74. if (k > 0)
  75. {
  76. starpu_tag_declare_deps(TAG21(k, j), 2, TAG11(k), TAG22(k-1, k, j));
  77. }
  78. else
  79. {
  80. starpu_tag_declare_deps(TAG21(k, j), 1, TAG11(k));
  81. }
  82. int n = starpu_matrix_get_nx(task->handles[0]);
  83. task->flops = FLOPS_STRSM(n, n);
  84. ret = starpu_task_submit(task);
  85. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  86. return ret;
  87. }
  88. static int create_task_22(unsigned k, unsigned i, unsigned j)
  89. {
  90. int ret;
  91. /* FPRINTF(stdout, "task 22 k,i,j = %d,%d,%d TAG = %llx\n", k,i,j, TAG22(k,i,j)); */
  92. struct starpu_task *task = create_task(TAG22(k, i, j));
  93. task->cl = &cl22;
  94. /* which sub-data is manipulated ? */
  95. task->handles[0] = A_state[i][k];
  96. task->handles[1] = A_state[j][k];
  97. task->handles[2] = A_state[j][i];
  98. if ( (i == k + 1) && (j == k +1) )
  99. {
  100. task->priority = STARPU_MAX_PRIO;
  101. }
  102. /* enforce dependencies ... */
  103. if (k > 0)
  104. {
  105. starpu_tag_declare_deps(TAG22(k, i, j), 3, TAG22(k-1, i, j), TAG21(k, i), TAG21(k, j));
  106. }
  107. else
  108. {
  109. starpu_tag_declare_deps(TAG22(k, i, j), 2, TAG21(k, i), TAG21(k, j));
  110. }
  111. int n = starpu_matrix_get_nx(task->handles[0]);
  112. task->flops = FLOPS_SGEMM(n, n, n);
  113. ret = starpu_task_submit(task);
  114. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  115. return ret;
  116. }
  117. /*
  118. * code to bootstrap the factorization
  119. * and construct the DAG
  120. */
  121. static int cholesky_no_stride(void)
  122. {
  123. int ret;
  124. double start;
  125. double end;
  126. struct starpu_task *entry_task = NULL;
  127. /* create all the DAG nodes */
  128. unsigned i,j,k;
  129. for (k = 0; k < nblocks; k++)
  130. {
  131. struct starpu_task *task = create_task_11(k, nblocks);
  132. /* we defer the launch of the first task */
  133. if (k == 0)
  134. {
  135. entry_task = task;
  136. }
  137. else
  138. {
  139. ret = starpu_task_submit(task);
  140. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  141. }
  142. for (j = k+1; j<nblocks; j++)
  143. {
  144. ret = create_task_21(k, j);
  145. if (ret == -ENODEV) return 77;
  146. for (i = k+1; i<nblocks; i++)
  147. {
  148. if (i <= j)
  149. {
  150. ret = create_task_22(k, i, j);
  151. if (ret == -ENODEV) return 77;
  152. }
  153. }
  154. }
  155. }
  156. /* schedule the codelet */
  157. start = starpu_timing_now();
  158. ret = starpu_task_submit(entry_task);
  159. if (ret == -ENODEV) return 77;
  160. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  161. /* stall the application until the end of computations */
  162. starpu_tag_wait(TAG11(nblocks-1));
  163. end = starpu_timing_now();
  164. double timing = end - start;
  165. double flop = (1.0f*size*size*size)/3.0f;
  166. PRINTF("# size\tms\tGFlops\n");
  167. PRINTF("%u\t%.0f\t%.1f\n", size, timing/1000, (flop/timing/1000.0f));
  168. return 0;
  169. }
  170. int main(int argc, char **argv)
  171. {
  172. unsigned x, y;
  173. int ret;
  174. parse_args(argc, argv);
  175. assert(nblocks <= NMAXBLOCKS);
  176. FPRINTF(stderr, "BLOCK SIZE = %d\n", size / nblocks);
  177. #ifdef STARPU_HAVE_MAGMA
  178. magma_init();
  179. #endif
  180. ret = starpu_init(NULL);
  181. if (ret == -ENODEV)
  182. return 77;
  183. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  184. #ifdef STARPU_USE_CUDA
  185. initialize_chol_model(&chol_model_11,"chol_model_11",cpu_chol_task_11_cost,cuda_chol_task_11_cost);
  186. initialize_chol_model(&chol_model_21,"chol_model_21",cpu_chol_task_21_cost,cuda_chol_task_21_cost);
  187. initialize_chol_model(&chol_model_22,"chol_model_22",cpu_chol_task_22_cost,cuda_chol_task_22_cost);
  188. #else
  189. initialize_chol_model(&chol_model_11,"chol_model_11",cpu_chol_task_11_cost,NULL);
  190. initialize_chol_model(&chol_model_21,"chol_model_21",cpu_chol_task_21_cost,NULL);
  191. initialize_chol_model(&chol_model_22,"chol_model_22",cpu_chol_task_22_cost,NULL);
  192. #endif
  193. /* Disable sequential consistency */
  194. starpu_data_set_default_sequential_consistency_flag(0);
  195. starpu_cublas_init();
  196. #ifndef STARPU_SIMGRID
  197. for (y = 0; y < nblocks; y++)
  198. for (x = 0; x < nblocks; x++)
  199. {
  200. if (x <= y)
  201. {
  202. #ifdef STARPU_HAVE_POSIX_MEMALIGN
  203. posix_memalign((void **)&A[y][x], 128, BLOCKSIZE*BLOCKSIZE*sizeof(float));
  204. #else
  205. A[y][x] = malloc(BLOCKSIZE*BLOCKSIZE*sizeof(float));
  206. #endif
  207. assert(A[y][x]);
  208. }
  209. }
  210. /* create a simple definite positive symetric matrix example
  211. *
  212. * Hilbert matrix : h(i,j) = 1/(i+j+1) ( + n In to make is stable )
  213. * */
  214. for (y = 0; y < nblocks; y++)
  215. for (x = 0; x < nblocks; x++)
  216. if (x <= y)
  217. {
  218. unsigned i, j;
  219. for (i = 0; i < BLOCKSIZE; i++)
  220. for (j = 0; j < BLOCKSIZE; j++)
  221. {
  222. A[y][x][i*BLOCKSIZE + j] =
  223. (float)(1.0f/((float) (1.0+(x*BLOCKSIZE+i)+(y*BLOCKSIZE+j))));
  224. /* make it a little more numerically stable ... ;) */
  225. if ((x == y) && (i == j))
  226. A[y][x][i*BLOCKSIZE + j] += (float)(2*size);
  227. }
  228. }
  229. #endif
  230. for (y = 0; y < nblocks; y++)
  231. for (x = 0; x < nblocks; x++)
  232. {
  233. if (x <= y)
  234. {
  235. starpu_matrix_data_register(&A_state[y][x], STARPU_MAIN_RAM, (uintptr_t)A[y][x],
  236. BLOCKSIZE, BLOCKSIZE, BLOCKSIZE, sizeof(float));
  237. }
  238. }
  239. ret = cholesky_no_stride();
  240. for (y = 0; y < nblocks; y++)
  241. for (x = 0; x < nblocks; x++)
  242. {
  243. if (x <= y)
  244. {
  245. starpu_data_unregister(A_state[y][x]);
  246. free(A[y][x]);
  247. }
  248. }
  249. starpu_cublas_shutdown();
  250. starpu_shutdown();
  251. return ret;
  252. }