starpu_mpi_comm.c 4.9 KB

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