starpu_mpi_coop_sends.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2013,2016-2017 Inria
  4. * Copyright (C) 2009-2018 Université de Bordeaux
  5. * Copyright (C) 2010-2018 CNRS
  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_mpi.h>
  19. #include <starpu_mpi_private.h>
  20. #include <datawizard/coherency.h>
  21. /*
  22. * One node sends the same data to several nodes. Gather them into a
  23. * "coop_sends", which then has a global view of all the required sends, and can
  24. * establish a diffusion tree by telling receiving nodes to retransmit what they
  25. * received (forwards) to others, and to others that they will receive from the
  26. * former (redirects).
  27. */
  28. /* This is called after a request is finished processing, to release the data */
  29. void _starpu_mpi_release_req_data(struct _starpu_mpi_req *req)
  30. {
  31. if (!req->data_handle)
  32. return;
  33. if (_starpu_mpi_req_multilist_queued_coop_sends(req))
  34. {
  35. struct _starpu_mpi_coop_sends *coop_sends = req->coop_sends_head;
  36. struct _starpu_mpi_data *mpi_data = coop_sends->mpi_data;
  37. int last;
  38. _starpu_spin_lock(&mpi_data->coop_lock);
  39. /* Part of a cooperative send, dequeue ourself from others */
  40. _starpu_mpi_req_multilist_erase_coop_sends(&coop_sends->reqs, req);
  41. last = _starpu_mpi_req_multilist_empty_coop_sends(&coop_sends->reqs);
  42. _starpu_spin_unlock(&mpi_data->coop_lock);
  43. if (last)
  44. {
  45. /* We were last, release data */
  46. free(coop_sends->reqs_array);
  47. free(coop_sends);
  48. starpu_data_release(req->data_handle);
  49. }
  50. }
  51. else
  52. {
  53. /* Trivial request */
  54. starpu_data_release(req->data_handle);
  55. }
  56. }
  57. /* Comparison function for getting qsort to put requests with high priority first */
  58. static int _starpu_mpi_reqs_prio_compare(const void *a, const void *b)
  59. {
  60. const struct _starpu_mpi_req * const *ra = a;
  61. const struct _starpu_mpi_req * const *rb = b;
  62. return (*rb)->prio - (*ra)->prio;
  63. }
  64. /* Sort the requests by priority and build a diffusion tree. Actually does something only once per coop_sends bag. */
  65. static void _starpu_mpi_coop_sends_optimize(struct _starpu_mpi_coop_sends *coop_sends)
  66. {
  67. if (coop_sends->n == 1)
  68. /* Trivial case, don't optimize */
  69. return;
  70. _starpu_spin_lock(&coop_sends->lock);
  71. if (!coop_sends->reqs_array)
  72. {
  73. unsigned n = coop_sends->n, i;
  74. struct _starpu_mpi_req *cur;
  75. struct _starpu_mpi_req **reqs;
  76. _STARPU_MPI_DEBUG(0, "handling cooperative sends %p for %u neighbours\n", coop_sends, n);
  77. /* Store them in an array */
  78. _STARPU_CALLOC(reqs, n, sizeof(*reqs));
  79. for (cur = _starpu_mpi_req_multilist_begin_coop_sends(&coop_sends->reqs), i = 0;
  80. cur != _starpu_mpi_req_multilist_end_coop_sends(&coop_sends->reqs);
  81. cur = _starpu_mpi_req_multilist_next_coop_sends(cur), i++)
  82. reqs[i] = cur;
  83. coop_sends->reqs_array = reqs;
  84. /* Sort them */
  85. qsort(reqs, n, sizeof(*reqs), _starpu_mpi_reqs_prio_compare);
  86. /* And build the diffusion tree */
  87. _starpu_mpi_coop_sends_build_tree(coop_sends);
  88. }
  89. _starpu_spin_unlock(&coop_sends->lock);
  90. }
  91. /* This is called on completion of acquisition of data for a cooperative send */
  92. static void _starpu_mpi_coop_sends_data_ready(void *arg)
  93. {
  94. _STARPU_MPI_LOG_IN();
  95. struct _starpu_mpi_coop_sends *coop_sends = arg;
  96. struct _starpu_mpi_data *mpi_data = coop_sends->mpi_data;
  97. /* Take the cooperative send bag out from more submissions */
  98. if (mpi_data->coop_sends == coop_sends)
  99. {
  100. _starpu_spin_lock(&mpi_data->coop_lock);
  101. if (mpi_data->coop_sends == coop_sends)
  102. mpi_data->coop_sends = NULL;
  103. _starpu_spin_unlock(&mpi_data->coop_lock);
  104. }
  105. /* Build diffusion tree */
  106. _starpu_mpi_coop_sends_optimize(coop_sends);
  107. if (coop_sends->n == 1)
  108. {
  109. /* Trivial case, just submit it */
  110. _starpu_mpi_submit_ready_request(_starpu_mpi_req_multilist_begin_coop_sends(&coop_sends->reqs));
  111. }
  112. else
  113. {
  114. /* And submit them */
  115. if (STARPU_TEST_AND_SET(&coop_sends->redirects_sent, 1) == 0)
  116. _starpu_mpi_submit_coop_sends(coop_sends, 1, 1);
  117. else
  118. _starpu_mpi_submit_coop_sends(coop_sends, 0, 1);
  119. }
  120. _STARPU_MPI_LOG_OUT();
  121. }
  122. /* This is called when we want to stop including new members in a cooperative send,
  123. * either because we know there won't be any other members due to the algorithm
  124. * or because the value has changed. */
  125. static void _starpu_mpi_coop_send_flush(struct _starpu_mpi_coop_sends *coop_sends)
  126. {
  127. if (!coop_sends)
  128. return;
  129. /* Build diffusion tree */
  130. _starpu_mpi_coop_sends_optimize(coop_sends);
  131. if (coop_sends->n == 1)
  132. /* Trivial case, we will just send the data */
  133. return;
  134. /* And submit them */
  135. if (STARPU_TEST_AND_SET(&coop_sends->redirects_sent, 1) == 0)
  136. _starpu_mpi_submit_coop_sends(coop_sends, 1, 0);
  137. }
  138. /* This is called when a write to the data was just submitted, which means we
  139. * can't make future sends cooperate with past sends since it's not the same value
  140. */
  141. void _starpu_mpi_data_flush(starpu_data_handle_t data_handle)
  142. {
  143. struct _starpu_mpi_data *mpi_data = data_handle->mpi_data;
  144. struct _starpu_mpi_coop_sends *coop_sends;
  145. if (!mpi_data)
  146. return;
  147. _starpu_spin_lock(&mpi_data->coop_lock);
  148. coop_sends = mpi_data->coop_sends;
  149. if (coop_sends)
  150. mpi_data->coop_sends = NULL;
  151. _starpu_spin_unlock(&mpi_data->coop_lock);
  152. if (coop_sends)
  153. {
  154. _STARPU_MPI_DEBUG(0, "%p: data written to, flush cooperative sends %p\n", data_handle, coop_sends);
  155. _starpu_mpi_coop_send_flush(coop_sends);
  156. }
  157. }
  158. /* Test whether a request is compatible with a cooperative send */
  159. static int _starpu_mpi_coop_send_compatible(struct _starpu_mpi_req *req, struct _starpu_mpi_coop_sends *coop_sends)
  160. {
  161. struct _starpu_mpi_req *prevreq;
  162. prevreq = _starpu_mpi_req_multilist_begin_coop_sends(&coop_sends->reqs);
  163. return /* we can cope with tag being different */
  164. prevreq->node_tag.comm == req->node_tag.comm
  165. && prevreq->sequential_consistency == req->sequential_consistency;
  166. }
  167. void _starpu_mpi_coop_send(starpu_data_handle_t data_handle, struct _starpu_mpi_req *req, enum starpu_data_access_mode mode, int sequential_consistency)
  168. {
  169. struct _starpu_mpi_data *mpi_data = _starpu_mpi_data_get(data_handle);
  170. struct _starpu_mpi_coop_sends *coop_sends = NULL, *tofree = NULL;
  171. int done = 0, queue, first = 1;
  172. /* Try to add ourself to something existing, otherwise create one. */
  173. while (!done)
  174. {
  175. _starpu_spin_lock(&mpi_data->coop_lock);
  176. if (mpi_data->coop_sends)
  177. {
  178. /* Already something, check we are coherent with it */
  179. queue = _starpu_mpi_coop_send_compatible(req, mpi_data->coop_sends);
  180. if (queue)
  181. {
  182. /* Yes, queue ourself there */
  183. if (coop_sends)
  184. {
  185. /* Remove ourself from what we created for ourself first */
  186. _starpu_mpi_req_multilist_erase_coop_sends(&coop_sends->reqs, req);
  187. tofree = coop_sends;
  188. }
  189. coop_sends = mpi_data->coop_sends;
  190. _STARPU_MPI_DEBUG(0, "%p: add to cooperative sends %p, dest %d\n", data_handle, coop_sends, req->node_tag.rank);
  191. _starpu_mpi_req_multilist_push_back_coop_sends(&coop_sends->reqs, req);
  192. coop_sends->n++;
  193. req->coop_sends_head = coop_sends;
  194. first = 0;
  195. done = 1;
  196. }
  197. else
  198. {
  199. /* Nope, incompatible, put ours instead */
  200. _STARPU_MPI_DEBUG(0, "%p: new cooperative sends %p, dest %d\n", data_handle, coop_sends, req->node_tag.rank);
  201. mpi_data->coop_sends = coop_sends;
  202. first = 1;
  203. _starpu_spin_unlock(&mpi_data->coop_lock);
  204. /* and flush it */
  205. _starpu_mpi_coop_send_flush(coop_sends);
  206. break;
  207. }
  208. }
  209. else if (coop_sends)
  210. {
  211. /* Nobody else and we have allocated one, we're first! */
  212. _STARPU_MPI_DEBUG(0, "%p: new cooperative sends %p, dest %d\n", data_handle, coop_sends, req->node_tag.rank);
  213. mpi_data->coop_sends = coop_sends;
  214. first = 1;
  215. done = 1;
  216. }
  217. _starpu_spin_unlock(&mpi_data->coop_lock);
  218. if (!done && !coop_sends)
  219. {
  220. /* Didn't find something to join, create one out of critical section */
  221. _STARPU_MPI_CALLOC(coop_sends, 1, sizeof(*coop_sends));
  222. coop_sends->redirects_sent = 0;
  223. coop_sends->n = 1;
  224. _starpu_mpi_req_multilist_head_init_coop_sends(&coop_sends->reqs);
  225. _starpu_mpi_req_multilist_push_back_coop_sends(&coop_sends->reqs, req);
  226. _starpu_spin_init(&coop_sends->lock);
  227. req->coop_sends_head = coop_sends;
  228. coop_sends->mpi_data = mpi_data;
  229. }
  230. /* We at worse do two iteration */
  231. STARPU_ASSERT(done || coop_sends);
  232. }
  233. /* In case we created one for nothing after all */
  234. free(tofree);
  235. if (first)
  236. {
  237. /* We were first, we are responsible for acquiring the data for everybody */
  238. starpu_data_acquire_on_node_cb_sequential_consistency_sync_jobids(req->data_handle, STARPU_MAIN_RAM, mode, _starpu_mpi_coop_sends_data_ready, coop_sends, sequential_consistency, 0, &req->pre_sync_jobid, NULL);
  239. }
  240. }