mpi_scatter_gather.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 main(int argc, char **argv)
  49. {
  50. int rank, nodes;
  51. float ***bmat;
  52. starpu_data_handle *data_handles;
  53. unsigned i,j,x,y;
  54. unsigned nblocks=4;
  55. unsigned block_size=2;
  56. unsigned size = nblocks*block_size;
  57. unsigned ld = size / nblocks;
  58. starpu_init(NULL);
  59. starpu_mpi_initialize_extended(&rank, &nodes);
  60. if (rank == 0)
  61. {
  62. /* Allocate the matrix */
  63. int block_number=10;
  64. bmat = malloc(nblocks * sizeof(float *));
  65. for(x=0 ; x<nblocks ; x++)
  66. {
  67. bmat[x] = malloc(nblocks * sizeof(float *));
  68. for(y=0 ; y<nblocks ; y++)
  69. {
  70. float value=0.0;
  71. starpu_malloc((void **)&bmat[x][y], block_size*block_size*sizeof(float));
  72. for (i = 0; i < block_size; i++)
  73. {
  74. for (j = 0; j < block_size; j++)
  75. {
  76. bmat[x][y][j +i*block_size] = block_number + value;
  77. value++;
  78. }
  79. }
  80. block_number += 10;
  81. }
  82. }
  83. }
  84. #if 0
  85. // Print matrix
  86. if (rank == 0)
  87. {
  88. fprintf(stderr, "Input matrix\n");
  89. for(x=0 ; x<nblocks ; x++)
  90. {
  91. for(y=0 ; y<nblocks ; y++)
  92. {
  93. for (j = 0; j < block_size; j++)
  94. {
  95. for (i = 0; i < block_size; i++)
  96. {
  97. fprintf(stderr, "%2.2f\t", bmat[x][y][j+i*block_size]);
  98. }
  99. fprintf(stderr,"\n");
  100. }
  101. fprintf(stderr,"\n");
  102. }
  103. }
  104. }
  105. #endif
  106. /* Allocate data handles and register data to StarPU */
  107. data_handles = malloc(nblocks*nblocks*sizeof(starpu_data_handle *));
  108. for(x = 0; x < nblocks ; x++)
  109. {
  110. for (y = 0; y < nblocks; y++)
  111. {
  112. int mpi_rank = my_distrib(x, y, nodes);
  113. if (rank == 0)
  114. {
  115. starpu_matrix_data_register(&data_handles[x+y*nblocks], 0, (uintptr_t)bmat[x][y],
  116. ld, size/nblocks, size/nblocks, sizeof(float));
  117. }
  118. else if ((mpi_rank == rank) || ((rank == mpi_rank+1 || rank == mpi_rank-1)))
  119. {
  120. /* I own that index, or i will need it for my computations */
  121. //fprintf(stderr, "[%d] Owning or neighbor of data[%d][%d]\n", rank, x, y);
  122. starpu_matrix_data_register(&data_handles[x+y*nblocks], -1, (uintptr_t)NULL,
  123. ld, size/nblocks, size/nblocks, sizeof(float));
  124. }
  125. else
  126. {
  127. /* I know it's useless to allocate anything for this */
  128. data_handles[x+y*nblocks] = NULL;
  129. }
  130. if (data_handles[x+y*nblocks])
  131. {
  132. starpu_data_set_rank(data_handles[x+y*nblocks], mpi_rank);
  133. }
  134. }
  135. }
  136. /* Scatter the matrix among the nodes */
  137. starpu_mpi_scatter_detached(data_handles, nblocks*nblocks, 0, MPI_COMM_WORLD);
  138. /* Calculation */
  139. for(x = 0; x < nblocks*nblocks ; x++)
  140. {
  141. if (data_handles[x])
  142. {
  143. int owner = starpu_data_get_rank(data_handles[x]);
  144. if (owner == rank)
  145. {
  146. //fprintf(stderr,"[%d] Computing on data[%d]\n", rank, x);
  147. starpu_insert_task(&cl,
  148. STARPU_VALUE, &rank, sizeof(rank),
  149. STARPU_RW, data_handles[x],
  150. 0);
  151. }
  152. }
  153. }
  154. /* Gather the matrix on main node */
  155. starpu_mpi_gather_detached(data_handles, nblocks*nblocks, 0, MPI_COMM_WORLD);
  156. /* Unregister matrix from StarPU */
  157. for(x=0 ; x<nblocks*nblocks ; x++)
  158. {
  159. if (data_handles[x])
  160. {
  161. starpu_data_unregister(data_handles[x]);
  162. }
  163. }
  164. #if 0
  165. // Print matrix
  166. if (rank == 0)
  167. {
  168. fprintf(stderr, "Output matrix\n");
  169. for(x=0 ; x<nblocks ; x++)
  170. {
  171. for(y=0 ; y<nblocks ; y++)
  172. {
  173. for (j = 0; j < block_size; j++)
  174. {
  175. for (i = 0; i < block_size; i++)
  176. {
  177. fprintf(stderr, "%2.2f\t", bmat[x][y][j+i*block_size]);
  178. }
  179. fprintf(stderr,"\n");
  180. }
  181. fprintf(stderr,"\n");
  182. }
  183. }
  184. }
  185. #endif
  186. // Free memory
  187. free(data_handles);
  188. if (rank == 0)
  189. {
  190. for(x=0 ; x<nblocks ; x++)
  191. {
  192. for(y=0 ; y<nblocks ; y++)
  193. {
  194. starpu_free((void *)bmat[x][y]);
  195. }
  196. free(bmat[x]);
  197. }
  198. free(bmat);
  199. }
  200. starpu_mpi_shutdown();
  201. starpu_shutdown();
  202. return 0;
  203. }