mpi_cholesky_codelets.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include "mpi_cholesky.h"
  17. #include <common/blas.h>
  18. #include <sys/time.h>
  19. #include <limits.h>
  20. #include <math.h>
  21. /*
  22. * Create the codelets
  23. */
  24. static struct starpu_codelet cl11 =
  25. {
  26. .cpu_funcs = {chol_cpu_codelet_update_u11},
  27. #ifdef STARPU_USE_CUDA
  28. .cuda_funcs = {chol_cublas_codelet_update_u11},
  29. #elif defined(STARPU_SIMGRID)
  30. .cuda_funcs = {(void*)1},
  31. #endif
  32. .nbuffers = 1,
  33. .modes = {STARPU_RW},
  34. .model = &chol_model_11,
  35. .color = 0xffff00,
  36. };
  37. static struct starpu_codelet cl21 =
  38. {
  39. .cpu_funcs = {chol_cpu_codelet_update_u21},
  40. #ifdef STARPU_USE_CUDA
  41. .cuda_funcs = {chol_cublas_codelet_update_u21},
  42. #elif defined(STARPU_SIMGRID)
  43. .cuda_funcs = {(void*)1},
  44. #endif
  45. .cuda_flags = {STARPU_CUDA_ASYNC},
  46. .nbuffers = 2,
  47. .modes = {STARPU_R, STARPU_RW},
  48. .model = &chol_model_21,
  49. .color = 0x8080ff,
  50. };
  51. static struct starpu_codelet cl22 =
  52. {
  53. .cpu_funcs = {chol_cpu_codelet_update_u22},
  54. #ifdef STARPU_USE_CUDA
  55. .cuda_funcs = {chol_cublas_codelet_update_u22},
  56. #elif defined(STARPU_SIMGRID)
  57. .cuda_funcs = {(void*)1},
  58. #endif
  59. .cuda_flags = {STARPU_CUDA_ASYNC},
  60. .nbuffers = 3,
  61. .modes = {STARPU_R, STARPU_R, STARPU_RW | STARPU_COMMUTE},
  62. .model = &chol_model_22,
  63. .color = 0x00ff00,
  64. };
  65. static void run_cholesky(starpu_data_handle_t **data_handles, int rank, int nodes)
  66. {
  67. unsigned k, m, n;
  68. unsigned unbound_prio = STARPU_MAX_PRIO == INT_MAX && STARPU_MIN_PRIO == INT_MIN;
  69. for (k = 0; k < nblocks; k++)
  70. {
  71. starpu_iteration_push(k);
  72. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl11,
  73. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k) : STARPU_MAX_PRIO,
  74. STARPU_RW, data_handles[k][k],
  75. 0);
  76. for (m = k+1; m<nblocks; m++)
  77. {
  78. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl21,
  79. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m) : (m == k+1)?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  80. STARPU_R, data_handles[k][k],
  81. STARPU_RW, data_handles[m][k],
  82. 0);
  83. starpu_mpi_cache_flush(MPI_COMM_WORLD, data_handles[k][k]);
  84. if (my_distrib(k, k, nodes) == rank)
  85. starpu_data_wont_use(data_handles[k][k]);
  86. for (n = k+1; n<nblocks; n++)
  87. {
  88. if (n <= m)
  89. {
  90. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl22,
  91. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m - n) : ((n == k+1) && (m == k+1))?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  92. STARPU_R, data_handles[n][k],
  93. STARPU_R, data_handles[m][k],
  94. STARPU_RW | STARPU_COMMUTE, data_handles[m][n],
  95. 0);
  96. }
  97. }
  98. starpu_mpi_cache_flush(MPI_COMM_WORLD, data_handles[m][k]);
  99. if (my_distrib(m, k, nodes) == rank)
  100. starpu_data_wont_use(data_handles[m][k]);
  101. }
  102. starpu_iteration_pop();
  103. }
  104. }
  105. /*
  106. * code to bootstrap the factorization
  107. * and construct the DAG
  108. */
  109. void dw_cholesky(float ***matA, unsigned ld, int rank, int nodes, double *timing, double *flops)
  110. {
  111. double start;
  112. double end;
  113. starpu_data_handle_t **data_handles;
  114. unsigned k, m, n;
  115. /* create all the DAG nodes */
  116. data_handles = malloc(nblocks*sizeof(starpu_data_handle_t *));
  117. for(m=0 ; m<nblocks ; m++) data_handles[m] = malloc(nblocks*sizeof(starpu_data_handle_t));
  118. for (m = 0; m < nblocks; m++)
  119. {
  120. for(n = 0; n < nblocks ; n++)
  121. {
  122. int mpi_rank = my_distrib(m, n, nodes);
  123. if (mpi_rank == rank || (check && rank == 0))
  124. {
  125. //fprintf(stderr, "[%d] Owning data[%d][%d]\n", rank, n, m);
  126. starpu_matrix_data_register(&data_handles[m][n], STARPU_MAIN_RAM, (uintptr_t)matA[m][n],
  127. ld, size/nblocks, size/nblocks, sizeof(float));
  128. }
  129. #ifdef STARPU_DEVEL
  130. #warning TODO: make better test to only register what is needed
  131. #endif
  132. else
  133. {
  134. /* I don't own this index, but will need it for my computations */
  135. //fprintf(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, n, m);
  136. starpu_matrix_data_register(&data_handles[m][n], -1, (uintptr_t)NULL,
  137. ld, size/nblocks, size/nblocks, sizeof(float));
  138. }
  139. if (data_handles[m][n])
  140. {
  141. starpu_data_set_coordinates(data_handles[m][n], 2, n, m);
  142. starpu_mpi_data_register(data_handles[m][n], (m*nblocks)+n, mpi_rank);
  143. }
  144. }
  145. }
  146. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  147. starpu_mpi_barrier(MPI_COMM_WORLD);
  148. start = starpu_timing_now();
  149. run_cholesky(data_handles, rank, nodes);
  150. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  151. starpu_mpi_barrier(MPI_COMM_WORLD);
  152. end = starpu_timing_now();
  153. for (m = 0; m < nblocks; m++)
  154. {
  155. for(n = 0; n < nblocks ; n++)
  156. {
  157. /* Get back data on node 0 for the check */
  158. if (check && data_handles[m][n])
  159. starpu_mpi_get_data_on_node(MPI_COMM_WORLD, data_handles[m][n], 0);
  160. if (data_handles[m][n])
  161. starpu_data_unregister(data_handles[m][n]);
  162. }
  163. free(data_handles[m]);
  164. }
  165. free(data_handles);
  166. if (rank == 0)
  167. {
  168. *timing = end - start;
  169. *flops = (1.0f*size*size*size)/3.0f;
  170. }
  171. }
  172. void dw_cholesky_check_computation(float ***matA, int rank, int nodes, int *correctness, double *flops, double epsilon)
  173. {
  174. unsigned nn,mm,n,m;
  175. float *rmat = malloc(size*size*sizeof(float));
  176. for(n=0 ; n<nblocks ; n++)
  177. {
  178. for(m=0 ; m<nblocks ; m++)
  179. {
  180. for (nn = 0; nn < BLOCKSIZE; nn++)
  181. {
  182. for (mm = 0; mm < BLOCKSIZE; mm++)
  183. {
  184. rmat[mm+(m*BLOCKSIZE)+(nn+(n*BLOCKSIZE))*size] = matA[m][n][mm +nn*BLOCKSIZE];
  185. }
  186. }
  187. }
  188. }
  189. FPRINTF(stderr, "[%d] compute explicit LLt ...\n", rank);
  190. for (mm = 0; mm < size; mm++)
  191. {
  192. for (nn = 0; nn < size; nn++)
  193. {
  194. if (nn > mm)
  195. {
  196. rmat[mm+nn*size] = 0.0f; // debug
  197. }
  198. }
  199. }
  200. float *test_mat = malloc(size*size*sizeof(float));
  201. STARPU_ASSERT(test_mat);
  202. STARPU_SSYRK("L", "N", size, size, 1.0f,
  203. rmat, size, 0.0f, test_mat, size);
  204. FPRINTF(stderr, "[%d] comparing results ...\n", rank);
  205. if (display)
  206. {
  207. for (mm = 0; mm < size; mm++)
  208. {
  209. for (nn = 0; nn < size; nn++)
  210. {
  211. if (nn <= mm)
  212. {
  213. printf("%2.2f\t", test_mat[mm +nn*size]);
  214. }
  215. else
  216. {
  217. printf(".\t");
  218. }
  219. }
  220. printf("\n");
  221. }
  222. }
  223. *correctness = 1;
  224. for(n = 0; n < nblocks ; n++)
  225. {
  226. for (m = 0; m < nblocks; m++)
  227. {
  228. for (nn = BLOCKSIZE*n ; nn < BLOCKSIZE*(n+1); nn++)
  229. {
  230. for (mm = BLOCKSIZE*m ; mm < BLOCKSIZE*(m+1); mm++)
  231. {
  232. if (nn <= mm)
  233. {
  234. float orig = (1.0f/(1.0f+nn+mm)) + ((nn == mm)?1.0f*size:0.0f);
  235. float err = fabsf(test_mat[mm +nn*size] - orig) / orig;
  236. if (err > epsilon)
  237. {
  238. FPRINTF(stderr, "[%d] Error[%u, %u] --> %2.20f != %2.20f (err %2.20f)\n", rank, nn, mm, test_mat[mm +nn*size], orig, err);
  239. *correctness = 0;
  240. *flops = 0;
  241. break;
  242. }
  243. }
  244. }
  245. }
  246. }
  247. }
  248. free(rmat);
  249. free(test_mat);
  250. }