xlu_implicit_pivot.c 8.1 KB

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