mpi_scatter_gather.c 5.4 KB

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