mpi_cholesky_codelets.c 6.3 KB

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