dw_cholesky.c 8.3 KB

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