mpi_cholesky_codelets.c 6.8 KB

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