mpi_cholesky_codelets.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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_cholesky_params.h"
  20. #include "mpi_cholesky_models.h"
  21. #include "mpi_cholesky_codelets.h"
  22. #include "mpi_cholesky_kernels.h"
  23. /*
  24. * Create the codelets
  25. */
  26. static struct starpu_codelet cl11 =
  27. {
  28. .where = STARPU_CPU|STARPU_CUDA,
  29. .cpu_funcs = {chol_cpu_codelet_update_u11, NULL},
  30. #ifdef STARPU_USE_CUDA
  31. .cuda_funcs = {chol_cublas_codelet_update_u11, NULL},
  32. #endif
  33. .nbuffers = 1,
  34. .modes = {STARPU_RW},
  35. .model = &chol_model_11
  36. };
  37. static struct starpu_codelet cl21 =
  38. {
  39. .where = STARPU_CPU|STARPU_CUDA,
  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. .where = STARPU_CPU|STARPU_CUDA,
  51. .cpu_funcs = {chol_cpu_codelet_update_u22, NULL},
  52. #ifdef STARPU_USE_CUDA
  53. .cuda_funcs = {chol_cublas_codelet_update_u22, NULL},
  54. #endif
  55. .nbuffers = 3,
  56. .modes = {STARPU_R, STARPU_R, STARPU_RW},
  57. .model = &chol_model_22
  58. };
  59. /* Returns the MPI node number where data indexes index is */
  60. int my_distrib(int x, int y, int nb_nodes)
  61. {
  62. //return (x+y) % nb_nodes;
  63. return (x%dblockx)+(y%dblocky)*dblockx;
  64. }
  65. /*
  66. * code to bootstrap the factorization
  67. * and construct the DAG
  68. */
  69. void dw_cholesky(float ***matA, unsigned size, unsigned ld, unsigned nblocks, int rank, int nodes, double *timing, double *flops)
  70. {
  71. struct timeval start;
  72. struct timeval end;
  73. starpu_data_handle_t **data_handles;
  74. int x, y;
  75. /* create all the DAG nodes */
  76. unsigned i,j,k;
  77. data_handles = malloc(nblocks*sizeof(starpu_data_handle_t *));
  78. for(x=0 ; x<nblocks ; x++) data_handles[x] = malloc(nblocks*sizeof(starpu_data_handle_t));
  79. for(x = 0; x < nblocks ; x++)
  80. {
  81. for (y = 0; y < nblocks; y++)
  82. {
  83. int mpi_rank = my_distrib(x, y, nodes);
  84. if (mpi_rank == rank)
  85. {
  86. //fprintf(stderr, "[%d] Owning data[%d][%d]\n", rank, x, y);
  87. starpu_matrix_data_register(&data_handles[x][y], 0, (uintptr_t)matA[x][y],
  88. ld, size/nblocks, size/nblocks, sizeof(float));
  89. }
  90. #warning TODO: make better test to only register what is needed
  91. else
  92. {
  93. /* I don't own that index, but will need it for my computations */
  94. //fprintf(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, x, y);
  95. starpu_matrix_data_register(&data_handles[x][y], -1, (uintptr_t)NULL,
  96. ld, size/nblocks, size/nblocks, sizeof(float));
  97. }
  98. if (data_handles[x][y])
  99. {
  100. starpu_data_set_rank(data_handles[x][y], mpi_rank);
  101. starpu_data_set_tag(data_handles[x][y], (y*nblocks)+x);
  102. }
  103. }
  104. }
  105. starpu_mpi_barrier(MPI_COMM_WORLD);
  106. gettimeofday(&start, NULL);
  107. for (k = 0; k < nblocks; k++)
  108. {
  109. int prio = STARPU_DEFAULT_PRIO;
  110. if (!noprio) prio = STARPU_MAX_PRIO;
  111. starpu_mpi_insert_task(MPI_COMM_WORLD, &cl11,
  112. STARPU_PRIORITY, prio,
  113. STARPU_RW, data_handles[k][k],
  114. 0);
  115. for (j = k+1; j<nblocks; j++)
  116. {
  117. prio = STARPU_DEFAULT_PRIO;
  118. if (!noprio&& (j == k+1)) prio = STARPU_MAX_PRIO;
  119. starpu_mpi_insert_task(MPI_COMM_WORLD, &cl21,
  120. STARPU_PRIORITY, prio,
  121. STARPU_R, data_handles[k][k],
  122. STARPU_RW, data_handles[k][j],
  123. 0);
  124. for (i = k+1; i<nblocks; i++)
  125. {
  126. if (i <= j)
  127. {
  128. prio = STARPU_DEFAULT_PRIO;
  129. if (!noprio && (i == k + 1) && (j == k +1) ) prio = STARPU_MAX_PRIO;
  130. starpu_mpi_insert_task(MPI_COMM_WORLD, &cl22,
  131. STARPU_PRIORITY, prio,
  132. STARPU_R, data_handles[k][i],
  133. STARPU_R, data_handles[k][j],
  134. STARPU_RW, data_handles[i][j],
  135. 0);
  136. }
  137. }
  138. }
  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. gettimeofday(&end, NULL);
  153. if (rank == 0)
  154. {
  155. *timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  156. *flops = (1.0f*size*size*size)/3.0f;
  157. }
  158. }
  159. void dw_cholesky_check_computation(float ***matA, unsigned size, 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. 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. }