mpi_scatter_gather.c 4.8 KB

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