xlu_implicit_pivot.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012, 2014-2015 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. /* LU StarPU implementation using implicit task dependencies and partial
  19. * pivoting */
  20. #include "xlu.h"
  21. #include "xlu_kernels.h"
  22. static unsigned no_prio = 0;
  23. /*
  24. * Construct the DAG
  25. */
  26. static int create_task_pivot(starpu_data_handle_t *dataAp, unsigned nblocks,
  27. struct piv_s *piv_description,
  28. unsigned k, unsigned i,
  29. starpu_data_handle_t (* get_block)(starpu_data_handle_t *, unsigned, unsigned, unsigned))
  30. {
  31. int ret;
  32. struct starpu_task *task = starpu_task_create();
  33. task->cl = &cl_pivot;
  34. /* which sub-data is manipulated ? */
  35. task->handles[0] = get_block(dataAp, nblocks, k, i);
  36. task->tag_id = PIVOT(k, i);
  37. task->cl_arg = &piv_description[k];
  38. /* this is an important task */
  39. if (!no_prio && (i == k+1))
  40. task->priority = STARPU_MAX_PRIO;
  41. ret = starpu_task_submit(task);
  42. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  43. return ret;
  44. }
  45. static int create_task_11_pivot(starpu_data_handle_t *dataAp, unsigned nblocks,
  46. unsigned k, struct piv_s *piv_description,
  47. starpu_data_handle_t (* get_block)(starpu_data_handle_t *, unsigned, unsigned, unsigned))
  48. {
  49. int ret;
  50. struct starpu_task *task = starpu_task_create();
  51. task->cl = &cl11_pivot;
  52. task->cl_arg = &piv_description[k];
  53. /* which sub-data is manipulated ? */
  54. task->handles[0] = get_block(dataAp, nblocks, k, k);
  55. task->tag_id = TAG11(k);
  56. /* this is an important task */
  57. if (!no_prio)
  58. task->priority = STARPU_MAX_PRIO;
  59. ret = starpu_task_submit(task);
  60. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  61. return ret;
  62. }
  63. static int create_task_12(starpu_data_handle_t *dataAp, unsigned nblocks, unsigned k, unsigned j,
  64. starpu_data_handle_t (* get_block)(starpu_data_handle_t *, unsigned, unsigned, unsigned))
  65. {
  66. int ret;
  67. struct starpu_task *task = starpu_task_create();
  68. task->cl = &cl12;
  69. /* which sub-data is manipulated ? */
  70. task->handles[0] = get_block(dataAp, nblocks, k, k);
  71. task->handles[1] = get_block(dataAp, nblocks, j, k);
  72. task->tag_id = TAG12(k,j);
  73. if (!no_prio && (j == k+1))
  74. task->priority = STARPU_MAX_PRIO;
  75. ret = starpu_task_submit(task);
  76. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  77. return ret;
  78. }
  79. static int create_task_21(starpu_data_handle_t *dataAp, unsigned nblocks, unsigned k, unsigned i,
  80. starpu_data_handle_t (* get_block)(starpu_data_handle_t *, unsigned, unsigned, unsigned))
  81. {
  82. int ret;
  83. struct starpu_task *task = starpu_task_create();
  84. task->cl = &cl21;
  85. /* which sub-data is manipulated ? */
  86. task->handles[0] = get_block(dataAp, nblocks, k, k);
  87. task->handles[1] = get_block(dataAp, nblocks, k, i);
  88. task->tag_id = TAG21(k,i);
  89. if (!no_prio && (i == k+1))
  90. task->priority = STARPU_MAX_PRIO;
  91. ret = starpu_task_submit(task);
  92. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  93. return ret;
  94. }
  95. static int create_task_22(starpu_data_handle_t *dataAp, unsigned nblocks, unsigned k, unsigned i, unsigned j,
  96. starpu_data_handle_t (* get_block)(starpu_data_handle_t *, unsigned, unsigned, unsigned))
  97. {
  98. int ret;
  99. struct starpu_task *task = starpu_task_create();
  100. task->cl = &cl22;
  101. /* which sub-data is manipulated ? */
  102. task->handles[0] = get_block(dataAp, nblocks, k, i);
  103. task->handles[1] = get_block(dataAp, nblocks, j, k);
  104. task->handles[2] = get_block(dataAp, nblocks, j, i);
  105. task->tag_id = TAG22(k,i,j);
  106. if (!no_prio && (i == k + 1) && (j == k +1) )
  107. task->priority = STARPU_MAX_PRIO;
  108. ret = starpu_task_submit(task);
  109. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  110. return ret;
  111. }
  112. /*
  113. * code to bootstrap the factorization
  114. */
  115. static int dw_codelet_facto_pivot(starpu_data_handle_t *dataAp,
  116. struct piv_s *piv_description,
  117. unsigned nblocks,
  118. starpu_data_handle_t (* get_block)(starpu_data_handle_t *, unsigned, unsigned, unsigned),
  119. double *timing)
  120. {
  121. double start;
  122. double end;
  123. /* create all the DAG nodes */
  124. unsigned i,j,k;
  125. if (bound)
  126. starpu_bound_start(bounddeps, boundprio);
  127. start = starpu_timing_now();
  128. for (k = 0; k < nblocks; k++)
  129. {
  130. int ret;
  131. ret = create_task_11_pivot(dataAp, nblocks, k, piv_description, get_block);
  132. if (ret == -ENODEV) return ret;
  133. for (i = 0; i < nblocks; i++)
  134. {
  135. if (i != k)
  136. {
  137. ret = create_task_pivot(dataAp, nblocks, piv_description, k, i, get_block);
  138. if (ret == -ENODEV) return ret;
  139. }
  140. }
  141. for (i = k+1; i<nblocks; i++)
  142. {
  143. ret = create_task_12(dataAp, nblocks, k, i, get_block);
  144. if (ret == -ENODEV) return ret;
  145. ret = create_task_21(dataAp, nblocks, k, i, get_block);
  146. if (ret == -ENODEV) return ret;
  147. }
  148. starpu_data_wont_use(get_block(dataAp, nblocks, k, k));
  149. for (i = k+1; i<nblocks; i++)
  150. for (j = k+1; j<nblocks; j++)
  151. {
  152. ret = create_task_22(dataAp, nblocks, k, i, j, get_block);
  153. if (ret == -ENODEV) return ret;
  154. }
  155. for (i = k+1; i<nblocks; i++)
  156. {
  157. starpu_data_wont_use(get_block(dataAp, nblocks, k, i));
  158. starpu_data_wont_use(get_block(dataAp, nblocks, i, k));
  159. }
  160. }
  161. /* stall the application until the end of computations */
  162. starpu_task_wait_for_all();
  163. end = starpu_timing_now();
  164. if (bound)
  165. starpu_bound_stop();
  166. *timing = end - start;
  167. return 0;
  168. }
  169. starpu_data_handle_t get_block_with_striding(starpu_data_handle_t *dataAp,
  170. unsigned nblocks STARPU_ATTRIBUTE_UNUSED, unsigned j, unsigned i)
  171. {
  172. /* we use filters */
  173. return starpu_data_get_sub_data(*dataAp, 2, j, i);
  174. }
  175. int STARPU_LU(lu_decomposition_pivot)(TYPE *matA, unsigned *ipiv, unsigned size, unsigned ld, unsigned nblocks)
  176. {
  177. starpu_data_handle_t dataA;
  178. /* monitor and partition the A matrix into blocks :
  179. * one block is now determined by 2 unsigned (i,j) */
  180. starpu_matrix_data_register(&dataA, STARPU_MAIN_RAM, (uintptr_t)matA, ld, size, size, sizeof(TYPE));
  181. struct starpu_data_filter f =
  182. {
  183. .filter_func = starpu_matrix_filter_vertical_block,
  184. .nchildren = nblocks
  185. };
  186. struct starpu_data_filter f2 =
  187. {
  188. .filter_func = starpu_matrix_filter_block,
  189. .nchildren = nblocks
  190. };
  191. starpu_data_map_filters(dataA, 2, &f, &f2);
  192. unsigned i;
  193. for (i = 0; i < size; i++)
  194. ipiv[i] = i;
  195. struct piv_s *piv_description = malloc(nblocks*sizeof(struct piv_s));
  196. unsigned block;
  197. for (block = 0; block < nblocks; block++)
  198. {
  199. piv_description[block].piv = ipiv;
  200. piv_description[block].first = block * (size / nblocks);
  201. piv_description[block].last = (block + 1) * (size / nblocks);
  202. }
  203. double timing;
  204. int ret = dw_codelet_facto_pivot(&dataA, piv_description, nblocks, get_block_with_striding, &timing);
  205. if (ret)
  206. return ret;
  207. unsigned n = starpu_matrix_get_nx(dataA);
  208. double flop = (2.0f*n*n*n)/3.0f;
  209. PRINTF("# size\tms\tGFlops");
  210. if (bound)
  211. PRINTF("\tTms\tTGFlops");
  212. PRINTF("\n");
  213. PRINTF("%u\t%.0f\t%.1f", n, timing/1000, flop/timing/1000.0f);
  214. if (bound)
  215. {
  216. double min;
  217. starpu_bound_compute(&min, NULL, 0);
  218. PRINTF("\t%.0f\t%.1f", min, flop/min/1000000.0f);
  219. }
  220. PRINTF("\n");
  221. /* gather all the data */
  222. starpu_data_unpartition(dataA, STARPU_MAIN_RAM);
  223. starpu_data_unregister(dataA);
  224. free(piv_description);
  225. return ret;
  226. }
  227. starpu_data_handle_t get_block_with_no_striding(starpu_data_handle_t *dataAp, unsigned nblocks, unsigned j, unsigned i)
  228. {
  229. /* dataAp is an array of data handle */
  230. return dataAp[i+j*nblocks];
  231. }
  232. int STARPU_LU(lu_decomposition_pivot_no_stride)(TYPE **matA, unsigned *ipiv, unsigned size, unsigned ld, unsigned nblocks)
  233. {
  234. starpu_data_handle_t *dataAp = malloc(nblocks*nblocks*sizeof(starpu_data_handle_t));
  235. /* monitor and partition the A matrix into blocks :
  236. * one block is now determined by 2 unsigned (i,j) */
  237. unsigned bi, bj;
  238. for (bj = 0; bj < nblocks; bj++)
  239. for (bi = 0; bi < nblocks; bi++)
  240. {
  241. starpu_matrix_data_register(&dataAp[bi+nblocks*bj], STARPU_MAIN_RAM,
  242. (uintptr_t)matA[bi+nblocks*bj], size/nblocks,
  243. size/nblocks, size/nblocks, sizeof(TYPE));
  244. }
  245. unsigned i;
  246. for (i = 0; i < size; i++)
  247. ipiv[i] = i;
  248. struct piv_s *piv_description = malloc(nblocks*sizeof(struct piv_s));
  249. unsigned block;
  250. for (block = 0; block < nblocks; block++)
  251. {
  252. piv_description[block].piv = ipiv;
  253. piv_description[block].first = block * (size / nblocks);
  254. piv_description[block].last = (block + 1) * (size / nblocks);
  255. }
  256. double timing;
  257. int ret = dw_codelet_facto_pivot(dataAp, piv_description, nblocks, get_block_with_no_striding, &timing);
  258. if (ret)
  259. return ret;
  260. unsigned n = starpu_matrix_get_nx(dataAp[0])*nblocks;
  261. double flop = (2.0f*n*n*n)/3.0f;
  262. PRINTF("# size\tms\tGFlops");
  263. if (bound)
  264. PRINTF("\tTms\tTGFlops");
  265. PRINTF("\n");
  266. PRINTF("%u\t%.0f\t%.1f", n, timing/1000, flop/timing/1000.0f);
  267. if (bound)
  268. {
  269. double min;
  270. starpu_bound_compute(&min, NULL, 0);
  271. PRINTF("\t%.0f\t%.1f", min, flop/min/1000000.0f);
  272. }
  273. PRINTF("\n");
  274. for (bj = 0; bj < nblocks; bj++)
  275. for (bi = 0; bi < nblocks; bi++)
  276. {
  277. starpu_data_unregister(dataAp[bi+nblocks*bj]);
  278. }
  279. free(dataAp);
  280. return ret;
  281. }