starpu_mpi_task_insert.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012, 2013, 2014, 2015 CNRS
  4. * Copyright (C) 2011-2016 Université de Bordeaux
  5. * Copyright (C) 2014 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 <stdarg.h>
  19. #include <mpi.h>
  20. #include <starpu.h>
  21. #include <starpu_data.h>
  22. #include <common/utils.h>
  23. #include <util/starpu_task_insert_utils.h>
  24. #include <datawizard/coherency.h>
  25. #include <core/task.h>
  26. #include <starpu_mpi_private.h>
  27. #include <starpu_mpi_cache.h>
  28. #include <starpu_mpi_select_node.h>
  29. #define _SEND_DATA(data, mode, dest, data_tag, comm, callback, arg) \
  30. if (mode & STARPU_SSEND) \
  31. starpu_mpi_issend_detached(data, dest, data_tag, comm, callback, arg); \
  32. else \
  33. starpu_mpi_isend_detached(data, dest, data_tag, comm, callback, arg);
  34. static
  35. int _starpu_mpi_find_executee_node(starpu_data_handle_t data, enum starpu_data_access_mode mode, int me, int *do_execute, int *inconsistent_execute, int *xrank)
  36. {
  37. if (mode & STARPU_W)
  38. {
  39. if (!data)
  40. {
  41. /* We don't have anything allocated for this.
  42. * The application knows we won't do anything
  43. * about this task */
  44. /* Yes, the app could actually not call
  45. * task_insert at all itself, this is just a
  46. * safeguard. */
  47. _STARPU_MPI_DEBUG(3, "oh oh\n");
  48. _STARPU_MPI_LOG_OUT();
  49. return -EINVAL;
  50. }
  51. int mpi_rank = starpu_mpi_data_get_rank(data);
  52. if (mpi_rank == -1)
  53. {
  54. _STARPU_ERROR("Data %p with mode STARPU_W needs to have a valid rank", data);
  55. }
  56. if (*xrank == -1)
  57. {
  58. // No node has been selected yet
  59. *xrank = mpi_rank;
  60. _STARPU_MPI_DEBUG(100, "Codelet is going to be executed by node %d\n", *xrank);
  61. *do_execute = (mpi_rank == me);
  62. }
  63. else if (mpi_rank != *xrank)
  64. {
  65. _STARPU_MPI_DEBUG(100, "Another node %d had already been selected to execute the codelet\n", *xrank);
  66. *inconsistent_execute = 1;
  67. }
  68. }
  69. _STARPU_MPI_DEBUG(100, "Executing: inconsistent=%d, do_execute=%d, xrank=%d\n", *inconsistent_execute, *do_execute, *xrank);
  70. return 0;
  71. }
  72. static
  73. void _starpu_mpi_exchange_data_before_execution(starpu_data_handle_t data, enum starpu_data_access_mode mode, int me, int xrank, int do_execute, MPI_Comm comm)
  74. {
  75. if (data && mode & STARPU_R)
  76. {
  77. int mpi_rank = starpu_mpi_data_get_rank(data);
  78. int data_tag = starpu_mpi_data_get_tag(data);
  79. if (mpi_rank == -1)
  80. {
  81. fprintf(stderr,"StarPU needs to be told the MPI rank of this data, using starpu_mpi_data_register\n");
  82. STARPU_ABORT();
  83. }
  84. if (data_tag == -1)
  85. {
  86. fprintf(stderr,"StarPU needs to be told the MPI tag of this data, using starpu_mpi_data_register\n");
  87. STARPU_ABORT();
  88. }
  89. if (do_execute && mpi_rank != me)
  90. {
  91. /* The node is going to execute the codelet, but it does not own the data, it needs to receive the data from the owner node */
  92. void *already_received = _starpu_mpi_cache_received_data_set(data, mpi_rank);
  93. if (already_received == NULL)
  94. {
  95. _STARPU_MPI_DEBUG(1, "Receiving data %p from %d\n", data, mpi_rank);
  96. starpu_mpi_irecv_detached(data, mpi_rank, data_tag, comm, NULL, NULL);
  97. }
  98. // else the node has already received the data
  99. }
  100. if (!do_execute && mpi_rank == me)
  101. {
  102. /* The node owns the data, but another node is going to execute the codelet, the node needs to send the data to the executee node. */
  103. void *already_sent = _starpu_mpi_cache_sent_data_set(data, xrank);
  104. if (already_sent == NULL)
  105. {
  106. _STARPU_MPI_DEBUG(1, "Sending data %p to %d\n", data, xrank);
  107. _SEND_DATA(data, mode, xrank, data_tag, comm, NULL, NULL);
  108. }
  109. // Else the data has already been sent
  110. }
  111. }
  112. }
  113. static
  114. void _starpu_mpi_exchange_data_after_execution(starpu_data_handle_t data, enum starpu_data_access_mode mode, int me, int xrank, int do_execute, MPI_Comm comm)
  115. {
  116. if (mode & STARPU_W)
  117. {
  118. int mpi_rank = starpu_mpi_data_get_rank(data);
  119. int data_tag = starpu_mpi_data_get_tag(data);
  120. if(mpi_rank == -1)
  121. {
  122. fprintf(stderr,"StarPU needs to be told the MPI rank of this data, using starpu_mpi_data_register\n");
  123. STARPU_ABORT();
  124. }
  125. if(data_tag == -1)
  126. {
  127. fprintf(stderr,"StarPU needs to be told the MPI tag of this data, using starpu_mpi_data_register\n");
  128. STARPU_ABORT();
  129. }
  130. if (mpi_rank == me)
  131. {
  132. if (xrank != -1 && me != xrank)
  133. {
  134. _STARPU_MPI_DEBUG(1, "Receive data %p back from the task %d which executed the codelet ...\n", data, xrank);
  135. starpu_mpi_irecv_detached(data, xrank, data_tag, comm, NULL, NULL);
  136. }
  137. }
  138. else if (do_execute)
  139. {
  140. _STARPU_MPI_DEBUG(1, "Send data %p back to its owner %d...\n", data, mpi_rank);
  141. _SEND_DATA(data, mode, mpi_rank, data_tag, comm, NULL, NULL);
  142. }
  143. }
  144. }
  145. static
  146. void _starpu_mpi_clear_data_after_execution(starpu_data_handle_t data, enum starpu_data_access_mode mode, int me, int do_execute, MPI_Comm comm)
  147. {
  148. if (_starpu_cache_enabled)
  149. {
  150. if (mode & STARPU_W || mode & STARPU_REDUX)
  151. {
  152. /* The data has been modified, it MUST be removed from the cache */
  153. _starpu_mpi_cache_sent_data_clear(comm, data);
  154. _starpu_mpi_cache_received_data_clear(data);
  155. }
  156. }
  157. else
  158. {
  159. /* We allocated a temporary buffer for the received data, now drop it */
  160. if ((mode & STARPU_R) && do_execute)
  161. {
  162. int mpi_rank = starpu_mpi_data_get_rank(data);
  163. if (mpi_rank != me && mpi_rank != -1)
  164. {
  165. starpu_data_invalidate_submit(data);
  166. }
  167. }
  168. }
  169. }
  170. static
  171. int _starpu_mpi_task_decode_v(struct starpu_codelet *codelet, int me, int nb_nodes, int *xrank, int *do_execute, struct starpu_data_descr **descrs_p, int *nb_data_p, va_list varg_list)
  172. {
  173. va_list varg_list_copy;
  174. int inconsistent_execute = 0;
  175. int arg_type, arg_type_nocommute;
  176. int node_selected = 0;
  177. int nb_allocated_data = 16;
  178. struct starpu_data_descr *descrs;
  179. int nb_data;
  180. int select_node_policy = STARPU_MPI_NODE_SELECTION_CURRENT_POLICY;
  181. _STARPU_TRACE_TASK_MPI_DECODE_START();
  182. descrs = (struct starpu_data_descr *)malloc(nb_allocated_data * sizeof(struct starpu_data_descr));
  183. nb_data = 0;
  184. *do_execute = -1;
  185. *xrank = -1;
  186. va_copy(varg_list_copy, varg_list);
  187. while ((arg_type = va_arg(varg_list_copy, int)) != 0)
  188. {
  189. arg_type_nocommute = arg_type & ~STARPU_COMMUTE;
  190. if (arg_type==STARPU_EXECUTE_ON_NODE)
  191. {
  192. *xrank = va_arg(varg_list_copy, int);
  193. if (node_selected == 0)
  194. {
  195. _STARPU_MPI_DEBUG(100, "Executing on node %d\n", *xrank);
  196. *do_execute = 1;
  197. node_selected = 1;
  198. inconsistent_execute = 0;
  199. }
  200. }
  201. else if (arg_type==STARPU_EXECUTE_ON_DATA)
  202. {
  203. starpu_data_handle_t data = va_arg(varg_list_copy, starpu_data_handle_t);
  204. if (node_selected == 0)
  205. {
  206. *xrank = starpu_mpi_data_get_rank(data);
  207. STARPU_ASSERT_MSG(*xrank != -1, "Rank of the data must be set using starpu_mpi_data_register() or starpu_data_set_rank()");
  208. _STARPU_MPI_DEBUG(100, "Executing on data node %d\n", *xrank);
  209. STARPU_ASSERT_MSG(*xrank <= nb_nodes, "Node %d to execute codelet is not a valid node (%d)", *xrank, nb_nodes);
  210. *do_execute = 1;
  211. node_selected = 1;
  212. inconsistent_execute = 0;
  213. }
  214. }
  215. else if (arg_type_nocommute & STARPU_R || arg_type_nocommute & STARPU_W || arg_type_nocommute & STARPU_RW || arg_type & STARPU_SCRATCH || arg_type & STARPU_REDUX)
  216. {
  217. starpu_data_handle_t data = va_arg(varg_list_copy, starpu_data_handle_t);
  218. enum starpu_data_access_mode mode = (enum starpu_data_access_mode) arg_type;
  219. if (node_selected == 0)
  220. {
  221. int ret = _starpu_mpi_find_executee_node(data, mode, me, do_execute, &inconsistent_execute, xrank);
  222. if (ret == -EINVAL)
  223. {
  224. free(descrs);
  225. va_end(varg_list_copy);
  226. _STARPU_TRACE_TASK_MPI_DECODE_END();
  227. return ret;
  228. }
  229. }
  230. if (nb_data >= nb_allocated_data)
  231. {
  232. nb_allocated_data *= 2;
  233. descrs = (struct starpu_data_descr *)realloc(descrs, nb_allocated_data * sizeof(struct starpu_data_descr));
  234. }
  235. descrs[nb_data].handle = data;
  236. descrs[nb_data].mode = mode;
  237. nb_data ++;
  238. }
  239. else if (arg_type == STARPU_DATA_ARRAY)
  240. {
  241. starpu_data_handle_t *datas = va_arg(varg_list_copy, starpu_data_handle_t *);
  242. int nb_handles = va_arg(varg_list_copy, int);
  243. int i;
  244. for(i=0 ; i<nb_handles ; i++)
  245. {
  246. enum starpu_data_access_mode mode = STARPU_CODELET_GET_MODE(codelet, nb_data);
  247. if (node_selected == 0)
  248. {
  249. int ret = _starpu_mpi_find_executee_node(datas[i], mode, me, do_execute, &inconsistent_execute, xrank);
  250. if (ret == -EINVAL)
  251. {
  252. free(descrs);
  253. va_end(varg_list_copy);
  254. _STARPU_TRACE_TASK_MPI_DECODE_END();
  255. return ret;
  256. }
  257. }
  258. if (nb_data >= nb_allocated_data)
  259. {
  260. nb_allocated_data *= 2;
  261. descrs = (struct starpu_data_descr *)realloc(descrs, nb_allocated_data * sizeof(struct starpu_data_descr));
  262. }
  263. descrs[nb_data].handle = datas[i];
  264. descrs[nb_data].mode = mode;
  265. nb_data ++;
  266. }
  267. }
  268. else if (arg_type == STARPU_DATA_MODE_ARRAY)
  269. {
  270. struct starpu_data_descr *_descrs = va_arg(varg_list_copy, struct starpu_data_descr*);
  271. int nb_handles = va_arg(varg_list_copy, int);
  272. int i;
  273. for(i=0 ; i<nb_handles ; i++)
  274. {
  275. enum starpu_data_access_mode mode = _descrs[i].mode;
  276. if (node_selected == 0)
  277. {
  278. int ret = _starpu_mpi_find_executee_node(_descrs[i].handle, mode, me, do_execute, &inconsistent_execute, xrank);
  279. if (ret == -EINVAL)
  280. {
  281. free(descrs);
  282. va_end(varg_list_copy);
  283. _STARPU_TRACE_TASK_MPI_DECODE_END();
  284. return ret;
  285. }
  286. }
  287. if (nb_data >= nb_allocated_data)
  288. {
  289. nb_allocated_data *= 2;
  290. descrs = (struct starpu_data_descr *)realloc(descrs, nb_allocated_data * sizeof(struct starpu_data_descr));
  291. }
  292. descrs[nb_data].handle = _descrs[i].handle;
  293. descrs[nb_data].mode = mode;
  294. nb_data ++;
  295. }
  296. }
  297. else if (arg_type==STARPU_VALUE)
  298. {
  299. (void)va_arg(varg_list_copy, void *);
  300. (void)va_arg(varg_list_copy, size_t);
  301. }
  302. else if (arg_type==STARPU_CALLBACK)
  303. {
  304. (void)va_arg(varg_list_copy, _starpu_callback_func_t);
  305. }
  306. else if (arg_type==STARPU_CALLBACK_WITH_ARG)
  307. {
  308. (void)va_arg(varg_list_copy, _starpu_callback_func_t);
  309. (void)va_arg(varg_list_copy, void *);
  310. }
  311. else if (arg_type==STARPU_CALLBACK_ARG)
  312. {
  313. (void)va_arg(varg_list_copy, void *);
  314. }
  315. else if (arg_type==STARPU_PRIORITY)
  316. {
  317. (void)va_arg(varg_list_copy, int);
  318. }
  319. /* STARPU_EXECUTE_ON_NODE handled above */
  320. /* STARPU_EXECUTE_ON_DATA handled above */
  321. /* STARPU_DATA_ARRAY handled above */
  322. /* STARPU_DATA_MODE_ARRAY handled above */
  323. else if (arg_type==STARPU_TAG)
  324. {
  325. (void)va_arg(varg_list_copy, starpu_tag_t);
  326. }
  327. else if (arg_type==STARPU_HYPERVISOR_TAG)
  328. {
  329. (void)va_arg(varg_list_copy, int);
  330. }
  331. else if (arg_type==STARPU_FLOPS)
  332. {
  333. (void)va_arg(varg_list_copy, double);
  334. }
  335. else if (arg_type==STARPU_SCHED_CTX)
  336. {
  337. (void)va_arg(varg_list_copy, unsigned);
  338. }
  339. else if (arg_type==STARPU_PROLOGUE_CALLBACK)
  340. {
  341. (void)va_arg(varg_list_copy, _starpu_callback_func_t);
  342. }
  343. else if (arg_type==STARPU_PROLOGUE_CALLBACK_ARG)
  344. {
  345. (void)va_arg(varg_list_copy, void *);
  346. }
  347. else if (arg_type==STARPU_PROLOGUE_CALLBACK_POP)
  348. {
  349. (void)va_arg(varg_list_copy, _starpu_callback_func_t);
  350. }
  351. else if (arg_type==STARPU_PROLOGUE_CALLBACK_POP_ARG)
  352. {
  353. (void)va_arg(varg_list_copy, void *);
  354. }
  355. else if (arg_type==STARPU_EXECUTE_ON_WORKER)
  356. {
  357. // the flag is decoded and set later when
  358. // calling function _starpu_task_insert_create()
  359. (void)va_arg(varg_list_copy, int);
  360. }
  361. else if (arg_type==STARPU_TAG_ONLY)
  362. {
  363. (void)va_arg(varg_list_copy, starpu_tag_t);
  364. }
  365. else if (arg_type==STARPU_NAME)
  366. {
  367. (void)va_arg(varg_list_copy, const char *);
  368. }
  369. else if (arg_type==STARPU_POSSIBLY_PARALLEL)
  370. {
  371. (void)va_arg(varg_list_copy, unsigned);
  372. }
  373. else if (arg_type==STARPU_WORKER_ORDER)
  374. {
  375. // the flag is decoded and set later when
  376. // calling function _starpu_task_insert_create()
  377. (void)va_arg(varg_list_copy, unsigned);
  378. }
  379. else if (arg_type==STARPU_NODE_SELECTION_POLICY)
  380. {
  381. select_node_policy = va_arg(varg_list_copy, int);
  382. }
  383. else
  384. {
  385. STARPU_ABORT_MSG("Unrecognized argument %d\n", arg_type);
  386. }
  387. }
  388. va_end(varg_list_copy);
  389. if (inconsistent_execute == 1 || *xrank == -1)
  390. {
  391. // We need to find out which node is going to execute the codelet.
  392. _STARPU_MPI_DISP("Different nodes are owning W data. The node to execute the codelet is going to be selected with the current selection node policy. See starpu_mpi_node_selection_set_current_policy() to change the policy, or use STARPU_EXECUTE_ON_NODE or STARPU_EXECUTE_ON_DATA to specify the node\n");
  393. *xrank = _starpu_mpi_select_node(me, nb_nodes, descrs, nb_data, select_node_policy);
  394. *do_execute = (me == *xrank);
  395. }
  396. else
  397. {
  398. _STARPU_MPI_DEBUG(100, "Inconsistent=%d - xrank=%d\n", inconsistent_execute, *xrank);
  399. *do_execute = (me == *xrank);
  400. }
  401. _STARPU_MPI_DEBUG(100, "do_execute=%d\n", *do_execute);
  402. *descrs_p = descrs;
  403. *nb_data_p = nb_data;
  404. _STARPU_TRACE_TASK_MPI_DECODE_END();
  405. return 0;
  406. }
  407. static
  408. int _starpu_mpi_task_build_v(MPI_Comm comm, struct starpu_codelet *codelet, struct starpu_task **task, int *xrank_p, struct starpu_data_descr **descrs_p, int *nb_data_p, va_list varg_list)
  409. {
  410. va_list varg_list_copy;
  411. int me, do_execute, xrank, nb_nodes;
  412. int ret;
  413. int i;
  414. struct starpu_data_descr *descrs;
  415. int nb_data;
  416. _STARPU_MPI_LOG_IN();
  417. starpu_mpi_comm_rank(comm, &me);
  418. starpu_mpi_comm_size(comm, &nb_nodes);
  419. /* Find out whether we are to execute the data because we own the data to be written to. */
  420. ret = _starpu_mpi_task_decode_v(codelet, me, nb_nodes, &xrank, &do_execute, &descrs, &nb_data, varg_list);
  421. if (ret < 0) return ret;
  422. _STARPU_TRACE_TASK_MPI_PRE_START();
  423. /* Send and receive data as requested */
  424. for(i=0 ; i<nb_data ; i++)
  425. {
  426. _starpu_mpi_exchange_data_before_execution(descrs[i].handle, descrs[i].mode, me, xrank, do_execute, comm);
  427. }
  428. if (xrank_p) *xrank_p = xrank;
  429. if (nb_data_p) *nb_data_p = nb_data;
  430. if (descrs_p)
  431. *descrs_p = descrs;
  432. else
  433. free(descrs);
  434. _STARPU_TRACE_TASK_MPI_PRE_END();
  435. if (do_execute == 0) return 1;
  436. else
  437. {
  438. _STARPU_MPI_DEBUG(100, "Execution of the codelet %p (%s)\n", codelet, codelet?codelet->name:NULL);
  439. *task = starpu_task_create();
  440. (*task)->cl_arg_free = 1;
  441. va_copy(varg_list_copy, varg_list);
  442. _starpu_task_insert_create(codelet, task, varg_list_copy);
  443. va_end(varg_list_copy);
  444. return 0;
  445. }
  446. }
  447. static
  448. int _starpu_mpi_task_postbuild_v(MPI_Comm comm, int xrank, int do_execute, struct starpu_data_descr *descrs, int nb_data)
  449. {
  450. int me, i;
  451. _STARPU_TRACE_TASK_MPI_POST_START();
  452. starpu_mpi_comm_rank(comm, &me);
  453. for(i=0 ; i<nb_data ; i++)
  454. {
  455. _starpu_mpi_exchange_data_after_execution(descrs[i].handle, descrs[i].mode, me, xrank, do_execute, comm);
  456. _starpu_mpi_clear_data_after_execution(descrs[i].handle, descrs[i].mode, me, do_execute, comm);
  457. }
  458. free(descrs);
  459. _STARPU_TRACE_TASK_MPI_POST_END();
  460. _STARPU_MPI_LOG_OUT();
  461. return 0;
  462. }
  463. static
  464. int _starpu_mpi_task_insert_v(MPI_Comm comm, struct starpu_codelet *codelet, va_list varg_list)
  465. {
  466. struct starpu_task *task;
  467. int ret;
  468. int xrank;
  469. int do_execute = 0;
  470. struct starpu_data_descr *descrs;
  471. int nb_data;
  472. ret = _starpu_mpi_task_build_v(comm, codelet, &task, &xrank, &descrs, &nb_data, varg_list);
  473. if (ret < 0) return ret;
  474. if (ret == 0)
  475. {
  476. do_execute = 1;
  477. ret = starpu_task_submit(task);
  478. if (STARPU_UNLIKELY(ret == -ENODEV))
  479. {
  480. fprintf(stderr, "submission of task %p wih codelet %p failed (symbol `%s') (err: ENODEV)\n",
  481. task, task->cl,
  482. (codelet == NULL) ? "none" :
  483. task->cl->name ? task->cl->name :
  484. (task->cl->model && task->cl->model->symbol)?task->cl->model->symbol:"none");
  485. task->destroy = 0;
  486. starpu_task_destroy(task);
  487. }
  488. }
  489. return _starpu_mpi_task_postbuild_v(comm, xrank, do_execute, descrs, nb_data);
  490. }
  491. int starpu_mpi_task_insert(MPI_Comm comm, struct starpu_codelet *codelet, ...)
  492. {
  493. va_list varg_list;
  494. int ret;
  495. va_start(varg_list, codelet);
  496. ret = _starpu_mpi_task_insert_v(comm, codelet, varg_list);
  497. va_end(varg_list);
  498. return ret;
  499. }
  500. int starpu_mpi_insert_task(MPI_Comm comm, struct starpu_codelet *codelet, ...)
  501. {
  502. va_list varg_list;
  503. int ret;
  504. va_start(varg_list, codelet);
  505. ret = _starpu_mpi_task_insert_v(comm, codelet, varg_list);
  506. va_end(varg_list);
  507. return ret;
  508. }
  509. struct starpu_task *starpu_mpi_task_build(MPI_Comm comm, struct starpu_codelet *codelet, ...)
  510. {
  511. va_list varg_list;
  512. struct starpu_task *task;
  513. int ret;
  514. va_start(varg_list, codelet);
  515. ret = _starpu_mpi_task_build_v(comm, codelet, &task, NULL, NULL, NULL, varg_list);
  516. va_end(varg_list);
  517. STARPU_ASSERT(ret >= 0);
  518. if (ret > 0) return NULL; else return task;
  519. }
  520. int starpu_mpi_task_post_build(MPI_Comm comm, struct starpu_codelet *codelet, ...)
  521. {
  522. int xrank, do_execute;
  523. int ret, me, nb_nodes;
  524. va_list varg_list;
  525. struct starpu_data_descr *descrs;
  526. int nb_data;
  527. starpu_mpi_comm_rank(comm, &me);
  528. starpu_mpi_comm_size(comm, &nb_nodes);
  529. va_start(varg_list, codelet);
  530. /* Find out whether we are to execute the data because we own the data to be written to. */
  531. ret = _starpu_mpi_task_decode_v(codelet, me, nb_nodes, &xrank, &do_execute, &descrs, &nb_data, varg_list);
  532. va_end(varg_list);
  533. if (ret < 0) return ret;
  534. return _starpu_mpi_task_postbuild_v(comm, xrank, do_execute, descrs, nb_data);
  535. }
  536. void starpu_mpi_get_data_on_node_detached(MPI_Comm comm, starpu_data_handle_t data_handle, int node, void (*callback)(void*), void *arg)
  537. {
  538. int me, rank, tag;
  539. rank = starpu_mpi_data_get_rank(data_handle);
  540. tag = starpu_mpi_data_get_tag(data_handle);
  541. if (rank == -1)
  542. {
  543. _STARPU_ERROR("StarPU needs to be told the MPI rank of this data, using starpu_mpi_data_register() or starpu_mpi_data_register()\n");
  544. }
  545. if (tag == -1)
  546. {
  547. _STARPU_ERROR("StarPU needs to be told the MPI tag of this data, using starpu_mpi_data_register() or starpu_mpi_data_register()\n");
  548. }
  549. starpu_mpi_comm_rank(comm, &me);
  550. if (node == rank) return;
  551. if (me == node)
  552. {
  553. starpu_mpi_irecv_detached(data_handle, rank, tag, comm, callback, arg);
  554. }
  555. else if (me == rank)
  556. {
  557. starpu_mpi_isend_detached(data_handle, node, tag, comm, NULL, NULL);
  558. }
  559. }
  560. void starpu_mpi_get_data_on_node(MPI_Comm comm, starpu_data_handle_t data_handle, int node)
  561. {
  562. int me, rank, tag;
  563. rank = starpu_mpi_data_get_rank(data_handle);
  564. tag = starpu_mpi_data_get_tag(data_handle);
  565. if (rank == -1)
  566. {
  567. fprintf(stderr,"StarPU needs to be told the MPI rank of this data, using starpu_mpi_data_register\n");
  568. STARPU_ABORT();
  569. }
  570. if (tag == -1)
  571. {
  572. fprintf(stderr,"StarPU needs to be told the MPI tag of this data, using starpu_mpi_data_register\n");
  573. STARPU_ABORT();
  574. }
  575. starpu_mpi_comm_rank(comm, &me);
  576. if (node == rank) return;
  577. if (me == node)
  578. {
  579. MPI_Status status;
  580. starpu_mpi_recv(data_handle, rank, tag, comm, &status);
  581. }
  582. else if (me == rank)
  583. {
  584. starpu_mpi_send(data_handle, node, tag, comm);
  585. }
  586. }
  587. struct _starpu_mpi_redux_data_args
  588. {
  589. starpu_data_handle_t data_handle;
  590. starpu_data_handle_t new_handle;
  591. int tag;
  592. int node;
  593. MPI_Comm comm;
  594. struct starpu_task *taskB;
  595. };
  596. void _starpu_mpi_redux_data_dummy_func(STARPU_ATTRIBUTE_UNUSED void *buffers[], STARPU_ATTRIBUTE_UNUSED void *cl_arg)
  597. {
  598. }
  599. /* Dummy cost function for simgrid */
  600. static double cost_function(struct starpu_task *task STARPU_ATTRIBUTE_UNUSED, unsigned nimpl STARPU_ATTRIBUTE_UNUSED)
  601. {
  602. return 0.000001;
  603. }
  604. static struct starpu_perfmodel dumb_model =
  605. {
  606. .type = STARPU_COMMON,
  607. .cost_function = cost_function
  608. };
  609. static
  610. struct starpu_codelet _starpu_mpi_redux_data_read_cl =
  611. {
  612. .cpu_funcs = {_starpu_mpi_redux_data_dummy_func},
  613. .cuda_funcs = {_starpu_mpi_redux_data_dummy_func},
  614. .opencl_funcs = {_starpu_mpi_redux_data_dummy_func},
  615. .nbuffers = 1,
  616. .modes = {STARPU_R},
  617. .model = &dumb_model,
  618. .name = "_starpu_mpi_redux_data_read_cl"
  619. };
  620. struct starpu_codelet _starpu_mpi_redux_data_readwrite_cl =
  621. {
  622. .cpu_funcs = {_starpu_mpi_redux_data_dummy_func},
  623. .cuda_funcs = {_starpu_mpi_redux_data_dummy_func},
  624. .opencl_funcs = {_starpu_mpi_redux_data_dummy_func},
  625. .nbuffers = 1,
  626. .modes = {STARPU_RW},
  627. .model = &dumb_model,
  628. .name = "_starpu_mpi_redux_data_write_cl"
  629. };
  630. static
  631. void _starpu_mpi_redux_data_detached_callback(void *arg)
  632. {
  633. struct _starpu_mpi_redux_data_args *args = (struct _starpu_mpi_redux_data_args *) arg;
  634. STARPU_TASK_SET_HANDLE(args->taskB, args->new_handle, 1);
  635. int ret = starpu_task_submit(args->taskB);
  636. STARPU_ASSERT(ret == 0);
  637. starpu_data_unregister_submit(args->new_handle);
  638. free(args);
  639. }
  640. static
  641. void _starpu_mpi_redux_data_recv_callback(void *callback_arg)
  642. {
  643. struct _starpu_mpi_redux_data_args *args = (struct _starpu_mpi_redux_data_args *) callback_arg;
  644. starpu_data_register_same(&args->new_handle, args->data_handle);
  645. starpu_mpi_irecv_detached_sequential_consistency(args->new_handle, args->node, args->tag, args->comm, _starpu_mpi_redux_data_detached_callback, args, 0);
  646. }
  647. /* TODO: this should rather be implicitly called by starpu_mpi_task_insert when
  648. * a data previously accessed in REDUX mode gets accessed in R mode. */
  649. void starpu_mpi_redux_data(MPI_Comm comm, starpu_data_handle_t data_handle)
  650. {
  651. int me, rank, tag, nb_nodes;
  652. rank = starpu_mpi_data_get_rank(data_handle);
  653. tag = starpu_mpi_data_get_tag(data_handle);
  654. if (rank == -1)
  655. {
  656. fprintf(stderr,"StarPU needs to be told the MPI rank of this data, using starpu_mpi_data_register\n");
  657. STARPU_ABORT();
  658. }
  659. if (tag == -1)
  660. {
  661. fprintf(stderr,"StarPU needs to be told the MPI tag of this data, using starpu_mpi_data_register\n");
  662. STARPU_ABORT();
  663. }
  664. starpu_mpi_comm_rank(comm, &me);
  665. starpu_mpi_comm_size(comm, &nb_nodes);
  666. _STARPU_MPI_DEBUG(1, "Doing reduction for data %p on node %d with %d nodes ...\n", data_handle, rank, nb_nodes);
  667. // need to count how many nodes have the data in redux mode
  668. if (me == rank)
  669. {
  670. int i, j=0;
  671. struct starpu_task *taskBs[nb_nodes];
  672. for(i=0 ; i<nb_nodes ; i++)
  673. {
  674. if (i != rank)
  675. {
  676. /* We need to make sure all is
  677. * executed after data_handle finished
  678. * its last read access, we hence do
  679. * the following:
  680. * - submit an empty task A reading
  681. * data_handle whose callback submits
  682. * the mpi comm with sequential
  683. * consistency set to 0, whose
  684. * callback submits the redux_cl task
  685. * B with sequential consistency set
  686. * to 0,
  687. * - submit an empty task C reading
  688. * and writing data_handle and
  689. * depending on task B, just to replug
  690. * with implicit data dependencies
  691. * with tasks inserted after this
  692. * reduction.
  693. */
  694. struct _starpu_mpi_redux_data_args *args = malloc(sizeof(struct _starpu_mpi_redux_data_args));
  695. args->data_handle = data_handle;
  696. args->tag = tag;
  697. args->node = i;
  698. args->comm = comm;
  699. // We need to create taskB early as
  700. // taskC declares a dependancy on it
  701. args->taskB = starpu_task_create();
  702. args->taskB->cl = args->data_handle->redux_cl;
  703. args->taskB->sequential_consistency = 0;
  704. STARPU_TASK_SET_HANDLE(args->taskB, args->data_handle, 0);
  705. taskBs[j] = args->taskB; j++;
  706. // Submit taskA
  707. starpu_task_insert(&_starpu_mpi_redux_data_read_cl,
  708. STARPU_R, data_handle,
  709. STARPU_CALLBACK_WITH_ARG, _starpu_mpi_redux_data_recv_callback, args,
  710. 0);
  711. }
  712. }
  713. // Submit taskC which depends on all taskBs created
  714. struct starpu_task *taskC = starpu_task_create();
  715. taskC->cl = &_starpu_mpi_redux_data_readwrite_cl;
  716. STARPU_TASK_SET_HANDLE(taskC, data_handle, 0);
  717. starpu_task_declare_deps_array(taskC, j, taskBs);
  718. int ret = starpu_task_submit(taskC);
  719. STARPU_ASSERT(ret == 0);
  720. }
  721. else
  722. {
  723. _STARPU_MPI_DEBUG(1, "Sending redux handle to %d ...\n", rank);
  724. starpu_mpi_isend_detached(data_handle, rank, tag, comm, NULL, NULL);
  725. starpu_task_insert(data_handle->init_cl, STARPU_W, data_handle, 0);
  726. }
  727. /* FIXME: In order to prevent simultaneous receive submissions
  728. * on the same handle, we need to wait that all the starpu_mpi
  729. * tasks are done before submitting next tasks. The current
  730. * version of the implementation does not support multiple
  731. * simultaneous receive requests on the same handle.*/
  732. starpu_task_wait_for_all();
  733. }