starpu_mpi_task_insert.c 24 KB

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