starpu_mpi.c 13 KB

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