mpi_scatter_gather.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Centre National de la Recherche Scientifique
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu_mpi.h>
  17. /* Returns the MPI node number where data indexes index is */
  18. int my_distrib(int x, int y, int nb_nodes) {
  19. return (x+y) % nb_nodes;
  20. }
  21. void cpu_codelet(void *descr[], void *_args)
  22. {
  23. float *block;
  24. unsigned nx = STARPU_MATRIX_GET_NY(descr[0]);
  25. unsigned ld = STARPU_MATRIX_GET_LD(descr[0]);
  26. unsigned i,j;
  27. int rank;
  28. float factor;
  29. block = (float *)STARPU_MATRIX_GET_PTR(descr[0]);
  30. starpu_unpack_cl_args(_args, &rank);
  31. factor = block[0];
  32. //fprintf(stderr,"rank %d factor %f\n", rank, factor);
  33. for (j = 0; j < nx; j++)
  34. {
  35. for (i = 0; i < nx; i++)
  36. {
  37. //fprintf(stderr,"rank %d factor %f --> %f %f\n", rank, factor, block[j+i*ld], block[j+i*ld]*factor);
  38. block[j+i*ld] *= factor;
  39. }
  40. }
  41. }
  42. static starpu_codelet cl =
  43. {
  44. .where = STARPU_CPU,
  45. .cpu_func = cpu_codelet,
  46. .nbuffers = 1
  47. };
  48. int starpu_mpi_scatter(starpu_data_handle *data_handles, int count, int root, MPI_Comm comm)
  49. {
  50. int rank;
  51. int x;
  52. int mpi_tag = 0;
  53. MPI_Comm_rank(comm, &rank);
  54. for(x = 0; x < count ; x++)
  55. {
  56. if (data_handles[x])
  57. {
  58. int owner = starpu_data_get_rank(data_handles[x]);
  59. if ((rank == root) && (owner != root))
  60. {
  61. //fprintf(stderr, "[%d] Sending data[%d] to %d\n", rank, x, owner);
  62. starpu_mpi_isend_detached(data_handles[x], owner, mpi_tag, comm, NULL, NULL);
  63. }
  64. if ((rank != root) && (owner == rank))
  65. {
  66. //fprintf(stderr, "[%d] Receiving data[%d] from %d\n", rank, x, root);
  67. //MPI_Status status;
  68. starpu_mpi_irecv_detached(data_handles[x], root, mpi_tag, comm, NULL, NULL);
  69. }
  70. }
  71. mpi_tag++;
  72. }
  73. return 0;
  74. }
  75. int starpu_mpi_gather(starpu_data_handle *data_handles, int count, int root, MPI_Comm comm)
  76. {
  77. int rank;
  78. int x;
  79. int mpi_tag = 0;
  80. MPI_Comm_rank(comm, &rank);
  81. for(x = 0; x < count ; x++)
  82. {
  83. if (data_handles[x])
  84. {
  85. int owner = starpu_data_get_rank(data_handles[x]);
  86. if ((rank == root) && (owner != root))
  87. {
  88. //fprintf(stderr, "[%d] Receiving data[%d] from %d\n", rank, x, owner);
  89. //MPI_Status status;
  90. starpu_mpi_irecv_detached(data_handles[x], owner, mpi_tag, comm, NULL, NULL);
  91. }
  92. if ((rank != root) && (owner == rank))
  93. {
  94. //fprintf(stderr, "[%d] Sending data[%d] to %d\n", rank, x, root);
  95. starpu_mpi_isend_detached(data_handles[x], root, mpi_tag, comm, NULL, NULL);
  96. }
  97. }
  98. mpi_tag ++;
  99. }
  100. return 0;
  101. }
  102. int main(int argc, char **argv)
  103. {
  104. int rank, nodes;
  105. float ***bmat;
  106. starpu_data_handle *data_handles;
  107. unsigned i,j,x,y;
  108. unsigned nblocks=4;
  109. unsigned block_size=2;
  110. unsigned size = nblocks*block_size;
  111. unsigned ld = size / nblocks;
  112. starpu_init(NULL);
  113. starpu_mpi_initialize_extended(&rank, &nodes);
  114. if (rank == 0)
  115. {
  116. /* Allocate the matrix */
  117. int block_number=10;
  118. bmat = malloc(nblocks * sizeof(float *));
  119. for(x=0 ; x<nblocks ; x++)
  120. {
  121. bmat[x] = malloc(nblocks * sizeof(float *));
  122. for(y=0 ; y<nblocks ; y++)
  123. {
  124. float value=0.0;
  125. starpu_malloc((void **)&bmat[x][y], block_size*block_size*sizeof(float));
  126. for (i = 0; i < block_size; i++)
  127. {
  128. for (j = 0; j < block_size; j++)
  129. {
  130. bmat[x][y][j +i*block_size] = block_number + value;
  131. value++;
  132. }
  133. }
  134. block_number += 10;
  135. }
  136. }
  137. }
  138. #if 0
  139. // Print matrix
  140. if (rank == 0)
  141. {
  142. fprintf(stderr, "Input matrix\n");
  143. for(x=0 ; x<nblocks ; x++)
  144. {
  145. for(y=0 ; y<nblocks ; y++)
  146. {
  147. for (j = 0; j < block_size; j++)
  148. {
  149. for (i = 0; i < block_size; i++)
  150. {
  151. fprintf(stderr, "%2.2f\t", bmat[x][y][j+i*block_size]);
  152. }
  153. fprintf(stderr,"\n");
  154. }
  155. fprintf(stderr,"\n");
  156. }
  157. }
  158. }
  159. #endif
  160. /* Allocate data handles and register data to StarPU */
  161. data_handles = malloc(nblocks*nblocks*sizeof(starpu_data_handle *));
  162. for(x = 0; x < nblocks ; x++)
  163. {
  164. for (y = 0; y < nblocks; y++)
  165. {
  166. int mpi_rank = my_distrib(x, y, nodes);
  167. if (rank == 0)
  168. starpu_matrix_data_register(&data_handles[x+y*nblocks], 0, (uintptr_t)bmat[x][y],
  169. ld, size/nblocks, size/nblocks, sizeof(float));
  170. else {
  171. if ((mpi_rank == rank) || ((rank == mpi_rank+1 || rank == mpi_rank-1)))
  172. {
  173. /* I own that index, or i will need it for my computations */
  174. //fprintf(stderr, "[%d] Owning or neighbor of data[%d][%d]\n", rank, x, y);
  175. starpu_matrix_data_register(&data_handles[x+y*nblocks], -1, (uintptr_t)NULL,
  176. ld, size/nblocks, size/nblocks, sizeof(float));
  177. }
  178. else
  179. {
  180. /* I know it's useless to allocate anything for this */
  181. data_handles[x+y*nblocks] = NULL;
  182. }
  183. }
  184. if (data_handles[x+y*nblocks])
  185. {
  186. starpu_data_set_rank(data_handles[x+y*nblocks], mpi_rank);
  187. }
  188. }
  189. }
  190. /* Scatter the matrix among the nodes */
  191. starpu_mpi_scatter(data_handles, nblocks*nblocks, 0, MPI_COMM_WORLD);
  192. /* Calculation */
  193. for(x = 0; x < nblocks*nblocks ; x++)
  194. {
  195. if (data_handles[x])
  196. {
  197. int owner = starpu_data_get_rank(data_handles[x]);
  198. if (owner == rank)
  199. {
  200. //fprintf(stderr,"[%d] Computing on data[%d]\n", rank, x);
  201. starpu_insert_task(&cl,
  202. STARPU_VALUE, &rank, sizeof(rank),
  203. STARPU_RW, data_handles[x],
  204. 0);
  205. starpu_task_wait_for_all();
  206. }
  207. }
  208. }
  209. /* Gather the matrix on main node */
  210. starpu_mpi_gather(data_handles, nblocks*nblocks, 0, MPI_COMM_WORLD);
  211. /* Unregister matrix from StarPU */
  212. for(x=0 ; x<nblocks*nblocks ; x++)
  213. {
  214. if (data_handles[x])
  215. {
  216. starpu_data_unregister(data_handles[x]);
  217. }
  218. }
  219. #if 0
  220. // Print matrix
  221. if (rank == 0)
  222. {
  223. fprintf(stderr, "Output matrix\n");
  224. for(x=0 ; x<nblocks ; x++)
  225. {
  226. for(y=0 ; y<nblocks ; y++)
  227. {
  228. for (j = 0; j < block_size; j++)
  229. {
  230. for (i = 0; i < block_size; i++)
  231. {
  232. fprintf(stderr, "%2.2f\t", bmat[x][y][j+i*block_size]);
  233. }
  234. fprintf(stderr,"\n");
  235. }
  236. fprintf(stderr,"\n");
  237. }
  238. }
  239. }
  240. #endif
  241. // Free memory
  242. free(data_handles);
  243. if (rank == 0)
  244. {
  245. for(x=0 ; x<nblocks ; x++)
  246. {
  247. for(y=0 ; y<nblocks ; y++)
  248. {
  249. starpu_free((void *)bmat[x][y]);
  250. }
  251. free(bmat[x]);
  252. }
  253. free(bmat);
  254. }
  255. starpu_mpi_shutdown();
  256. starpu_shutdown();
  257. return 0;
  258. }