xlu_implicit_pivot.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012, 2014 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  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. start = starpu_timing_now();
  125. for (k = 0; k < nblocks; k++)
  126. {
  127. ret = create_task_11_pivot(dataAp, nblocks, k, piv_description, get_block);
  128. if (ret == -ENODEV) return ret;
  129. for (i = 0; i < nblocks; i++)
  130. {
  131. if (i != k)
  132. {
  133. ret = create_task_pivot(dataAp, nblocks, piv_description, k, i, get_block);
  134. if (ret == -ENODEV) return ret;
  135. }
  136. }
  137. for (i = k+1; i<nblocks; i++)
  138. {
  139. ret = create_task_12(dataAp, nblocks, k, i, get_block);
  140. if (ret == -ENODEV) return ret;
  141. ret = create_task_21(dataAp, nblocks, k, i, get_block);
  142. if (ret == -ENODEV) return ret;
  143. }
  144. for (i = k+1; i<nblocks; i++)
  145. for (j = k+1; j<nblocks; j++)
  146. {
  147. ret = create_task_22(dataAp, nblocks, k, i, j, get_block);
  148. if (ret == -ENODEV) return ret;
  149. }
  150. }
  151. /* stall the application until the end of computations */
  152. starpu_task_wait_for_all();
  153. end = starpu_timing_now();
  154. *timing = end - start;
  155. return 0;
  156. }
  157. starpu_data_handle_t get_block_with_striding(starpu_data_handle_t *dataAp,
  158. unsigned nblocks STARPU_ATTRIBUTE_UNUSED, unsigned j, unsigned i)
  159. {
  160. /* we use filters */
  161. return starpu_data_get_sub_data(*dataAp, 2, j, i);
  162. }
  163. int STARPU_LU(lu_decomposition_pivot)(TYPE *matA, unsigned *ipiv, unsigned size, unsigned ld, unsigned nblocks)
  164. {
  165. starpu_data_handle_t dataA;
  166. /* monitor and partition the A matrix into blocks :
  167. * one block is now determined by 2 unsigned (i,j) */
  168. starpu_matrix_data_register(&dataA, STARPU_MAIN_RAM, (uintptr_t)matA, ld, size, size, sizeof(TYPE));
  169. struct starpu_data_filter f =
  170. {
  171. .filter_func = starpu_matrix_filter_vertical_block,
  172. .nchildren = nblocks
  173. };
  174. struct starpu_data_filter f2 =
  175. {
  176. .filter_func = starpu_matrix_filter_block,
  177. .nchildren = nblocks
  178. };
  179. starpu_data_map_filters(dataA, 2, &f, &f2);
  180. unsigned i;
  181. for (i = 0; i < size; i++)
  182. ipiv[i] = i;
  183. struct piv_s *piv_description = malloc(nblocks*sizeof(struct piv_s));
  184. unsigned block;
  185. for (block = 0; block < nblocks; block++)
  186. {
  187. piv_description[block].piv = ipiv;
  188. piv_description[block].first = block * (size / nblocks);
  189. piv_description[block].last = (block + 1) * (size / nblocks);
  190. }
  191. double timing;
  192. int ret = dw_codelet_facto_pivot(&dataA, piv_description, nblocks, get_block_with_striding, &timing);
  193. if (ret)
  194. return ret;
  195. FPRINTF(stderr, "Computation took (in ms)\n");
  196. FPRINTF(stderr, "%2.2f\n", timing/1000);
  197. unsigned n = starpu_matrix_get_nx(dataA);
  198. double flop = (2.0f*n*n*n)/3.0f;
  199. FPRINTF(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  200. /* gather all the data */
  201. starpu_data_unpartition(dataA, STARPU_MAIN_RAM);
  202. starpu_data_unregister(dataA);
  203. free(piv_description);
  204. return ret;
  205. }
  206. starpu_data_handle_t get_block_with_no_striding(starpu_data_handle_t *dataAp, unsigned nblocks, unsigned j, unsigned i)
  207. {
  208. /* dataAp is an array of data handle */
  209. return dataAp[i+j*nblocks];
  210. }
  211. int STARPU_LU(lu_decomposition_pivot_no_stride)(TYPE **matA, unsigned *ipiv, unsigned size, unsigned ld, unsigned nblocks)
  212. {
  213. starpu_data_handle_t *dataAp = malloc(nblocks*nblocks*sizeof(starpu_data_handle_t));
  214. /* monitor and partition the A matrix into blocks :
  215. * one block is now determined by 2 unsigned (i,j) */
  216. unsigned bi, bj;
  217. for (bj = 0; bj < nblocks; bj++)
  218. for (bi = 0; bi < nblocks; bi++)
  219. {
  220. starpu_matrix_data_register(&dataAp[bi+nblocks*bj], STARPU_MAIN_RAM,
  221. (uintptr_t)matA[bi+nblocks*bj], size/nblocks,
  222. size/nblocks, size/nblocks, sizeof(TYPE));
  223. }
  224. unsigned i;
  225. for (i = 0; i < size; i++)
  226. ipiv[i] = i;
  227. struct piv_s *piv_description = malloc(nblocks*sizeof(struct piv_s));
  228. unsigned block;
  229. for (block = 0; block < nblocks; block++)
  230. {
  231. piv_description[block].piv = ipiv;
  232. piv_description[block].first = block * (size / nblocks);
  233. piv_description[block].last = (block + 1) * (size / nblocks);
  234. }
  235. double timing;
  236. int ret = dw_codelet_facto_pivot(dataAp, piv_description, nblocks, get_block_with_no_striding, &timing);
  237. if (ret)
  238. return ret;
  239. FPRINTF(stderr, "Computation took (in ms)\n");
  240. FPRINTF(stderr, "%2.2f\n", timing/1000);
  241. unsigned n = starpu_matrix_get_nx(dataAp[0])*nblocks;
  242. double flop = (2.0f*n*n*n)/3.0f;
  243. FPRINTF(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  244. for (bj = 0; bj < nblocks; bj++)
  245. for (bi = 0; bi < nblocks; bi++)
  246. {
  247. starpu_data_unregister(dataAp[bi+nblocks*bj]);
  248. }
  249. free(dataAp);
  250. return ret;
  251. }