mpi_cholesky_codelets.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. /*
  66. * code to bootstrap the factorization
  67. * and construct the DAG
  68. */
  69. void dw_cholesky(float ***matA, unsigned ld, int rank, int nodes, double *timing, double *flops)
  70. {
  71. double start;
  72. double end;
  73. starpu_data_handle_t **data_handles;
  74. unsigned k, m, n;
  75. unsigned unbound_prio = STARPU_MAX_PRIO == INT_MAX && STARPU_MIN_PRIO == INT_MIN;
  76. /* create all the DAG nodes */
  77. data_handles = malloc(nblocks*sizeof(starpu_data_handle_t *));
  78. for(m=0 ; m<nblocks ; m++) data_handles[m] = malloc(nblocks*sizeof(starpu_data_handle_t));
  79. for (m = 0; m < nblocks; m++)
  80. {
  81. for(n = 0; n < nblocks ; n++)
  82. {
  83. int mpi_rank = my_distrib(m, n, nodes);
  84. if (mpi_rank == rank)
  85. {
  86. //fprintf(stderr, "[%d] Owning data[%d][%d]\n", rank, n, m);
  87. starpu_matrix_data_register(&data_handles[m][n], STARPU_MAIN_RAM, (uintptr_t)matA[m][n],
  88. ld, size/nblocks, size/nblocks, sizeof(float));
  89. }
  90. #ifdef STARPU_DEVEL
  91. #warning TODO: make better test to only register what is needed
  92. #endif
  93. else
  94. {
  95. /* I don't own this index, but will need it for my computations */
  96. //fprintf(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, n, m);
  97. starpu_matrix_data_register(&data_handles[m][n], -1, (uintptr_t)NULL,
  98. ld, size/nblocks, size/nblocks, sizeof(float));
  99. }
  100. if (data_handles[m][n])
  101. {
  102. starpu_data_set_coordinates(data_handles[m][n], 2, n, m);
  103. starpu_mpi_data_register(data_handles[m][n], (m*nblocks)+n, mpi_rank);
  104. }
  105. }
  106. }
  107. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  108. starpu_mpi_barrier(MPI_COMM_WORLD);
  109. start = starpu_timing_now();
  110. for (k = 0; k < nblocks; k++)
  111. {
  112. starpu_iteration_push(k);
  113. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl11,
  114. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k) : STARPU_MAX_PRIO,
  115. STARPU_RW, data_handles[k][k],
  116. 0);
  117. for (m = k+1; m<nblocks; m++)
  118. {
  119. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl21,
  120. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - m) : (m == k+1)?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  121. STARPU_R, data_handles[k][k],
  122. STARPU_RW, data_handles[m][k],
  123. 0);
  124. starpu_mpi_cache_flush(MPI_COMM_WORLD, data_handles[k][k]);
  125. if (my_distrib(k, k, nodes) == rank)
  126. starpu_data_wont_use(data_handles[k][k]);
  127. for (n = k+1; n<nblocks; n++)
  128. {
  129. if (n <= m)
  130. {
  131. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl22,
  132. 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,
  133. STARPU_R, data_handles[n][k],
  134. STARPU_R, data_handles[m][k],
  135. STARPU_RW | STARPU_COMMUTE, data_handles[m][n],
  136. 0);
  137. }
  138. }
  139. starpu_mpi_cache_flush(MPI_COMM_WORLD, data_handles[m][k]);
  140. if (my_distrib(m, k, nodes) == rank)
  141. starpu_data_wont_use(data_handles[m][k]);
  142. }
  143. starpu_iteration_pop();
  144. }
  145. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  146. starpu_mpi_barrier(MPI_COMM_WORLD);
  147. end = starpu_timing_now();
  148. for (m = 0; m < nblocks; m++)
  149. {
  150. for(n = 0; n < nblocks ; n++)
  151. {
  152. /* Get back data on node 0 for the check */
  153. if (check)
  154. starpu_mpi_get_data_on_node(MPI_COMM_WORLD, data_handles[m][n], 0);
  155. if (data_handles[m][n])
  156. starpu_data_unregister(data_handles[m][n]);
  157. }
  158. free(data_handles[m]);
  159. }
  160. free(data_handles);
  161. if (rank == 0)
  162. {
  163. *timing = end - start;
  164. *flops = (1.0f*size*size*size)/3.0f;
  165. }
  166. }
  167. void dw_cholesky_check_computation(float ***matA, int rank, int nodes, int *correctness, double *flops, double epsilon)
  168. {
  169. unsigned nn,mm,n,m;
  170. float *rmat = malloc(size*size*sizeof(float));
  171. for(n=0 ; n<nblocks ; n++)
  172. {
  173. for(m=0 ; m<nblocks ; m++)
  174. {
  175. for (nn = 0; nn < BLOCKSIZE; nn++)
  176. {
  177. for (mm = 0; mm < BLOCKSIZE; mm++)
  178. {
  179. rmat[mm+(m*BLOCKSIZE)+(nn+(n*BLOCKSIZE))*size] = matA[m][n][mm +nn*BLOCKSIZE];
  180. }
  181. }
  182. }
  183. }
  184. FPRINTF(stderr, "[%d] compute explicit LLt ...\n", rank);
  185. for (mm = 0; mm < size; mm++)
  186. {
  187. for (nn = 0; nn < size; nn++)
  188. {
  189. if (nn > mm)
  190. {
  191. rmat[mm+nn*size] = 0.0f; // debug
  192. }
  193. }
  194. }
  195. float *test_mat = malloc(size*size*sizeof(float));
  196. STARPU_ASSERT(test_mat);
  197. STARPU_SSYRK("L", "N", size, size, 1.0f,
  198. rmat, size, 0.0f, test_mat, size);
  199. FPRINTF(stderr, "[%d] comparing results ...\n", rank);
  200. if (display)
  201. {
  202. for (mm = 0; mm < size; mm++)
  203. {
  204. for (nn = 0; nn < size; nn++)
  205. {
  206. if (nn <= mm)
  207. {
  208. printf("%2.2f\t", test_mat[mm +nn*size]);
  209. }
  210. else
  211. {
  212. printf(".\t");
  213. }
  214. }
  215. printf("\n");
  216. }
  217. }
  218. *correctness = 1;
  219. for(n = 0; n < nblocks ; n++)
  220. {
  221. for (m = 0; m < nblocks; m++)
  222. {
  223. int mpi_rank = my_distrib(m, n, nodes);
  224. if (mpi_rank == rank)
  225. {
  226. for (nn = (size/nblocks)*n ; nn < (size/nblocks)*n+(size/nblocks); nn++)
  227. {
  228. for (mm = (size/nblocks)*m ; mm < (size/nblocks)*m+(size/nblocks); mm++)
  229. {
  230. if (nn <= mm)
  231. {
  232. float orig = (1.0f/(1.0f+nn+mm)) + ((nn == mm)?1.0f*size:0.0f);
  233. float err = fabsf(test_mat[mm +nn*size] - orig) / orig;
  234. if (err > epsilon)
  235. {
  236. FPRINTF(stderr, "[%d] Error[%u, %u] --> %2.20f != %2.20f (err %2.20f)\n", rank, nn, mm, test_mat[mm +nn*size], orig, err);
  237. *correctness = 0;
  238. *flops = 0;
  239. break;
  240. }
  241. }
  242. }
  243. }
  244. }
  245. }
  246. }
  247. free(rmat);
  248. free(test_mat);
  249. }