xlu_implicit_pivot.c 9.4 KB

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