mpi_earlyrecv2_sync.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2017 CNRS
  4. * Copyright (C) 2009,2010,2015,2018 Université de Bordeaux
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu_mpi.h>
  18. #include "helper.h"
  19. #include <unistd.h>
  20. #include <interface/complex_interface.h>
  21. #define NB 6
  22. typedef void (*check_func)(starpu_data_handle_t handle, int i, int rank, int *error);
  23. int exchange(int rank, starpu_data_handle_t *handles, check_func func)
  24. {
  25. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  26. int i;
  27. int ret=0;
  28. starpu_mpi_req req[NB];
  29. memset(req, 0, NB*sizeof(starpu_mpi_req));
  30. if (rank%2)
  31. {
  32. starpu_mpi_issend(handles[0], &req[0], other_rank, 0, MPI_COMM_WORLD);
  33. starpu_mpi_isend(handles[NB-1], &req[NB-1], other_rank, NB-1, MPI_COMM_WORLD);
  34. starpu_mpi_issend(handles[NB-2], &req[NB-2], other_rank, NB-2, MPI_COMM_WORLD);
  35. for(i=1 ; i<NB-2 ; i++)
  36. {
  37. if (i%2)
  38. {
  39. FPRINTF_MPI(stderr, "iSsending value %d\n", i);
  40. starpu_mpi_issend(handles[i], &req[i], other_rank, i, MPI_COMM_WORLD);
  41. }
  42. else
  43. {
  44. FPRINTF_MPI(stderr, "isending value %d\n", i);
  45. starpu_mpi_isend(handles[i], &req[i], other_rank, i, MPI_COMM_WORLD);
  46. }
  47. }
  48. for(i=0 ; i<NB ; i++)
  49. {
  50. starpu_mpi_wait(&req[i], MPI_STATUS_IGNORE);
  51. }
  52. }
  53. else
  54. {
  55. starpu_mpi_irecv(handles[0], &req[0], other_rank, 0, MPI_COMM_WORLD);
  56. STARPU_ASSERT(req[0] != NULL);
  57. starpu_mpi_irecv(handles[1], &req[1], other_rank, 1, MPI_COMM_WORLD);
  58. STARPU_ASSERT(req[1] != NULL);
  59. // We sleep to make sure that the data for the tag 8 and the tag 9 will be received before the recv are posted
  60. usleep(2000000);
  61. for(i=2 ; i<NB ; i++)
  62. {
  63. starpu_mpi_irecv(handles[i], &req[i], other_rank, i, MPI_COMM_WORLD);
  64. STARPU_ASSERT(req[i] != NULL);
  65. }
  66. for(i=0 ; i<NB ; i++)
  67. {
  68. starpu_mpi_wait(&req[i], MPI_STATUS_IGNORE);
  69. if (func)
  70. func(handles[i], i, rank, &ret);
  71. }
  72. }
  73. return ret;
  74. }
  75. void check_variable(starpu_data_handle_t handle, int i, int rank, int *error)
  76. {
  77. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  78. int *rvalue = (int *)starpu_data_get_local_ptr(handle);
  79. if (*rvalue != i*other_rank)
  80. {
  81. FPRINTF_MPI(stderr, "Incorrect received value: %d != %d\n", *rvalue, i*other_rank);
  82. *error = 1;
  83. }
  84. else
  85. {
  86. FPRINTF_MPI(stderr, "Correct received value: %d == %d\n", *rvalue, i*other_rank);
  87. }
  88. }
  89. int exchange_variable(int rank)
  90. {
  91. int ret, i;
  92. starpu_data_handle_t tab_handle[NB];
  93. int value[NB];
  94. ret = starpu_mpi_init_conf(NULL, NULL, 0, MPI_COMM_WORLD, NULL);
  95. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  96. FPRINTF_MPI(stderr, "Exchanging variable data\n");
  97. for(i=0 ; i<NB ; i++)
  98. {
  99. value[i]=i*rank;
  100. starpu_variable_data_register(&tab_handle[i], STARPU_MAIN_RAM, (uintptr_t)&value[i], sizeof(int));
  101. starpu_mpi_data_register(tab_handle[i], i, rank);
  102. }
  103. ret = exchange(rank, tab_handle, check_variable);
  104. for(i=0 ; i<NB ; i++)
  105. starpu_data_unregister(tab_handle[i]);
  106. starpu_mpi_shutdown();
  107. return ret;
  108. }
  109. int exchange_void(int rank)
  110. {
  111. int ret, i;
  112. starpu_data_handle_t tab_handle[NB];
  113. // This test is not run with valgrind as valgrind falsely detects error when exchanging NULL pointers
  114. STARPU_SKIP_IF_VALGRIND_RETURN_ZERO;
  115. ret = starpu_init(NULL);
  116. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  117. ret = starpu_mpi_init(NULL, NULL, 0);
  118. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  119. FPRINTF_MPI(stderr, "Exchanging void data\n");
  120. for(i=0 ; i<NB ; i++)
  121. {
  122. starpu_void_data_register(&tab_handle[i]);
  123. starpu_mpi_data_register(tab_handle[i], i, rank);
  124. }
  125. ret = exchange(rank, tab_handle, NULL);
  126. for(i=0 ; i<NB ; i++)
  127. starpu_data_unregister(tab_handle[i]);
  128. starpu_mpi_shutdown();
  129. return ret;
  130. }
  131. void check_complex(starpu_data_handle_t handle, int i, int rank, int *error)
  132. {
  133. double *real = starpu_complex_get_real(handle);
  134. double *imaginary = starpu_complex_get_imaginary(handle);
  135. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  136. if ((*real != ((i*other_rank)+12)) || (*imaginary != ((i*other_rank)+45)))
  137. {
  138. FPRINTF_MPI(stderr, "Incorrect received value: %f != %d || %f != %d\n", *real, ((i*other_rank)+12), *imaginary, ((i*other_rank)+45));
  139. *error = 1;
  140. }
  141. else
  142. {
  143. FPRINTF_MPI(stderr, "Correct received value: %f == %d || %f == %d\n", *real, ((i*other_rank)+12), *imaginary, ((i*other_rank)+45));
  144. }
  145. }
  146. int exchange_complex(int rank)
  147. {
  148. int ret, i;
  149. starpu_data_handle_t handle[NB];
  150. double real[NB];
  151. double imaginary[NB];
  152. ret = starpu_init(NULL);
  153. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  154. ret = starpu_mpi_init(NULL, NULL, 0);
  155. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  156. FPRINTF_MPI(stderr, "Exchanging complex data\n");
  157. for(i=0 ; i<NB ; i++)
  158. {
  159. real[i] = (i*rank)+12;
  160. imaginary[i] = (i*rank)+45;
  161. starpu_complex_data_register(&handle[i], STARPU_MAIN_RAM, &real[i], &imaginary[i], 1);
  162. starpu_mpi_data_register(handle[i], i, rank);
  163. }
  164. ret = exchange(rank, handle, check_complex);
  165. for(i=0 ; i<NB ; i++)
  166. starpu_data_unregister(handle[i]);
  167. starpu_mpi_shutdown();
  168. return ret;
  169. }
  170. int main(int argc, char **argv)
  171. {
  172. int ret=0, global_ret=0;
  173. int rank, size;
  174. MPI_INIT_THREAD_real(&argc, &argv, MPI_THREAD_SERIALIZED);
  175. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  176. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  177. if (size%2 != 0)
  178. {
  179. FPRINTF(stderr, "We need a even number of processes.\n");
  180. MPI_Finalize();
  181. return STARPU_TEST_SKIPPED;
  182. }
  183. ret = exchange_variable(rank);
  184. if (ret != 0)
  185. global_ret = ret;
  186. ret = exchange_void(rank);
  187. if (ret != 0)
  188. global_ret = ret;
  189. ret = exchange_complex(rank);
  190. if (ret != 0)
  191. global_ret = ret;
  192. MPI_Finalize();
  193. return global_ret;
  194. }