mpi_earlyrecv2_sync.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015 CNRS
  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 4
  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. #if 1
  33. starpu_mpi_issend(handles[0], &req[0], other_rank, 0, MPI_COMM_WORLD);
  34. starpu_mpi_isend(handles[NB-1], &req[NB-1], other_rank, NB-1, MPI_COMM_WORLD);
  35. starpu_mpi_issend(handles[NB-2], &req[NB-2], other_rank, NB-2, MPI_COMM_WORLD);
  36. for(i=1 ; i<NB-2 ; i++)
  37. #else
  38. for(i=0 ; i<NB ; i++)
  39. #endif
  40. {
  41. if (i%2)
  42. starpu_mpi_issend(handles[i], &req[i], other_rank, i, MPI_COMM_WORLD);
  43. else
  44. starpu_mpi_isend(handles[i], &req[i], other_rank, i, MPI_COMM_WORLD);
  45. }
  46. for(i=0 ; i<NB ; i++)
  47. {
  48. starpu_mpi_wait(&req[i], MPI_STATUS_IGNORE);
  49. }
  50. }
  51. else
  52. {
  53. #if 1
  54. starpu_mpi_irecv(handles[0], &req[0], other_rank, 0, MPI_COMM_WORLD);
  55. STARPU_ASSERT(req[0] != NULL);
  56. starpu_mpi_irecv(handles[1], &req[1], other_rank, 1, MPI_COMM_WORLD);
  57. STARPU_ASSERT(req[1] != NULL);
  58. // We sleep to make sure that the data for the tag 8 and the tag 9 will be received before the recv are posted
  59. usleep(2000000);
  60. for(i=2 ; i<NB ; i++)
  61. #else
  62. for(i=0 ; i<NB ; i++)
  63. #endif
  64. {
  65. starpu_mpi_irecv(handles[i], &req[i], other_rank, i, MPI_COMM_WORLD);
  66. STARPU_ASSERT(req[i] != NULL);
  67. }
  68. for(i=0 ; i<NB ; i++)
  69. {
  70. starpu_mpi_wait(&req[i], MPI_STATUS_IGNORE);
  71. if (func)
  72. func(handles[i], i, rank, &ret);
  73. }
  74. }
  75. return ret;
  76. }
  77. void check_variable(starpu_data_handle_t handle, int i, int rank, int *error)
  78. {
  79. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  80. int *rvalue = (int *)starpu_data_get_local_ptr(handle);
  81. if (*rvalue != i*other_rank)
  82. {
  83. FPRINTF_MPI(stderr, "Incorrect received value: %d != %d\n", *rvalue, i*other_rank);
  84. *error = 1;
  85. }
  86. else
  87. {
  88. FPRINTF_MPI(stderr, "Correct received value: %d == %d\n", *rvalue, i*other_rank);
  89. }
  90. }
  91. int exchange_variable(int rank)
  92. {
  93. int ret, i;
  94. starpu_data_handle_t tab_handle[NB];
  95. int value[NB];
  96. ret = starpu_init(NULL);
  97. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  98. ret = starpu_mpi_init(NULL, NULL, 0);
  99. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  100. FPRINTF_MPI(stderr, "Exchanging variable data\n");
  101. for(i=0 ; i<NB ; i++)
  102. {
  103. value[i]=i*rank;
  104. starpu_variable_data_register(&tab_handle[i], STARPU_MAIN_RAM, (uintptr_t)&value[i], sizeof(int));
  105. starpu_mpi_data_register(tab_handle[i], i, rank);
  106. }
  107. ret = exchange(rank, tab_handle, check_variable);
  108. for(i=0 ; i<NB ; i++)
  109. starpu_data_unregister(tab_handle[i]);
  110. starpu_mpi_shutdown();
  111. starpu_shutdown();
  112. return ret;
  113. }
  114. int exchange_void(int rank)
  115. {
  116. int ret, i;
  117. starpu_data_handle_t tab_handle[NB];
  118. ret = starpu_init(NULL);
  119. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  120. ret = starpu_mpi_init(NULL, NULL, 0);
  121. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  122. FPRINTF_MPI(stderr, "Exchanging void data\n");
  123. for(i=0 ; i<NB ; i++)
  124. {
  125. starpu_void_data_register(&tab_handle[i]);
  126. starpu_mpi_data_register(tab_handle[i], i, rank);
  127. }
  128. ret = exchange(rank, tab_handle, NULL);
  129. for(i=0 ; i<NB ; i++)
  130. starpu_data_unregister(tab_handle[i]);
  131. starpu_mpi_shutdown();
  132. starpu_shutdown();
  133. return ret;
  134. }
  135. void check_complex(starpu_data_handle_t handle, int i, int rank, int *error)
  136. {
  137. double *real = starpu_complex_get_real(handle);
  138. double *imaginary = starpu_complex_get_imaginary(handle);
  139. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  140. if ((*real != ((i*other_rank)+12)) || (*imaginary != ((i*other_rank)+45)))
  141. {
  142. FPRINTF_MPI(stderr, "Incorrect received value: %f != %d || %f != %d\n", *real, ((i*other_rank)+12), *imaginary, ((i*other_rank)+45));
  143. *error = 1;
  144. }
  145. else
  146. {
  147. FPRINTF_MPI(stderr, "Correct received value: %f == %d || %f == %d\n", *real, ((i*other_rank)+12), *imaginary, ((i*other_rank)+45));
  148. }
  149. }
  150. int exchange_complex(int rank)
  151. {
  152. int ret, i;
  153. starpu_data_handle_t handle[NB];
  154. double real[NB];
  155. double imaginary[NB];
  156. ret = starpu_init(NULL);
  157. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  158. ret = starpu_mpi_init(NULL, NULL, 0);
  159. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  160. FPRINTF_MPI(stderr, "Exchanging complex data\n");
  161. for(i=0 ; i<NB ; i++)
  162. {
  163. real[i] = (i*rank)+12;
  164. imaginary[i] = (i*rank)+45;
  165. starpu_complex_data_register(&handle[i], STARPU_MAIN_RAM, &real[i], &imaginary[i], 1);
  166. starpu_mpi_data_register(handle[i], i, rank);
  167. }
  168. ret = exchange(rank, handle, check_complex);
  169. for(i=0 ; i<NB ; i++)
  170. starpu_data_unregister(handle[i]);
  171. starpu_mpi_shutdown();
  172. starpu_shutdown();
  173. return ret;
  174. }
  175. int main(int argc, char **argv)
  176. {
  177. int ret=0, global_ret=0;
  178. int rank, size;
  179. MPI_Init(&argc, &argv);
  180. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  181. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  182. if (size%2 != 0)
  183. {
  184. FPRINTF(stderr, "We need a even number of processes.\n");
  185. MPI_Finalize();
  186. return STARPU_TEST_SKIPPED;
  187. }
  188. ret = exchange_variable(rank);
  189. if (ret != 0) global_ret = ret;
  190. ret = exchange_void(rank);
  191. if (ret != 0) global_ret = ret;
  192. ret = exchange_complex(rank);
  193. if (ret != 0) global_ret = ret;
  194. MPI_Finalize();
  195. return global_ret;
  196. }