mpi_scatter_gather.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016, 2017 CNRS
  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_nop_perf_model,
  43. #endif
  44. };
  45. void scallback(void *arg STARPU_ATTRIBUTE_UNUSED)
  46. {
  47. char *msg = arg;
  48. FPRINTF_MPI(stderr, "Sending completed for <%s>\n", msg);
  49. }
  50. void rcallback(void *arg STARPU_ATTRIBUTE_UNUSED)
  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_init(NULL);
  62. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  63. ret = starpu_mpi_init(&argc, &argv, 1);
  64. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  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. starpu_shutdown();
  73. return STARPU_TEST_SKIPPED;
  74. }
  75. if (rank == 0)
  76. {
  77. /* Allocate the vector */
  78. vector = malloc(size * sizeof(int));
  79. for(x=0 ; x<size ; x++)
  80. vector[x] = x+10;
  81. // Print vector
  82. FPRINTF_MPI(stderr, " Input vector: ");
  83. for(x=0 ; x<size ; x++)
  84. {
  85. FPRINTF(stderr, "%d\t", vector[x]);
  86. }
  87. FPRINTF(stderr,"\n");
  88. }
  89. /* Allocate data handles and register data to StarPU */
  90. data_handles = (starpu_data_handle_t *) calloc(size, sizeof(starpu_data_handle_t));
  91. for(x = 0; x < size ; x++)
  92. {
  93. int mpi_rank = my_distrib(x, nodes);
  94. if (rank == 0)
  95. {
  96. starpu_vector_data_register(&data_handles[x], 0, (uintptr_t)&vector[x], 1, sizeof(int));
  97. }
  98. else if (mpi_rank == rank)
  99. {
  100. /* I do not own that index but i will need it for my computations */
  101. starpu_vector_data_register(&data_handles[x], -1, (uintptr_t)NULL, 1, sizeof(int));
  102. }
  103. else
  104. {
  105. /* I know it's useless to allocate anything for this */
  106. data_handles[x] = NULL;
  107. }
  108. if (data_handles[x])
  109. {
  110. starpu_mpi_data_register(data_handles[x], x, 0);
  111. }
  112. }
  113. /* Scatter the matrix among the nodes */
  114. for(x = 0; x < size ; x++)
  115. {
  116. if (data_handles[x])
  117. {
  118. int mpi_rank = my_distrib(x, nodes);
  119. starpu_mpi_data_set_rank(data_handles[x], mpi_rank);
  120. }
  121. }
  122. starpu_mpi_scatter_detached(data_handles, size, 0, MPI_COMM_WORLD, scallback, "scatter", NULL, NULL);
  123. /* Calculation */
  124. for(x = 0; x < size ; x++)
  125. {
  126. if (data_handles[x])
  127. {
  128. int owner = starpu_mpi_data_get_rank(data_handles[x]);
  129. if (owner == rank)
  130. {
  131. FPRINTF_MPI(stderr,"Computing on data[%d]\n", x);
  132. starpu_task_insert(&cl,
  133. STARPU_VALUE, &rank, sizeof(rank),
  134. STARPU_RW, data_handles[x],
  135. 0);
  136. }
  137. }
  138. }
  139. /* Gather the matrix on main node */
  140. starpu_mpi_gather_detached(data_handles, size, 0, MPI_COMM_WORLD, scallback, "gather", rcallback, "gather");
  141. for(x = 0; x < size ; x++)
  142. {
  143. if (data_handles[x])
  144. {
  145. starpu_mpi_data_set_rank(data_handles[x], 0);
  146. }
  147. }
  148. /* Unregister matrix from StarPU */
  149. for(x=0 ; x<size ; x++)
  150. {
  151. if (data_handles[x])
  152. {
  153. starpu_data_unregister(data_handles[x]);
  154. }
  155. }
  156. // Print vector
  157. if (rank == 0)
  158. {
  159. FPRINTF_MPI(stderr, "Output vector: ");
  160. for(x=0 ; x<size ; x++)
  161. {
  162. FPRINTF(stderr, "%d\t", vector[x]);
  163. }
  164. FPRINTF(stderr,"\n");
  165. for(x=0 ; x<size ; x++)
  166. {
  167. int mpi_rank = my_distrib(x, nodes);
  168. if (vector[x] != (x+10) * (mpi_rank+2))
  169. {
  170. FPRINTF_MPI(stderr, "Incorrect value for vector[%d]. computed %d != expected %d\n", x, vector[x], (x+10) * (mpi_rank+2));
  171. ret = 1;
  172. }
  173. }
  174. free(vector);
  175. }
  176. // Free memory
  177. free(data_handles);
  178. starpu_mpi_shutdown();
  179. starpu_shutdown();
  180. return (rank == 0) ? ret : 0;
  181. }