mpi_earlyrecv2.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2017 CNRS
  4. * Copyright (C) 2009,2010,2014,2015,2017,2018 Université de Bordeaux
  5. * Copyright (C) 2013 Thibaut Lambert
  6. * Copyright (C) 2013 Inria
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <starpu_mpi.h>
  20. #include "helper.h"
  21. #include <unistd.h>
  22. #include <interface/complex_interface.h>
  23. #define NB 10
  24. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  25. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  26. void callback(void *arg)
  27. {
  28. unsigned *received = arg;
  29. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  30. *received = *received + 1;
  31. FPRINTF_MPI(stderr, "Requests %u received\n", *received);
  32. STARPU_PTHREAD_COND_SIGNAL(&cond);
  33. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  34. }
  35. typedef void (*check_func)(starpu_data_handle_t handle, int i, int rank, int *error);
  36. int exchange(int rank, starpu_data_handle_t *handles, check_func func, int detached)
  37. {
  38. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  39. int i;
  40. if (rank%2)
  41. {
  42. starpu_mpi_send(handles[0], other_rank, 0, MPI_COMM_WORLD);
  43. starpu_mpi_send(handles[NB-1], other_rank, NB-1, MPI_COMM_WORLD);
  44. for(i=1 ; i<NB-1 ; i++)
  45. {
  46. starpu_mpi_send(handles[i], other_rank, i, MPI_COMM_WORLD);
  47. }
  48. return 0;
  49. }
  50. else
  51. {
  52. int ret=0;
  53. starpu_mpi_req req[NB];
  54. int received = 0;
  55. if (detached)
  56. {
  57. starpu_mpi_irecv_detached(handles[0], other_rank, 0, MPI_COMM_WORLD, callback, &received);
  58. }
  59. else
  60. {
  61. memset(req, 0, NB*sizeof(starpu_mpi_req));
  62. starpu_mpi_irecv(handles[0], &req[0], other_rank, 0, MPI_COMM_WORLD);
  63. STARPU_ASSERT(req[0] != NULL);
  64. }
  65. // We sleep to make sure that the data for the tag 9 will be received before the recv is posted
  66. usleep(2000000);
  67. for(i=1 ; i<NB ; i++)
  68. {
  69. if (detached)
  70. {
  71. starpu_mpi_irecv_detached(handles[i], other_rank, i, MPI_COMM_WORLD, callback, &received);
  72. }
  73. else
  74. {
  75. starpu_mpi_irecv(handles[i], &req[i], other_rank, i, MPI_COMM_WORLD);
  76. STARPU_ASSERT(req[i] != NULL);
  77. }
  78. }
  79. if (detached)
  80. {
  81. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  82. while (received != NB)
  83. {
  84. FPRINTF_MPI(stderr, "Received %d messages\n", received);
  85. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  86. }
  87. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  88. }
  89. else
  90. {
  91. for(i=0 ; i<NB ; i++)
  92. {
  93. starpu_mpi_wait(&req[i], MPI_STATUS_IGNORE);
  94. func(handles[i], i, rank, &ret);
  95. }
  96. }
  97. return ret;
  98. }
  99. }
  100. void check_variable(starpu_data_handle_t handle, int i, int rank, int *error)
  101. {
  102. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  103. int *rvalue = (int *)starpu_data_get_local_ptr(handle);
  104. if (*rvalue != i*other_rank)
  105. {
  106. FPRINTF_MPI(stderr, "Incorrect received value: %d != %d\n", *rvalue, i*other_rank);
  107. *error = 1;
  108. }
  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. }