mpi_cholesky_codelets.c 6.2 KB

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