dw_factolu_grain.c 10 KB

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