starpu_mpi_comm.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016 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. };
  30. struct _starpu_mpi_comm_hashtable
  31. {
  32. UT_hash_handle hh;
  33. MPI_Comm comm;
  34. };
  35. static starpu_pthread_mutex_t _starpu_mpi_comms_mutex;
  36. struct _starpu_mpi_comm_hashtable *_starpu_mpi_comms_cache;
  37. struct _starpu_mpi_comm **_starpu_mpi_comms;
  38. int _starpu_mpi_comm_nb;
  39. int _starpu_mpi_comm_allocated;
  40. int _starpu_mpi_comm_tested;
  41. void _starpu_mpi_comm_init(MPI_Comm comm)
  42. {
  43. _STARPU_MPI_DEBUG(10, "allocating for %d communicators\n", _starpu_mpi_comm_allocated);
  44. _starpu_mpi_comm_allocated=10;
  45. _starpu_mpi_comms = calloc(_starpu_mpi_comm_allocated, sizeof(struct _starpu_mpi_comm *));
  46. _starpu_mpi_comm_nb=0;
  47. _starpu_mpi_comm_tested=0;
  48. _starpu_mpi_comms_cache = NULL;
  49. STARPU_PTHREAD_MUTEX_INIT(&_starpu_mpi_comms_mutex, NULL);
  50. _starpu_mpi_comm_register(comm);
  51. }
  52. void _starpu_mpi_comm_free()
  53. {
  54. int i;
  55. for(i=0 ; i<_starpu_mpi_comm_nb ; i++)
  56. {
  57. struct _starpu_mpi_comm *_comm = _starpu_mpi_comms[i]; // get the ith _comm;
  58. free(_comm->envelope);
  59. free(_comm);
  60. }
  61. free(_starpu_mpi_comms);
  62. struct _starpu_mpi_comm_hashtable *entry, *tmp;
  63. HASH_ITER(hh, _starpu_mpi_comms_cache, entry, tmp)
  64. {
  65. HASH_DEL(_starpu_mpi_comms_cache, entry);
  66. free(entry);
  67. }
  68. STARPU_PTHREAD_MUTEX_DESTROY(&_starpu_mpi_comms_mutex);
  69. }
  70. void _starpu_mpi_comm_register(MPI_Comm comm)
  71. {
  72. struct _starpu_mpi_comm_hashtable *found;
  73. STARPU_PTHREAD_MUTEX_LOCK(&_starpu_mpi_comms_mutex);
  74. HASH_FIND(hh, _starpu_mpi_comms_cache, &comm, sizeof(MPI_Comm), found);
  75. if (found)
  76. {
  77. _STARPU_MPI_DEBUG(10, "comm %d (%d) already registered\n", comm, MPI_COMM_WORLD);
  78. }
  79. else
  80. {
  81. if (_starpu_mpi_comm_nb == _starpu_mpi_comm_allocated)
  82. {
  83. _starpu_mpi_comm_allocated *= 2;
  84. _STARPU_MPI_DEBUG(10, "reallocating for %d communicators\n", _starpu_mpi_comm_allocated);
  85. _starpu_mpi_comms = realloc(_starpu_mpi_comms, _starpu_mpi_comm_allocated * sizeof(struct _starpu_mpi_comm *));
  86. }
  87. _STARPU_MPI_DEBUG(10, "registering comm %d (%d) number %d\n", comm, MPI_COMM_WORLD, _starpu_mpi_comm_nb);
  88. struct _starpu_mpi_comm *_comm = calloc(1, sizeof(struct _starpu_mpi_comm));
  89. _comm->comm = comm;
  90. _comm->envelope = calloc(1,sizeof(struct _starpu_mpi_envelope));
  91. _comm->posted = 0;
  92. _starpu_mpi_comms[_starpu_mpi_comm_nb] = _comm;
  93. _starpu_mpi_comm_nb++;
  94. struct _starpu_mpi_comm_hashtable *entry = (struct _starpu_mpi_comm_hashtable *)malloc(sizeof(*entry));
  95. entry->comm = comm;
  96. HASH_ADD(hh, _starpu_mpi_comms_cache, comm, sizeof(entry->comm), entry);
  97. }
  98. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_comms_mutex);
  99. }
  100. void _starpu_mpi_comm_post_recv()
  101. {
  102. int i;
  103. STARPU_PTHREAD_MUTEX_LOCK(&_starpu_mpi_comms_mutex);
  104. for(i=0 ; i<_starpu_mpi_comm_nb ; i++)
  105. {
  106. struct _starpu_mpi_comm *_comm = _starpu_mpi_comms[i]; // get the ith _comm;
  107. if (_comm->posted == 0)
  108. {
  109. _STARPU_MPI_DEBUG(3, "Posting a receive to get a data envelop on comm %d %d\n", i, _comm->comm);
  110. _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);
  111. MPI_Irecv(_comm->envelope, sizeof(struct _starpu_mpi_envelope), MPI_BYTE, MPI_ANY_SOURCE, _STARPU_MPI_TAG_ENVELOPE, _comm->comm, &_comm->request);
  112. _comm->posted = 1;
  113. }
  114. }
  115. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_comms_mutex);
  116. }
  117. int _starpu_mpi_comm_test_recv(MPI_Status *status, struct _starpu_mpi_envelope **envelope, MPI_Comm *comm)
  118. {
  119. int i=_starpu_mpi_comm_tested;
  120. STARPU_PTHREAD_MUTEX_LOCK(&_starpu_mpi_comms_mutex);
  121. while (1)
  122. {
  123. int flag, res;
  124. struct _starpu_mpi_comm *_comm = _starpu_mpi_comms[i]; // get the ith _comm;
  125. if (_comm->posted)
  126. {
  127. /* test whether an envelope has arrived. */
  128. #ifdef STARPU_SIMGRID
  129. MSG_process_sleep(0.000001);
  130. #endif
  131. res = MPI_Test(&_comm->request, &flag, status);
  132. STARPU_ASSERT(res == MPI_SUCCESS);
  133. if (flag)
  134. {
  135. _comm->posted = 0;
  136. _starpu_mpi_comm_tested++;
  137. if (_starpu_mpi_comm_tested == _starpu_mpi_comm_nb)
  138. _starpu_mpi_comm_tested = 0;
  139. *envelope = _comm->envelope;
  140. *comm = _comm->comm;
  141. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_comms_mutex);
  142. return 1;
  143. }
  144. }
  145. i++;
  146. if (i == _starpu_mpi_comm_nb) i=0;
  147. if (i == _starpu_mpi_comm_tested)
  148. {
  149. // We have tested all the requests, none has completed
  150. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_comms_mutex);
  151. return 0;
  152. }
  153. }
  154. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_comms_mutex);
  155. return 0;
  156. }
  157. void _starpu_mpi_comm_cancel_recv()
  158. {
  159. int i;
  160. STARPU_PTHREAD_MUTEX_LOCK(&_starpu_mpi_comms_mutex);
  161. for(i=0 ; i<_starpu_mpi_comm_nb ; i++)
  162. {
  163. struct _starpu_mpi_comm *_comm = _starpu_mpi_comms[i]; // get the ith _comm;
  164. if (_comm->posted == 1)
  165. {
  166. MPI_Status status;
  167. MPI_Cancel(&_comm->request);
  168. MPI_Wait(&_comm->request, &status);
  169. _comm->posted = 0;
  170. }
  171. }
  172. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_mpi_comms_mutex);
  173. }