dw_factolu_grain.c 9.2 KB

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