mpi_scatter_gather.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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. #include "helper.h"
  18. /* Returns the MPI node number where data indexes index is */
  19. int my_distrib(int x, int nb_nodes)
  20. {
  21. return x % nb_nodes;
  22. }
  23. void cpu_codelet(void *descr[], void *_args)
  24. {
  25. int *vector = (int *)STARPU_VECTOR_GET_PTR(descr[0]);
  26. unsigned nx = STARPU_VECTOR_GET_NX(descr[0]);
  27. unsigned i;
  28. int rank;
  29. starpu_codelet_unpack_args(_args, &rank);
  30. for (i = 0; i < nx; i++)
  31. {
  32. //fprintf(stderr,"rank %d v[%d] = %d\n", rank, i, vector[i]);
  33. vector[i] *= rank+2;
  34. }
  35. }
  36. static struct starpu_codelet cl =
  37. {
  38. .cpu_funcs = {cpu_codelet},
  39. .nbuffers = 1,
  40. .modes = {STARPU_RW},
  41. #ifdef STARPU_SIMGRID
  42. .model = &starpu_perfmodel_nop,
  43. #endif
  44. };
  45. void scallback(void *arg)
  46. {
  47. char *msg = arg;
  48. FPRINTF_MPI(stderr, "Sending completed for <%s>\n", msg);
  49. }
  50. void rcallback(void *arg)
  51. {
  52. char *msg = arg;
  53. FPRINTF_MPI(stderr, "Reception completed for <%s>\n", msg);
  54. }
  55. int main(int argc, char **argv)
  56. {
  57. int rank, nodes, ret, x;
  58. int *vector = NULL;
  59. starpu_data_handle_t *data_handles;
  60. int size=10;
  61. ret = starpu_mpi_init_conf(&argc, &argv, 1, MPI_COMM_WORLD, NULL);
  62. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  63. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  64. starpu_mpi_comm_size(MPI_COMM_WORLD, &nodes);
  65. if (starpu_cpu_worker_get_count() == 0)
  66. {
  67. if (rank == 0)
  68. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  69. starpu_mpi_shutdown();
  70. return STARPU_TEST_SKIPPED;
  71. }
  72. if (rank == 0)
  73. {
  74. /* Allocate the vector */
  75. vector = malloc(size * sizeof(int));
  76. for(x=0 ; x<size ; x++)
  77. vector[x] = x+10;
  78. // Print vector
  79. FPRINTF_MPI(stderr, " Input vector: ");
  80. for(x=0 ; x<size ; x++)
  81. {
  82. FPRINTF(stderr, "%d\t", vector[x]);
  83. }
  84. FPRINTF(stderr,"\n");
  85. }
  86. /* Allocate data handles and register data to StarPU */
  87. data_handles = (starpu_data_handle_t *) calloc(size, sizeof(starpu_data_handle_t));
  88. for(x = 0; x < size ; x++)
  89. {
  90. int mpi_rank = my_distrib(x, nodes);
  91. if (rank == 0)
  92. {
  93. starpu_vector_data_register(&data_handles[x], 0, (uintptr_t)&vector[x], 1, sizeof(int));
  94. }
  95. else if (mpi_rank == rank)
  96. {
  97. /* I do not own this index but i will need it for my computations */
  98. starpu_vector_data_register(&data_handles[x], -1, (uintptr_t)NULL, 1, sizeof(int));
  99. }
  100. else
  101. {
  102. /* I know it's useless to allocate anything for this */
  103. data_handles[x] = NULL;
  104. }
  105. if (data_handles[x])
  106. {
  107. starpu_mpi_data_register(data_handles[x], x, 0);
  108. }
  109. }
  110. /* Scatter the matrix among the nodes */
  111. for(x = 0; x < size ; x++)
  112. {
  113. if (data_handles[x])
  114. {
  115. int mpi_rank = my_distrib(x, nodes);
  116. starpu_mpi_data_set_rank(data_handles[x], mpi_rank);
  117. }
  118. }
  119. starpu_mpi_scatter_detached(data_handles, size, 0, MPI_COMM_WORLD, scallback, "scatter", NULL, NULL);
  120. /* Calculation */
  121. for(x = 0; x < size ; x++)
  122. {
  123. if (data_handles[x])
  124. {
  125. int owner = starpu_mpi_data_get_rank(data_handles[x]);
  126. if (owner == rank)
  127. {
  128. FPRINTF_MPI(stderr,"Computing on data[%d]\n", x);
  129. starpu_task_insert(&cl,
  130. STARPU_VALUE, &rank, sizeof(rank),
  131. STARPU_RW, data_handles[x],
  132. 0);
  133. }
  134. }
  135. }
  136. /* Gather the matrix on main node */
  137. starpu_mpi_gather_detached(data_handles, size, 0, MPI_COMM_WORLD, scallback, "gather", rcallback, "gather");
  138. for(x = 0; x < size ; x++)
  139. {
  140. if (data_handles[x])
  141. {
  142. starpu_mpi_data_set_rank(data_handles[x], 0);
  143. }
  144. }
  145. /* Unregister matrix from StarPU */
  146. for(x=0 ; x<size ; x++)
  147. {
  148. if (data_handles[x])
  149. {
  150. starpu_data_unregister(data_handles[x]);
  151. }
  152. }
  153. // Print vector
  154. if (rank == 0)
  155. {
  156. FPRINTF_MPI(stderr, "Output vector: ");
  157. for(x=0 ; x<size ; x++)
  158. {
  159. FPRINTF(stderr, "%d\t", vector[x]);
  160. }
  161. FPRINTF(stderr,"\n");
  162. for(x=0 ; x<size ; x++)
  163. {
  164. int mpi_rank = my_distrib(x, nodes);
  165. if (vector[x] != (x+10) * (mpi_rank+2))
  166. {
  167. FPRINTF_MPI(stderr, "Incorrect value for vector[%d]. computed %d != expected %d\n", x, vector[x], (x+10) * (mpi_rank+2));
  168. ret = 1;
  169. }
  170. }
  171. free(vector);
  172. }
  173. // Free memory
  174. free(data_handles);
  175. starpu_mpi_shutdown();
  176. return (rank == 0) ? ret : 0;
  177. }