dw_cholesky.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (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. /*
  19. * Some useful functions
  20. */
  21. static struct starpu_task *create_task(starpu_tag_t id)
  22. {
  23. struct starpu_task *task = starpu_task_create();
  24. task->cl_arg = NULL;
  25. task->use_tag = 1;
  26. task->tag_id = id;
  27. return task;
  28. }
  29. /*
  30. * Create the codelets
  31. */
  32. static starpu_codelet cl11 =
  33. {
  34. .where = STARPU_CPU|STARPU_CUDA,
  35. .cpu_func = chol_cpu_codelet_update_u11,
  36. #ifdef STARPU_USE_CUDA
  37. .cuda_func = chol_cublas_codelet_update_u11,
  38. #endif
  39. .nbuffers = 1,
  40. .model = &chol_model_11
  41. };
  42. static struct starpu_task * create_task_11(starpu_data_handle dataA, unsigned k)
  43. {
  44. // printf("task 11 k = %d TAG = %llx\n", k, (TAG11(k)));
  45. struct starpu_task *task = create_task(TAG11(k));
  46. task->cl = &cl11;
  47. /* which sub-data is manipulated ? */
  48. task->buffers[0].handle = starpu_data_get_sub_data(dataA, 2, k, k);
  49. task->buffers[0].mode = STARPU_RW;
  50. /* this is an important task */
  51. if (!noprio)
  52. task->priority = STARPU_MAX_PRIO;
  53. /* enforce dependencies ... */
  54. if (k > 0) {
  55. starpu_tag_declare_deps(TAG11(k), 1, TAG22(k-1, k, k));
  56. }
  57. return task;
  58. }
  59. static starpu_codelet cl21 =
  60. {
  61. .where = STARPU_CPU|STARPU_CUDA,
  62. .cpu_func = chol_cpu_codelet_update_u21,
  63. #ifdef STARPU_USE_CUDA
  64. .cuda_func = chol_cublas_codelet_update_u21,
  65. #endif
  66. .nbuffers = 2,
  67. .model = &chol_model_21
  68. };
  69. static void create_task_21(starpu_data_handle dataA, unsigned k, unsigned j)
  70. {
  71. struct starpu_task *task = create_task(TAG21(k, j));
  72. task->cl = &cl21;
  73. /* which sub-data is manipulated ? */
  74. task->buffers[0].handle = starpu_data_get_sub_data(dataA, 2, k, k);
  75. task->buffers[0].mode = STARPU_R;
  76. task->buffers[1].handle = starpu_data_get_sub_data(dataA, 2, k, j);
  77. task->buffers[1].mode = STARPU_RW;
  78. if (!noprio && (j == k+1)) {
  79. task->priority = STARPU_MAX_PRIO;
  80. }
  81. /* enforce dependencies ... */
  82. if (k > 0) {
  83. starpu_tag_declare_deps(TAG21(k, j), 2, TAG11(k), TAG22(k-1, k, j));
  84. }
  85. else {
  86. starpu_tag_declare_deps(TAG21(k, j), 1, TAG11(k));
  87. }
  88. int ret = starpu_task_submit(task);
  89. if (STARPU_UNLIKELY(ret == -ENODEV)) {
  90. fprintf(stderr, "No worker may execute this task\n");
  91. exit(0);
  92. }
  93. }
  94. static starpu_codelet cl22 =
  95. {
  96. .where = STARPU_CPU|STARPU_CUDA,
  97. .cpu_func = chol_cpu_codelet_update_u22,
  98. #ifdef STARPU_USE_CUDA
  99. .cuda_func = chol_cublas_codelet_update_u22,
  100. #endif
  101. .nbuffers = 3,
  102. .model = &chol_model_22
  103. };
  104. static void create_task_22(starpu_data_handle dataA, unsigned k, unsigned i, unsigned j)
  105. {
  106. // printf("task 22 k,i,j = %d,%d,%d TAG = %llx\n", k,i,j, TAG22(k,i,j));
  107. struct starpu_task *task = create_task(TAG22(k, i, j));
  108. task->cl = &cl22;
  109. /* which sub-data is manipulated ? */
  110. task->buffers[0].handle = starpu_data_get_sub_data(dataA, 2, k, i);
  111. task->buffers[0].mode = STARPU_R;
  112. task->buffers[1].handle = starpu_data_get_sub_data(dataA, 2, k, j);
  113. task->buffers[1].mode = STARPU_R;
  114. task->buffers[2].handle = starpu_data_get_sub_data(dataA, 2, i, j);
  115. task->buffers[2].mode = STARPU_RW;
  116. if (!noprio && (i == k + 1) && (j == k +1) ) {
  117. task->priority = STARPU_MAX_PRIO;
  118. }
  119. /* enforce dependencies ... */
  120. if (k > 0) {
  121. starpu_tag_declare_deps(TAG22(k, i, j), 3, TAG22(k-1, i, j), TAG21(k, i), TAG21(k, j));
  122. }
  123. else {
  124. starpu_tag_declare_deps(TAG22(k, i, j), 2, TAG21(k, i), TAG21(k, j));
  125. }
  126. int ret = starpu_task_submit(task);
  127. if (STARPU_UNLIKELY(ret == -ENODEV)) {
  128. fprintf(stderr, "No worker may execute this task\n");
  129. exit(0);
  130. }
  131. }
  132. /*
  133. * code to bootstrap the factorization
  134. * and construct the DAG
  135. */
  136. static void _dw_cholesky(starpu_data_handle dataA, unsigned nblocks)
  137. {
  138. struct timeval start;
  139. struct timeval end;
  140. struct starpu_task *entry_task = NULL;
  141. /* create all the DAG nodes */
  142. unsigned i,j,k;
  143. gettimeofday(&start, NULL);
  144. for (k = 0; k < nblocks; k++)
  145. {
  146. struct starpu_task *task = create_task_11(dataA, k);
  147. /* we defer the launch of the first task */
  148. if (k == 0) {
  149. entry_task = task;
  150. }
  151. else {
  152. int ret = starpu_task_submit(task);
  153. if (STARPU_UNLIKELY(ret == -ENODEV)) {
  154. fprintf(stderr, "No worker may execute this task\n");
  155. exit(0);
  156. }
  157. }
  158. for (j = k+1; j<nblocks; j++)
  159. {
  160. create_task_21(dataA, k, j);
  161. for (i = k+1; i<nblocks; i++)
  162. {
  163. if (i <= j)
  164. create_task_22(dataA, k, i, j);
  165. }
  166. }
  167. }
  168. /* schedule the codelet */
  169. int ret = starpu_task_submit(entry_task);
  170. if (STARPU_UNLIKELY(ret == -ENODEV)) {
  171. fprintf(stderr, "No worker may execute this task\n");
  172. exit(0);
  173. }
  174. /* stall the application until the end of computations */
  175. starpu_tag_wait(TAG11(nblocks-1));
  176. starpu_data_unpartition(dataA, 0);
  177. gettimeofday(&end, NULL);
  178. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  179. fprintf(stderr, "Computation took (in ms)\n");
  180. printf("%2.2f\n", timing/1000);
  181. unsigned n = starpu_matrix_get_nx(dataA);
  182. double flop = (1.0f*n*n*n)/3.0f;
  183. fprintf(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  184. }
  185. void initialize_system(float **A, unsigned dim, unsigned pinned)
  186. {
  187. starpu_init(NULL);
  188. starpu_helper_cublas_init();
  189. if (pinned)
  190. {
  191. starpu_data_malloc_pinned_if_possible((void **)A, (size_t)dim*dim*sizeof(float));
  192. }
  193. else {
  194. *A = malloc(dim*dim*sizeof(float));
  195. }
  196. }
  197. void dw_cholesky(float *matA, unsigned size, unsigned ld, unsigned nblocks)
  198. {
  199. starpu_data_handle dataA;
  200. /* monitor and partition the A matrix into blocks :
  201. * one block is now determined by 2 unsigned (i,j) */
  202. starpu_matrix_data_register(&dataA, 0, (uintptr_t)matA, ld, size, size, sizeof(float));
  203. starpu_data_set_sequential_consistency_flag(dataA, 0);
  204. struct starpu_data_filter f;
  205. f.filter_func = starpu_vertical_block_filter_func;
  206. f.nchildren = nblocks;
  207. f.get_nchildren = NULL;
  208. f.get_child_ops = NULL;
  209. struct starpu_data_filter f2;
  210. f2.filter_func = starpu_block_filter_func;
  211. f2.nchildren = nblocks;
  212. f2.get_nchildren = NULL;
  213. f2.get_child_ops = NULL;
  214. starpu_data_map_filters(dataA, 2, &f, &f2);
  215. _dw_cholesky(dataA, nblocks);
  216. starpu_helper_cublas_shutdown();
  217. starpu_shutdown();
  218. }
  219. int main(int argc, char **argv)
  220. {
  221. /* create a simple definite positive symetric matrix example
  222. *
  223. * Hilbert matrix : h(i,j) = 1/(i+j+1)
  224. * */
  225. parse_args(argc, argv);
  226. float *mat;
  227. mat = malloc(size*size*sizeof(float));
  228. initialize_system(&mat, size, pinned);
  229. unsigned i,j;
  230. for (i = 0; i < size; i++)
  231. {
  232. for (j = 0; j < size; j++)
  233. {
  234. mat[j +i*size] = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  235. //mat[j +i*size] = ((i == j)?1.0f*size:0.0f);
  236. }
  237. }
  238. #ifdef CHECK_OUTPUT
  239. printf("Input :\n");
  240. for (j = 0; j < size; j++)
  241. {
  242. for (i = 0; i < size; i++)
  243. {
  244. if (i <= j) {
  245. printf("%2.2f\t", mat[j +i*size]);
  246. }
  247. else {
  248. printf(".\t");
  249. }
  250. }
  251. printf("\n");
  252. }
  253. #endif
  254. dw_cholesky(mat, size, size, nblocks);
  255. #ifdef CHECK_OUTPUT
  256. printf("Results :\n");
  257. for (j = 0; j < size; j++)
  258. {
  259. for (i = 0; i < size; i++)
  260. {
  261. if (i <= j) {
  262. printf("%2.2f\t", mat[j +i*size]);
  263. }
  264. else {
  265. printf(".\t");
  266. mat[j+i*size] = 0.0f; // debug
  267. }
  268. }
  269. printf("\n");
  270. }
  271. fprintf(stderr, "compute explicit LLt ...\n");
  272. float *test_mat = malloc(size*size*sizeof(float));
  273. STARPU_ASSERT(test_mat);
  274. SSYRK("L", "N", size, size, 1.0f,
  275. mat, size, 0.0f, test_mat, size);
  276. fprintf(stderr, "comparing results ...\n");
  277. for (j = 0; j < size; j++)
  278. {
  279. for (i = 0; i < size; i++)
  280. {
  281. if (i <= j) {
  282. printf("%2.2f\t", test_mat[j +i*size]);
  283. }
  284. else {
  285. printf(".\t");
  286. }
  287. }
  288. printf("\n");
  289. }
  290. #endif
  291. return 0;
  292. }