mpi_cholesky_codelets.c 6.7 KB

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