mpi_earlyrecv2.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Thibaut Lambert
  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 10
  22. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  23. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  24. void callback(void *arg)
  25. {
  26. unsigned *received = arg;
  27. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  28. *received = *received + 1;
  29. FPRINTF_MPI(stderr, "Requests %u received\n", *received);
  30. STARPU_PTHREAD_COND_SIGNAL(&cond);
  31. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  32. }
  33. typedef void (*check_func)(starpu_data_handle_t handle, int i, int rank, int *error);
  34. int exchange(int rank, starpu_data_handle_t *handles, check_func func, int detached)
  35. {
  36. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  37. int i;
  38. if (rank%2)
  39. {
  40. starpu_mpi_send(handles[0], other_rank, 0, MPI_COMM_WORLD);
  41. starpu_mpi_send(handles[NB-1], other_rank, NB-1, MPI_COMM_WORLD);
  42. for(i=1 ; i<NB-1 ; i++)
  43. {
  44. starpu_mpi_send(handles[i], other_rank, i, MPI_COMM_WORLD);
  45. }
  46. return 0;
  47. }
  48. else
  49. {
  50. int ret=0;
  51. starpu_mpi_req req[NB];
  52. int received = 0;
  53. if (detached)
  54. {
  55. starpu_mpi_irecv_detached(handles[0], other_rank, 0, MPI_COMM_WORLD, callback, &received);
  56. }
  57. else
  58. {
  59. memset(req, 0, NB*sizeof(starpu_mpi_req));
  60. starpu_mpi_irecv(handles[0], &req[0], other_rank, 0, MPI_COMM_WORLD);
  61. STARPU_ASSERT(req[0] != NULL);
  62. }
  63. // We sleep to make sure that the data for the tag 9 will be received before the recv is posted
  64. starpu_sleep(2);
  65. for(i=1 ; i<NB ; i++)
  66. {
  67. if (detached)
  68. {
  69. starpu_mpi_irecv_detached(handles[i], other_rank, i, MPI_COMM_WORLD, callback, &received);
  70. }
  71. else
  72. {
  73. starpu_mpi_irecv(handles[i], &req[i], other_rank, i, MPI_COMM_WORLD);
  74. STARPU_ASSERT(req[i] != NULL);
  75. }
  76. }
  77. if (detached)
  78. {
  79. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  80. while (received != NB)
  81. {
  82. FPRINTF_MPI(stderr, "Received %d messages\n", received);
  83. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  84. }
  85. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  86. }
  87. else
  88. {
  89. for(i=0 ; i<NB ; i++)
  90. {
  91. starpu_mpi_wait(&req[i], MPI_STATUS_IGNORE);
  92. func(handles[i], i, rank, &ret);
  93. }
  94. }
  95. return ret;
  96. }
  97. }
  98. void check_variable(starpu_data_handle_t handle, int i, int rank, int *error)
  99. {
  100. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  101. int *rvalue = (int *)starpu_data_get_local_ptr(handle);
  102. if (*rvalue != i*other_rank)
  103. {
  104. FPRINTF_MPI(stderr, "Incorrect received value: %d != %d\n", *rvalue, i*other_rank);
  105. *error = 1;
  106. }
  107. }
  108. int exchange_variable(int rank, int detached)
  109. {
  110. int ret, i;
  111. starpu_data_handle_t tab_handle[NB];
  112. int value[NB];
  113. FPRINTF_MPI(stderr, "Exchanging variable data with detached=%d\n", detached);
  114. for(i=0 ; i<NB ; i++)
  115. {
  116. value[i]=i*rank;
  117. starpu_variable_data_register(&tab_handle[i], STARPU_MAIN_RAM, (uintptr_t)&value[i], sizeof(int));
  118. starpu_mpi_data_register(tab_handle[i], i, rank);
  119. }
  120. ret = exchange(rank, tab_handle, check_variable, detached);
  121. for(i=0 ; i<NB ; i++)
  122. starpu_data_unregister(tab_handle[i]);
  123. return ret;
  124. }
  125. void check_void(starpu_data_handle_t handle, int i, int rank, int *error)
  126. {
  127. (void)handle;
  128. (void)i;
  129. (void)rank;
  130. (void)error;
  131. }
  132. int exchange_void(int rank, int detached)
  133. {
  134. int ret, i;
  135. starpu_data_handle_t tab_handle[NB];
  136. // This test is not run with valgrind as valgrind falsely detects error when exchanging NULL pointers
  137. STARPU_SKIP_IF_VALGRIND_RETURN_ZERO;
  138. FPRINTF_MPI(stderr, "Exchanging void data with detached=%d\n", detached);
  139. for(i=0 ; i<NB ; i++)
  140. {
  141. starpu_void_data_register(&tab_handle[i]);
  142. starpu_mpi_data_register(tab_handle[i], i, rank);
  143. }
  144. ret = exchange(rank, tab_handle, check_void, detached);
  145. for(i=0 ; i<NB ; i++)
  146. starpu_data_unregister(tab_handle[i]);
  147. return ret;
  148. }
  149. void check_complex(starpu_data_handle_t handle, int i, int rank, int *error)
  150. {
  151. double *real = starpu_complex_get_real(handle);
  152. double *imaginary = starpu_complex_get_imaginary(handle);
  153. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  154. if ((*real != ((i*other_rank)+12)) || (*imaginary != ((i*other_rank)+45)))
  155. {
  156. FPRINTF_MPI(stderr, "Incorrect received value: %f != %d || %f != %d\n", *real, ((i*other_rank)+12), *imaginary, ((i*other_rank)+45));
  157. *error = 1;
  158. }
  159. }
  160. int exchange_complex(int rank, int detached)
  161. {
  162. int ret, i;
  163. starpu_data_handle_t handle[NB];
  164. double real[NB];
  165. double imaginary[NB];
  166. FPRINTF_MPI(stderr, "Exchanging complex data with detached=%d\n", detached);
  167. for(i=0 ; i<NB ; i++)
  168. {
  169. real[i] = (i*rank)+12;
  170. imaginary[i] = (i*rank)+45;
  171. starpu_complex_data_register(&handle[i], STARPU_MAIN_RAM, &real[i], &imaginary[i], 1);
  172. starpu_mpi_data_register(handle[i], i, rank);
  173. }
  174. ret = exchange(rank, handle, check_complex, detached);
  175. for(i=0 ; i<NB ; i++)
  176. starpu_data_unregister(handle[i]);
  177. return ret;
  178. }
  179. int main(int argc, char **argv)
  180. {
  181. int ret=0, global_ret=0;
  182. int rank, size;
  183. int mpi_init;
  184. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  185. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  186. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  187. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  188. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  189. if (size%2 != 0)
  190. {
  191. FPRINTF(stderr, "We need a even number of processes.\n");
  192. starpu_mpi_shutdown();
  193. if (!mpi_init)
  194. MPI_Finalize();
  195. return STARPU_TEST_SKIPPED;
  196. }
  197. ret = exchange_variable(rank, 0);
  198. if (ret != 0)
  199. global_ret = ret;
  200. ret = exchange_variable(rank, 1);
  201. if (ret != 0)
  202. global_ret = ret;
  203. ret = exchange_void(rank, 0);
  204. if (ret != 0)
  205. global_ret = ret;
  206. ret = exchange_void(rank, 1);
  207. if (ret != 0)
  208. global_ret = ret;
  209. ret = exchange_complex(rank, 0);
  210. if (ret != 0)
  211. global_ret = ret;
  212. ret = exchange_complex(rank, 1);
  213. if (ret != 0)
  214. global_ret = ret;
  215. starpu_mpi_shutdown();
  216. if (!mpi_init)
  217. MPI_Finalize();
  218. return global_ret;
  219. }