xlu_implicit_pivot.c 10 KB

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