dw_factolu_grain.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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, 2012 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_factolu.h"
  19. #define TAG11(k, prefix) ((starpu_tag_t)( (((unsigned long long)(prefix))<<60) | (1ULL<<56) | (unsigned long long)(k)))
  20. #define TAG12(k,i, prefix) ((starpu_tag_t)((((unsigned long long)(prefix))<<60) | ((2ULL<<56) | (((unsigned long long)(k))<<32) \
  21. | (unsigned long long)(i))))
  22. #define TAG21(k,j, prefix) ((starpu_tag_t)( (((unsigned long long)(prefix))<<60) | ((3ULL<<56) | (((unsigned long long)(k))<<32) \
  23. | (unsigned long long)(j))))
  24. #define TAG22(k,i,j, prefix) ((starpu_tag_t)( (((unsigned long long)(prefix))<<60) | ((4ULL<<56) | ((unsigned long long)(k)<<32) \
  25. | ((unsigned long long)(i)<<16) \
  26. | (unsigned long long)(j))))
  27. /*
  28. * Construct the DAG
  29. */
  30. static struct starpu_task *create_task(starpu_tag_t id)
  31. {
  32. struct starpu_task *task = starpu_task_create();
  33. task->cl_arg = NULL;
  34. task->use_tag = 1;
  35. task->tag_id = id;
  36. return task;
  37. }
  38. static struct starpu_codelet cl11 =
  39. {
  40. .modes = { STARPU_RW },
  41. .cpu_funcs = {dw_cpu_codelet_update_u11, NULL},
  42. #ifdef STARPU_USE_CUDA
  43. .cuda_funcs = {dw_cublas_codelet_update_u11, NULL},
  44. #endif
  45. .nbuffers = 1,
  46. .model = &model_11
  47. };
  48. static struct starpu_task *create_task_11(starpu_data_handle_t dataA, unsigned k, unsigned tag_prefix)
  49. {
  50. /* FPRINTF(stdout, "task 11 k = %d TAG = %llx\n", k, (TAG11(k))); */
  51. struct starpu_task *task = create_task(TAG11(k, tag_prefix));
  52. task->cl = &cl11;
  53. /* which sub-data is manipulated ? */
  54. task->handles[0] = starpu_data_get_sub_data(dataA, 2, k, k);
  55. /* this is an important task */
  56. task->priority = STARPU_MAX_PRIO;
  57. /* enforce dependencies ... */
  58. if (k > 0)
  59. {
  60. starpu_tag_declare_deps(TAG11(k, tag_prefix), 1, TAG22(k-1, k, k, tag_prefix));
  61. }
  62. return task;
  63. }
  64. static struct starpu_codelet cl12 =
  65. {
  66. .modes = { STARPU_R, STARPU_RW },
  67. .cpu_funcs = {dw_cpu_codelet_update_u12, NULL},
  68. #ifdef STARPU_USE_CUDA
  69. .cuda_funcs = {dw_cublas_codelet_update_u12, NULL},
  70. #endif
  71. .nbuffers = 2,
  72. .model = &model_12
  73. };
  74. static void create_task_12(starpu_data_handle_t dataA, unsigned k, unsigned i, unsigned tag_prefix)
  75. {
  76. int ret;
  77. /* FPRINTF(stdout, "task 12 k,i = %d,%d TAG = %llx\n", k,i, TAG12(k,i)); */
  78. struct starpu_task *task = create_task(TAG12(k, i, tag_prefix));
  79. task->cl = &cl12;
  80. /* which sub-data is manipulated ? */
  81. task->handles[0] = starpu_data_get_sub_data(dataA, 2, k, k);
  82. task->handles[1] = starpu_data_get_sub_data(dataA, 2, i, k);
  83. if (i == k+1)
  84. {
  85. task->priority = STARPU_MAX_PRIO;
  86. }
  87. /* enforce dependencies ... */
  88. if (k > 0)
  89. {
  90. starpu_tag_declare_deps(TAG12(k, i, tag_prefix), 2, TAG11(k, tag_prefix), TAG22(k-1, i, k, tag_prefix));
  91. }
  92. else
  93. {
  94. starpu_tag_declare_deps(TAG12(k, i, tag_prefix), 1, TAG11(k, tag_prefix));
  95. }
  96. ret = starpu_task_submit(task);
  97. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  98. }
  99. static struct starpu_codelet cl21 =
  100. {
  101. .modes = { STARPU_R, STARPU_RW },
  102. .cpu_funcs = {dw_cpu_codelet_update_u21, NULL},
  103. #ifdef STARPU_USE_CUDA
  104. .cuda_funcs = {dw_cublas_codelet_update_u21, NULL},
  105. #endif
  106. .nbuffers = 2,
  107. .model = &model_21
  108. };
  109. static void create_task_21(starpu_data_handle_t dataA, unsigned k, unsigned j, unsigned tag_prefix)
  110. {
  111. int ret;
  112. struct starpu_task *task = create_task(TAG21(k, j, tag_prefix));
  113. task->cl = &cl21;
  114. /* which sub-data is manipulated ? */
  115. task->handles[0] = starpu_data_get_sub_data(dataA, 2, k, k);
  116. task->handles[1] = starpu_data_get_sub_data(dataA, 2, k, j);
  117. if (j == k+1)
  118. {
  119. task->priority = STARPU_MAX_PRIO;
  120. }
  121. /* enforce dependencies ... */
  122. if (k > 0)
  123. {
  124. starpu_tag_declare_deps(TAG21(k, j, tag_prefix), 2, TAG11(k, tag_prefix), TAG22(k-1, k, j, tag_prefix));
  125. }
  126. else
  127. {
  128. starpu_tag_declare_deps(TAG21(k, j, tag_prefix), 1, TAG11(k, tag_prefix));
  129. }
  130. ret = starpu_task_submit(task);
  131. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  132. }
  133. static struct starpu_codelet cl22 =
  134. {
  135. .modes = { STARPU_R, STARPU_R, STARPU_RW },
  136. .cpu_funcs = {dw_cpu_codelet_update_u22, NULL},
  137. #ifdef STARPU_USE_CUDA
  138. .cuda_funcs = {dw_cublas_codelet_update_u22, NULL},
  139. #endif
  140. .nbuffers = 3,
  141. .model = &model_22
  142. };
  143. static void create_task_22(starpu_data_handle_t dataA, unsigned k, unsigned i, unsigned j, unsigned tag_prefix)
  144. {
  145. int ret;
  146. /* FPRINTF(stdout, "task 22 k,i,j = %d,%d,%d TAG = %llx\n", k,i,j, TAG22(k,i,j)); */
  147. struct starpu_task *task = create_task(TAG22(k, i, j, tag_prefix));
  148. task->cl = &cl22;
  149. /* which sub-data is manipulated ? */
  150. task->handles[0] = starpu_data_get_sub_data(dataA, 2, i, k);
  151. task->handles[1] = starpu_data_get_sub_data(dataA, 2, k, j);
  152. task->handles[2] = starpu_data_get_sub_data(dataA, 2, i, j);
  153. if ( (i == k + 1) && (j == k +1) )
  154. {
  155. task->priority = STARPU_MAX_PRIO;
  156. }
  157. /* enforce dependencies ... */
  158. if (k > 0)
  159. {
  160. starpu_tag_declare_deps(TAG22(k, i, j, tag_prefix), 3, TAG22(k-1, i, j, tag_prefix), TAG12(k, i, tag_prefix), TAG21(k, j, tag_prefix));
  161. }
  162. else
  163. {
  164. starpu_tag_declare_deps(TAG22(k, i, j, tag_prefix), 2, TAG12(k, i, tag_prefix), TAG21(k, j, tag_prefix));
  165. }
  166. ret = starpu_task_submit(task);
  167. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  168. }
  169. static void dw_factoLU_grain_inner(float *matA, unsigned size, unsigned inner_size,
  170. unsigned ld, unsigned blocksize, unsigned tag_prefix)
  171. {
  172. int ret;
  173. /*
  174. * (re)partition data
  175. */
  176. starpu_data_handle_t dataA;
  177. starpu_matrix_data_register(&dataA, STARPU_MAIN_RAM, (uintptr_t)matA, ld, size, size, sizeof(float));
  178. STARPU_ASSERT((size % blocksize) == 0);
  179. STARPU_ASSERT((inner_size % blocksize) == 0);
  180. unsigned nblocks = size / blocksize;
  181. unsigned maxk = inner_size / blocksize;
  182. struct starpu_data_filter f =
  183. {
  184. .filter_func = starpu_matrix_filter_vertical_block,
  185. .nchildren = nblocks
  186. };
  187. struct starpu_data_filter f2 =
  188. {
  189. .filter_func = starpu_matrix_filter_block,
  190. .nchildren = nblocks
  191. };
  192. starpu_data_map_filters(dataA, 2, &f, &f2);
  193. /*
  194. * submit tasks
  195. */
  196. struct starpu_task *entry_task = NULL;
  197. /* create all the DAG nodes */
  198. unsigned i,j,k;
  199. /* if maxk < nblocks we'll stop before the LU decomposition is totally done */
  200. for (k = 0; k < maxk; k++)
  201. {
  202. struct starpu_task *task = create_task_11(dataA, k, tag_prefix);
  203. /* we defer the launch of the first task */
  204. if (k == 0)
  205. {
  206. entry_task = task;
  207. }
  208. else
  209. {
  210. ret = starpu_task_submit(task);
  211. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  212. }
  213. for (i = k+1; i<nblocks; i++)
  214. {
  215. create_task_12(dataA, k, i, tag_prefix);
  216. create_task_21(dataA, k, i, tag_prefix);
  217. }
  218. for (i = k+1; i<nblocks; i++)
  219. {
  220. for (j = k+1; j<nblocks; j++)
  221. {
  222. create_task_22(dataA, k, i, j, tag_prefix);
  223. }
  224. }
  225. }
  226. ret = starpu_task_submit(entry_task);
  227. if (STARPU_UNLIKELY(ret == -ENODEV))
  228. {
  229. FPRINTF(stderr, "No worker may execute this task\n");
  230. exit(-1);
  231. }
  232. /* is this the last call to dw_factoLU_grain_inner ? */
  233. if (inner_size == size)
  234. {
  235. /* we wait for the last task and we are done */
  236. starpu_tag_wait(TAG11(nblocks-1, tag_prefix));
  237. starpu_data_unpartition(dataA, STARPU_MAIN_RAM);
  238. return;
  239. }
  240. else
  241. {
  242. /*
  243. * call dw_factoLU_grain_inner recursively in the remaining blocks
  244. */
  245. unsigned ndeps_tags = (nblocks - maxk)*(nblocks - maxk);
  246. starpu_tag_t *tag_array = malloc(ndeps_tags*sizeof(starpu_tag_t));
  247. STARPU_ASSERT(tag_array);
  248. unsigned ind = 0;
  249. for (i = maxk; i < nblocks; i++)
  250. for (j = maxk; j < nblocks; j++)
  251. {
  252. tag_array[ind++] = TAG22(maxk-1, i, j, tag_prefix);
  253. }
  254. starpu_tag_wait_array(ndeps_tags, tag_array);
  255. free(tag_array);
  256. starpu_data_unpartition(dataA, STARPU_MAIN_RAM);
  257. starpu_data_unregister(dataA);
  258. float *newmatA = &matA[inner_size*(ld+1)];
  259. /* if (tag_prefix < 2)
  260. {
  261. dw_factoLU_grain_inner(newmatA, size-inner_size, (size-inner_size)/2, ld, blocksize/2, tag_prefix+1);
  262. }
  263. else
  264. { */
  265. dw_factoLU_grain_inner(newmatA, size-inner_size, size-inner_size, ld, blocksize/2, tag_prefix+1);
  266. /* } */
  267. }
  268. }
  269. void dw_factoLU_grain(float *matA, unsigned size, unsigned ld, unsigned nblocks, unsigned nbigblocks)
  270. {
  271. #ifdef CHECK_RESULTS
  272. FPRINTF(stderr, "Checking results ...\n");
  273. float *Asaved;
  274. Asaved = malloc(ld*ld*sizeof(float));
  275. memcpy(Asaved, matA, ld*ld*sizeof(float));
  276. #endif
  277. struct timeval start;
  278. struct timeval end;
  279. /* schedule the codelet */
  280. gettimeofday(&start, NULL);
  281. /* that's only ok for powers of 2 yet ! */
  282. dw_factoLU_grain_inner(matA, size, (size/nblocks) * nbigblocks, ld, size/nblocks, 0);
  283. gettimeofday(&end, NULL);
  284. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  285. FPRINTF(stderr, "Computation took (in ms)\n");
  286. FPRINTF(stdout, "%2.2f\n", timing/1000);
  287. unsigned n = size;
  288. double flop = (2.0f*n*n*n)/3.0f;
  289. FPRINTF(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  290. #ifdef CHECK_RESULTS
  291. compare_A_LU(Asaved, matA, size, ld);
  292. #endif
  293. }