mpi_cholesky_codelets.c 6.6 KB

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