xlu_implicit_pivot.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012, 2014-2015, 2017 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. starpu_iteration_push(k);
  132. ret = create_task_11_pivot(dataAp, nblocks, k, piv_description, get_block);
  133. if (ret == -ENODEV) return ret;
  134. for (i = 0; i < nblocks; i++)
  135. {
  136. if (i != k)
  137. {
  138. ret = create_task_pivot(dataAp, nblocks, piv_description, k, i, get_block);
  139. if (ret == -ENODEV) return ret;
  140. }
  141. }
  142. for (i = k+1; i<nblocks; i++)
  143. {
  144. ret = create_task_12(dataAp, nblocks, k, i, get_block);
  145. if (ret == -ENODEV) return ret;
  146. ret = create_task_21(dataAp, nblocks, k, i, get_block);
  147. if (ret == -ENODEV) return ret;
  148. }
  149. starpu_data_wont_use(get_block(dataAp, nblocks, k, k));
  150. for (i = k+1; i<nblocks; i++)
  151. for (j = k+1; j<nblocks; j++)
  152. {
  153. ret = create_task_22(dataAp, nblocks, k, i, j, get_block);
  154. if (ret == -ENODEV) return ret;
  155. }
  156. for (i = k+1; i<nblocks; i++)
  157. {
  158. starpu_data_wont_use(get_block(dataAp, nblocks, k, i));
  159. starpu_data_wont_use(get_block(dataAp, nblocks, i, k));
  160. }
  161. starpu_iteration_pop();
  162. }
  163. /* stall the application until the end of computations */
  164. starpu_task_wait_for_all();
  165. end = starpu_timing_now();
  166. if (bound)
  167. starpu_bound_stop();
  168. *timing = end - start;
  169. return 0;
  170. }
  171. starpu_data_handle_t get_block_with_striding(starpu_data_handle_t *dataAp,
  172. unsigned nblocks STARPU_ATTRIBUTE_UNUSED, unsigned j, unsigned i)
  173. {
  174. /* we use filters */
  175. return starpu_data_get_sub_data(*dataAp, 2, j, i);
  176. }
  177. int STARPU_LU(lu_decomposition_pivot)(TYPE *matA, unsigned *ipiv, unsigned size, unsigned ld, unsigned nblocks)
  178. {
  179. if (starpu_mic_worker_get_count() || starpu_scc_worker_get_count() || starpu_mpi_ms_worker_get_count())
  180. /* These won't work with pivoting: we pass a pointer in cl_args */
  181. return -ENODEV;
  182. starpu_data_handle_t dataA;
  183. /* monitor and partition the A matrix into blocks :
  184. * one block is now determined by 2 unsigned (i,j) */
  185. starpu_matrix_data_register(&dataA, STARPU_MAIN_RAM, (uintptr_t)matA, ld, size, size, sizeof(TYPE));
  186. struct starpu_data_filter f =
  187. {
  188. .filter_func = starpu_matrix_filter_vertical_block,
  189. .nchildren = nblocks
  190. };
  191. struct starpu_data_filter f2 =
  192. {
  193. .filter_func = starpu_matrix_filter_block,
  194. .nchildren = nblocks
  195. };
  196. starpu_data_map_filters(dataA, 2, &f, &f2);
  197. unsigned i;
  198. for (i = 0; i < size; i++)
  199. ipiv[i] = i;
  200. struct piv_s *piv_description = malloc(nblocks*sizeof(struct piv_s));
  201. unsigned block;
  202. for (block = 0; block < nblocks; block++)
  203. {
  204. piv_description[block].piv = ipiv;
  205. piv_description[block].first = block * (size / nblocks);
  206. piv_description[block].last = (block + 1) * (size / nblocks);
  207. }
  208. double timing;
  209. int ret = dw_codelet_facto_pivot(&dataA, piv_description, nblocks, get_block_with_striding, &timing);
  210. if (ret)
  211. return ret;
  212. unsigned n = starpu_matrix_get_nx(dataA);
  213. double flop = (2.0f*n*n*n)/3.0f;
  214. PRINTF("# size\tms\tGFlops");
  215. if (bound)
  216. PRINTF("\tTms\tTGFlops");
  217. PRINTF("\n");
  218. PRINTF("%u\t%.0f\t%.1f", n, timing/1000, flop/timing/1000.0f);
  219. if (bound)
  220. {
  221. double min;
  222. starpu_bound_compute(&min, NULL, 0);
  223. PRINTF("\t%.0f\t%.1f", min, flop/min/1000000.0f);
  224. }
  225. PRINTF("\n");
  226. /* gather all the data */
  227. starpu_data_unpartition(dataA, STARPU_MAIN_RAM);
  228. starpu_data_unregister(dataA);
  229. free(piv_description);
  230. return ret;
  231. }
  232. starpu_data_handle_t get_block_with_no_striding(starpu_data_handle_t *dataAp, unsigned nblocks, unsigned j, unsigned i)
  233. {
  234. /* dataAp is an array of data handle */
  235. return dataAp[i+j*nblocks];
  236. }
  237. int STARPU_LU(lu_decomposition_pivot_no_stride)(TYPE **matA, unsigned *ipiv, unsigned size, unsigned ld, unsigned nblocks)
  238. {
  239. starpu_data_handle_t *dataAp = malloc(nblocks*nblocks*sizeof(starpu_data_handle_t));
  240. /* monitor and partition the A matrix into blocks :
  241. * one block is now determined by 2 unsigned (i,j) */
  242. unsigned bi, bj;
  243. for (bj = 0; bj < nblocks; bj++)
  244. for (bi = 0; bi < nblocks; bi++)
  245. {
  246. starpu_matrix_data_register(&dataAp[bi+nblocks*bj], STARPU_MAIN_RAM,
  247. (uintptr_t)matA[bi+nblocks*bj], size/nblocks,
  248. size/nblocks, size/nblocks, sizeof(TYPE));
  249. }
  250. unsigned i;
  251. for (i = 0; i < size; i++)
  252. ipiv[i] = i;
  253. struct piv_s *piv_description = malloc(nblocks*sizeof(struct piv_s));
  254. unsigned block;
  255. for (block = 0; block < nblocks; block++)
  256. {
  257. piv_description[block].piv = ipiv;
  258. piv_description[block].first = block * (size / nblocks);
  259. piv_description[block].last = (block + 1) * (size / nblocks);
  260. }
  261. double timing;
  262. int ret = dw_codelet_facto_pivot(dataAp, piv_description, nblocks, get_block_with_no_striding, &timing);
  263. if (ret)
  264. return ret;
  265. unsigned n = starpu_matrix_get_nx(dataAp[0])*nblocks;
  266. double flop = (2.0f*n*n*n)/3.0f;
  267. PRINTF("# size\tms\tGFlops");
  268. if (bound)
  269. PRINTF("\tTms\tTGFlops");
  270. PRINTF("\n");
  271. PRINTF("%u\t%.0f\t%.1f", n, timing/1000, flop/timing/1000.0f);
  272. if (bound)
  273. {
  274. double min;
  275. starpu_bound_compute(&min, NULL, 0);
  276. PRINTF("\t%.0f\t%.1f", min, flop/min/1000000.0f);
  277. }
  278. PRINTF("\n");
  279. for (bj = 0; bj < nblocks; bj++)
  280. for (bi = 0; bi < nblocks; bi++)
  281. {
  282. starpu_data_unregister(dataAp[bi+nblocks*bj]);
  283. }
  284. free(dataAp);
  285. return ret;
  286. }