mpi_cholesky_codelets.c 7.0 KB

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