starpu_mpi_coop_sends.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu_mpi.h>
  17. #include <starpu_mpi_private.h>
  18. #include <datawizard/coherency.h>
  19. /*
  20. * One node sends the same data to several nodes. Gather them into a
  21. * "coop_sends", which then has a global view of all the required sends, and can
  22. * establish a diffusion tree by telling receiving nodes to retransmit what they
  23. * received (forwards) to others, and to others that they will receive from the
  24. * former (redirects).
  25. */
  26. /* This is called after a request is finished processing, to release the data */
  27. void _starpu_mpi_release_req_data(struct _starpu_mpi_req *req)
  28. {
  29. if (!req->data_handle)
  30. return;
  31. if (_starpu_mpi_req_multilist_queued_coop_sends(req))
  32. {
  33. struct _starpu_mpi_coop_sends *coop_sends = req->coop_sends_head;
  34. struct _starpu_mpi_data *mpi_data = coop_sends->mpi_data;
  35. int last;
  36. _starpu_spin_lock(&mpi_data->coop_lock);
  37. /* Part of a cooperative send, dequeue ourself from others */
  38. _starpu_mpi_req_multilist_erase_coop_sends(&coop_sends->reqs, req);
  39. last = _starpu_mpi_req_multilist_empty_coop_sends(&coop_sends->reqs);
  40. _starpu_spin_unlock(&mpi_data->coop_lock);
  41. if (last)
  42. {
  43. /* We were last, release data */
  44. free(coop_sends->reqs_array);
  45. free(coop_sends);
  46. starpu_data_release_on_node(req->data_handle, STARPU_MAIN_RAM);
  47. }
  48. }
  49. else
  50. {
  51. /* Trivial request */
  52. starpu_data_release_on_node(req->data_handle, STARPU_MAIN_RAM);
  53. }
  54. }
  55. /* Comparison function for getting qsort to put requests with high priority first */
  56. static int _starpu_mpi_reqs_prio_compare(const void *a, const void *b)
  57. {
  58. const struct _starpu_mpi_req * const *ra = a;
  59. const struct _starpu_mpi_req * const *rb = b;
  60. if ((*rb)->prio < (*ra)->prio)
  61. return -1;
  62. else if ((*rb)->prio == (*ra)->prio)
  63. return 0;
  64. else
  65. return 1;
  66. }
  67. /* Sort the requests by priority and build a diffusion tree. Actually does something only once per coop_sends bag. */
  68. static void _starpu_mpi_coop_sends_optimize(struct _starpu_mpi_coop_sends *coop_sends)
  69. {
  70. STARPU_ASSERT(coop_sends->n > 1);
  71. _starpu_spin_lock(&coop_sends->lock);
  72. if (!coop_sends->reqs_array)
  73. {
  74. unsigned n = coop_sends->n, i;
  75. struct _starpu_mpi_req *cur;
  76. struct _starpu_mpi_req **reqs;
  77. _STARPU_MPI_DEBUG(0, "handling cooperative sends %p for %u neighbours\n", coop_sends, n);
  78. /* Store them in an array */
  79. _STARPU_CALLOC(reqs, n, sizeof(*reqs));
  80. for (cur = _starpu_mpi_req_multilist_begin_coop_sends(&coop_sends->reqs), i = 0;
  81. cur != _starpu_mpi_req_multilist_end_coop_sends(&coop_sends->reqs);
  82. cur = _starpu_mpi_req_multilist_next_coop_sends(cur), i++)
  83. reqs[i] = cur;
  84. coop_sends->reqs_array = reqs;
  85. /* Sort them */
  86. qsort(reqs, n, sizeof(*reqs), _starpu_mpi_reqs_prio_compare);
  87. #if 0
  88. /* And build the diffusion tree */
  89. _starpu_mpi_coop_sends_build_tree(coop_sends);
  90. #endif
  91. }
  92. _starpu_spin_unlock(&coop_sends->lock);
  93. }
  94. /* This is called on completion of acquisition of data for a cooperative send */
  95. static void _starpu_mpi_coop_sends_data_ready(void *arg)
  96. {
  97. _STARPU_MPI_LOG_IN();
  98. struct _starpu_mpi_coop_sends *coop_sends = arg;
  99. struct _starpu_mpi_data *mpi_data = coop_sends->mpi_data;
  100. /* Take the cooperative send bag out from more submissions */
  101. if (mpi_data->coop_sends == coop_sends)
  102. {
  103. _starpu_spin_lock(&mpi_data->coop_lock);
  104. if (mpi_data->coop_sends == coop_sends)
  105. mpi_data->coop_sends = NULL;
  106. _starpu_spin_unlock(&mpi_data->coop_lock);
  107. }
  108. if (coop_sends->n == 1)
  109. {
  110. /* Trivial case, just submit it */
  111. _starpu_mpi_submit_ready_request(_starpu_mpi_req_multilist_begin_coop_sends(&coop_sends->reqs));
  112. }
  113. else
  114. {
  115. /* Build diffusion tree */
  116. _starpu_mpi_coop_sends_optimize(coop_sends);
  117. /* And submit them */
  118. if (STARPU_TEST_AND_SET(&coop_sends->redirects_sent, 1) == 0)
  119. _starpu_mpi_submit_coop_sends(coop_sends, 1, 1);
  120. else
  121. _starpu_mpi_submit_coop_sends(coop_sends, 0, 1);
  122. }
  123. _STARPU_MPI_LOG_OUT();
  124. }
  125. /* This is called when we want to stop including new members in a cooperative send,
  126. * either because we know there won't be any other members due to the algorithm
  127. * or because the value has changed. */
  128. static void _starpu_mpi_coop_send_flush(struct _starpu_mpi_coop_sends *coop_sends)
  129. {
  130. if (!coop_sends || coop_sends->n == 1)
  131. return;
  132. /* Build diffusion tree */
  133. _starpu_mpi_coop_sends_optimize(coop_sends);
  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.node.comm == req->node_tag.node.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.node.rank);
  191. /* Get the pre_sync_jobid of the first send request, to build a coherent DAG in the traces: */
  192. struct _starpu_mpi_req *firstreq;
  193. firstreq = _starpu_mpi_req_multilist_begin_coop_sends(&coop_sends->reqs);
  194. req->pre_sync_jobid = firstreq->pre_sync_jobid;
  195. _starpu_mpi_req_multilist_push_back_coop_sends(&coop_sends->reqs, req);
  196. coop_sends->n++;
  197. req->coop_sends_head = coop_sends;
  198. first = 0;
  199. done = 1;
  200. }
  201. else
  202. {
  203. /* Nope, incompatible, put ours instead */
  204. _STARPU_MPI_DEBUG(0, "%p: new cooperative sends %p for tag %"PRIi64", dest %d\n", data_handle, coop_sends, req->node_tag.data_tag, req->node_tag.node.rank);
  205. mpi_data->coop_sends = coop_sends;
  206. first = 1;
  207. _starpu_spin_unlock(&mpi_data->coop_lock);
  208. /* and flush it */
  209. _starpu_mpi_coop_send_flush(coop_sends);
  210. break;
  211. }
  212. }
  213. else if (coop_sends)
  214. {
  215. /* Nobody else and we have allocated one, we're first! */
  216. _STARPU_MPI_DEBUG(0, "%p: new cooperative sends %p for tag %"PRIi64", dest %d\n", data_handle, coop_sends, req->node_tag.data_tag, req->node_tag.node.rank);
  217. mpi_data->coop_sends = coop_sends;
  218. first = 1;
  219. done = 1;
  220. }
  221. _starpu_spin_unlock(&mpi_data->coop_lock);
  222. if (!done && !coop_sends)
  223. {
  224. /* Didn't find something to join, create one out of critical section */
  225. _STARPU_MPI_CALLOC(coop_sends, 1, sizeof(*coop_sends));
  226. coop_sends->redirects_sent = 0;
  227. coop_sends->n = 1;
  228. _starpu_mpi_req_multilist_head_init_coop_sends(&coop_sends->reqs);
  229. _starpu_mpi_req_multilist_push_back_coop_sends(&coop_sends->reqs, req);
  230. _starpu_spin_init(&coop_sends->lock);
  231. req->coop_sends_head = coop_sends;
  232. coop_sends->mpi_data = mpi_data;
  233. }
  234. /* We at worse do two iteration */
  235. STARPU_ASSERT(done || coop_sends);
  236. }
  237. /* In case we created one for nothing after all */
  238. free(tofree);
  239. if (first)
  240. {
  241. /* We were first, we are responsible for acquiring the data for everybody */
  242. 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);
  243. }
  244. }