cholesky_tile_tag.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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 "cholesky.h"
  18. /* A [ y ] [ x ] */
  19. float *A[NMAXBLOCKS][NMAXBLOCKS];
  20. starpu_data_handle_t 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 struct starpu_codelet cl11 =
  36. {
  37. .modes = { STARPU_RW },
  38. .where = STARPU_CPU|STARPU_CUDA|STARPU_GORDON,
  39. .cpu_funcs = {chol_cpu_codelet_update_u11, NULL},
  40. #ifdef STARPU_USE_CUDA
  41. .cuda_funcs = {chol_cublas_codelet_update_u11, NULL},
  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. /* FPRINTF(stdout, "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->handles[0] = A_state[k][k];
  60. /* this is an important task */
  61. task->priority = STARPU_MAX_PRIO;
  62. /* enforce dependencies ... */
  63. if (k > 0)
  64. {
  65. starpu_tag_declare_deps(TAG11(k), 1, TAG22(k-1, k, k));
  66. }
  67. return task;
  68. }
  69. static struct starpu_codelet cl21 =
  70. {
  71. .modes = { STARPU_R, STARPU_RW },
  72. .where = STARPU_CPU|STARPU_CUDA|STARPU_GORDON,
  73. .cpu_funcs = {chol_cpu_codelet_update_u21, NULL},
  74. #ifdef STARPU_USE_CUDA
  75. .cuda_funcs = {chol_cublas_codelet_update_u21, NULL},
  76. #endif
  77. #ifdef STARPU_USE_GORDON
  78. #ifdef SPU_FUNC_STRSM
  79. .gordon_func = SPU_FUNC_STRSM,
  80. #else
  81. #warning SPU_FUNC_STRSM is not available
  82. #endif
  83. #endif
  84. .nbuffers = 2,
  85. .model = &chol_model_21
  86. };
  87. static int create_task_21(unsigned k, unsigned j)
  88. {
  89. int ret;
  90. struct starpu_task *task = create_task(TAG21(k, j));
  91. task->cl = &cl21;
  92. /* which sub-data is manipulated ? */
  93. task->handles[0] = A_state[k][k];
  94. task->handles[1] = A_state[j][k];
  95. if (j == k+1)
  96. {
  97. task->priority = STARPU_MAX_PRIO;
  98. }
  99. /* enforce dependencies ... */
  100. if (k > 0)
  101. {
  102. starpu_tag_declare_deps(TAG21(k, j), 2, TAG11(k), TAG22(k-1, k, j));
  103. }
  104. else
  105. {
  106. starpu_tag_declare_deps(TAG21(k, j), 1, TAG11(k));
  107. }
  108. ret = starpu_task_submit(task);
  109. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  110. return ret;
  111. }
  112. static struct starpu_codelet cl22 =
  113. {
  114. .modes = { STARPU_R, STARPU_R, STARPU_RW },
  115. .where = STARPU_CPU|STARPU_CUDA|STARPU_GORDON,
  116. .cpu_funcs = {chol_cpu_codelet_update_u22, NULL},
  117. #ifdef STARPU_USE_CUDA
  118. .cuda_funcs = {chol_cublas_codelet_update_u22, NULL},
  119. #endif
  120. #ifdef STARPU_USE_GORDON
  121. #ifdef SPU_FUNC_SGEMM
  122. .gordon_func = SPU_FUNC_SGEMM,
  123. #else
  124. #warning SPU_FUNC_SGEMM is not available
  125. #endif
  126. #endif
  127. .nbuffers = 3,
  128. .model = &chol_model_22
  129. };
  130. static int create_task_22(unsigned k, unsigned i, unsigned j)
  131. {
  132. int ret;
  133. /* FPRINTF(stdout, "task 22 k,i,j = %d,%d,%d TAG = %llx\n", k,i,j, TAG22(k,i,j)); */
  134. struct starpu_task *task = create_task(TAG22(k, i, j));
  135. task->cl = &cl22;
  136. /* which sub-data is manipulated ? */
  137. task->handles[0] = A_state[i][k];
  138. task->handles[1] = A_state[j][k];
  139. task->handles[2] = A_state[j][i];
  140. if ( (i == k + 1) && (j == k +1) )
  141. {
  142. task->priority = STARPU_MAX_PRIO;
  143. }
  144. /* enforce dependencies ... */
  145. if (k > 0)
  146. {
  147. starpu_tag_declare_deps(TAG22(k, i, j), 3, TAG22(k-1, i, j), TAG21(k, i), TAG21(k, j));
  148. }
  149. else
  150. {
  151. starpu_tag_declare_deps(TAG22(k, i, j), 2, TAG21(k, i), TAG21(k, j));
  152. }
  153. ret = starpu_task_submit(task);
  154. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  155. return ret;
  156. }
  157. /*
  158. * code to bootstrap the factorization
  159. * and construct the DAG
  160. */
  161. static int cholesky_no_stride(void)
  162. {
  163. int ret;
  164. struct timeval start;
  165. struct timeval end;
  166. struct starpu_task *entry_task = NULL;
  167. /* create all the DAG nodes */
  168. unsigned i,j,k;
  169. for (k = 0; k < nblocks; k++)
  170. {
  171. struct starpu_task *task = create_task_11(k, nblocks);
  172. /* we defer the launch of the first task */
  173. if (k == 0)
  174. {
  175. entry_task = task;
  176. }
  177. else
  178. {
  179. ret = starpu_task_submit(task);
  180. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  181. }
  182. for (j = k+1; j<nblocks; j++)
  183. {
  184. ret = create_task_21(k, j);
  185. if (ret == -ENODEV) return 77;
  186. for (i = k+1; i<nblocks; i++)
  187. {
  188. if (i <= j)
  189. {
  190. ret = create_task_22(k, i, j);
  191. if (ret == -ENODEV) return 77;
  192. }
  193. }
  194. }
  195. }
  196. /* schedule the codelet */
  197. gettimeofday(&start, NULL);
  198. ret = starpu_task_submit(entry_task);
  199. if (ret == -ENODEV) return 77;
  200. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  201. /* stall the application until the end of computations */
  202. starpu_tag_wait(TAG11(nblocks-1));
  203. gettimeofday(&end, NULL);
  204. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  205. FPRINTF(stderr, "Computation took (in ms)\n");
  206. FPRINTF(stdout, "%2.2f\n", timing/1000);
  207. double flop = (1.0f*size*size*size)/3.0f;
  208. FPRINTF(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  209. return 0;
  210. }
  211. int main(int argc, char **argv)
  212. {
  213. unsigned x, y;
  214. unsigned i, j;
  215. int ret;
  216. parse_args(argc, argv);
  217. assert(nblocks <= NMAXBLOCKS);
  218. FPRINTF(stderr, "BLOCK SIZE = %d\n", size / nblocks);
  219. ret = starpu_init(NULL);
  220. if (ret == -ENODEV)
  221. return 77;
  222. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  223. /* Disable sequential consistency */
  224. starpu_data_set_default_sequential_consistency_flag(0);
  225. starpu_helper_cublas_init();
  226. for (y = 0; y < nblocks; y++)
  227. for (x = 0; x < nblocks; x++)
  228. {
  229. if (x <= y)
  230. {
  231. #ifdef STARPU_HAVE_POSIX_MEMALIGN
  232. posix_memalign((void **)&A[y][x], 128, BLOCKSIZE*BLOCKSIZE*sizeof(float));
  233. #else
  234. A[y][x] = malloc(BLOCKSIZE*BLOCKSIZE*sizeof(float));
  235. #endif
  236. assert(A[y][x]);
  237. }
  238. }
  239. /* create a simple definite positive symetric matrix example
  240. *
  241. * Hilbert matrix : h(i,j) = 1/(i+j+1) ( + n In to make is stable )
  242. * */
  243. for (y = 0; y < nblocks; y++)
  244. for (x = 0; x < nblocks; x++)
  245. if (x <= y)
  246. {
  247. for (i = 0; i < BLOCKSIZE; i++)
  248. for (j = 0; j < BLOCKSIZE; j++)
  249. {
  250. A[y][x][i*BLOCKSIZE + j] =
  251. (float)(1.0f/((float) (1.0+(x*BLOCKSIZE+i)+(y*BLOCKSIZE+j))));
  252. /* make it a little more numerically stable ... ;) */
  253. if ((x == y) && (i == j))
  254. A[y][x][i*BLOCKSIZE + j] += (float)(2*size);
  255. }
  256. }
  257. for (y = 0; y < nblocks; y++)
  258. for (x = 0; x < nblocks; x++)
  259. {
  260. if (x <= y)
  261. {
  262. starpu_matrix_data_register(&A_state[y][x], 0, (uintptr_t)A[y][x],
  263. BLOCKSIZE, BLOCKSIZE, BLOCKSIZE, sizeof(float));
  264. }
  265. }
  266. ret = cholesky_no_stride();
  267. for (y = 0; y < nblocks; y++)
  268. for (x = 0; x < nblocks; x++)
  269. {
  270. if (x <= y)
  271. {
  272. starpu_data_unregister(A_state[y][x]);
  273. free(A[y][x]);
  274. }
  275. }
  276. starpu_helper_cublas_shutdown();
  277. starpu_shutdown();
  278. return ret;
  279. }