cholesky_tag.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include "cholesky.h"
  19. /*
  20. * Some useful functions
  21. */
  22. static struct starpu_task *create_task(starpu_tag_t id)
  23. {
  24. struct starpu_task *task = starpu_task_create();
  25. task->cl_arg = NULL;
  26. task->use_tag = 1;
  27. task->tag_id = id;
  28. return task;
  29. }
  30. /*
  31. * Create the codelets
  32. */
  33. static struct starpu_codelet cl11 =
  34. {
  35. .modes = { STARPU_RW },
  36. .where = STARPU_CPU|STARPU_CUDA,
  37. .cpu_funcs = {chol_cpu_codelet_update_u11, NULL},
  38. #ifdef STARPU_USE_CUDA
  39. .cuda_funcs = {chol_cublas_codelet_update_u11, NULL},
  40. #endif
  41. .nbuffers = 1,
  42. .model = &chol_model_11
  43. };
  44. static struct starpu_task * create_task_11(starpu_data_handle_t dataA, unsigned k)
  45. {
  46. /* FPRINTF(stdout, "task 11 k = %d TAG = %llx\n", k, (TAG11(k))); */
  47. struct starpu_task *task = create_task(TAG11(k));
  48. task->cl = &cl11;
  49. /* which sub-data is manipulated ? */
  50. task->handles[0] = starpu_data_get_sub_data(dataA, 2, k, k);
  51. /* this is an important task */
  52. if (!noprio)
  53. task->priority = STARPU_MAX_PRIO;
  54. /* enforce dependencies ... */
  55. if (k > 0)
  56. {
  57. starpu_tag_declare_deps(TAG11(k), 1, TAG22(k-1, k, k));
  58. }
  59. return task;
  60. }
  61. static struct starpu_codelet cl21 =
  62. {
  63. .modes = { STARPU_R, STARPU_RW },
  64. .where = STARPU_CPU|STARPU_CUDA,
  65. .cpu_funcs = {chol_cpu_codelet_update_u21, NULL},
  66. #ifdef STARPU_USE_CUDA
  67. .cuda_funcs = {chol_cublas_codelet_update_u21, NULL},
  68. #endif
  69. .nbuffers = 2,
  70. .model = &chol_model_21
  71. };
  72. static void create_task_21(starpu_data_handle_t dataA, unsigned k, unsigned j)
  73. {
  74. struct starpu_task *task = create_task(TAG21(k, j));
  75. task->cl = &cl21;
  76. /* which sub-data is manipulated ? */
  77. task->handles[0] = starpu_data_get_sub_data(dataA, 2, k, k);
  78. task->handles[1] = starpu_data_get_sub_data(dataA, 2, k, j);
  79. if (!noprio && (j == k+1))
  80. {
  81. task->priority = STARPU_MAX_PRIO;
  82. }
  83. /* enforce dependencies ... */
  84. if (k > 0)
  85. {
  86. starpu_tag_declare_deps(TAG21(k, j), 2, TAG11(k), TAG22(k-1, k, j));
  87. }
  88. else
  89. {
  90. starpu_tag_declare_deps(TAG21(k, j), 1, TAG11(k));
  91. }
  92. int ret = starpu_task_submit(task);
  93. if (STARPU_UNLIKELY(ret == -ENODEV))
  94. {
  95. FPRINTF(stderr, "No worker may execute this task\n");
  96. exit(0);
  97. }
  98. }
  99. static struct starpu_codelet cl22 =
  100. {
  101. .modes = { STARPU_R, STARPU_R, STARPU_RW },
  102. .where = STARPU_CPU|STARPU_CUDA,
  103. .cpu_funcs = {chol_cpu_codelet_update_u22, NULL},
  104. #ifdef STARPU_USE_CUDA
  105. .cuda_funcs = {chol_cublas_codelet_update_u22, NULL},
  106. #endif
  107. .nbuffers = 3,
  108. .model = &chol_model_22
  109. };
  110. static void create_task_22(starpu_data_handle_t dataA, unsigned k, unsigned i, unsigned j)
  111. {
  112. /* FPRINTF(stdout, "task 22 k,i,j = %d,%d,%d TAG = %llx\n", k,i,j, TAG22(k,i,j)); */
  113. struct starpu_task *task = create_task(TAG22(k, i, j));
  114. task->cl = &cl22;
  115. /* which sub-data is manipulated ? */
  116. task->handles[0] = starpu_data_get_sub_data(dataA, 2, k, i);
  117. task->handles[1] = starpu_data_get_sub_data(dataA, 2, k, j);
  118. task->handles[2] = starpu_data_get_sub_data(dataA, 2, i, j);
  119. if (!noprio && (i == k + 1) && (j == k +1) )
  120. {
  121. task->priority = STARPU_MAX_PRIO;
  122. }
  123. /* enforce dependencies ... */
  124. if (k > 0)
  125. {
  126. starpu_tag_declare_deps(TAG22(k, i, j), 3, TAG22(k-1, i, j), TAG21(k, i), TAG21(k, j));
  127. }
  128. else
  129. {
  130. starpu_tag_declare_deps(TAG22(k, i, j), 2, TAG21(k, i), TAG21(k, j));
  131. }
  132. int ret = starpu_task_submit(task);
  133. if (STARPU_UNLIKELY(ret == -ENODEV))
  134. {
  135. FPRINTF(stderr, "No worker may execute this task\n");
  136. exit(0);
  137. }
  138. }
  139. /*
  140. * code to bootstrap the factorization
  141. * and construct the DAG
  142. */
  143. static void _cholesky(starpu_data_handle_t dataA, unsigned nblocks)
  144. {
  145. struct timeval start;
  146. struct timeval end;
  147. struct starpu_task *entry_task = NULL;
  148. /* create all the DAG nodes */
  149. unsigned i,j,k;
  150. gettimeofday(&start, NULL);
  151. for (k = 0; k < nblocks; k++)
  152. {
  153. struct starpu_task *task = create_task_11(dataA, k);
  154. /* we defer the launch of the first task */
  155. if (k == 0)
  156. {
  157. entry_task = task;
  158. }
  159. else
  160. {
  161. int ret = starpu_task_submit(task);
  162. if (STARPU_UNLIKELY(ret == -ENODEV))
  163. {
  164. FPRINTF(stderr, "No worker may execute this task\n");
  165. exit(0);
  166. }
  167. }
  168. for (j = k+1; j<nblocks; j++)
  169. {
  170. create_task_21(dataA, k, j);
  171. for (i = k+1; i<nblocks; i++)
  172. {
  173. if (i <= j)
  174. create_task_22(dataA, k, i, j);
  175. }
  176. }
  177. }
  178. /* schedule the codelet */
  179. int ret = starpu_task_submit(entry_task);
  180. if (STARPU_UNLIKELY(ret == -ENODEV))
  181. {
  182. FPRINTF(stderr, "No worker may execute this task\n");
  183. exit(0);
  184. }
  185. /* stall the application until the end of computations */
  186. starpu_tag_wait(TAG11(nblocks-1));
  187. starpu_data_unpartition(dataA, 0);
  188. gettimeofday(&end, NULL);
  189. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  190. FPRINTF(stderr, "Computation took (in ms)\n");
  191. FPRINTF(stdout, "%2.2f\n", timing/1000);
  192. unsigned n = starpu_matrix_get_nx(dataA);
  193. double flop = (1.0f*n*n*n)/3.0f;
  194. FPRINTF(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  195. }
  196. static void initialize_system(float **A, unsigned dim, unsigned pinned)
  197. {
  198. starpu_init(NULL);
  199. starpu_helper_cublas_init();
  200. if (pinned)
  201. {
  202. starpu_malloc((void **)A, (size_t)dim*dim*sizeof(float));
  203. }
  204. else
  205. {
  206. *A = malloc(dim*dim*sizeof(float));
  207. }
  208. }
  209. static void cholesky(float *matA, unsigned size, unsigned ld, unsigned nblocks)
  210. {
  211. starpu_data_handle_t dataA;
  212. /* monitor and partition the A matrix into blocks :
  213. * one block is now determined by 2 unsigned (i,j) */
  214. starpu_matrix_data_register(&dataA, 0, (uintptr_t)matA, ld, size, size, sizeof(float));
  215. starpu_data_set_sequential_consistency_flag(dataA, 0);
  216. struct starpu_data_filter f =
  217. {
  218. .filter_func = starpu_vertical_block_filter_func,
  219. .nchildren = nblocks
  220. };
  221. struct starpu_data_filter f2 =
  222. {
  223. .filter_func = starpu_block_filter_func,
  224. .nchildren = nblocks
  225. };
  226. starpu_data_map_filters(dataA, 2, &f, &f2);
  227. _cholesky(dataA, nblocks);
  228. starpu_data_unregister(dataA);
  229. starpu_helper_cublas_shutdown();
  230. starpu_shutdown();
  231. }
  232. int main(int argc, char **argv)
  233. {
  234. /* create a simple definite positive symetric matrix example
  235. *
  236. * Hilbert matrix : h(i,j) = 1/(i+j+1)
  237. * */
  238. parse_args(argc, argv);
  239. float *mat;
  240. mat = malloc(size*size*sizeof(float));
  241. initialize_system(&mat, size, pinned);
  242. unsigned i,j;
  243. for (i = 0; i < size; i++)
  244. {
  245. for (j = 0; j < size; j++)
  246. {
  247. mat[j +i*size] = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  248. /* mat[j +i*size] = ((i == j)?1.0f*size:0.0f); */
  249. }
  250. }
  251. #ifdef CHECK_OUTPUT
  252. FPRINTF(stdout, "Input :\n");
  253. for (j = 0; j < size; j++)
  254. {
  255. for (i = 0; i < size; i++)
  256. {
  257. if (i <= j)
  258. {
  259. FPRINTF(stdout, "%2.2f\t", mat[j +i*size]);
  260. }
  261. else
  262. {
  263. FPRINTF(stdout, ".\t");
  264. }
  265. }
  266. FPRINTF(stdout, "\n");
  267. }
  268. #endif
  269. cholesky(mat, size, size, nblocks);
  270. #ifdef CHECK_OUTPUT
  271. FPRINTF(stdout, "Results :\n");
  272. for (j = 0; j < size; j++)
  273. {
  274. for (i = 0; i < size; i++)
  275. {
  276. if (i <= j)
  277. {
  278. FPRINTF(stdout, "%2.2f\t", mat[j +i*size]);
  279. }
  280. else
  281. {
  282. FPRINTF(stdout, ".\t");
  283. mat[j+i*size] = 0.0f; /* debug */
  284. }
  285. }
  286. FPRINTF(stdout, "\n");
  287. }
  288. FPRINTF(stderr, "compute explicit LLt ...\n");
  289. float *test_mat = malloc(size*size*sizeof(float));
  290. STARPU_ASSERT(test_mat);
  291. SSYRK("L", "N", size, size, 1.0f,
  292. mat, size, 0.0f, test_mat, size);
  293. FPRINTF(stderr, "comparing results ...\n");
  294. for (j = 0; j < size; j++)
  295. {
  296. for (i = 0; i < size; i++)
  297. {
  298. if (i <= j)
  299. {
  300. FPRINTF(stdout, "%2.2f\t", test_mat[j +i*size]);
  301. }
  302. else
  303. {
  304. FPRINTF(stdout, ".\t");
  305. }
  306. }
  307. FPRINTF(stdout, "\n");
  308. }
  309. #endif
  310. return 0;
  311. }