dw_factolu_grain.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011,2014-2017, 2019 Université de Bordeaux
  4. * Copyright (C) 2012,2013 Inria
  5. * Copyright (C) 2010 Mehdi Juhoor
  6. * Copyright (C) 2010-2013,2015-2017 CNRS
  7. * Copyright (C) 2013 Thibaut Lambert
  8. *
  9. * StarPU is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * StarPU is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  19. */
  20. /*
  21. * This implements an LU factorization.
  22. * The task graph is submitted through dependency tags.
  23. * It also changes the partitioning during execution: when called first,
  24. * dw_factoLU_grain_inner splits the matrix with a big granularity (nblocks)
  25. * and processes nbigblocks blocks, before calling itself again, to process the
  26. * remainder of the matrix with a smaller granularity.
  27. */
  28. #include "dw_factolu.h"
  29. #define TAG11(k, prefix) ((starpu_tag_t)( (((unsigned long long)(prefix))<<60) | (1ULL<<56) | (unsigned long long)(k)))
  30. #define TAG12(k,i, prefix) ((starpu_tag_t)((((unsigned long long)(prefix))<<60) | ((2ULL<<56) | (((unsigned long long)(k))<<32) \
  31. | (unsigned long long)(i))))
  32. #define TAG21(k,j, prefix) ((starpu_tag_t)( (((unsigned long long)(prefix))<<60) | ((3ULL<<56) | (((unsigned long long)(k))<<32) \
  33. | (unsigned long long)(j))))
  34. #define TAG22(k,i,j, prefix) ((starpu_tag_t)( (((unsigned long long)(prefix))<<60) | ((4ULL<<56) | ((unsigned long long)(k)<<32) \
  35. | ((unsigned long long)(i)<<16) \
  36. | (unsigned long long)(j))))
  37. /*
  38. * Construct the DAG
  39. */
  40. static struct starpu_task *create_task(starpu_tag_t id)
  41. {
  42. struct starpu_task *task = starpu_task_create();
  43. task->cl_arg = NULL;
  44. task->use_tag = 1;
  45. task->tag_id = id;
  46. return task;
  47. }
  48. static struct starpu_codelet cl11 =
  49. {
  50. .modes = { STARPU_RW },
  51. .cpu_funcs = {dw_cpu_codelet_update_u11},
  52. .cpu_funcs_name = {"dw_cpu_codelet_update_u11"},
  53. #ifdef STARPU_USE_CUDA
  54. .cuda_funcs = {dw_cublas_codelet_update_u11},
  55. #endif
  56. .nbuffers = 1,
  57. .model = &model_11
  58. };
  59. static struct starpu_task *create_task_11(starpu_data_handle_t dataA, unsigned k, unsigned tag_prefix)
  60. {
  61. /* FPRINTF(stdout, "task 11 k = %d TAG = %llx\n", k, (TAG11(k))); */
  62. struct starpu_task *task = create_task(TAG11(k, tag_prefix));
  63. task->cl = &cl11;
  64. /* which sub-data is manipulated ? */
  65. task->handles[0] = starpu_data_get_sub_data(dataA, 2, k, k);
  66. /* this is an important task */
  67. task->priority = STARPU_MAX_PRIO;
  68. /* enforce dependencies ... */
  69. if (k > 0)
  70. {
  71. starpu_tag_declare_deps(TAG11(k, tag_prefix), 1, TAG22(k-1, k, k, tag_prefix));
  72. }
  73. return task;
  74. }
  75. static struct starpu_codelet cl12 =
  76. {
  77. .modes = { STARPU_R, STARPU_RW },
  78. .cpu_funcs = {dw_cpu_codelet_update_u12},
  79. .cpu_funcs_name = {"dw_cpu_codelet_update_u12"},
  80. #ifdef STARPU_USE_CUDA
  81. .cuda_funcs = {dw_cublas_codelet_update_u12},
  82. #endif
  83. .cuda_flags = {STARPU_CUDA_ASYNC},
  84. .nbuffers = 2,
  85. .model = &model_12
  86. };
  87. static void create_task_12(starpu_data_handle_t dataA, unsigned k, unsigned i, unsigned tag_prefix)
  88. {
  89. int ret;
  90. /* FPRINTF(stdout, "task 12 k,i = %d,%d TAG = %llx\n", k,i, TAG12(k,i)); */
  91. struct starpu_task *task = create_task(TAG12(k, i, tag_prefix));
  92. task->cl = &cl12;
  93. /* which sub-data is manipulated ? */
  94. task->handles[0] = starpu_data_get_sub_data(dataA, 2, k, k);
  95. task->handles[1] = starpu_data_get_sub_data(dataA, 2, i, k);
  96. if (i == k+1)
  97. {
  98. task->priority = STARPU_MAX_PRIO;
  99. }
  100. /* enforce dependencies ... */
  101. if (k > 0)
  102. {
  103. starpu_tag_declare_deps(TAG12(k, i, tag_prefix), 2, TAG11(k, tag_prefix), TAG22(k-1, i, k, tag_prefix));
  104. }
  105. else
  106. {
  107. starpu_tag_declare_deps(TAG12(k, i, tag_prefix), 1, TAG11(k, tag_prefix));
  108. }
  109. ret = starpu_task_submit(task);
  110. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  111. }
  112. static struct starpu_codelet cl21 =
  113. {
  114. .modes = { STARPU_R, STARPU_RW },
  115. .cpu_funcs = {dw_cpu_codelet_update_u21},
  116. .cpu_funcs_name = {"dw_cpu_codelet_update_u21"},
  117. #ifdef STARPU_USE_CUDA
  118. .cuda_funcs = {dw_cublas_codelet_update_u21},
  119. #endif
  120. .cuda_flags = {STARPU_CUDA_ASYNC},
  121. .nbuffers = 2,
  122. .model = &model_21
  123. };
  124. static void create_task_21(starpu_data_handle_t dataA, unsigned k, unsigned j, unsigned tag_prefix)
  125. {
  126. int ret;
  127. struct starpu_task *task = create_task(TAG21(k, j, tag_prefix));
  128. task->cl = &cl21;
  129. /* which sub-data is manipulated ? */
  130. task->handles[0] = starpu_data_get_sub_data(dataA, 2, k, k);
  131. task->handles[1] = starpu_data_get_sub_data(dataA, 2, k, j);
  132. if (j == k+1)
  133. {
  134. task->priority = STARPU_MAX_PRIO;
  135. }
  136. /* enforce dependencies ... */
  137. if (k > 0)
  138. {
  139. starpu_tag_declare_deps(TAG21(k, j, tag_prefix), 2, TAG11(k, tag_prefix), TAG22(k-1, k, j, tag_prefix));
  140. }
  141. else
  142. {
  143. starpu_tag_declare_deps(TAG21(k, j, tag_prefix), 1, TAG11(k, tag_prefix));
  144. }
  145. ret = starpu_task_submit(task);
  146. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  147. }
  148. static struct starpu_codelet cl22 =
  149. {
  150. .modes = { STARPU_R, STARPU_R, STARPU_RW },
  151. .cpu_funcs = {dw_cpu_codelet_update_u22},
  152. .cpu_funcs_name = {"dw_cpu_codelet_update_u22"},
  153. #ifdef STARPU_USE_CUDA
  154. .cuda_funcs = {dw_cublas_codelet_update_u22},
  155. #endif
  156. .cuda_flags = {STARPU_CUDA_ASYNC},
  157. .nbuffers = 3,
  158. .model = &model_22
  159. };
  160. static void create_task_22(starpu_data_handle_t dataA, unsigned k, unsigned i, unsigned j, unsigned tag_prefix)
  161. {
  162. int ret;
  163. /* FPRINTF(stdout, "task 22 k,i,j = %d,%d,%d TAG = %llx\n", k,i,j, TAG22(k,i,j)); */
  164. struct starpu_task *task = create_task(TAG22(k, i, j, tag_prefix));
  165. task->cl = &cl22;
  166. /* which sub-data is manipulated ? */
  167. task->handles[0] = starpu_data_get_sub_data(dataA, 2, i, k);
  168. task->handles[1] = starpu_data_get_sub_data(dataA, 2, k, j);
  169. task->handles[2] = starpu_data_get_sub_data(dataA, 2, i, j);
  170. if ( (i == k + 1) && (j == k +1) )
  171. {
  172. task->priority = STARPU_MAX_PRIO;
  173. }
  174. /* enforce dependencies ... */
  175. if (k > 0)
  176. {
  177. 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));
  178. }
  179. else
  180. {
  181. starpu_tag_declare_deps(TAG22(k, i, j, tag_prefix), 2, TAG12(k, i, tag_prefix), TAG21(k, j, tag_prefix));
  182. }
  183. ret = starpu_task_submit(task);
  184. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  185. }
  186. static void dw_factoLU_grain_inner(float *matA, unsigned size, unsigned inner_size,
  187. unsigned ld, unsigned blocksize, unsigned tag_prefix)
  188. {
  189. int ret;
  190. /*
  191. * (re)partition data
  192. */
  193. starpu_data_handle_t dataA;
  194. starpu_matrix_data_register(&dataA, STARPU_MAIN_RAM, (uintptr_t)matA, ld, size, size, sizeof(float));
  195. STARPU_ASSERT((size % blocksize) == 0);
  196. STARPU_ASSERT((inner_size % blocksize) == 0);
  197. unsigned nblocks = size / blocksize;
  198. unsigned maxk = inner_size / blocksize;
  199. struct starpu_data_filter f =
  200. {
  201. .filter_func = starpu_matrix_filter_vertical_block,
  202. .nchildren = nblocks
  203. };
  204. struct starpu_data_filter f2 =
  205. {
  206. .filter_func = starpu_matrix_filter_block,
  207. .nchildren = nblocks
  208. };
  209. starpu_data_map_filters(dataA, 2, &f, &f2);
  210. /*
  211. * submit tasks
  212. */
  213. struct starpu_task *entry_task = NULL;
  214. /* create all the DAG nodes */
  215. unsigned i,j,k;
  216. /* if maxk < nblocks we'll stop before the LU decomposition is totally done */
  217. for (k = 0; k < maxk; k++)
  218. {
  219. struct starpu_task *task = create_task_11(dataA, k, tag_prefix);
  220. /* we defer the launch of the first task */
  221. if (k == 0)
  222. {
  223. entry_task = task;
  224. }
  225. else
  226. {
  227. ret = starpu_task_submit(task);
  228. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  229. }
  230. for (i = k+1; i<nblocks; i++)
  231. {
  232. create_task_12(dataA, k, i, tag_prefix);
  233. create_task_21(dataA, k, i, tag_prefix);
  234. }
  235. for (i = k+1; i<nblocks; i++)
  236. {
  237. for (j = k+1; j<nblocks; j++)
  238. {
  239. create_task_22(dataA, k, i, j, tag_prefix);
  240. }
  241. }
  242. }
  243. ret = starpu_task_submit(entry_task);
  244. if (STARPU_UNLIKELY(ret == -ENODEV))
  245. {
  246. FPRINTF(stderr, "No worker may execute this task\n");
  247. exit(-1);
  248. }
  249. /* is this the last call to dw_factoLU_grain_inner ? */
  250. if (inner_size == size)
  251. {
  252. /* we wait for the last task and we are done */
  253. starpu_tag_wait(TAG11(nblocks-1, tag_prefix));
  254. starpu_data_unpartition(dataA, STARPU_MAIN_RAM);
  255. return;
  256. }
  257. else
  258. {
  259. /*
  260. * call dw_factoLU_grain_inner recursively in the remaining blocks
  261. */
  262. unsigned ndeps_tags = (nblocks - maxk)*(nblocks - maxk);
  263. starpu_tag_t *tag_array = calloc(ndeps_tags, sizeof(starpu_tag_t));
  264. STARPU_ASSERT(tag_array);
  265. unsigned ind = 0;
  266. for (i = maxk; i < nblocks; i++)
  267. for (j = maxk; j < nblocks; j++)
  268. {
  269. tag_array[ind++] = TAG22(maxk-1, i, j, tag_prefix);
  270. }
  271. starpu_tag_wait_array(ind, tag_array);
  272. free(tag_array);
  273. starpu_data_unpartition(dataA, STARPU_MAIN_RAM);
  274. starpu_data_unregister(dataA);
  275. float *newmatA = &matA[inner_size*(ld+1)];
  276. /* if (tag_prefix < 2)
  277. {
  278. dw_factoLU_grain_inner(newmatA, size-inner_size, (size-inner_size)/2, ld, blocksize/2, tag_prefix+1);
  279. }
  280. else
  281. { */
  282. dw_factoLU_grain_inner(newmatA, size-inner_size, size-inner_size, ld, blocksize/2, tag_prefix+1);
  283. /* } */
  284. }
  285. }
  286. void dw_factoLU_grain(float *matA, unsigned size, unsigned ld, unsigned nblocks, unsigned nbigblocks)
  287. {
  288. #ifdef CHECK_RESULTS
  289. FPRINTF(stderr, "Checking results ...\n");
  290. float *Asaved;
  291. Asaved = malloc(ld*ld*sizeof(float));
  292. memcpy(Asaved, matA, ld*ld*sizeof(float));
  293. #endif
  294. double start;
  295. double end;
  296. /* schedule the codelet */
  297. start = starpu_timing_now();
  298. /* that's only ok for powers of 2 yet ! */
  299. dw_factoLU_grain_inner(matA, size, (size/nblocks) * nbigblocks, ld, size/nblocks, 0);
  300. end = starpu_timing_now();
  301. double timing = end - start;
  302. unsigned n = size;
  303. double flop = (2.0f*n*n*n)/3.0f;
  304. PRINTF("# size\tms\tGFlops\n");
  305. PRINTF("%u\t%.0f\t%.1f\n", n, timing/1000, flop/timing/1000.0f);
  306. #ifdef CHECK_RESULTS
  307. compare_A_LU(Asaved, matA, size, ld);
  308. free(Asaved);
  309. #endif
  310. }