starpu_mpi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012,2013,2016,2017 Inria
  4. * Copyright (C) 2010-2019 CNRS
  5. * Copyright (C) 2009-2018 Université de Bordeaux
  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 <stdlib.h>
  19. #include <limits.h>
  20. #include <starpu_mpi.h>
  21. #include <starpu_mpi_datatype.h>
  22. #include <starpu_mpi_private.h>
  23. #include <starpu_mpi_cache.h>
  24. #include <starpu_profiling.h>
  25. #include <starpu_mpi_stats.h>
  26. #include <starpu_mpi_cache.h>
  27. #include <starpu_mpi_select_node.h>
  28. #include <starpu_mpi_init.h>
  29. #include <common/config.h>
  30. #include <common/thread.h>
  31. #include <datawizard/interfaces/data_interface.h>
  32. #include <datawizard/coherency.h>
  33. #include <core/simgrid.h>
  34. #include <core/task.h>
  35. #include <core/topology.h>
  36. #include <core/workers.h>
  37. static void _starpu_mpi_isend_irecv_common(struct _starpu_mpi_req *req, enum starpu_data_access_mode mode, int sequential_consistency)
  38. {
  39. /* Asynchronously request StarPU to fetch the data in main memory: when
  40. * it is available in main memory, _starpu_mpi_submit_ready_request(req) is called and
  41. * the request is actually submitted */
  42. starpu_data_acquire_on_node_cb_sequential_consistency_sync_jobids(req->data_handle, STARPU_MAIN_RAM, mode, _starpu_mpi_submit_ready_request, (void *)req, sequential_consistency, 1, &req->pre_sync_jobid, &req->post_sync_jobid);
  43. }
  44. static struct _starpu_mpi_req *_starpu_mpi_isend_common(starpu_data_handle_t data_handle, int dest, starpu_mpi_tag_t data_tag, MPI_Comm comm, unsigned detached, unsigned sync, int prio, void (*callback)(void *), void *arg, int sequential_consistency)
  45. {
  46. if (_starpu_mpi_fake_world_size != -1)
  47. {
  48. /* Don't actually do the communication */
  49. return NULL;
  50. }
  51. #ifdef STARPU_MPI_PEDANTIC_ISEND
  52. enum starpu_data_access_mode mode = STARPU_RW;
  53. #else
  54. enum starpu_data_access_mode mode = STARPU_R;
  55. #endif
  56. struct _starpu_mpi_req *req = _starpu_mpi_request_fill(data_handle, dest, data_tag, comm, detached, sync, prio, callback, arg, SEND_REQ, _starpu_mpi_isend_size_func, sequential_consistency, 0, 0);
  57. _starpu_mpi_req_willpost(req);
  58. if (_starpu_mpi_use_coop_sends && detached == 1 && sync == 0 && callback == NULL)
  59. {
  60. /* It's a send & forget send, we can perhaps optimize its distribution over several nodes */
  61. _starpu_mpi_coop_send(data_handle, req, mode, sequential_consistency);
  62. return req;
  63. }
  64. /* Post normally */
  65. _starpu_mpi_isend_irecv_common(req, mode, sequential_consistency);
  66. return req;
  67. }
  68. int starpu_mpi_isend_prio(starpu_data_handle_t data_handle, starpu_mpi_req *public_req, int dest, starpu_mpi_tag_t data_tag, int prio, MPI_Comm comm)
  69. {
  70. _STARPU_MPI_LOG_IN();
  71. STARPU_MPI_ASSERT_MSG(public_req, "starpu_mpi_isend needs a valid starpu_mpi_req");
  72. struct _starpu_mpi_req *req;
  73. _STARPU_MPI_TRACE_ISEND_COMPLETE_BEGIN(dest, data_tag, 0);
  74. req = _starpu_mpi_isend_common(data_handle, dest, data_tag, comm, 0, 0, prio, NULL, NULL, 1);
  75. _STARPU_MPI_TRACE_ISEND_COMPLETE_END(dest, data_tag, 0);
  76. STARPU_MPI_ASSERT_MSG(req, "Invalid return for _starpu_mpi_isend_common");
  77. *public_req = req;
  78. _STARPU_MPI_LOG_OUT();
  79. return 0;
  80. }
  81. int starpu_mpi_isend(starpu_data_handle_t data_handle, starpu_mpi_req *public_req, int dest, starpu_mpi_tag_t data_tag, MPI_Comm comm)
  82. {
  83. return starpu_mpi_isend_prio(data_handle, public_req, dest, data_tag, 0, comm);
  84. }
  85. int starpu_mpi_isend_detached_prio(starpu_data_handle_t data_handle, int dest, starpu_mpi_tag_t data_tag, int prio, MPI_Comm comm, void (*callback)(void *), void *arg)
  86. {
  87. _STARPU_MPI_LOG_IN();
  88. _starpu_mpi_isend_common(data_handle, dest, data_tag, comm, 1, 0, prio, callback, arg, 1);
  89. _STARPU_MPI_LOG_OUT();
  90. return 0;
  91. }
  92. int starpu_mpi_isend_detached(starpu_data_handle_t data_handle, int dest, starpu_mpi_tag_t data_tag, MPI_Comm comm, void (*callback)(void *), void *arg)
  93. {
  94. return starpu_mpi_isend_detached_prio(data_handle, dest, data_tag, 0, comm, callback, arg);
  95. }
  96. int starpu_mpi_send_prio(starpu_data_handle_t data_handle, int dest, starpu_mpi_tag_t data_tag, int prio, MPI_Comm comm)
  97. {
  98. starpu_mpi_req req;
  99. MPI_Status status;
  100. _STARPU_MPI_LOG_IN();
  101. memset(&status, 0, sizeof(MPI_Status));
  102. starpu_mpi_isend_prio(data_handle, &req, dest, data_tag, prio, comm);
  103. starpu_mpi_wait(&req, &status);
  104. _STARPU_MPI_LOG_OUT();
  105. return 0;
  106. }
  107. int starpu_mpi_send(starpu_data_handle_t data_handle, int dest, starpu_mpi_tag_t data_tag, MPI_Comm comm)
  108. {
  109. return starpu_mpi_send_prio(data_handle, dest, data_tag, 0, comm);
  110. }
  111. int starpu_mpi_issend_prio(starpu_data_handle_t data_handle, starpu_mpi_req *public_req, int dest, starpu_mpi_tag_t data_tag, int prio, MPI_Comm comm)
  112. {
  113. _STARPU_MPI_LOG_IN();
  114. STARPU_MPI_ASSERT_MSG(public_req, "starpu_mpi_issend needs a valid starpu_mpi_req");
  115. struct _starpu_mpi_req *req;
  116. req = _starpu_mpi_isend_common(data_handle, dest, data_tag, comm, 0, 1, prio, NULL, NULL, 1);
  117. STARPU_MPI_ASSERT_MSG(req, "Invalid return for _starpu_mpi_isend_common");
  118. *public_req = req;
  119. _STARPU_MPI_LOG_OUT();
  120. return 0;
  121. }
  122. int starpu_mpi_issend(starpu_data_handle_t data_handle, starpu_mpi_req *public_req, int dest, starpu_mpi_tag_t data_tag, MPI_Comm comm)
  123. {
  124. return starpu_mpi_issend_prio(data_handle, public_req, dest, data_tag, 0, comm);
  125. }
  126. int starpu_mpi_issend_detached_prio(starpu_data_handle_t data_handle, int dest, starpu_mpi_tag_t data_tag, int prio, MPI_Comm comm, void (*callback)(void *), void *arg)
  127. {
  128. _STARPU_MPI_LOG_IN();
  129. _starpu_mpi_isend_common(data_handle, dest, data_tag, comm, 1, 1, prio, callback, arg, 1);
  130. _STARPU_MPI_LOG_OUT();
  131. return 0;
  132. }
  133. int starpu_mpi_issend_detached(starpu_data_handle_t data_handle, int dest, starpu_mpi_tag_t data_tag, MPI_Comm comm, void (*callback)(void *), void *arg)
  134. {
  135. return starpu_mpi_issend_detached_prio(data_handle, dest, data_tag, 0, comm, callback, arg);
  136. }
  137. struct _starpu_mpi_req *_starpu_mpi_irecv_common(starpu_data_handle_t data_handle, int source, starpu_mpi_tag_t data_tag, MPI_Comm comm, unsigned detached, unsigned sync, void (*callback)(void *), void *arg, int sequential_consistency, int is_internal_req, starpu_ssize_t count)
  138. {
  139. if (_starpu_mpi_fake_world_size != -1)
  140. {
  141. /* Don't actually do the communication */
  142. return NULL;
  143. }
  144. struct _starpu_mpi_req *req = _starpu_mpi_request_fill(data_handle, source, data_tag, comm, detached, sync, 0, callback, arg, RECV_REQ, _starpu_mpi_irecv_size_func, sequential_consistency, is_internal_req, count);
  145. _starpu_mpi_req_willpost(req);
  146. _starpu_mpi_isend_irecv_common(req, STARPU_W, sequential_consistency);
  147. return req;
  148. }
  149. int starpu_mpi_irecv(starpu_data_handle_t data_handle, starpu_mpi_req *public_req, int source, starpu_mpi_tag_t data_tag, MPI_Comm comm)
  150. {
  151. _STARPU_MPI_LOG_IN();
  152. STARPU_MPI_ASSERT_MSG(public_req, "starpu_mpi_irecv needs a valid starpu_mpi_req");
  153. struct _starpu_mpi_req *req;
  154. _STARPU_MPI_TRACE_IRECV_COMPLETE_BEGIN(source, data_tag);
  155. req = _starpu_mpi_irecv_common(data_handle, source, data_tag, comm, 0, 0, NULL, NULL, 1, 0, 0);
  156. _STARPU_MPI_TRACE_IRECV_COMPLETE_END(source, data_tag);
  157. STARPU_MPI_ASSERT_MSG(req, "Invalid return for _starpu_mpi_irecv_common");
  158. *public_req = req;
  159. _STARPU_MPI_LOG_OUT();
  160. return 0;
  161. }
  162. int starpu_mpi_irecv_detached(starpu_data_handle_t data_handle, int source, starpu_mpi_tag_t data_tag, MPI_Comm comm, void (*callback)(void *), void *arg)
  163. {
  164. _STARPU_MPI_LOG_IN();
  165. _starpu_mpi_irecv_common(data_handle, source, data_tag, comm, 1, 0, callback, arg, 1, 0, 0);
  166. _STARPU_MPI_LOG_OUT();
  167. return 0;
  168. }
  169. int starpu_mpi_irecv_detached_sequential_consistency(starpu_data_handle_t data_handle, int source, starpu_mpi_tag_t data_tag, MPI_Comm comm, void (*callback)(void *), void *arg, int sequential_consistency)
  170. {
  171. _STARPU_MPI_LOG_IN();
  172. _starpu_mpi_irecv_common(data_handle, source, data_tag, comm, 1, 0, callback, arg, sequential_consistency, 0, 0);
  173. _STARPU_MPI_LOG_OUT();
  174. return 0;
  175. }
  176. int starpu_mpi_recv(starpu_data_handle_t data_handle, int source, starpu_mpi_tag_t data_tag, MPI_Comm comm, MPI_Status *status)
  177. {
  178. starpu_mpi_req req;
  179. _STARPU_MPI_LOG_IN();
  180. starpu_mpi_irecv(data_handle, &req, source, data_tag, comm);
  181. starpu_mpi_wait(&req, status);
  182. _STARPU_MPI_LOG_OUT();
  183. return 0;
  184. }
  185. int starpu_mpi_wait(starpu_mpi_req *public_req, MPI_Status *status)
  186. {
  187. return _starpu_mpi_wait(public_req, status);
  188. }
  189. int starpu_mpi_test(starpu_mpi_req *public_req, int *flag, MPI_Status *status)
  190. {
  191. return _starpu_mpi_test(public_req, flag, status);
  192. }
  193. int starpu_mpi_barrier(MPI_Comm comm)
  194. {
  195. return _starpu_mpi_barrier(comm);
  196. }
  197. void _starpu_mpi_data_clear(starpu_data_handle_t data_handle)
  198. {
  199. _mpi_backend._starpu_mpi_backend_data_clear(data_handle);
  200. _starpu_mpi_cache_data_clear(data_handle);
  201. free(data_handle->mpi_data);
  202. data_handle->mpi_data = NULL;
  203. }
  204. struct _starpu_mpi_data *_starpu_mpi_data_get(starpu_data_handle_t data_handle)
  205. {
  206. struct _starpu_mpi_data *mpi_data = data_handle->mpi_data;
  207. if (mpi_data)
  208. {
  209. STARPU_ASSERT(mpi_data->magic == 42);
  210. }
  211. else
  212. {
  213. _STARPU_CALLOC(mpi_data, 1, sizeof(struct _starpu_mpi_data));
  214. mpi_data->magic = 42;
  215. mpi_data->node_tag.data_tag = -1;
  216. mpi_data->node_tag.node.rank = -1;
  217. mpi_data->node_tag.node.comm = MPI_COMM_WORLD;
  218. _starpu_spin_init(&mpi_data->coop_lock);
  219. data_handle->mpi_data = mpi_data;
  220. _starpu_mpi_cache_data_init(data_handle);
  221. _starpu_data_set_unregister_hook(data_handle, _starpu_mpi_data_clear);
  222. }
  223. return mpi_data;
  224. }
  225. void starpu_mpi_data_register_comm(starpu_data_handle_t data_handle, starpu_mpi_tag_t data_tag, int rank, MPI_Comm comm)
  226. {
  227. struct _starpu_mpi_data *mpi_data = _starpu_mpi_data_get(data_handle);
  228. if (data_tag != -1)
  229. {
  230. _mpi_backend._starpu_mpi_backend_data_register(data_handle, data_tag);
  231. mpi_data->node_tag.data_tag = data_tag;
  232. }
  233. if (rank != -1)
  234. {
  235. _STARPU_MPI_TRACE_DATA_SET_RANK(data_handle, rank);
  236. mpi_data->node_tag.node.rank = rank;
  237. mpi_data->node_tag.node.comm = comm;
  238. }
  239. }
  240. void starpu_mpi_data_set_rank_comm(starpu_data_handle_t handle, int rank, MPI_Comm comm)
  241. {
  242. starpu_mpi_data_register_comm(handle, -1, rank, comm);
  243. }
  244. void starpu_mpi_data_set_tag(starpu_data_handle_t handle, starpu_mpi_tag_t data_tag)
  245. {
  246. starpu_mpi_data_register_comm(handle, data_tag, -1, MPI_COMM_WORLD);
  247. }
  248. int starpu_mpi_data_get_rank(starpu_data_handle_t data)
  249. {
  250. STARPU_ASSERT_MSG(data->mpi_data, "starpu_mpi_data_register MUST be called for data %p\n", data);
  251. return ((struct _starpu_mpi_data *)(data->mpi_data))->node_tag.node.rank;
  252. }
  253. starpu_mpi_tag_t starpu_mpi_data_get_tag(starpu_data_handle_t data)
  254. {
  255. STARPU_ASSERT_MSG(data->mpi_data, "starpu_mpi_data_register MUST be called for data %p\n", data);
  256. return ((struct _starpu_mpi_data *)(data->mpi_data))->node_tag.data_tag;
  257. }
  258. void starpu_mpi_get_data_on_node_detached(MPI_Comm comm, starpu_data_handle_t data_handle, int node, void (*callback)(void*), void *arg)
  259. {
  260. int me, rank, tag;
  261. rank = starpu_mpi_data_get_rank(data_handle);
  262. if (rank == -1)
  263. {
  264. _STARPU_ERROR("StarPU needs to be told the MPI rank of this data, using starpu_mpi_data_register() or starpu_mpi_data_register()\n");
  265. }
  266. starpu_mpi_comm_rank(comm, &me);
  267. if (node == rank)
  268. return;
  269. tag = starpu_mpi_data_get_tag(data_handle);
  270. if (tag == -1)
  271. {
  272. _STARPU_ERROR("StarPU needs to be told the MPI tag of this data, using starpu_mpi_data_register() or starpu_mpi_data_register()\n");
  273. }
  274. if (me == node)
  275. {
  276. _STARPU_MPI_DEBUG(1, "Migrating data %p from %d to %d\n", data_handle, rank, node);
  277. int already_received = _starpu_mpi_cache_received_data_set(data_handle);
  278. if (already_received == 0)
  279. {
  280. _STARPU_MPI_DEBUG(1, "Receiving data %p from %d\n", data_handle, rank);
  281. starpu_mpi_irecv_detached(data_handle, rank, tag, comm, callback, arg);
  282. }
  283. }
  284. else if (me == rank)
  285. {
  286. _STARPU_MPI_DEBUG(1, "Migrating data %p from %d to %d\n", data_handle, rank, node);
  287. int already_sent = _starpu_mpi_cache_sent_data_set(data_handle, node);
  288. if (already_sent == 0)
  289. {
  290. _STARPU_MPI_DEBUG(1, "Sending data %p to %d\n", data_handle, node);
  291. starpu_mpi_isend_detached(data_handle, node, tag, comm, NULL, NULL);
  292. }
  293. }
  294. }
  295. void starpu_mpi_get_data_on_node(MPI_Comm comm, starpu_data_handle_t data_handle, int node)
  296. {
  297. int me, rank, tag;
  298. rank = starpu_mpi_data_get_rank(data_handle);
  299. if (rank == -1)
  300. {
  301. _STARPU_ERROR("StarPU needs to be told the MPI rank of this data, using starpu_mpi_data_register\n");
  302. }
  303. starpu_mpi_comm_rank(comm, &me);
  304. if (node == rank)
  305. return;
  306. tag = starpu_mpi_data_get_tag(data_handle);
  307. if (tag == -1)
  308. {
  309. _STARPU_ERROR("StarPU needs to be told the MPI tag of this data, using starpu_mpi_data_register\n");
  310. }
  311. if (me == node)
  312. {
  313. MPI_Status status;
  314. _STARPU_MPI_DEBUG(1, "Migrating data %p from %d to %d\n", data_handle, rank, node);
  315. int already_received = _starpu_mpi_cache_received_data_set(data_handle);
  316. if (already_received == 0)
  317. {
  318. _STARPU_MPI_DEBUG(1, "Receiving data %p from %d\n", data_handle, rank);
  319. starpu_mpi_recv(data_handle, rank, tag, comm, &status);
  320. }
  321. }
  322. else if (me == rank)
  323. {
  324. _STARPU_MPI_DEBUG(1, "Migrating data %p from %d to %d\n", data_handle, rank, node);
  325. int already_sent = _starpu_mpi_cache_sent_data_set(data_handle, node);
  326. if (already_sent == 0)
  327. {
  328. _STARPU_MPI_DEBUG(1, "Sending data %p to %d\n", data_handle, node);
  329. starpu_mpi_send(data_handle, node, tag, comm);
  330. }
  331. }
  332. }
  333. void starpu_mpi_get_data_on_all_nodes_detached(MPI_Comm comm, starpu_data_handle_t data_handle)
  334. {
  335. int size, i;
  336. starpu_mpi_comm_size(comm, &size);
  337. for (i = 0; i < size; i++)
  338. starpu_mpi_get_data_on_node_detached(comm, data_handle, i, NULL, NULL);
  339. }
  340. void starpu_mpi_data_migrate(MPI_Comm comm, starpu_data_handle_t data, int new_rank)
  341. {
  342. int old_rank = starpu_mpi_data_get_rank(data);
  343. if (new_rank == old_rank)
  344. /* Already there */
  345. return;
  346. /* First submit data migration if it's not already on destination */
  347. starpu_mpi_get_data_on_node_detached(comm, data, new_rank, NULL, NULL);
  348. /* And note new owner */
  349. starpu_mpi_data_set_rank_comm(data, new_rank, comm);
  350. /* Flush cache in all other nodes */
  351. /* TODO: Ideally we'd transmit the knowledge of who owns it */
  352. starpu_mpi_cache_flush(comm, data);
  353. return;
  354. }
  355. int starpu_mpi_wait_for_all(MPI_Comm comm)
  356. {
  357. starpu_task_wait_for_all();
  358. starpu_mpi_barrier(comm);
  359. return 0;
  360. }