dw_factolu_grain.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. .nbuffers = 2,
  86. .model = &model_12
  87. };
  88. static void create_task_12(starpu_data_handle_t dataA, unsigned k, unsigned i, unsigned tag_prefix)
  89. {
  90. int ret;
  91. /* FPRINTF(stdout, "task 12 k,i = %d,%d TAG = %llx\n", k,i, TAG12(k,i)); */
  92. struct starpu_task *task = create_task(TAG12(k, i, tag_prefix));
  93. task->cl = &cl12;
  94. /* which sub-data is manipulated ? */
  95. task->handles[0] = starpu_data_get_sub_data(dataA, 2, k, k);
  96. task->handles[1] = starpu_data_get_sub_data(dataA, 2, i, k);
  97. if (i == k+1)
  98. {
  99. task->priority = STARPU_MAX_PRIO;
  100. }
  101. /* enforce dependencies ... */
  102. if (k > 0)
  103. {
  104. starpu_tag_declare_deps(TAG12(k, i, tag_prefix), 2, TAG11(k, tag_prefix), TAG22(k-1, i, k, tag_prefix));
  105. }
  106. else
  107. {
  108. starpu_tag_declare_deps(TAG12(k, i, tag_prefix), 1, TAG11(k, tag_prefix));
  109. }
  110. ret = starpu_task_submit(task);
  111. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  112. }
  113. static struct starpu_codelet cl21 =
  114. {
  115. .modes = { STARPU_R, STARPU_RW },
  116. .cpu_funcs = {dw_cpu_codelet_update_u21},
  117. .cpu_funcs_name = {"dw_cpu_codelet_update_u21"},
  118. #ifdef STARPU_USE_CUDA
  119. .cuda_funcs = {dw_cublas_codelet_update_u21},
  120. #endif
  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. .nbuffers = 3,
  157. .model = &model_22
  158. };
  159. static void create_task_22(starpu_data_handle_t dataA, unsigned k, unsigned i, unsigned j, unsigned tag_prefix)
  160. {
  161. int ret;
  162. /* FPRINTF(stdout, "task 22 k,i,j = %d,%d,%d TAG = %llx\n", k,i,j, TAG22(k,i,j)); */
  163. struct starpu_task *task = create_task(TAG22(k, i, j, tag_prefix));
  164. task->cl = &cl22;
  165. /* which sub-data is manipulated ? */
  166. task->handles[0] = starpu_data_get_sub_data(dataA, 2, i, k);
  167. task->handles[1] = starpu_data_get_sub_data(dataA, 2, k, j);
  168. task->handles[2] = starpu_data_get_sub_data(dataA, 2, i, j);
  169. if ( (i == k + 1) && (j == k +1) )
  170. {
  171. task->priority = STARPU_MAX_PRIO;
  172. }
  173. /* enforce dependencies ... */
  174. if (k > 0)
  175. {
  176. 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));
  177. }
  178. else
  179. {
  180. starpu_tag_declare_deps(TAG22(k, i, j, tag_prefix), 2, TAG12(k, i, tag_prefix), TAG21(k, j, tag_prefix));
  181. }
  182. ret = starpu_task_submit(task);
  183. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  184. }
  185. static void dw_factoLU_grain_inner(float *matA, unsigned size, unsigned inner_size,
  186. unsigned ld, unsigned blocksize, unsigned tag_prefix)
  187. {
  188. int ret;
  189. /*
  190. * (re)partition data
  191. */
  192. starpu_data_handle_t dataA;
  193. starpu_matrix_data_register(&dataA, STARPU_MAIN_RAM, (uintptr_t)matA, ld, size, size, sizeof(float));
  194. STARPU_ASSERT((size % blocksize) == 0);
  195. STARPU_ASSERT((inner_size % blocksize) == 0);
  196. unsigned nblocks = size / blocksize;
  197. unsigned maxk = inner_size / blocksize;
  198. struct starpu_data_filter f =
  199. {
  200. .filter_func = starpu_matrix_filter_vertical_block,
  201. .nchildren = nblocks
  202. };
  203. struct starpu_data_filter f2 =
  204. {
  205. .filter_func = starpu_matrix_filter_block,
  206. .nchildren = nblocks
  207. };
  208. starpu_data_map_filters(dataA, 2, &f, &f2);
  209. /*
  210. * submit tasks
  211. */
  212. struct starpu_task *entry_task = NULL;
  213. /* create all the DAG nodes */
  214. unsigned i,j,k;
  215. /* if maxk < nblocks we'll stop before the LU decomposition is totally done */
  216. for (k = 0; k < maxk; k++)
  217. {
  218. struct starpu_task *task = create_task_11(dataA, k, tag_prefix);
  219. /* we defer the launch of the first task */
  220. if (k == 0)
  221. {
  222. entry_task = task;
  223. }
  224. else
  225. {
  226. ret = starpu_task_submit(task);
  227. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  228. }
  229. for (i = k+1; i<nblocks; i++)
  230. {
  231. create_task_12(dataA, k, i, tag_prefix);
  232. create_task_21(dataA, k, i, tag_prefix);
  233. }
  234. for (i = k+1; i<nblocks; i++)
  235. {
  236. for (j = k+1; j<nblocks; j++)
  237. {
  238. create_task_22(dataA, k, i, j, tag_prefix);
  239. }
  240. }
  241. }
  242. ret = starpu_task_submit(entry_task);
  243. if (STARPU_UNLIKELY(ret == -ENODEV))
  244. {
  245. FPRINTF(stderr, "No worker may execute this task\n");
  246. exit(-1);
  247. }
  248. /* is this the last call to dw_factoLU_grain_inner ? */
  249. if (inner_size == size)
  250. {
  251. /* we wait for the last task and we are done */
  252. starpu_tag_wait(TAG11(nblocks-1, tag_prefix));
  253. starpu_data_unpartition(dataA, STARPU_MAIN_RAM);
  254. return;
  255. }
  256. else
  257. {
  258. /*
  259. * call dw_factoLU_grain_inner recursively in the remaining blocks
  260. */
  261. unsigned ndeps_tags = (nblocks - maxk)*(nblocks - maxk);
  262. starpu_tag_t *tag_array = calloc(ndeps_tags, sizeof(starpu_tag_t));
  263. STARPU_ASSERT(tag_array);
  264. unsigned ind = 0;
  265. for (i = maxk; i < nblocks; i++)
  266. for (j = maxk; j < nblocks; j++)
  267. {
  268. tag_array[ind++] = TAG22(maxk-1, i, j, tag_prefix);
  269. }
  270. starpu_tag_wait_array(ind, tag_array);
  271. free(tag_array);
  272. starpu_data_unpartition(dataA, STARPU_MAIN_RAM);
  273. starpu_data_unregister(dataA);
  274. float *newmatA = &matA[inner_size*(ld+1)];
  275. /* if (tag_prefix < 2)
  276. {
  277. dw_factoLU_grain_inner(newmatA, size-inner_size, (size-inner_size)/2, ld, blocksize/2, tag_prefix+1);
  278. }
  279. else
  280. { */
  281. dw_factoLU_grain_inner(newmatA, size-inner_size, size-inner_size, ld, blocksize/2, tag_prefix+1);
  282. /* } */
  283. }
  284. }
  285. void dw_factoLU_grain(float *matA, unsigned size, unsigned ld, unsigned nblocks, unsigned nbigblocks)
  286. {
  287. #ifdef CHECK_RESULTS
  288. FPRINTF(stderr, "Checking results ...\n");
  289. float *Asaved;
  290. Asaved = malloc(ld*ld*sizeof(float));
  291. memcpy(Asaved, matA, ld*ld*sizeof(float));
  292. #endif
  293. double start;
  294. double end;
  295. /* schedule the codelet */
  296. start = starpu_timing_now();
  297. /* that's only ok for powers of 2 yet ! */
  298. dw_factoLU_grain_inner(matA, size, (size/nblocks) * nbigblocks, ld, size/nblocks, 0);
  299. end = starpu_timing_now();
  300. double timing = end - start;
  301. unsigned n = size;
  302. double flop = (2.0f*n*n*n)/3.0f;
  303. PRINTF("# size\tms\tGFlops\n");
  304. PRINTF("%u\t%.0f\t%.1f\n", n, timing/1000, flop/timing/1000.0f);
  305. #ifdef CHECK_RESULTS
  306. compare_A_LU(Asaved, matA, size, ld);
  307. #endif
  308. }