mpi_cholesky_codelets.c 6.3 KB

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