starpu_mpi_task_insert.c 23 KB

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