mpi_cholesky.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu_mpi.h>
  19. #include "mpi_cholesky.h"
  20. #include "mpi_cholesky_models.h"
  21. /*
  22. * Create the codelets
  23. */
  24. static starpu_codelet cl11 =
  25. {
  26. .where = STARPU_CPU|STARPU_CUDA,
  27. .cpu_func = chol_cpu_codelet_update_u11,
  28. #ifdef STARPU_USE_CUDA
  29. .cuda_func = chol_cublas_codelet_update_u11,
  30. #endif
  31. .nbuffers = 1,
  32. .model = &chol_model_11
  33. };
  34. static starpu_codelet cl21 =
  35. {
  36. .where = STARPU_CPU|STARPU_CUDA,
  37. .cpu_func = chol_cpu_codelet_update_u21,
  38. #ifdef STARPU_USE_CUDA
  39. .cuda_func = chol_cublas_codelet_update_u21,
  40. #endif
  41. .nbuffers = 2,
  42. .model = &chol_model_21
  43. };
  44. static starpu_codelet cl22 =
  45. {
  46. .where = STARPU_CPU|STARPU_CUDA,
  47. .cpu_func = chol_cpu_codelet_update_u22,
  48. #ifdef STARPU_USE_CUDA
  49. .cuda_func = chol_cublas_codelet_update_u22,
  50. #endif
  51. .nbuffers = 3,
  52. .model = &chol_model_22
  53. };
  54. /* Returns the MPI node number where data indexes index is */
  55. int my_distrib(int x, int nb_nodes) {
  56. return x % nb_nodes;
  57. }
  58. /*
  59. * code to bootstrap the factorization
  60. * and construct the DAG
  61. */
  62. static void dw_cholesky(float *matA, unsigned size, unsigned ld, unsigned nblocks, int rank, int nodes)
  63. {
  64. struct timeval start;
  65. struct timeval end;
  66. starpu_data_handle **data_handles;
  67. int x, y;
  68. /* create all the DAG nodes */
  69. unsigned i,j,k;
  70. data_handles = malloc(nblocks*sizeof(starpu_data_handle *));
  71. for(x=0 ; x<nblocks ; x++) data_handles[x] = malloc(nblocks*sizeof(starpu_data_handle));
  72. gettimeofday(&start, NULL);
  73. for(x = 0; x < nblocks ; x++) {
  74. for (y = 0; y < nblocks; y++) {
  75. int mpi_rank = my_distrib(x, nodes);
  76. if (mpi_rank == rank) {
  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[((size/nblocks)*x) + ((size/nblocks)*y) * ld]),
  79. ld, size/nblocks, size/nblocks, sizeof(float));
  80. }
  81. else if (rank == mpi_rank+1 || rank == mpi_rank-1) {
  82. /* I don't own that index, but will need it for my computations */
  83. //fprintf(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, x, y);
  84. starpu_matrix_data_register(&data_handles[x][y], -1, (uintptr_t)NULL,
  85. ld, size/nblocks, size/nblocks, sizeof(float));
  86. }
  87. else {
  88. /* I know it's useless to allocate anything for this */
  89. data_handles[x][y] = NULL;
  90. }
  91. if (data_handles[x][y])
  92. starpu_data_set_rank(data_handles[x][y], mpi_rank);
  93. }
  94. }
  95. for (k = 0; k < nblocks; k++)
  96. {
  97. int prio = STARPU_DEFAULT_PRIO;
  98. if (!noprio) prio = STARPU_MAX_PRIO;
  99. starpu_mpi_insert_task(MPI_COMM_WORLD, &cl11,
  100. STARPU_PRIORITY, prio,
  101. STARPU_RW, data_handles[k][k],
  102. 0);
  103. for (j = k+1; j<nblocks; j++)
  104. {
  105. prio = STARPU_DEFAULT_PRIO;
  106. if (!noprio&& (j == k+1)) prio = STARPU_MAX_PRIO;
  107. starpu_mpi_insert_task(MPI_COMM_WORLD, &cl21,
  108. STARPU_PRIORITY, prio,
  109. STARPU_R, data_handles[k][k],
  110. STARPU_RW, data_handles[k][j],
  111. 0);
  112. for (i = k+1; i<nblocks; i++)
  113. {
  114. if (i <= j)
  115. {
  116. prio = STARPU_DEFAULT_PRIO;
  117. if (!noprio && (i == k + 1) && (j == k +1) ) prio = STARPU_MAX_PRIO;
  118. starpu_mpi_insert_task(MPI_COMM_WORLD, &cl22,
  119. STARPU_PRIORITY, prio,
  120. STARPU_R, data_handles[k][i],
  121. STARPU_R, data_handles[k][j],
  122. STARPU_RW, data_handles[i][j],
  123. 0);
  124. }
  125. }
  126. }
  127. }
  128. starpu_task_wait_for_all();
  129. gettimeofday(&end, NULL);
  130. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  131. fprintf(stderr, "Computation took (in ms)\n");
  132. printf("%2.2f\n", timing/1000);
  133. double flop = (1.0f*size*size*size)/3.0f;
  134. fprintf(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  135. }
  136. void initialize_system(float **A, unsigned dim, unsigned pinned, int *rank, int *nodes)
  137. {
  138. starpu_init(NULL);
  139. starpu_mpi_initialize_extended(1, rank, nodes);
  140. starpu_helper_cublas_init();
  141. if (pinned)
  142. {
  143. starpu_data_malloc_pinned_if_possible((void **)A, (size_t)dim*dim*sizeof(float));
  144. }
  145. else {
  146. *A = malloc(dim*dim*sizeof(float));
  147. }
  148. }
  149. int main(int argc, char **argv)
  150. {
  151. /* create a simple definite positive symetric matrix example
  152. *
  153. * Hilbert matrix : h(i,j) = 1/(i+j+1)
  154. * */
  155. float *mat;
  156. int rank, nodes;
  157. parse_args(argc, argv);
  158. mat = malloc(size*size*sizeof(float));
  159. initialize_system(&mat, size, pinned, &rank, &nodes);
  160. unsigned i,j;
  161. for (i = 0; i < size; i++)
  162. {
  163. for (j = 0; j < size; j++)
  164. {
  165. mat[j +i*size] = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  166. //mat[j +i*size] = ((i == j)?1.0f*size:0.0f);
  167. }
  168. }
  169. //#define PRINT_OUTPUT
  170. #ifdef PRINT_OUTPUT
  171. printf("Input :\n");
  172. for (j = 0; j < size; j++)
  173. {
  174. for (i = 0; i < size; i++)
  175. {
  176. if (i <= j) {
  177. printf("%2.2f\t", mat[j +i*size]);
  178. }
  179. else {
  180. printf(".\t");
  181. }
  182. }
  183. printf("\n");
  184. }
  185. #endif
  186. dw_cholesky(mat, size, size, nblocks, rank, nodes);
  187. starpu_helper_cublas_shutdown();
  188. starpu_mpi_shutdown();
  189. starpu_shutdown();
  190. #ifdef PRINT_OUTPUT
  191. printf("Results :\n");
  192. for (j = 0; j < size; j++)
  193. {
  194. for (i = 0; i < size; i++)
  195. {
  196. if (i <= j) {
  197. printf("%2.2f\t", mat[j +i*size]);
  198. }
  199. else {
  200. printf(".\t");
  201. }
  202. }
  203. printf("\n");
  204. }
  205. #endif
  206. fprintf(stderr, "compute explicit LLt ...\n");
  207. for (j = 0; j < size; j++)
  208. {
  209. for (i = 0; i < size; i++)
  210. {
  211. if (i > j) {
  212. mat[j+i*size] = 0.0f; // debug
  213. }
  214. }
  215. }
  216. float *test_mat = malloc(size*size*sizeof(float));
  217. STARPU_ASSERT(test_mat);
  218. SSYRK("L", "N", size, size, 1.0f,
  219. mat, size, 0.0f, test_mat, size);
  220. fprintf(stderr, "comparing results ...\n");
  221. #ifdef PRINT_OUTPUT
  222. for (j = 0; j < size; j++)
  223. {
  224. for (i = 0; i < size; i++)
  225. {
  226. if (i <= j) {
  227. printf("%2.2f\t", test_mat[j +i*size]);
  228. }
  229. else {
  230. printf(".\t");
  231. }
  232. }
  233. printf("\n");
  234. }
  235. #endif
  236. #warning check result
  237. #if 0
  238. for (j = 0; j < size; j++)
  239. {
  240. for (i = 0; i < size; i++)
  241. {
  242. if (i <= j) {
  243. float orig = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  244. float err = abs(test_mat[j +i*size] - orig);
  245. if (err > 0.00001) {
  246. fprintf(stderr, "Error[%d, %d] --> %2.2f != %2.2f (err %2.2f)\n", i, j, test_mat[j +i*size], orig, err);
  247. // assert(0);
  248. }
  249. }
  250. }
  251. }
  252. #endif
  253. return 0;
  254. }