mpi_cholesky_codelets.c 6.3 KB

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