mpi_cholesky_codelets.c 7.2 KB

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