starpu_mpi_coop_sends.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2021 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, req->node);
  47. }
  48. }
  49. else
  50. {
  51. /* Trivial request */
  52. starpu_data_release_on_node(req->data_handle, req->node);
  53. }
  54. }
  55. /* The data was acquired in terms of dependencies, we can now look the
  56. * current state of the handle and decide which node we prefer for the data
  57. * fetch */
  58. static void _starpu_mpi_coop_send_acquired_callback(void *arg, int *nodep, enum starpu_data_access_mode mode)
  59. {
  60. struct _starpu_mpi_coop_sends *coop_sends = arg;
  61. int node = *nodep;
  62. if (node < 0)
  63. node = _starpu_mpi_choose_node(coop_sends->data_handle, mode);
  64. /* Record the node in the first req */
  65. _starpu_mpi_req_multilist_begin_coop_sends(&coop_sends->reqs)->node = node;
  66. *nodep = node;
  67. }
  68. /* Comparison function for getting qsort to put requests with high priority first */
  69. static int _starpu_mpi_reqs_prio_compare(const void *a, const void *b)
  70. {
  71. const struct _starpu_mpi_req * const *ra = a;
  72. const struct _starpu_mpi_req * const *rb = b;
  73. if ((*rb)->prio < (*ra)->prio)
  74. return -1;
  75. else if ((*rb)->prio == (*ra)->prio)
  76. return 0;
  77. else
  78. return 1;
  79. }
  80. /* Sort the requests by priority and build a diffusion tree. Actually does something only once per coop_sends bag. */
  81. static void _starpu_mpi_coop_sends_optimize(struct _starpu_mpi_coop_sends *coop_sends)
  82. {
  83. STARPU_ASSERT(coop_sends->n > 1);
  84. _starpu_spin_lock(&coop_sends->lock);
  85. if (!coop_sends->reqs_array)
  86. {
  87. unsigned n = coop_sends->n, i;
  88. struct _starpu_mpi_req *cur;
  89. struct _starpu_mpi_req **reqs;
  90. _STARPU_MPI_DEBUG(0, "handling cooperative sends %p for %u neighbours\n", coop_sends, n);
  91. /* Store them in an array */
  92. _STARPU_CALLOC(reqs, n, sizeof(*reqs));
  93. for (cur = _starpu_mpi_req_multilist_begin_coop_sends(&coop_sends->reqs), i = 0;
  94. cur != _starpu_mpi_req_multilist_end_coop_sends(&coop_sends->reqs);
  95. cur = _starpu_mpi_req_multilist_next_coop_sends(cur), i++)
  96. reqs[i] = cur;
  97. coop_sends->reqs_array = reqs;
  98. /* Sort them */
  99. qsort(reqs, n, sizeof(*reqs), _starpu_mpi_reqs_prio_compare);
  100. #if 0
  101. /* And build the diffusion tree */
  102. _starpu_mpi_coop_sends_build_tree(coop_sends);
  103. #endif
  104. }
  105. _starpu_spin_unlock(&coop_sends->lock);
  106. }
  107. /* This is called on completion of acquisition of data for a cooperative send */
  108. static void _starpu_mpi_coop_sends_data_ready(void *arg)
  109. {
  110. _STARPU_MPI_LOG_IN();
  111. struct _starpu_mpi_coop_sends *coop_sends = arg;
  112. struct _starpu_mpi_data *mpi_data = coop_sends->mpi_data;
  113. struct _starpu_mpi_req *cur;
  114. unsigned node;
  115. /* Take the cooperative send bag out from more submissions */
  116. if (mpi_data->coop_sends == coop_sends)
  117. {
  118. _starpu_spin_lock(&mpi_data->coop_lock);
  119. if (mpi_data->coop_sends == coop_sends)
  120. mpi_data->coop_sends = NULL;
  121. _starpu_spin_unlock(&mpi_data->coop_lock);
  122. }
  123. /* Copy over the memory node number */
  124. cur = _starpu_mpi_req_multilist_begin_coop_sends(&coop_sends->reqs);
  125. node = cur->node;
  126. for ( ;
  127. cur != _starpu_mpi_req_multilist_end_coop_sends(&coop_sends->reqs);
  128. cur = _starpu_mpi_req_multilist_next_coop_sends(cur))
  129. cur->node = node;
  130. if (coop_sends->n == 1)
  131. {
  132. /* Trivial case, just submit it */
  133. _starpu_mpi_submit_ready_request(_starpu_mpi_req_multilist_begin_coop_sends(&coop_sends->reqs));
  134. }
  135. else
  136. {
  137. /* Build diffusion tree */
  138. _starpu_mpi_coop_sends_optimize(coop_sends);
  139. /* And submit them */
  140. if (STARPU_TEST_AND_SET(&coop_sends->redirects_sent, 1) == 0)
  141. _starpu_mpi_submit_coop_sends(coop_sends, 1, 1);
  142. else
  143. _starpu_mpi_submit_coop_sends(coop_sends, 0, 1);
  144. }
  145. _STARPU_MPI_LOG_OUT();
  146. }
  147. /* This is called when we want to stop including new members in a cooperative send,
  148. * either because we know there won't be any other members due to the algorithm
  149. * or because the value has changed. */
  150. static void _starpu_mpi_coop_send_flush(struct _starpu_mpi_coop_sends *coop_sends)
  151. {
  152. if (!coop_sends || coop_sends->n == 1)
  153. return;
  154. /* Build diffusion tree */
  155. _starpu_mpi_coop_sends_optimize(coop_sends);
  156. /* And submit them */
  157. if (STARPU_TEST_AND_SET(&coop_sends->redirects_sent, 1) == 0)
  158. _starpu_mpi_submit_coop_sends(coop_sends, 1, 0);
  159. }
  160. /* This is called when a write to the data was just submitted, which means we
  161. * can't make future sends cooperate with past sends since it's not the same value
  162. */
  163. void _starpu_mpi_data_flush(starpu_data_handle_t data_handle)
  164. {
  165. struct _starpu_mpi_data *mpi_data = data_handle->mpi_data;
  166. struct _starpu_mpi_coop_sends *coop_sends;
  167. if (!mpi_data)
  168. return;
  169. _starpu_spin_lock(&mpi_data->coop_lock);
  170. coop_sends = mpi_data->coop_sends;
  171. if (coop_sends)
  172. mpi_data->coop_sends = NULL;
  173. _starpu_spin_unlock(&mpi_data->coop_lock);
  174. if (coop_sends)
  175. {
  176. _STARPU_MPI_DEBUG(0, "%p: data written to, flush cooperative sends %p\n", data_handle, coop_sends);
  177. _starpu_mpi_coop_send_flush(coop_sends);
  178. }
  179. }
  180. /* Test whether a request is compatible with a cooperative send */
  181. static int _starpu_mpi_coop_send_compatible(struct _starpu_mpi_req *req, struct _starpu_mpi_coop_sends *coop_sends)
  182. {
  183. struct _starpu_mpi_req *prevreq;
  184. prevreq = _starpu_mpi_req_multilist_begin_coop_sends(&coop_sends->reqs);
  185. return /* we can cope with tag being different */
  186. prevreq->node_tag.node.comm == req->node_tag.node.comm
  187. && prevreq->sequential_consistency == req->sequential_consistency;
  188. }
  189. 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)
  190. {
  191. struct _starpu_mpi_data *mpi_data = _starpu_mpi_data_get(data_handle);
  192. struct _starpu_mpi_coop_sends *coop_sends = NULL, *tofree = NULL;
  193. int done = 0, queue, first = 1;
  194. /* Try to add ourself to something existing, otherwise create one. */
  195. while (!done)
  196. {
  197. _starpu_spin_lock(&mpi_data->coop_lock);
  198. if (mpi_data->coop_sends)
  199. {
  200. /* Already something, check we are coherent with it */
  201. queue = _starpu_mpi_coop_send_compatible(req, mpi_data->coop_sends);
  202. if (queue)
  203. {
  204. /* Yes, queue ourself there */
  205. if (coop_sends)
  206. {
  207. /* Remove ourself from what we created for ourself first */
  208. _starpu_mpi_req_multilist_erase_coop_sends(&coop_sends->reqs, req);
  209. tofree = coop_sends;
  210. }
  211. coop_sends = mpi_data->coop_sends;
  212. _STARPU_MPI_DEBUG(0, "%p: add to cooperative sends %p, dest %d\n", data_handle, coop_sends, req->node_tag.node.rank);
  213. /* Get the pre_sync_jobid of the first send request, to build a coherent DAG in the traces: */
  214. struct _starpu_mpi_req *firstreq;
  215. firstreq = _starpu_mpi_req_multilist_begin_coop_sends(&coop_sends->reqs);
  216. req->pre_sync_jobid = firstreq->pre_sync_jobid;
  217. _starpu_mpi_req_multilist_push_back_coop_sends(&coop_sends->reqs, req);
  218. coop_sends->n++;
  219. req->coop_sends_head = coop_sends;
  220. first = 0;
  221. done = 1;
  222. }
  223. else
  224. {
  225. /* Nope, incompatible, put ours instead */
  226. _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);
  227. mpi_data->coop_sends = coop_sends;
  228. first = 1;
  229. _starpu_spin_unlock(&mpi_data->coop_lock);
  230. /* and flush it */
  231. _starpu_mpi_coop_send_flush(coop_sends);
  232. break;
  233. }
  234. }
  235. else if (coop_sends)
  236. {
  237. /* Nobody else and we have allocated one, we're first! */
  238. _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);
  239. mpi_data->coop_sends = coop_sends;
  240. first = 1;
  241. done = 1;
  242. }
  243. _starpu_spin_unlock(&mpi_data->coop_lock);
  244. if (!done && !coop_sends)
  245. {
  246. /* Didn't find something to join, create one out of critical section */
  247. _STARPU_MPI_CALLOC(coop_sends, 1, sizeof(*coop_sends));
  248. coop_sends->data_handle = data_handle;
  249. coop_sends->redirects_sent = 0;
  250. coop_sends->n = 1;
  251. _starpu_mpi_req_multilist_head_init_coop_sends(&coop_sends->reqs);
  252. _starpu_mpi_req_multilist_push_back_coop_sends(&coop_sends->reqs, req);
  253. _starpu_spin_init(&coop_sends->lock);
  254. req->coop_sends_head = coop_sends;
  255. coop_sends->mpi_data = mpi_data;
  256. }
  257. /* We at worse do two iteration */
  258. STARPU_ASSERT(done || coop_sends);
  259. }
  260. /* In case we created one for nothing after all */
  261. free(tofree);
  262. if (first)
  263. /* We were first, we are responsible for acquiring the data for everybody */
  264. starpu_data_acquire_on_node_cb_sequential_consistency_sync_jobids(req->data_handle, -1, mode, _starpu_mpi_coop_send_acquired_callback, _starpu_mpi_coop_sends_data_ready, coop_sends, sequential_consistency, 0, &coop_sends->pre_sync_jobid, NULL, req->prio);
  265. else
  266. req->pre_sync_jobid = coop_sends->pre_sync_jobid;
  267. }