xlu_implicit_pivot.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2010 Mehdi Juhoor
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /* LU StarPU implementation using implicit task dependencies and partial
  18. * pivoting */
  19. #include "xlu.h"
  20. #include "xlu_kernels.h"
  21. /*
  22. * Construct the DAG
  23. */
  24. static int create_task_pivot(starpu_data_handle_t *dataAp, unsigned nblocks,
  25. struct piv_s *piv_description,
  26. unsigned k, unsigned i,
  27. starpu_data_handle_t (* get_block)(starpu_data_handle_t *, unsigned, unsigned, unsigned), unsigned no_prio)
  28. {
  29. int ret;
  30. struct starpu_task *task = starpu_task_create();
  31. task->cl = &cl_pivot;
  32. task->color = 0xc0c000;
  33. /* which sub-data is manipulated ? */
  34. task->handles[0] = get_block(dataAp, nblocks, k, i);
  35. task->tag_id = PIVOT(k, i);
  36. task->cl_arg = &piv_description[k];
  37. /* this is an important task */
  38. if (!no_prio && (i == k+1))
  39. task->priority = STARPU_MAX_PRIO;
  40. ret = starpu_task_submit(task);
  41. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  42. return ret;
  43. }
  44. static int create_task_11_pivot(starpu_data_handle_t *dataAp, unsigned nblocks,
  45. unsigned k, struct piv_s *piv_description,
  46. starpu_data_handle_t (* get_block)(starpu_data_handle_t *, unsigned, unsigned, unsigned), unsigned no_prio)
  47. {
  48. int ret;
  49. struct starpu_task *task = starpu_task_create();
  50. task->cl = &cl11_pivot;
  51. task->color = 0xffff00;
  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), unsigned no_prio)
  65. {
  66. int ret;
  67. struct starpu_task *task = starpu_task_create();
  68. task->cl = &cl12;
  69. task->color = 0x8080ff;
  70. /* which sub-data is manipulated ? */
  71. task->handles[0] = get_block(dataAp, nblocks, k, k);
  72. task->handles[1] = get_block(dataAp, nblocks, j, k);
  73. task->tag_id = TAG12(k,j);
  74. if (!no_prio && (j == k+1))
  75. task->priority = STARPU_MAX_PRIO;
  76. ret = starpu_task_submit(task);
  77. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  78. return ret;
  79. }
  80. static int create_task_21(starpu_data_handle_t *dataAp, unsigned nblocks, unsigned k, unsigned i,
  81. starpu_data_handle_t (* get_block)(starpu_data_handle_t *, unsigned, unsigned, unsigned), unsigned no_prio)
  82. {
  83. int ret;
  84. struct starpu_task *task = starpu_task_create();
  85. task->cl = &cl21;
  86. task->color = 0x8080c0;
  87. /* which sub-data is manipulated ? */
  88. task->handles[0] = get_block(dataAp, nblocks, k, k);
  89. task->handles[1] = get_block(dataAp, nblocks, k, i);
  90. task->tag_id = TAG21(k,i);
  91. if (!no_prio && (i == k+1))
  92. task->priority = STARPU_MAX_PRIO;
  93. ret = starpu_task_submit(task);
  94. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  95. return ret;
  96. }
  97. static int create_task_22(starpu_data_handle_t *dataAp, unsigned nblocks, unsigned k, unsigned i, unsigned j,
  98. starpu_data_handle_t (* get_block)(starpu_data_handle_t *, unsigned, unsigned, unsigned), unsigned no_prio)
  99. {
  100. int ret;
  101. struct starpu_task *task = starpu_task_create();
  102. task->cl = &cl22;
  103. task->color = 0x00ff00;
  104. /* which sub-data is manipulated ? */
  105. task->handles[0] = get_block(dataAp, nblocks, k, i);
  106. task->handles[1] = get_block(dataAp, nblocks, j, k);
  107. task->handles[2] = get_block(dataAp, nblocks, j, i);
  108. task->tag_id = TAG22(k,i,j);
  109. if (!no_prio && (i == k + 1) && (j == k +1) )
  110. task->priority = STARPU_MAX_PRIO;
  111. ret = starpu_task_submit(task);
  112. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  113. return ret;
  114. }
  115. /*
  116. * code to bootstrap the factorization
  117. */
  118. static int dw_codelet_facto_pivot(starpu_data_handle_t *dataAp,
  119. struct piv_s *piv_description,
  120. unsigned nblocks,
  121. starpu_data_handle_t (* get_block)(starpu_data_handle_t *, unsigned, unsigned, unsigned),
  122. double *timing, unsigned no_prio)
  123. {
  124. double start;
  125. double end;
  126. /* create all the DAG nodes */
  127. unsigned i,j,k;
  128. if (bound)
  129. starpu_bound_start(bounddeps, boundprio);
  130. start = starpu_timing_now();
  131. for (k = 0; k < nblocks; k++)
  132. {
  133. int ret;
  134. starpu_iteration_push(k);
  135. ret = create_task_11_pivot(dataAp, nblocks, k, piv_description, get_block, no_prio);
  136. if (ret == -ENODEV) return ret;
  137. for (i = 0; i < nblocks; i++)
  138. {
  139. if (i != k)
  140. {
  141. ret = create_task_pivot(dataAp, nblocks, piv_description, k, i, get_block, no_prio);
  142. if (ret == -ENODEV) return ret;
  143. }
  144. }
  145. for (i = k+1; i<nblocks; i++)
  146. {
  147. ret = create_task_12(dataAp, nblocks, k, i, get_block, no_prio);
  148. if (ret == -ENODEV) return ret;
  149. ret = create_task_21(dataAp, nblocks, k, i, get_block, no_prio);
  150. if (ret == -ENODEV) return ret;
  151. }
  152. starpu_data_wont_use(get_block(dataAp, nblocks, k, k));
  153. for (i = k+1; i<nblocks; i++)
  154. for (j = k+1; j<nblocks; j++)
  155. {
  156. ret = create_task_22(dataAp, nblocks, k, i, j, get_block, no_prio);
  157. if (ret == -ENODEV) return ret;
  158. }
  159. for (i = k+1; i<nblocks; i++)
  160. {
  161. starpu_data_wont_use(get_block(dataAp, nblocks, k, i));
  162. starpu_data_wont_use(get_block(dataAp, nblocks, i, k));
  163. }
  164. starpu_iteration_pop();
  165. }
  166. /* stall the application until the end of computations */
  167. starpu_task_wait_for_all();
  168. end = starpu_timing_now();
  169. if (bound)
  170. starpu_bound_stop();
  171. *timing = end - start;
  172. return 0;
  173. }
  174. starpu_data_handle_t get_block_with_striding(starpu_data_handle_t *dataAp, unsigned nblocks, unsigned j, unsigned i)
  175. {
  176. /* we use filters */
  177. (void)nblocks;
  178. return starpu_data_get_sub_data(*dataAp, 2, j, i);
  179. }
  180. int STARPU_LU(lu_decomposition_pivot)(TYPE *matA, unsigned *ipiv, unsigned size, unsigned ld, unsigned nblocks, unsigned no_prio)
  181. {
  182. if (starpu_mpi_ms_worker_get_count())
  183. /* These won't work with pivoting: we pass a pointer in cl_args */
  184. return -ENODEV;
  185. starpu_data_handle_t dataA;
  186. /* monitor and partition the A matrix into blocks :
  187. * one block is now determined by 2 unsigned (i,j) */
  188. starpu_matrix_data_register(&dataA, STARPU_MAIN_RAM, (uintptr_t)matA, ld, size, size, sizeof(TYPE));
  189. struct starpu_data_filter f =
  190. {
  191. .filter_func = starpu_matrix_filter_vertical_block,
  192. .nchildren = nblocks
  193. };
  194. struct starpu_data_filter f2 =
  195. {
  196. .filter_func = starpu_matrix_filter_block,
  197. .nchildren = nblocks
  198. };
  199. starpu_data_map_filters(dataA, 2, &f, &f2);
  200. unsigned i;
  201. for (i = 0; i < size; i++)
  202. ipiv[i] = i;
  203. struct piv_s *piv_description = malloc(nblocks*sizeof(struct piv_s));
  204. unsigned block;
  205. for (block = 0; block < nblocks; block++)
  206. {
  207. piv_description[block].piv = ipiv;
  208. piv_description[block].first = block * (size / nblocks);
  209. piv_description[block].last = (block + 1) * (size / nblocks);
  210. }
  211. double timing;
  212. int ret = dw_codelet_facto_pivot(&dataA, piv_description, nblocks, get_block_with_striding, &timing, no_prio);
  213. if (ret)
  214. return ret;
  215. unsigned n = starpu_matrix_get_nx(dataA);
  216. double flop = (2.0f*n*n*n)/3.0f;
  217. PRINTF("# size\tms\tGFlops");
  218. if (bound)
  219. PRINTF("\tTms\tTGFlops");
  220. PRINTF("\n");
  221. PRINTF("%u\t%.0f\t%.1f", n, timing/1000, flop/timing/1000.0f);
  222. if (bound)
  223. {
  224. double min;
  225. starpu_bound_compute(&min, NULL, 0);
  226. PRINTF("\t%.0f\t%.1f", min, flop/min/1000000.0f);
  227. }
  228. PRINTF("\n");
  229. /* gather all the data */
  230. starpu_data_unpartition(dataA, STARPU_MAIN_RAM);
  231. starpu_data_unregister(dataA);
  232. free(piv_description);
  233. return 0;
  234. }
  235. starpu_data_handle_t get_block_with_no_striding(starpu_data_handle_t *dataAp, unsigned nblocks, unsigned j, unsigned i)
  236. {
  237. /* dataAp is an array of data handle */
  238. return dataAp[i+j*nblocks];
  239. }
  240. int STARPU_LU(lu_decomposition_pivot_no_stride)(TYPE **matA, unsigned *ipiv, unsigned size, unsigned ld, unsigned nblocks, unsigned no_prio)
  241. {
  242. (void)ld;
  243. starpu_data_handle_t *dataAp = malloc(nblocks*nblocks*sizeof(starpu_data_handle_t));
  244. /* monitor and partition the A matrix into blocks :
  245. * one block is now determined by 2 unsigned (i,j) */
  246. unsigned bi, bj;
  247. for (bj = 0; bj < nblocks; bj++)
  248. for (bi = 0; bi < nblocks; bi++)
  249. {
  250. starpu_matrix_data_register(&dataAp[bi+nblocks*bj], STARPU_MAIN_RAM,
  251. (uintptr_t)matA[bi+nblocks*bj], size/nblocks,
  252. size/nblocks, size/nblocks, sizeof(TYPE));
  253. }
  254. unsigned i;
  255. for (i = 0; i < size; i++)
  256. ipiv[i] = i;
  257. struct piv_s *piv_description = malloc(nblocks*sizeof(struct piv_s));
  258. unsigned block;
  259. for (block = 0; block < nblocks; block++)
  260. {
  261. piv_description[block].piv = ipiv;
  262. piv_description[block].first = block * (size / nblocks);
  263. piv_description[block].last = (block + 1) * (size / nblocks);
  264. }
  265. double timing;
  266. int ret = dw_codelet_facto_pivot(dataAp, piv_description, nblocks, get_block_with_no_striding, &timing, no_prio);
  267. if (ret)
  268. return ret;
  269. unsigned n = starpu_matrix_get_nx(dataAp[0])*nblocks;
  270. double flop = (2.0f*n*n*n)/3.0f;
  271. PRINTF("# size\tms\tGFlops");
  272. if (bound)
  273. PRINTF("\tTms\tTGFlops");
  274. PRINTF("\n");
  275. PRINTF("%u\t%.0f\t%.1f", n, timing/1000, flop/timing/1000.0f);
  276. if (bound)
  277. {
  278. double min;
  279. starpu_bound_compute(&min, NULL, 0);
  280. PRINTF("\t%.0f\t%.1f", min, flop/min/1000000.0f);
  281. }
  282. PRINTF("\n");
  283. for (bj = 0; bj < nblocks; bj++)
  284. for (bi = 0; bi < nblocks; bi++)
  285. {
  286. starpu_data_unregister(dataAp[bi+nblocks*bj]);
  287. }
  288. free(dataAp);
  289. free(piv_description);
  290. return ret;
  291. }