mpi_earlyrecv2.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2021 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. starpu_data_acquire_on_node(handle, starpu_worker_get_local_memory_node(), STARPU_R);
  102. int *rvalue = (int *)starpu_data_get_local_ptr(handle);
  103. if (*rvalue != i*other_rank)
  104. {
  105. FPRINTF_MPI(stderr, "Incorrect received value: %d != %d\n", *rvalue, i*other_rank);
  106. *error = 1;
  107. }
  108. starpu_data_release(handle);
  109. }
  110. int exchange_variable(int rank, int detached)
  111. {
  112. int ret, i;
  113. starpu_data_handle_t tab_handle[NB];
  114. int value[NB];
  115. FPRINTF_MPI(stderr, "Exchanging variable data with detached=%d\n", detached);
  116. for(i=0 ; i<NB ; i++)
  117. {
  118. value[i]=i*rank;
  119. starpu_variable_data_register(&tab_handle[i], STARPU_MAIN_RAM, (uintptr_t)&value[i], sizeof(int));
  120. starpu_mpi_data_register(tab_handle[i], i, rank);
  121. }
  122. ret = exchange(rank, tab_handle, check_variable, detached);
  123. for(i=0 ; i<NB ; i++)
  124. starpu_data_unregister(tab_handle[i]);
  125. return ret;
  126. }
  127. void check_void(starpu_data_handle_t handle, int i, int rank, int *error)
  128. {
  129. (void)handle;
  130. (void)i;
  131. (void)rank;
  132. (void)error;
  133. }
  134. int exchange_void(int rank, int detached)
  135. {
  136. int ret, i;
  137. starpu_data_handle_t tab_handle[NB];
  138. // This test is not run with valgrind as valgrind falsely detects error when exchanging NULL pointers
  139. STARPU_SKIP_IF_VALGRIND_RETURN_ZERO;
  140. FPRINTF_MPI(stderr, "Exchanging void data with detached=%d\n", detached);
  141. for(i=0 ; i<NB ; i++)
  142. {
  143. starpu_void_data_register(&tab_handle[i]);
  144. starpu_mpi_data_register(tab_handle[i], i, rank);
  145. }
  146. ret = exchange(rank, tab_handle, check_void, detached);
  147. for(i=0 ; i<NB ; i++)
  148. starpu_data_unregister(tab_handle[i]);
  149. return ret;
  150. }
  151. void check_complex(starpu_data_handle_t handle, int i, int rank, int *error)
  152. {
  153. double *real = starpu_complex_get_real(handle);
  154. double *imaginary = starpu_complex_get_imaginary(handle);
  155. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  156. if ((*real != ((i*other_rank)+12)) || (*imaginary != ((i*other_rank)+45)))
  157. {
  158. FPRINTF_MPI(stderr, "Incorrect received value: %f != %d || %f != %d\n", *real, ((i*other_rank)+12), *imaginary, ((i*other_rank)+45));
  159. *error = 1;
  160. }
  161. }
  162. int exchange_complex(int rank, int detached)
  163. {
  164. int ret, i;
  165. starpu_data_handle_t handle[NB];
  166. double real[NB];
  167. double imaginary[NB];
  168. FPRINTF_MPI(stderr, "Exchanging complex data with detached=%d\n", detached);
  169. for(i=0 ; i<NB ; i++)
  170. {
  171. real[i] = (i*rank)+12;
  172. imaginary[i] = (i*rank)+45;
  173. starpu_complex_data_register(&handle[i], STARPU_MAIN_RAM, &real[i], &imaginary[i], 1);
  174. starpu_mpi_data_register(handle[i], i, rank);
  175. }
  176. ret = exchange(rank, handle, check_complex, detached);
  177. for(i=0 ; i<NB ; i++)
  178. starpu_data_unregister(handle[i]);
  179. return ret;
  180. }
  181. int main(int argc, char **argv)
  182. {
  183. int ret=0, global_ret=0;
  184. int rank, size;
  185. int mpi_init;
  186. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  187. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  188. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  189. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  190. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  191. if (size%2 != 0)
  192. {
  193. FPRINTF(stderr, "We need a even number of processes.\n");
  194. starpu_mpi_shutdown();
  195. if (!mpi_init)
  196. MPI_Finalize();
  197. return STARPU_TEST_SKIPPED;
  198. }
  199. ret = exchange_variable(rank, 0);
  200. if (ret != 0)
  201. global_ret = ret;
  202. ret = exchange_variable(rank, 1);
  203. if (ret != 0)
  204. global_ret = ret;
  205. ret = exchange_void(rank, 0);
  206. if (ret != 0)
  207. global_ret = ret;
  208. ret = exchange_void(rank, 1);
  209. if (ret != 0)
  210. global_ret = ret;
  211. ret = exchange_complex(rank, 0);
  212. if (ret != 0)
  213. global_ret = ret;
  214. ret = exchange_complex(rank, 1);
  215. if (ret != 0)
  216. global_ret = ret;
  217. starpu_mpi_shutdown();
  218. if (!mpi_init)
  219. MPI_Finalize();
  220. return global_ret;
  221. }