mpi_cholesky_codelets.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 x,y,i,j,k;
  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(x=0 ; x<nblocks ; x++) data_handles[x] = malloc(nblocks*sizeof(starpu_data_handle_t));
  81. for(x = 0; x < nblocks ; x++)
  82. {
  83. for (y = 0; y < nblocks; y++)
  84. {
  85. int mpi_rank = my_distrib(x, y, nodes);
  86. if (mpi_rank == rank)
  87. {
  88. //fprintf(stderr, "[%d] Owning data[%d][%d]\n", rank, x, y);
  89. starpu_matrix_data_register(&data_handles[x][y], STARPU_MAIN_RAM, (uintptr_t)matA[x][y],
  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, x, y);
  99. starpu_matrix_data_register(&data_handles[x][y], -1, (uintptr_t)NULL,
  100. ld, size/nblocks, size/nblocks, sizeof(float));
  101. }
  102. if (data_handles[x][y])
  103. {
  104. starpu_data_set_coordinates(data_handles[x][y], 2, x, y);
  105. starpu_mpi_data_register(data_handles[x][y], (y*nblocks)+x, 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 (j = k+1; j<nblocks; j++)
  119. {
  120. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl21,
  121. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - j) : (j == k+1)?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  122. STARPU_R, data_handles[k][k],
  123. STARPU_RW, data_handles[k][j],
  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 (i = k+1; i<nblocks; i++)
  129. {
  130. if (i <= j)
  131. {
  132. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl22,
  133. STARPU_PRIORITY, noprio ? STARPU_DEFAULT_PRIO : unbound_prio ? (int)(2*nblocks - 2*k - j - i) : ((i == k+1) && (j == k+1))?STARPU_MAX_PRIO:STARPU_DEFAULT_PRIO,
  134. STARPU_R, data_handles[k][i],
  135. STARPU_R, data_handles[k][j],
  136. STARPU_RW | STARPU_COMMUTE, data_handles[i][j],
  137. 0);
  138. }
  139. }
  140. starpu_mpi_cache_flush(MPI_COMM_WORLD, data_handles[k][j]);
  141. if (my_distrib(k, j, nodes) == rank)
  142. starpu_data_wont_use(data_handles[k][j]);
  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(x = 0; x < nblocks ; x++)
  150. {
  151. for (y = 0; y < nblocks; y++)
  152. {
  153. if (data_handles[x][y])
  154. starpu_data_unregister(data_handles[x][y]);
  155. }
  156. free(data_handles[x]);
  157. }
  158. free(data_handles);
  159. if (rank == 0)
  160. {
  161. *timing = end - start;
  162. *flops = (1.0f*size*size*size)/3.0f;
  163. }
  164. }
  165. void dw_cholesky_check_computation(float ***matA, int rank, int nodes, int *correctness, double *flops, double epsilon)
  166. {
  167. unsigned i,j,x,y;
  168. float *rmat = malloc(size*size*sizeof(float));
  169. for(x=0 ; x<nblocks ; x++)
  170. {
  171. for(y=0 ; y<nblocks ; y++)
  172. {
  173. for (i = 0; i < BLOCKSIZE; i++)
  174. {
  175. for (j = 0; j < BLOCKSIZE; j++)
  176. {
  177. rmat[j+(y*BLOCKSIZE)+(i+(x*BLOCKSIZE))*size] = matA[x][y][j +i*BLOCKSIZE];
  178. }
  179. }
  180. }
  181. }
  182. FPRINTF(stderr, "[%d] compute explicit LLt ...\n", rank);
  183. for (j = 0; j < size; j++)
  184. {
  185. for (i = 0; i < size; i++)
  186. {
  187. if (i > j)
  188. {
  189. rmat[j+i*size] = 0.0f; // debug
  190. }
  191. }
  192. }
  193. float *test_mat = malloc(size*size*sizeof(float));
  194. STARPU_ASSERT(test_mat);
  195. STARPU_SSYRK("L", "N", size, size, 1.0f,
  196. rmat, size, 0.0f, test_mat, size);
  197. FPRINTF(stderr, "[%d] comparing results ...\n", rank);
  198. if (display)
  199. {
  200. for (j = 0; j < size; j++)
  201. {
  202. for (i = 0; i < size; i++)
  203. {
  204. if (i <= j)
  205. {
  206. printf("%2.2f\t", test_mat[j +i*size]);
  207. }
  208. else
  209. {
  210. printf(".\t");
  211. }
  212. }
  213. printf("\n");
  214. }
  215. }
  216. *correctness = 1;
  217. for(x = 0; x < nblocks ; x++)
  218. {
  219. for (y = 0; y < nblocks; y++)
  220. {
  221. int mpi_rank = my_distrib(x, y, nodes);
  222. if (mpi_rank == rank)
  223. {
  224. for (i = (size/nblocks)*x ; i < (size/nblocks)*x+(size/nblocks); i++)
  225. {
  226. for (j = (size/nblocks)*y ; j < (size/nblocks)*y+(size/nblocks); j++)
  227. {
  228. if (i <= j)
  229. {
  230. float orig = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  231. float err = fabsf(test_mat[j +i*size] - orig) / orig;
  232. if (err > epsilon)
  233. {
  234. FPRINTF(stderr, "[%d] Error[%u, %u] --> %2.20f != %2.20f (err %2.20f)\n", rank, i, j, test_mat[j +i*size], orig, err);
  235. *correctness = 0;
  236. *flops = 0;
  237. break;
  238. }
  239. }
  240. }
  241. }
  242. }
  243. }
  244. }
  245. free(rmat);
  246. free(test_mat);
  247. }