xlu_implicit_pivot.c 9.2 KB

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