xlu_implicit_pivot.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 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->cl_arg = &piv_description[k];
  35. /* this is an important task */
  36. if (!no_prio && (i == k+1))
  37. task->priority = STARPU_MAX_PRIO;
  38. ret = starpu_task_submit(task);
  39. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  40. return ret;
  41. }
  42. static int create_task_11_pivot(starpu_data_handle_t *dataAp, unsigned nblocks,
  43. unsigned k, struct piv_s *piv_description,
  44. starpu_data_handle_t (* get_block)(starpu_data_handle_t *, unsigned, unsigned, unsigned))
  45. {
  46. int ret;
  47. struct starpu_task *task = starpu_task_create();
  48. task->cl = &cl11_pivot;
  49. task->cl_arg = &piv_description[k];
  50. /* which sub-data is manipulated ? */
  51. task->handles[0] = get_block(dataAp, nblocks, k, k);
  52. /* this is an important task */
  53. if (!no_prio)
  54. task->priority = STARPU_MAX_PRIO;
  55. ret = starpu_task_submit(task);
  56. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  57. return ret;
  58. }
  59. static int create_task_12(starpu_data_handle_t *dataAp, unsigned nblocks, unsigned k, unsigned j,
  60. starpu_data_handle_t (* get_block)(starpu_data_handle_t *, unsigned, unsigned, unsigned))
  61. {
  62. int ret;
  63. struct starpu_task *task = starpu_task_create();
  64. task->cl = &cl12;
  65. /* which sub-data is manipulated ? */
  66. task->handles[0] = get_block(dataAp, nblocks, k, k);
  67. task->handles[1] = get_block(dataAp, nblocks, j, k);
  68. if (!no_prio && (j == k+1))
  69. task->priority = STARPU_MAX_PRIO;
  70. ret = starpu_task_submit(task);
  71. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  72. return ret;
  73. }
  74. static int create_task_21(starpu_data_handle_t *dataAp, unsigned nblocks, unsigned k, unsigned i,
  75. starpu_data_handle_t (* get_block)(starpu_data_handle_t *, unsigned, unsigned, unsigned))
  76. {
  77. int ret;
  78. struct starpu_task *task = starpu_task_create();
  79. task->cl = &cl21;
  80. /* which sub-data is manipulated ? */
  81. task->handles[0] = get_block(dataAp, nblocks, k, k);
  82. task->handles[1] = get_block(dataAp, nblocks, k, i);
  83. if (!no_prio && (i == k+1))
  84. task->priority = STARPU_MAX_PRIO;
  85. ret = starpu_task_submit(task);
  86. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  87. return ret;
  88. }
  89. static int create_task_22(starpu_data_handle_t *dataAp, unsigned nblocks, unsigned k, unsigned i, unsigned j,
  90. starpu_data_handle_t (* get_block)(starpu_data_handle_t *, unsigned, unsigned, unsigned))
  91. {
  92. int ret;
  93. struct starpu_task *task = starpu_task_create();
  94. task->cl = &cl22;
  95. /* which sub-data is manipulated ? */
  96. task->handles[0] = get_block(dataAp, nblocks, k, i);
  97. task->handles[1] = get_block(dataAp, nblocks, j, k);
  98. task->handles[2] = get_block(dataAp, nblocks, j, i);
  99. if (!no_prio && (i == k + 1) && (j == k +1) )
  100. task->priority = STARPU_MAX_PRIO;
  101. ret = starpu_task_submit(task);
  102. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  103. return ret;
  104. }
  105. /*
  106. * code to bootstrap the factorization
  107. */
  108. static int dw_codelet_facto_pivot(starpu_data_handle_t *dataAp,
  109. struct piv_s *piv_description,
  110. unsigned nblocks,
  111. starpu_data_handle_t (* get_block)(starpu_data_handle_t *, unsigned, unsigned, unsigned),
  112. double *timing)
  113. {
  114. struct timeval start;
  115. struct timeval end;
  116. int ret;
  117. gettimeofday(&start, NULL);
  118. /* create all the DAG nodes */
  119. unsigned i,j,k;
  120. for (k = 0; k < nblocks; k++)
  121. {
  122. ret = create_task_11_pivot(dataAp, nblocks, k, piv_description, get_block);
  123. if (ret == -ENODEV) return ret;
  124. for (i = 0; i < nblocks; i++)
  125. {
  126. if (i != k)
  127. {
  128. ret = create_task_pivot(dataAp, nblocks, piv_description, k, i, get_block);
  129. if (ret == -ENODEV) return ret;
  130. }
  131. }
  132. for (i = k+1; i<nblocks; i++)
  133. {
  134. ret = create_task_12(dataAp, nblocks, k, i, get_block);
  135. if (ret == -ENODEV) return ret;
  136. ret = create_task_21(dataAp, nblocks, k, i, get_block);
  137. if (ret == -ENODEV) return ret;
  138. }
  139. for (i = k+1; i<nblocks; i++)
  140. for (j = k+1; j<nblocks; j++)
  141. {
  142. ret = create_task_22(dataAp, nblocks, k, i, j, get_block);
  143. if (ret == -ENODEV) return ret;
  144. }
  145. }
  146. /* stall the application until the end of computations */
  147. starpu_task_wait_for_all();
  148. gettimeofday(&end, NULL);
  149. *timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  150. return 0;
  151. }
  152. starpu_data_handle_t get_block_with_striding(starpu_data_handle_t *dataAp,
  153. unsigned nblocks STARPU_ATTRIBUTE_UNUSED, unsigned j, unsigned i)
  154. {
  155. /* we use filters */
  156. return starpu_data_get_sub_data(*dataAp, 2, j, i);
  157. }
  158. int STARPU_LU(lu_decomposition_pivot)(TYPE *matA, unsigned *ipiv, unsigned size, unsigned ld, unsigned nblocks)
  159. {
  160. starpu_data_handle_t dataA;
  161. /* monitor and partition the A matrix into blocks :
  162. * one block is now determined by 2 unsigned (i,j) */
  163. starpu_matrix_data_register(&dataA, 0, (uintptr_t)matA, ld, size, size, sizeof(TYPE));
  164. struct starpu_data_filter f =
  165. {
  166. .filter_func = starpu_matrix_filter_vertical_block,
  167. .nchildren = nblocks
  168. };
  169. struct starpu_data_filter f2 =
  170. {
  171. .filter_func = starpu_matrix_filter_block,
  172. .nchildren = nblocks
  173. };
  174. starpu_data_map_filters(dataA, 2, &f, &f2);
  175. unsigned i;
  176. for (i = 0; i < size; i++)
  177. ipiv[i] = i;
  178. struct piv_s *piv_description = malloc(nblocks*sizeof(struct piv_s));
  179. unsigned block;
  180. for (block = 0; block < nblocks; block++)
  181. {
  182. piv_description[block].piv = ipiv;
  183. piv_description[block].first = block * (size / nblocks);
  184. piv_description[block].last = (block + 1) * (size / nblocks);
  185. }
  186. double timing;
  187. int ret = dw_codelet_facto_pivot(&dataA, piv_description, nblocks, get_block_with_striding, &timing);
  188. FPRINTF(stderr, "Computation took (in ms)\n");
  189. FPRINTF(stderr, "%2.2f\n", timing/1000);
  190. unsigned n = starpu_matrix_get_nx(dataA);
  191. double flop = (2.0f*n*n*n)/3.0f;
  192. FPRINTF(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  193. /* gather all the data */
  194. starpu_data_unpartition(dataA, 0);
  195. starpu_data_unregister(dataA);
  196. free(piv_description);
  197. return ret;
  198. }
  199. starpu_data_handle_t get_block_with_no_striding(starpu_data_handle_t *dataAp, unsigned nblocks, unsigned j, unsigned i)
  200. {
  201. /* dataAp is an array of data handle */
  202. return dataAp[i+j*nblocks];
  203. }
  204. int STARPU_LU(lu_decomposition_pivot_no_stride)(TYPE **matA, unsigned *ipiv, unsigned size, unsigned ld, unsigned nblocks)
  205. {
  206. starpu_data_handle_t *dataAp = malloc(nblocks*nblocks*sizeof(starpu_data_handle_t));
  207. /* monitor and partition the A matrix into blocks :
  208. * one block is now determined by 2 unsigned (i,j) */
  209. unsigned bi, bj;
  210. for (bj = 0; bj < nblocks; bj++)
  211. for (bi = 0; bi < nblocks; bi++)
  212. {
  213. starpu_matrix_data_register(&dataAp[bi+nblocks*bj], 0,
  214. (uintptr_t)matA[bi+nblocks*bj], size/nblocks,
  215. size/nblocks, size/nblocks, sizeof(TYPE));
  216. }
  217. unsigned i;
  218. for (i = 0; i < size; i++)
  219. ipiv[i] = i;
  220. struct piv_s *piv_description = malloc(nblocks*sizeof(struct piv_s));
  221. unsigned block;
  222. for (block = 0; block < nblocks; block++)
  223. {
  224. piv_description[block].piv = ipiv;
  225. piv_description[block].first = block * (size / nblocks);
  226. piv_description[block].last = (block + 1) * (size / nblocks);
  227. }
  228. double timing;
  229. int ret = dw_codelet_facto_pivot(dataAp, piv_description, nblocks, get_block_with_no_striding, &timing);
  230. FPRINTF(stderr, "Computation took (in ms)\n");
  231. FPRINTF(stderr, "%2.2f\n", timing/1000);
  232. unsigned n = starpu_matrix_get_nx(dataAp[0])*nblocks;
  233. double flop = (2.0f*n*n*n)/3.0f;
  234. FPRINTF(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  235. for (bj = 0; bj < nblocks; bj++)
  236. for (bi = 0; bi < nblocks; bi++)
  237. {
  238. starpu_data_unregister(dataAp[bi+nblocks*bj]);
  239. }
  240. free(dataAp);
  241. return ret;
  242. }