dw_cholesky.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 = ANY,
  35. .core_func = chol_core_codelet_update_u11,
  36. #ifdef USE_CUDA
  37. .cublas_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].state = get_sub_data(dataA, 2, k, k);
  49. task->buffers[0].mode = RW;
  50. /* this is an important task */
  51. task->priority = MAX_PRIO;
  52. /* enforce dependencies ... */
  53. if (k > 0) {
  54. starpu_tag_declare_deps(TAG11(k), 1, TAG22(k-1, k, k));
  55. }
  56. return task;
  57. }
  58. static starpu_codelet cl21 =
  59. {
  60. .where = ANY,
  61. .core_func = chol_core_codelet_update_u21,
  62. #ifdef USE_CUDA
  63. .cublas_func = chol_cublas_codelet_update_u21,
  64. #endif
  65. .nbuffers = 2,
  66. .model = &chol_model_21
  67. };
  68. static void create_task_21(starpu_data_handle dataA, unsigned k, unsigned j)
  69. {
  70. struct starpu_task *task = create_task(TAG21(k, j));
  71. task->cl = &cl21;
  72. /* which sub-data is manipulated ? */
  73. task->buffers[0].state = get_sub_data(dataA, 2, k, k);
  74. task->buffers[0].mode = R;
  75. task->buffers[1].state = get_sub_data(dataA, 2, k, j);
  76. task->buffers[1].mode = RW;
  77. if (j == k+1) {
  78. task->priority = MAX_PRIO;
  79. }
  80. /* enforce dependencies ... */
  81. if (k > 0) {
  82. starpu_tag_declare_deps(TAG21(k, j), 2, TAG11(k), TAG22(k-1, k, j));
  83. }
  84. else {
  85. starpu_tag_declare_deps(TAG21(k, j), 1, TAG11(k));
  86. }
  87. starpu_submit_task(task);
  88. }
  89. static starpu_codelet cl22 =
  90. {
  91. .where = ANY,
  92. .core_func = chol_core_codelet_update_u22,
  93. #ifdef USE_CUDA
  94. .cublas_func = chol_cublas_codelet_update_u22,
  95. #endif
  96. .nbuffers = 3,
  97. .model = &chol_model_22
  98. };
  99. static void create_task_22(starpu_data_handle dataA, unsigned k, unsigned i, unsigned j)
  100. {
  101. // printf("task 22 k,i,j = %d,%d,%d TAG = %llx\n", k,i,j, TAG22(k,i,j));
  102. struct starpu_task *task = create_task(TAG22(k, i, j));
  103. task->cl = &cl22;
  104. /* which sub-data is manipulated ? */
  105. task->buffers[0].state = get_sub_data(dataA, 2, k, i);
  106. task->buffers[0].mode = R;
  107. task->buffers[1].state = get_sub_data(dataA, 2, k, j);
  108. task->buffers[1].mode = R;
  109. task->buffers[2].state = get_sub_data(dataA, 2, i, j);
  110. task->buffers[2].mode = RW;
  111. if ( (i == k + 1) && (j == k +1) ) {
  112. task->priority = MAX_PRIO;
  113. }
  114. /* enforce dependencies ... */
  115. if (k > 0) {
  116. starpu_tag_declare_deps(TAG22(k, i, j), 3, TAG22(k-1, i, j), TAG21(k, i), TAG21(k, j));
  117. }
  118. else {
  119. starpu_tag_declare_deps(TAG22(k, i, j), 2, TAG21(k, i), TAG21(k, j));
  120. }
  121. starpu_submit_task(task);
  122. }
  123. /*
  124. * code to bootstrap the factorization
  125. * and construct the DAG
  126. */
  127. static void _dw_cholesky(starpu_data_handle dataA, unsigned nblocks)
  128. {
  129. struct timeval start;
  130. struct timeval end;
  131. /* create a new codelet */
  132. sem_t sem;
  133. sem_init(&sem, 0, 0U);
  134. struct starpu_task *entry_task = NULL;
  135. /* create all the DAG nodes */
  136. unsigned i,j,k;
  137. for (k = 0; k < nblocks; k++)
  138. {
  139. struct starpu_task *task = create_task_11(dataA, k);
  140. /* we defer the launch of the first task */
  141. if (k == 0) {
  142. entry_task = task;
  143. }
  144. else {
  145. starpu_submit_task(task);
  146. }
  147. for (j = k+1; j<nblocks; j++)
  148. {
  149. create_task_21(dataA, k, j);
  150. for (i = k+1; i<nblocks; i++)
  151. {
  152. if (i <= j)
  153. create_task_22(dataA, k, i, j);
  154. }
  155. }
  156. }
  157. /* schedule the codelet */
  158. gettimeofday(&start, NULL);
  159. starpu_submit_task(entry_task);
  160. /* stall the application until the end of computations */
  161. starpu_tag_wait(TAG11(nblocks-1));
  162. gettimeofday(&end, NULL);
  163. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  164. fprintf(stderr, "Computation took (in ms)\n");
  165. printf("%2.2f\n", timing/1000);
  166. unsigned n = starpu_get_blas_nx(dataA);
  167. double flop = (1.0f*n*n*n)/3.0f;
  168. fprintf(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  169. }
  170. void initialize_system(float **A, unsigned dim, unsigned pinned)
  171. {
  172. starpu_init(NULL);
  173. timing_init();
  174. if (pinned)
  175. {
  176. starpu_malloc_pinned_if_possible(A, dim*dim*sizeof(float));
  177. }
  178. else {
  179. *A = malloc(dim*dim*sizeof(float));
  180. }
  181. }
  182. void dw_cholesky(float *matA, unsigned size, unsigned ld, unsigned nblocks)
  183. {
  184. starpu_data_handle dataA;
  185. /* monitor and partition the A matrix into blocks :
  186. * one block is now determined by 2 unsigned (i,j) */
  187. starpu_monitor_blas_data(&dataA, 0, (uintptr_t)matA, ld, size, size, sizeof(float));
  188. starpu_filter f;
  189. f.filter_func = starpu_vertical_block_filter_func;
  190. f.filter_arg = nblocks;
  191. starpu_filter f2;
  192. f2.filter_func = starpu_block_filter_func;
  193. f2.filter_arg = nblocks;
  194. starpu_map_filters(dataA, 2, &f, &f2);
  195. _dw_cholesky(dataA, nblocks);
  196. starpu_unpartition_data(dataA, 0);
  197. starpu_shutdown();
  198. }
  199. int main(int argc, char **argv)
  200. {
  201. /* create a simple definite positive symetric matrix example
  202. *
  203. * Hilbert matrix : h(i,j) = 1/(i+j+1)
  204. * */
  205. parse_args(argc, argv);
  206. float *mat;
  207. mat = malloc(size*size*sizeof(float));
  208. initialize_system(&mat, size, pinned);
  209. unsigned i,j;
  210. for (i = 0; i < size; i++)
  211. {
  212. for (j = 0; j < size; j++)
  213. {
  214. mat[j +i*size] = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  215. //mat[j +i*size] = ((i == j)?1.0f*size:0.0f);
  216. }
  217. }
  218. #ifdef CHECK_OUTPUT
  219. printf("Input :\n");
  220. for (j = 0; j < size; j++)
  221. {
  222. for (i = 0; i < size; i++)
  223. {
  224. if (i <= j) {
  225. printf("%2.2f\t", mat[j +i*size]);
  226. }
  227. else {
  228. printf(".\t");
  229. }
  230. }
  231. printf("\n");
  232. }
  233. #endif
  234. dw_cholesky(mat, size, size, nblocks);
  235. #ifdef CHECK_OUTPUT
  236. printf("Results :\n");
  237. for (j = 0; j < size; j++)
  238. {
  239. for (i = 0; i < size; i++)
  240. {
  241. if (i <= j) {
  242. printf("%2.2f\t", mat[j +i*size]);
  243. }
  244. else {
  245. printf(".\t");
  246. mat[j+i*size] = 0.0f; // debug
  247. }
  248. }
  249. printf("\n");
  250. }
  251. fprintf(stderr, "compute explicit LLt ...\n");
  252. float *test_mat = malloc(size*size*sizeof(float));
  253. STARPU_ASSERT(test_mat);
  254. SSYRK("L", "N", size, size, 1.0f,
  255. mat, size, 0.0f, test_mat, size);
  256. fprintf(stderr, "comparing 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", test_mat[j +i*size]);
  263. }
  264. else {
  265. printf(".\t");
  266. }
  267. }
  268. printf("\n");
  269. }
  270. #endif
  271. return 0;
  272. }