dw_factolu_grain.c 10.0 KB

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