starpu_mpi_task_insert.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016 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. STARPU_ASSERT_MSG(codelet->nbuffers == STARPU_VARIABLE_NBUFFERS || nb_data < codelet->nbuffers, "Too many data passed to starpu_mpi_task_insert");
  247. enum starpu_data_access_mode mode = STARPU_CODELET_GET_MODE(codelet, nb_data);
  248. if (node_selected == 0)
  249. {
  250. int ret = _starpu_mpi_find_executee_node(datas[i], mode, me, do_execute, &inconsistent_execute, xrank);
  251. if (ret == -EINVAL)
  252. {
  253. free(descrs);
  254. va_end(varg_list_copy);
  255. _STARPU_TRACE_TASK_MPI_DECODE_END();
  256. return ret;
  257. }
  258. }
  259. if (nb_data >= nb_allocated_data)
  260. {
  261. nb_allocated_data *= 2;
  262. descrs = (struct starpu_data_descr *)realloc(descrs, nb_allocated_data * sizeof(struct starpu_data_descr));
  263. }
  264. descrs[nb_data].handle = datas[i];
  265. descrs[nb_data].mode = mode;
  266. nb_data ++;
  267. }
  268. }
  269. else if (arg_type == STARPU_DATA_MODE_ARRAY)
  270. {
  271. struct starpu_data_descr *_descrs = va_arg(varg_list_copy, struct starpu_data_descr*);
  272. int nb_handles = va_arg(varg_list_copy, int);
  273. int i;
  274. for(i=0 ; i<nb_handles ; i++)
  275. {
  276. enum starpu_data_access_mode mode = _descrs[i].mode;
  277. if (node_selected == 0)
  278. {
  279. int ret = _starpu_mpi_find_executee_node(_descrs[i].handle, mode, me, do_execute, &inconsistent_execute, xrank);
  280. if (ret == -EINVAL)
  281. {
  282. free(descrs);
  283. va_end(varg_list_copy);
  284. _STARPU_TRACE_TASK_MPI_DECODE_END();
  285. return ret;
  286. }
  287. }
  288. if (nb_data >= nb_allocated_data)
  289. {
  290. nb_allocated_data *= 2;
  291. descrs = (struct starpu_data_descr *)realloc(descrs, nb_allocated_data * sizeof(struct starpu_data_descr));
  292. }
  293. descrs[nb_data].handle = _descrs[i].handle;
  294. descrs[nb_data].mode = mode;
  295. nb_data ++;
  296. }
  297. }
  298. else if (arg_type==STARPU_VALUE)
  299. {
  300. (void)va_arg(varg_list_copy, void *);
  301. (void)va_arg(varg_list_copy, size_t);
  302. }
  303. else if (arg_type==STARPU_CALLBACK)
  304. {
  305. (void)va_arg(varg_list_copy, _starpu_callback_func_t);
  306. }
  307. else if (arg_type==STARPU_CALLBACK_WITH_ARG)
  308. {
  309. (void)va_arg(varg_list_copy, _starpu_callback_func_t);
  310. (void)va_arg(varg_list_copy, void *);
  311. }
  312. else if (arg_type==STARPU_CALLBACK_ARG)
  313. {
  314. (void)va_arg(varg_list_copy, void *);
  315. }
  316. else if (arg_type==STARPU_PRIORITY)
  317. {
  318. (void)va_arg(varg_list_copy, int);
  319. }
  320. /* STARPU_EXECUTE_ON_NODE handled above */
  321. /* STARPU_EXECUTE_ON_DATA handled above */
  322. /* STARPU_DATA_ARRAY handled above */
  323. /* STARPU_DATA_MODE_ARRAY handled above */
  324. else if (arg_type==STARPU_TAG)
  325. {
  326. (void)va_arg(varg_list_copy, starpu_tag_t);
  327. }
  328. else if (arg_type==STARPU_HYPERVISOR_TAG)
  329. {
  330. (void)va_arg(varg_list_copy, int);
  331. }
  332. else if (arg_type==STARPU_FLOPS)
  333. {
  334. (void)va_arg(varg_list_copy, double);
  335. }
  336. else if (arg_type==STARPU_SCHED_CTX)
  337. {
  338. (void)va_arg(varg_list_copy, unsigned);
  339. }
  340. else if (arg_type==STARPU_PROLOGUE_CALLBACK)
  341. {
  342. (void)va_arg(varg_list_copy, _starpu_callback_func_t);
  343. }
  344. else if (arg_type==STARPU_PROLOGUE_CALLBACK_ARG)
  345. {
  346. (void)va_arg(varg_list_copy, void *);
  347. }
  348. else if (arg_type==STARPU_PROLOGUE_CALLBACK_POP)
  349. {
  350. (void)va_arg(varg_list_copy, _starpu_callback_func_t);
  351. }
  352. else if (arg_type==STARPU_PROLOGUE_CALLBACK_POP_ARG)
  353. {
  354. (void)va_arg(varg_list_copy, void *);
  355. }
  356. else if (arg_type==STARPU_EXECUTE_ON_WORKER)
  357. {
  358. // the flag is decoded and set later when
  359. // calling function _starpu_task_insert_create()
  360. (void)va_arg(varg_list_copy, int);
  361. }
  362. else if (arg_type==STARPU_TAG_ONLY)
  363. {
  364. (void)va_arg(varg_list_copy, starpu_tag_t);
  365. }
  366. else if (arg_type==STARPU_NAME)
  367. {
  368. (void)va_arg(varg_list_copy, const char *);
  369. }
  370. else if (arg_type==STARPU_POSSIBLY_PARALLEL)
  371. {
  372. (void)va_arg(varg_list_copy, unsigned);
  373. }
  374. else if (arg_type==STARPU_WORKER_ORDER)
  375. {
  376. // the flag is decoded and set later when
  377. // calling function _starpu_task_insert_create()
  378. (void)va_arg(varg_list_copy, unsigned);
  379. }
  380. else if (arg_type==STARPU_NODE_SELECTION_POLICY)
  381. {
  382. select_node_policy = va_arg(varg_list_copy, int);
  383. }
  384. else
  385. {
  386. STARPU_ABORT_MSG("Unrecognized argument %d\n", arg_type);
  387. }
  388. }
  389. va_end(varg_list_copy);
  390. if (inconsistent_execute == 1 || *xrank == -1)
  391. {
  392. // We need to find out which node is going to execute the codelet.
  393. _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");
  394. *xrank = _starpu_mpi_select_node(me, nb_nodes, descrs, nb_data, select_node_policy);
  395. *do_execute = (me == *xrank);
  396. }
  397. else
  398. {
  399. _STARPU_MPI_DEBUG(100, "Inconsistent=%d - xrank=%d\n", inconsistent_execute, *xrank);
  400. *do_execute = (me == *xrank);
  401. }
  402. _STARPU_MPI_DEBUG(100, "do_execute=%d\n", *do_execute);
  403. *descrs_p = descrs;
  404. *nb_data_p = nb_data;
  405. _STARPU_TRACE_TASK_MPI_DECODE_END();
  406. return 0;
  407. }
  408. static
  409. 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)
  410. {
  411. va_list varg_list_copy;
  412. int me, do_execute, xrank, nb_nodes;
  413. int ret;
  414. int i;
  415. struct starpu_data_descr *descrs;
  416. int nb_data;
  417. _STARPU_MPI_LOG_IN();
  418. starpu_mpi_comm_rank(comm, &me);
  419. starpu_mpi_comm_size(comm, &nb_nodes);
  420. /* Find out whether we are to execute the data because we own the data to be written to. */
  421. ret = _starpu_mpi_task_decode_v(codelet, me, nb_nodes, &xrank, &do_execute, &descrs, &nb_data, varg_list);
  422. if (ret < 0) return ret;
  423. _STARPU_TRACE_TASK_MPI_PRE_START();
  424. /* Send and receive data as requested */
  425. for(i=0 ; i<nb_data ; i++)
  426. {
  427. _starpu_mpi_exchange_data_before_execution(descrs[i].handle, descrs[i].mode, me, xrank, do_execute, comm);
  428. }
  429. if (xrank_p) *xrank_p = xrank;
  430. if (nb_data_p) *nb_data_p = nb_data;
  431. if (descrs_p)
  432. *descrs_p = descrs;
  433. else
  434. free(descrs);
  435. _STARPU_TRACE_TASK_MPI_PRE_END();
  436. if (do_execute == 0) return 1;
  437. else
  438. {
  439. _STARPU_MPI_DEBUG(100, "Execution of the codelet %p (%s)\n", codelet, codelet?codelet->name:NULL);
  440. *task = starpu_task_create();
  441. (*task)->cl_arg_free = 1;
  442. va_copy(varg_list_copy, varg_list);
  443. _starpu_task_insert_create(codelet, task, varg_list_copy);
  444. va_end(varg_list_copy);
  445. return 0;
  446. }
  447. }
  448. static
  449. int _starpu_mpi_task_postbuild_v(MPI_Comm comm, int xrank, int do_execute, struct starpu_data_descr *descrs, int nb_data)
  450. {
  451. int me, i;
  452. _STARPU_TRACE_TASK_MPI_POST_START();
  453. starpu_mpi_comm_rank(comm, &me);
  454. for(i=0 ; i<nb_data ; i++)
  455. {
  456. _starpu_mpi_exchange_data_after_execution(descrs[i].handle, descrs[i].mode, me, xrank, do_execute, comm);
  457. _starpu_mpi_clear_data_after_execution(descrs[i].handle, descrs[i].mode, me, do_execute, comm);
  458. }
  459. free(descrs);
  460. _STARPU_TRACE_TASK_MPI_POST_END();
  461. _STARPU_MPI_LOG_OUT();
  462. return 0;
  463. }
  464. static
  465. int _starpu_mpi_task_insert_v(MPI_Comm comm, struct starpu_codelet *codelet, va_list varg_list)
  466. {
  467. struct starpu_task *task;
  468. int ret;
  469. int xrank;
  470. int do_execute = 0;
  471. struct starpu_data_descr *descrs;
  472. int nb_data;
  473. ret = _starpu_mpi_task_build_v(comm, codelet, &task, &xrank, &descrs, &nb_data, varg_list);
  474. if (ret < 0) return ret;
  475. if (ret == 0)
  476. {
  477. do_execute = 1;
  478. ret = starpu_task_submit(task);
  479. if (STARPU_UNLIKELY(ret == -ENODEV))
  480. {
  481. fprintf(stderr, "submission of task %p wih codelet %p failed (symbol `%s') (err: ENODEV)\n",
  482. task, task->cl,
  483. (codelet == NULL) ? "none" :
  484. task->cl->name ? task->cl->name :
  485. (task->cl->model && task->cl->model->symbol)?task->cl->model->symbol:"none");
  486. task->destroy = 0;
  487. starpu_task_destroy(task);
  488. }
  489. }
  490. return _starpu_mpi_task_postbuild_v(comm, xrank, do_execute, descrs, nb_data);
  491. }
  492. int starpu_mpi_task_insert(MPI_Comm comm, struct starpu_codelet *codelet, ...)
  493. {
  494. va_list varg_list;
  495. int ret;
  496. va_start(varg_list, codelet);
  497. ret = _starpu_mpi_task_insert_v(comm, codelet, varg_list);
  498. va_end(varg_list);
  499. return ret;
  500. }
  501. int starpu_mpi_insert_task(MPI_Comm comm, struct starpu_codelet *codelet, ...)
  502. {
  503. va_list varg_list;
  504. int ret;
  505. va_start(varg_list, codelet);
  506. ret = _starpu_mpi_task_insert_v(comm, codelet, varg_list);
  507. va_end(varg_list);
  508. return ret;
  509. }
  510. struct starpu_task *starpu_mpi_task_build(MPI_Comm comm, struct starpu_codelet *codelet, ...)
  511. {
  512. va_list varg_list;
  513. struct starpu_task *task;
  514. int ret;
  515. va_start(varg_list, codelet);
  516. ret = _starpu_mpi_task_build_v(comm, codelet, &task, NULL, NULL, NULL, varg_list);
  517. va_end(varg_list);
  518. STARPU_ASSERT(ret >= 0);
  519. if (ret > 0) return NULL; else return task;
  520. }
  521. int starpu_mpi_task_post_build(MPI_Comm comm, struct starpu_codelet *codelet, ...)
  522. {
  523. int xrank, do_execute;
  524. int ret, me, nb_nodes;
  525. va_list varg_list;
  526. struct starpu_data_descr *descrs;
  527. int nb_data;
  528. starpu_mpi_comm_rank(comm, &me);
  529. starpu_mpi_comm_size(comm, &nb_nodes);
  530. va_start(varg_list, codelet);
  531. /* Find out whether we are to execute the data because we own the data to be written to. */
  532. ret = _starpu_mpi_task_decode_v(codelet, me, nb_nodes, &xrank, &do_execute, &descrs, &nb_data, varg_list);
  533. va_end(varg_list);
  534. if (ret < 0) return ret;
  535. return _starpu_mpi_task_postbuild_v(comm, xrank, do_execute, descrs, nb_data);
  536. }
  537. void starpu_mpi_get_data_on_node_detached(MPI_Comm comm, starpu_data_handle_t data_handle, int node, void (*callback)(void*), void *arg)
  538. {
  539. int me, rank, tag;
  540. rank = starpu_mpi_data_get_rank(data_handle);
  541. tag = starpu_mpi_data_get_tag(data_handle);
  542. if (rank == -1)
  543. {
  544. _STARPU_ERROR("StarPU needs to be told the MPI rank of this data, using starpu_mpi_data_register() or starpu_mpi_data_register()\n");
  545. }
  546. if (tag == -1)
  547. {
  548. _STARPU_ERROR("StarPU needs to be told the MPI tag of this data, using starpu_mpi_data_register() or starpu_mpi_data_register()\n");
  549. }
  550. starpu_mpi_comm_rank(comm, &me);
  551. if (node == rank) return;
  552. if (me == node)
  553. {
  554. starpu_mpi_irecv_detached(data_handle, rank, tag, comm, callback, arg);
  555. }
  556. else if (me == rank)
  557. {
  558. starpu_mpi_isend_detached(data_handle, node, tag, comm, NULL, NULL);
  559. }
  560. }
  561. void starpu_mpi_get_data_on_node(MPI_Comm comm, starpu_data_handle_t data_handle, int node)
  562. {
  563. int me, rank, tag;
  564. rank = starpu_mpi_data_get_rank(data_handle);
  565. tag = starpu_mpi_data_get_tag(data_handle);
  566. if (rank == -1)
  567. {
  568. fprintf(stderr,"StarPU needs to be told the MPI rank of this data, using starpu_mpi_data_register\n");
  569. STARPU_ABORT();
  570. }
  571. if (tag == -1)
  572. {
  573. fprintf(stderr,"StarPU needs to be told the MPI tag of this data, using starpu_mpi_data_register\n");
  574. STARPU_ABORT();
  575. }
  576. starpu_mpi_comm_rank(comm, &me);
  577. if (node == rank) return;
  578. if (me == node)
  579. {
  580. MPI_Status status;
  581. starpu_mpi_recv(data_handle, rank, tag, comm, &status);
  582. }
  583. else if (me == rank)
  584. {
  585. starpu_mpi_send(data_handle, node, tag, comm);
  586. }
  587. }
  588. struct _starpu_mpi_redux_data_args
  589. {
  590. starpu_data_handle_t data_handle;
  591. starpu_data_handle_t new_handle;
  592. int tag;
  593. int node;
  594. MPI_Comm comm;
  595. struct starpu_task *taskB;
  596. };
  597. void _starpu_mpi_redux_data_dummy_func(STARPU_ATTRIBUTE_UNUSED void *buffers[], STARPU_ATTRIBUTE_UNUSED void *cl_arg)
  598. {
  599. }
  600. /* Dummy cost function for simgrid */
  601. static double cost_function(struct starpu_task *task STARPU_ATTRIBUTE_UNUSED, unsigned nimpl STARPU_ATTRIBUTE_UNUSED)
  602. {
  603. return 0.000001;
  604. }
  605. static struct starpu_perfmodel dumb_model =
  606. {
  607. .type = STARPU_COMMON,
  608. .cost_function = cost_function
  609. };
  610. static
  611. struct starpu_codelet _starpu_mpi_redux_data_read_cl =
  612. {
  613. .cpu_funcs = {_starpu_mpi_redux_data_dummy_func},
  614. .cuda_funcs = {_starpu_mpi_redux_data_dummy_func},
  615. .opencl_funcs = {_starpu_mpi_redux_data_dummy_func},
  616. .nbuffers = 1,
  617. .modes = {STARPU_R},
  618. .model = &dumb_model,
  619. .name = "_starpu_mpi_redux_data_read_cl"
  620. };
  621. struct starpu_codelet _starpu_mpi_redux_data_readwrite_cl =
  622. {
  623. .cpu_funcs = {_starpu_mpi_redux_data_dummy_func},
  624. .cuda_funcs = {_starpu_mpi_redux_data_dummy_func},
  625. .opencl_funcs = {_starpu_mpi_redux_data_dummy_func},
  626. .nbuffers = 1,
  627. .modes = {STARPU_RW},
  628. .model = &dumb_model,
  629. .name = "_starpu_mpi_redux_data_write_cl"
  630. };
  631. static
  632. void _starpu_mpi_redux_data_detached_callback(void *arg)
  633. {
  634. struct _starpu_mpi_redux_data_args *args = (struct _starpu_mpi_redux_data_args *) arg;
  635. STARPU_TASK_SET_HANDLE(args->taskB, args->new_handle, 1);
  636. int ret = starpu_task_submit(args->taskB);
  637. STARPU_ASSERT(ret == 0);
  638. starpu_data_unregister_submit(args->new_handle);
  639. free(args);
  640. }
  641. static
  642. void _starpu_mpi_redux_data_recv_callback(void *callback_arg)
  643. {
  644. struct _starpu_mpi_redux_data_args *args = (struct _starpu_mpi_redux_data_args *) callback_arg;
  645. starpu_data_register_same(&args->new_handle, args->data_handle);
  646. starpu_mpi_irecv_detached_sequential_consistency(args->new_handle, args->node, args->tag, args->comm, _starpu_mpi_redux_data_detached_callback, args, 0);
  647. }
  648. /* TODO: this should rather be implicitly called by starpu_mpi_task_insert when
  649. * a data previously accessed in REDUX mode gets accessed in R mode. */
  650. void starpu_mpi_redux_data(MPI_Comm comm, starpu_data_handle_t data_handle)
  651. {
  652. int me, rank, tag, nb_nodes;
  653. rank = starpu_mpi_data_get_rank(data_handle);
  654. tag = starpu_mpi_data_get_tag(data_handle);
  655. if (rank == -1)
  656. {
  657. fprintf(stderr,"StarPU needs to be told the MPI rank of this data, using starpu_mpi_data_register\n");
  658. STARPU_ABORT();
  659. }
  660. if (tag == -1)
  661. {
  662. fprintf(stderr,"StarPU needs to be told the MPI tag of this data, using starpu_mpi_data_register\n");
  663. STARPU_ABORT();
  664. }
  665. starpu_mpi_comm_rank(comm, &me);
  666. starpu_mpi_comm_size(comm, &nb_nodes);
  667. _STARPU_MPI_DEBUG(1, "Doing reduction for data %p on node %d with %d nodes ...\n", data_handle, rank, nb_nodes);
  668. // need to count how many nodes have the data in redux mode
  669. if (me == rank)
  670. {
  671. int i, j=0;
  672. struct starpu_task *taskBs[nb_nodes];
  673. for(i=0 ; i<nb_nodes ; i++)
  674. {
  675. if (i != rank)
  676. {
  677. /* We need to make sure all is
  678. * executed after data_handle finished
  679. * its last read access, we hence do
  680. * the following:
  681. * - submit an empty task A reading
  682. * data_handle whose callback submits
  683. * the mpi comm with sequential
  684. * consistency set to 0, whose
  685. * callback submits the redux_cl task
  686. * B with sequential consistency set
  687. * to 0,
  688. * - submit an empty task C reading
  689. * and writing data_handle and
  690. * depending on task B, just to replug
  691. * with implicit data dependencies
  692. * with tasks inserted after this
  693. * reduction.
  694. */
  695. struct _starpu_mpi_redux_data_args *args = malloc(sizeof(struct _starpu_mpi_redux_data_args));
  696. args->data_handle = data_handle;
  697. args->tag = tag;
  698. args->node = i;
  699. args->comm = comm;
  700. // We need to create taskB early as
  701. // taskC declares a dependancy on it
  702. args->taskB = starpu_task_create();
  703. args->taskB->cl = args->data_handle->redux_cl;
  704. args->taskB->sequential_consistency = 0;
  705. STARPU_TASK_SET_HANDLE(args->taskB, args->data_handle, 0);
  706. taskBs[j] = args->taskB; j++;
  707. // Submit taskA
  708. starpu_task_insert(&_starpu_mpi_redux_data_read_cl,
  709. STARPU_R, data_handle,
  710. STARPU_CALLBACK_WITH_ARG, _starpu_mpi_redux_data_recv_callback, args,
  711. 0);
  712. }
  713. }
  714. // Submit taskC which depends on all taskBs created
  715. struct starpu_task *taskC = starpu_task_create();
  716. taskC->cl = &_starpu_mpi_redux_data_readwrite_cl;
  717. STARPU_TASK_SET_HANDLE(taskC, data_handle, 0);
  718. starpu_task_declare_deps_array(taskC, j, taskBs);
  719. int ret = starpu_task_submit(taskC);
  720. STARPU_ASSERT(ret == 0);
  721. }
  722. else
  723. {
  724. _STARPU_MPI_DEBUG(1, "Sending redux handle to %d ...\n", rank);
  725. starpu_mpi_isend_detached(data_handle, rank, tag, comm, NULL, NULL);
  726. starpu_task_insert(data_handle->init_cl, STARPU_W, data_handle, 0);
  727. }
  728. /* FIXME: In order to prevent simultaneous receive submissions
  729. * on the same handle, we need to wait that all the starpu_mpi
  730. * tasks are done before submitting next tasks. The current
  731. * version of the implementation does not support multiple
  732. * simultaneous receive requests on the same handle.*/
  733. starpu_task_wait_for_all();
  734. }