mpi_scatter_gather.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012 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. {
  20. return (x+y) % nb_nodes;
  21. }
  22. void cpu_codelet(void *descr[], void *_args)
  23. {
  24. float *block;
  25. unsigned nx = STARPU_MATRIX_GET_NY(descr[0]);
  26. unsigned ld = STARPU_MATRIX_GET_LD(descr[0]);
  27. unsigned i,j;
  28. int rank;
  29. float factor;
  30. block = (float *)STARPU_MATRIX_GET_PTR(descr[0]);
  31. starpu_codelet_unpack_args(_args, &rank);
  32. factor = block[0];
  33. //fprintf(stderr,"rank %d factor %f\n", rank, factor);
  34. for (j = 0; j < nx; j++)
  35. {
  36. for (i = 0; i < nx; i++)
  37. {
  38. //fprintf(stderr,"rank %d factor %f --> %f %f\n", rank, factor, block[j+i*ld], block[j+i*ld]*factor);
  39. block[j+i*ld] *= factor;
  40. }
  41. }
  42. }
  43. static struct starpu_codelet cl =
  44. {
  45. .cpu_funcs = {cpu_codelet, NULL},
  46. .nbuffers = 1,
  47. .modes = {STARPU_RW},
  48. };
  49. void scallback(void *arg STARPU_ATTRIBUTE_UNUSED)
  50. {
  51. char *msg = arg;
  52. fprintf(stderr, "Sending completed for <%s>\n", msg);
  53. }
  54. void rcallback(void *arg STARPU_ATTRIBUTE_UNUSED)
  55. {
  56. char *msg = arg;
  57. fprintf(stderr, "Reception completed for <%s>\n", msg);
  58. }
  59. int main(int argc, char **argv)
  60. {
  61. int rank, nodes;
  62. float ***bmat = NULL;
  63. starpu_data_handle_t *data_handles;
  64. unsigned i,j,x,y;
  65. unsigned nblocks=4;
  66. unsigned block_size=2;
  67. unsigned size = nblocks*block_size;
  68. unsigned ld = size / nblocks;
  69. int ret = starpu_init(NULL);
  70. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  71. ret = starpu_mpi_init(&argc, &argv, 1);
  72. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  73. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  74. MPI_Comm_size(MPI_COMM_WORLD, &nodes);
  75. if (rank == 0)
  76. {
  77. /* Allocate the matrix */
  78. int block_number=10;
  79. bmat = malloc(nblocks * sizeof(float *));
  80. for(x=0 ; x<nblocks ; x++)
  81. {
  82. bmat[x] = malloc(nblocks * sizeof(float *));
  83. for(y=0 ; y<nblocks ; y++)
  84. {
  85. float value=0.0;
  86. starpu_malloc((void **)&bmat[x][y], block_size*block_size*sizeof(float));
  87. for (i = 0; i < block_size; i++)
  88. {
  89. for (j = 0; j < block_size; j++)
  90. {
  91. bmat[x][y][j +i*block_size] = block_number + value;
  92. value++;
  93. }
  94. }
  95. block_number += 10;
  96. }
  97. }
  98. }
  99. #if 0
  100. // Print matrix
  101. if (rank == 0)
  102. {
  103. fprintf(stderr, "Input matrix\n");
  104. for(x=0 ; x<nblocks ; x++)
  105. {
  106. for(y=0 ; y<nblocks ; y++)
  107. {
  108. for (j = 0; j < block_size; j++)
  109. {
  110. for (i = 0; i < block_size; i++)
  111. {
  112. fprintf(stderr, "%2.2f\t", bmat[x][y][j+i*block_size]);
  113. }
  114. fprintf(stderr,"\n");
  115. }
  116. fprintf(stderr,"\n");
  117. }
  118. }
  119. }
  120. #endif
  121. /* Allocate data handles and register data to StarPU */
  122. data_handles = malloc(nblocks*nblocks*sizeof(starpu_data_handle_t *));
  123. for(x = 0; x < nblocks ; x++)
  124. {
  125. for (y = 0; y < nblocks; y++)
  126. {
  127. int mpi_rank = my_distrib(x, y, nodes);
  128. if (rank == 0)
  129. {
  130. starpu_matrix_data_register(&data_handles[x+y*nblocks], 0, (uintptr_t)bmat[x][y],
  131. ld, size/nblocks, size/nblocks, sizeof(float));
  132. }
  133. else if ((mpi_rank == rank) || ((rank == mpi_rank+1 || rank == mpi_rank-1)))
  134. {
  135. /* I own that index, or i will need it for my computations */
  136. //fprintf(stderr, "[%d] Owning or neighbor of data[%d][%d]\n", rank, x, y);
  137. starpu_matrix_data_register(&data_handles[x+y*nblocks], -1, (uintptr_t)NULL,
  138. ld, size/nblocks, size/nblocks, sizeof(float));
  139. }
  140. else
  141. {
  142. /* I know it's useless to allocate anything for this */
  143. data_handles[x+y*nblocks] = NULL;
  144. }
  145. if (data_handles[x+y*nblocks])
  146. {
  147. starpu_data_set_rank(data_handles[x+y*nblocks], mpi_rank);
  148. starpu_data_set_tag(data_handles[x+y*nblocks], (y*nblocks)+x);
  149. }
  150. }
  151. }
  152. /* Scatter the matrix among the nodes */
  153. starpu_mpi_scatter_detached(data_handles, nblocks*nblocks, 0, MPI_COMM_WORLD, scallback, "scatter", NULL, NULL);
  154. /* Calculation */
  155. for(x = 0; x < nblocks*nblocks ; x++)
  156. {
  157. if (data_handles[x])
  158. {
  159. int owner = starpu_data_get_rank(data_handles[x]);
  160. if (owner == rank)
  161. {
  162. //fprintf(stderr,"[%d] Computing on data[%d]\n", rank, x);
  163. starpu_insert_task(&cl,
  164. STARPU_VALUE, &rank, sizeof(rank),
  165. STARPU_RW, data_handles[x],
  166. 0);
  167. }
  168. }
  169. }
  170. /* Gather the matrix on main node */
  171. starpu_mpi_gather_detached(data_handles, nblocks*nblocks, 0, MPI_COMM_WORLD, scallback, "gather", rcallback, "gather");
  172. /* Unregister matrix from StarPU */
  173. for(x=0 ; x<nblocks*nblocks ; x++)
  174. {
  175. if (data_handles[x])
  176. {
  177. starpu_data_unregister(data_handles[x]);
  178. }
  179. }
  180. #if 0
  181. // Print matrix
  182. if (rank == 0)
  183. {
  184. fprintf(stderr, "Output matrix\n");
  185. for(x=0 ; x<nblocks ; x++)
  186. {
  187. for(y=0 ; y<nblocks ; y++)
  188. {
  189. for (j = 0; j < block_size; j++)
  190. {
  191. for (i = 0; i < block_size; i++)
  192. {
  193. fprintf(stderr, "%2.2f\t", bmat[x][y][j+i*block_size]);
  194. }
  195. fprintf(stderr,"\n");
  196. }
  197. fprintf(stderr,"\n");
  198. }
  199. }
  200. }
  201. #endif
  202. // Free memory
  203. free(data_handles);
  204. if (rank == 0)
  205. {
  206. for(x=0 ; x<nblocks ; x++)
  207. {
  208. for(y=0 ; y<nblocks ; y++)
  209. {
  210. starpu_free((void *)bmat[x][y]);
  211. }
  212. free(bmat[x]);
  213. }
  214. free(bmat);
  215. }
  216. starpu_mpi_shutdown();
  217. starpu_shutdown();
  218. return 0;
  219. }