xlu_implicit_pivot.c 9.8 KB

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