dw_factolu_grain.c 9.8 KB

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