mpi_cholesky_codelets.c 6.1 KB

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