starpu_mpi_comm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016, 2017 CNRS
  4. * Copyright (C) 2011-2016 Université de Bordeaux
  5. * Copyright (C) 2014 INRIA
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include <starpu_mpi.h>
  20. #include <starpu_mpi_private.h>
  21. #include <starpu_mpi_comm.h>
  22. #include <common/list.h>
  23. struct _starpu_mpi_comm
  24. {
  25. MPI_Comm comm;
  26. struct _starpu_mpi_envelope *envelope;
  27. MPI_Request request;
  28. int posted;
  29. #ifdef STARPU_SIMGRID
  30. MPI_Status status;
  31. starpu_pthread_queue_t queue;
  32. unsigned done;
  33. #endif
  34. };
  35. struct _starpu_mpi_comm_hashtable
  36. {
  37. UT_hash_handle hh;
  38. MPI_Comm comm;
  39. };
  40. static starpu_pthread_mutex_t _starpu_mpi_comms_mutex;
  41. struct _starpu_mpi_comm_hashtable *_starpu_mpi_comms_cache;
  42. struct _starpu_mpi_comm **_starpu_mpi_comms;
  43. int _starpu_mpi_comm_nb;
  44. int _starpu_mpi_comm_allocated;
  45. int _starpu_mpi_comm_tested;
  46. void _starpu_mpi_comm_init(MPI_Comm comm)
  47. {
  48. _STARPU_MPI_DEBUG(10, "allocating for %d communicators\n", _starpu_mpi_comm_allocated);
  49. _starpu_mpi_comm_allocated=10;
  50. _STARPU_MPI_CALLOC(_starpu_mpi_comms, _starpu_mpi_comm_allocated, sizeof(struct _starpu_mpi_comm *));
  51. _starpu_mpi_comm_nb=0;
  52. _starpu_mpi_comm_tested=0;
  53. _starpu_mpi_comms_cache = NULL;
  54. STARPU_PTHREAD_MUTEX_INIT(&_starpu_mpi_comms_mutex, NULL);
  55. _starpu_mpi_comm_register(comm);
  56. }
  57. void _starpu_mpi_comm_shutdown()
  58. {
  59. int i;
  60. for(i=0 ; i<_starpu_mpi_comm_nb ; i++)
  61. {
  62. struct _starpu_mpi_comm *_comm = _starpu_mpi_comms[i]; // get the ith _comm;
  63. free(_comm->envelope);
  64. #ifdef STARPU_SIMGRID
  65. starpu_pthread_queue_unregister(&wait, &_comm->queue);
  66. starpu_pthread_queue_destroy(&_comm->queue);
  67. #endif
  68. free(_comm);
  69. }
  70. free(_starpu_mpi_comms);
  71. struct _starpu_mpi_comm_hashtable *entry, *tmp;
  72. HASH_ITER(hh, _starpu_mpi_comms_cache, entry, tmp)
  73. {
  74. HASH_DEL(_starpu_mpi_comms_cache, entry);
  75. free(entry);
  76. }
  77. STARPU_PTHREAD_MUTEX_DESTROY(&_starpu_mpi_comms_mutex);
  78. }
  79. void _starpu_mpi_comm_register(MPI_Comm comm)
  80. {
  81. struct _starpu_mpi_comm_hashtable *found;
  82. STARPU_PTHREAD_MUTEX_LOCK(&_starpu_mpi_comms_mutex);
  83. HASH_FIND(hh, _starpu_mpi_comms_cache, &comm, sizeof(MPI_Comm), found);
  84. if (found)
  85. {
  86. _STARPU_MPI_DEBUG(10, "comm %ld (%ld) already registered\n", (long int)comm, (long int)MPI_COMM_WORLD);
  87. }
  88. else
  89. {
  90. if (_starpu_mpi_comm_nb == _starpu_mpi_comm_allocated)
  91. {
  92. _starpu_mpi_comm_allocated *= 2;
  93. _STARPU_MPI_DEBUG(10, "reallocating for %d communicators\n", _starpu_mpi_comm_allocated);
  94. _STARPU_MPI_REALLOC(_starpu_mpi_comms, _starpu_mpi_comm_allocated * sizeof(struct _starpu_mpi_comm *));
  95. }
  96. _STARPU_MPI_DEBUG(10, "registering comm %ld (%ld) number %d\n", (long int)comm, (long int)MPI_COMM_WORLD, _starpu_mpi_comm_nb);
  97. struct _starpu_mpi_comm *_comm;
  98. _STARPU_MPI_CALLOC(_comm, 1, sizeof(struct _starpu_mpi_comm));
  99. _comm->comm = comm;
  100. _STARPU_MPI_CALLOC(_comm->envelope, 1,sizeof(struct _starpu_mpi_envelope));
  101. _comm->posted = 0;
  102. _starpu_mpi_comms[_starpu_mpi_comm_nb] = _comm;
  103. _starpu_mpi_comm_nb++;
  104. struct _starpu_mpi_comm_hashtable *entry;
  105. _STARPU_MPI_MALLOC(entry, sizeof(*entry));
  106. entry->comm = comm;
  107. HASH_ADD(hh, _starpu_mpi_comms_cache, comm, sizeof(entry->comm), entry);
  108. #ifdef STARPU_SIMGRID
  109. starpu_pthread_queue_init(&_comm->queue);
  110. starpu_pthread_queue_register(&wait, &_comm->queue);
  111. _comm->done = 0;
  112. #endif
  113. }
  114. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_comms_mutex);
  115. }
  116. void _starpu_mpi_comm_post_recv()
  117. {
  118. int i;
  119. STARPU_PTHREAD_MUTEX_LOCK(&_starpu_mpi_comms_mutex);
  120. for(i=0 ; i<_starpu_mpi_comm_nb ; i++)
  121. {
  122. struct _starpu_mpi_comm *_comm = _starpu_mpi_comms[i]; // get the ith _comm;
  123. if (_comm->posted == 0)
  124. {
  125. _STARPU_MPI_DEBUG(3, "Posting a receive to get a data envelop on comm %d %ld\n", i, (long int)_comm->comm);
  126. _STARPU_MPI_COMM_FROM_DEBUG(sizeof(struct _starpu_mpi_envelope), MPI_BYTE, MPI_ANY_SOURCE, _STARPU_MPI_TAG_ENVELOPE, _STARPU_MPI_TAG_ENVELOPE, _comm->comm);
  127. MPI_Irecv(_comm->envelope, sizeof(struct _starpu_mpi_envelope), MPI_BYTE, MPI_ANY_SOURCE, _STARPU_MPI_TAG_ENVELOPE, _comm->comm, &_comm->request);
  128. #ifdef STARPU_SIMGRID
  129. _starpu_mpi_simgrid_wait_req(&_comm->request, &_comm->status, &_comm->queue, &_comm->done);
  130. #endif
  131. _comm->posted = 1;
  132. }
  133. }
  134. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_comms_mutex);
  135. }
  136. int _starpu_mpi_comm_test_recv(MPI_Status *status, struct _starpu_mpi_envelope **envelope, MPI_Comm *comm)
  137. {
  138. int i=_starpu_mpi_comm_tested;
  139. STARPU_PTHREAD_MUTEX_LOCK(&_starpu_mpi_comms_mutex);
  140. while (1)
  141. {
  142. struct _starpu_mpi_comm *_comm = _starpu_mpi_comms[i]; // get the ith _comm;
  143. if (_comm->posted)
  144. {
  145. int flag, res;
  146. /* test whether an envelope has arrived. */
  147. #ifdef STARPU_SIMGRID
  148. res = _starpu_mpi_simgrid_mpi_test(&_comm->done, &flag);
  149. memcpy(status, &_comm->status, sizeof(*status));
  150. #else
  151. res = MPI_Test(&_comm->request, &flag, status);
  152. #endif
  153. STARPU_ASSERT(res == MPI_SUCCESS);
  154. if (flag)
  155. {
  156. _comm->posted = 0;
  157. _starpu_mpi_comm_tested++;
  158. if (_starpu_mpi_comm_tested == _starpu_mpi_comm_nb)
  159. _starpu_mpi_comm_tested = 0;
  160. *envelope = _comm->envelope;
  161. *comm = _comm->comm;
  162. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_comms_mutex);
  163. return 1;
  164. }
  165. }
  166. i++;
  167. if (i == _starpu_mpi_comm_nb) i=0;
  168. if (i == _starpu_mpi_comm_tested)
  169. {
  170. // We have tested all the requests, none has completed
  171. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_comms_mutex);
  172. return 0;
  173. }
  174. }
  175. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_comms_mutex);
  176. return 0;
  177. }
  178. void _starpu_mpi_comm_cancel_recv()
  179. {
  180. int i;
  181. STARPU_PTHREAD_MUTEX_LOCK(&_starpu_mpi_comms_mutex);
  182. for(i=0 ; i<_starpu_mpi_comm_nb ; i++)
  183. {
  184. struct _starpu_mpi_comm *_comm = _starpu_mpi_comms[i]; // get the ith _comm;
  185. if (_comm->posted == 1)
  186. {
  187. MPI_Cancel(&_comm->request);
  188. #ifndef STARPU_SIMGRID
  189. {
  190. MPI_Status status;
  191. MPI_Wait(&_comm->request, &status);
  192. }
  193. #endif
  194. _comm->posted = 0;
  195. }
  196. }
  197. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_comms_mutex);
  198. }