dw_factolu_grain.c 9.7 KB

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