mpi_scatter_gather.c 4.5 KB

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