starpu_mpi_task_insert.c 23 KB

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