dw_cholesky.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. /*
  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. starpu_task_submit(task);
  89. }
  90. static starpu_codelet cl22 =
  91. {
  92. .where = STARPU_CPU|STARPU_CUDA,
  93. .cpu_func = chol_cpu_codelet_update_u22,
  94. #ifdef STARPU_USE_CUDA
  95. .cuda_func = chol_cublas_codelet_update_u22,
  96. #endif
  97. .nbuffers = 3,
  98. .model = &chol_model_22
  99. };
  100. static void create_task_22(starpu_data_handle dataA, unsigned k, unsigned i, unsigned j)
  101. {
  102. // printf("task 22 k,i,j = %d,%d,%d TAG = %llx\n", k,i,j, TAG22(k,i,j));
  103. struct starpu_task *task = create_task(TAG22(k, i, j));
  104. task->cl = &cl22;
  105. /* which sub-data is manipulated ? */
  106. task->buffers[0].handle = starpu_data_get_sub_data(dataA, 2, k, i);
  107. task->buffers[0].mode = STARPU_R;
  108. task->buffers[1].handle = starpu_data_get_sub_data(dataA, 2, k, j);
  109. task->buffers[1].mode = STARPU_R;
  110. task->buffers[2].handle = starpu_data_get_sub_data(dataA, 2, i, j);
  111. task->buffers[2].mode = STARPU_RW;
  112. if (!noprio && (i == k + 1) && (j == k +1) ) {
  113. task->priority = STARPU_MAX_PRIO;
  114. }
  115. /* enforce dependencies ... */
  116. if (k > 0) {
  117. starpu_tag_declare_deps(TAG22(k, i, j), 3, TAG22(k-1, i, j), TAG21(k, i), TAG21(k, j));
  118. }
  119. else {
  120. starpu_tag_declare_deps(TAG22(k, i, j), 2, TAG21(k, i), TAG21(k, j));
  121. }
  122. starpu_task_submit(task);
  123. }
  124. /*
  125. * code to bootstrap the factorization
  126. * and construct the DAG
  127. */
  128. static void _dw_cholesky(starpu_data_handle dataA, unsigned nblocks)
  129. {
  130. struct timeval start;
  131. struct timeval end;
  132. /* create a new codelet */
  133. sem_t sem;
  134. sem_init(&sem, 0, 0U);
  135. struct starpu_task *entry_task = NULL;
  136. /* create all the DAG nodes */
  137. unsigned i,j,k;
  138. gettimeofday(&start, NULL);
  139. for (k = 0; k < nblocks; k++)
  140. {
  141. struct starpu_task *task = create_task_11(dataA, k);
  142. /* we defer the launch of the first task */
  143. if (k == 0) {
  144. entry_task = task;
  145. }
  146. else {
  147. starpu_task_submit(task);
  148. }
  149. for (j = k+1; j<nblocks; j++)
  150. {
  151. create_task_21(dataA, k, j);
  152. for (i = k+1; i<nblocks; i++)
  153. {
  154. if (i <= j)
  155. create_task_22(dataA, k, i, j);
  156. }
  157. }
  158. }
  159. /* schedule the codelet */
  160. starpu_task_submit(entry_task);
  161. /* stall the application until the end of computations */
  162. starpu_tag_wait(TAG11(nblocks-1));
  163. starpu_data_unpartition(dataA, 0);
  164. gettimeofday(&end, NULL);
  165. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  166. fprintf(stderr, "Computation took (in ms)\n");
  167. printf("%2.2f\n", timing/1000);
  168. unsigned n = starpu_matrix_get_nx(dataA);
  169. double flop = (1.0f*n*n*n)/3.0f;
  170. fprintf(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  171. }
  172. void initialize_system(float **A, unsigned dim, unsigned pinned)
  173. {
  174. starpu_init(NULL);
  175. starpu_helper_cublas_init();
  176. _starpu_timing_init();
  177. if (pinned)
  178. {
  179. starpu_data_malloc_pinned_if_possible((void **)A, (size_t)dim*dim*sizeof(float));
  180. }
  181. else {
  182. *A = malloc(dim*dim*sizeof(float));
  183. }
  184. }
  185. void dw_cholesky(float *matA, unsigned size, unsigned ld, unsigned nblocks)
  186. {
  187. starpu_data_handle dataA;
  188. /* monitor and partition the A matrix into blocks :
  189. * one block is now determined by 2 unsigned (i,j) */
  190. starpu_matrix_data_register(&dataA, 0, (uintptr_t)matA, ld, size, size, sizeof(float));
  191. starpu_data_set_sequential_consistency_flag(dataA, 0);
  192. starpu_filter f;
  193. f.filter_func = starpu_vertical_block_filter_func;
  194. f.filter_arg = nblocks;
  195. starpu_filter f2;
  196. f2.filter_func = starpu_block_filter_func;
  197. f2.filter_arg = nblocks;
  198. starpu_map_filters(dataA, 2, &f, &f2);
  199. _dw_cholesky(dataA, nblocks);
  200. starpu_helper_cublas_shutdown();
  201. starpu_shutdown();
  202. }
  203. int main(int argc, char **argv)
  204. {
  205. /* create a simple definite positive symetric matrix example
  206. *
  207. * Hilbert matrix : h(i,j) = 1/(i+j+1)
  208. * */
  209. parse_args(argc, argv);
  210. float *mat;
  211. mat = malloc(size*size*sizeof(float));
  212. initialize_system(&mat, size, pinned);
  213. unsigned i,j;
  214. for (i = 0; i < size; i++)
  215. {
  216. for (j = 0; j < size; j++)
  217. {
  218. mat[j +i*size] = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  219. //mat[j +i*size] = ((i == j)?1.0f*size:0.0f);
  220. }
  221. }
  222. #ifdef CHECK_OUTPUT
  223. printf("Input :\n");
  224. for (j = 0; j < size; j++)
  225. {
  226. for (i = 0; i < size; i++)
  227. {
  228. if (i <= j) {
  229. printf("%2.2f\t", mat[j +i*size]);
  230. }
  231. else {
  232. printf(".\t");
  233. }
  234. }
  235. printf("\n");
  236. }
  237. #endif
  238. dw_cholesky(mat, size, size, nblocks);
  239. #ifdef CHECK_OUTPUT
  240. printf("Results :\n");
  241. for (j = 0; j < size; j++)
  242. {
  243. for (i = 0; i < size; i++)
  244. {
  245. if (i <= j) {
  246. printf("%2.2f\t", mat[j +i*size]);
  247. }
  248. else {
  249. printf(".\t");
  250. mat[j+i*size] = 0.0f; // debug
  251. }
  252. }
  253. printf("\n");
  254. }
  255. fprintf(stderr, "compute explicit LLt ...\n");
  256. float *test_mat = malloc(size*size*sizeof(float));
  257. STARPU_ASSERT(test_mat);
  258. SSYRK("L", "N", size, size, 1.0f,
  259. mat, size, 0.0f, test_mat, size);
  260. fprintf(stderr, "comparing results ...\n");
  261. for (j = 0; j < size; j++)
  262. {
  263. for (i = 0; i < size; i++)
  264. {
  265. if (i <= j) {
  266. printf("%2.2f\t", test_mat[j +i*size]);
  267. }
  268. else {
  269. printf(".\t");
  270. }
  271. }
  272. printf("\n");
  273. }
  274. #endif
  275. return 0;
  276. }