410_mpi_support.doxy 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2019 CNRS
  4. * Copyright (C) 2011-2013,2016,2017 Inria
  5. * Copyright (C) 2009-2011,2013-2019 Université de Bordeaux
  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. /*! \page MPISupport MPI Support
  19. The integration of MPI transfers within task parallelism is done in a
  20. very natural way by the means of asynchronous interactions between the
  21. application and StarPU. This is implemented in a separate <c>libstarpumpi</c> library
  22. which basically provides "StarPU" equivalents of <c>MPI_*</c> functions, where
  23. <c>void *</c> buffers are replaced with ::starpu_data_handle_t, and all
  24. GPU-RAM-NIC transfers are handled efficiently by StarPU-MPI. The user has to
  25. use the usual <c>mpirun</c> command of the MPI implementation to start StarPU on
  26. the different MPI nodes.
  27. An MPI Insert Task function provides an even more seamless transition to a
  28. distributed application, by automatically issuing all required data transfers
  29. according to the task graph and an application-provided distribution.
  30. \section ExampleDocumentation Example Used In This Documentation
  31. The example below will be used as the base for this documentation. It
  32. initializes a token on node 0, and the token is passed from node to node,
  33. incremented by one on each step. The code is not using StarPU yet.
  34. \code{.c}
  35. for (loop = 0; loop < nloops; loop++)
  36. {
  37. int tag = loop*size + rank;
  38. if (loop == 0 && rank == 0)
  39. {
  40. token = 0;
  41. fprintf(stdout, "Start with token value %d\n", token);
  42. }
  43. else
  44. {
  45. MPI_Recv(&token, 1, MPI_INT, (rank+size-1)%size, tag, MPI_COMM_WORLD);
  46. }
  47. token++;
  48. if (loop == last_loop && rank == last_rank)
  49. {
  50. fprintf(stdout, "Finished: token value %d\n", token);
  51. }
  52. else
  53. {
  54. MPI_Send(&token, 1, MPI_INT, (rank+1)%size, tag+1, MPI_COMM_WORLD);
  55. }
  56. }
  57. \endcode
  58. \section NotUsingMPISupport About Not Using The MPI Support
  59. Although StarPU provides MPI support, the application programmer may want to
  60. keep his MPI communications as they are for a start, and only delegate task
  61. execution to StarPU. This is possible by just using starpu_data_acquire(), for
  62. instance:
  63. \code{.c}
  64. for (loop = 0; loop < nloops; loop++)
  65. {
  66. int tag = loop*size + rank;
  67. /* Acquire the data to be able to write to it */
  68. starpu_data_acquire(token_handle, STARPU_W);
  69. if (loop == 0 && rank == 0)
  70. {
  71. token = 0;
  72. fprintf(stdout, "Start with token value %d\n", token);
  73. }
  74. else
  75. {
  76. MPI_Recv(&token, 1, MPI_INT, (rank+size-1)%size, tag, MPI_COMM_WORLD);
  77. }
  78. starpu_data_release(token_handle);
  79. /* Task delegation to StarPU to increment the token. The execution might
  80. * be performed on a CPU, a GPU, etc. */
  81. increment_token();
  82. /* Acquire the update data to be able to read from it */
  83. starpu_data_acquire(token_handle, STARPU_R);
  84. if (loop == last_loop && rank == last_rank)
  85. {
  86. fprintf(stdout, "Finished: token value %d\n", token);
  87. }
  88. else
  89. {
  90. MPI_Send(&token, 1, MPI_INT, (rank+1)%size, tag+1, MPI_COMM_WORLD);
  91. }
  92. starpu_data_release(token_handle);
  93. }
  94. \endcode
  95. In that case, <c>libstarpumpi</c> is not needed. One can also use <c>MPI_Isend()</c> and
  96. <c>MPI_Irecv()</c>, by calling starpu_data_release() after <c>MPI_Wait()</c> or <c>MPI_Test()</c>
  97. have notified completion.
  98. It is however better to use <c>libstarpumpi</c>, to save the application from having to
  99. synchronize with starpu_data_acquire(), and instead just submit all tasks and
  100. communications asynchronously, and wait for the overall completion.
  101. \section SimpleExample Simple Example
  102. The flags required to compile or link against the MPI layer are
  103. accessible with the following commands:
  104. \verbatim
  105. $ pkg-config --cflags starpumpi-1.3 # options for the compiler
  106. $ pkg-config --libs starpumpi-1.3 # options for the linker
  107. \endverbatim
  108. \code{.c}
  109. void increment_token(void)
  110. {
  111. struct starpu_task *task = starpu_task_create();
  112. task->cl = &increment_cl;
  113. task->handles[0] = token_handle;
  114. starpu_task_submit(task);
  115. }
  116. int main(int argc, char **argv)
  117. {
  118. int rank, size;
  119. starpu_mpi_init_conf(&argc, &argv, 1, MPI_COMM_WORLD, NULL);
  120. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  121. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  122. starpu_vector_data_register(&token_handle, STARPU_MAIN_RAM, (uintptr_t)&token, 1, sizeof(unsigned));
  123. unsigned nloops = NITER;
  124. unsigned loop;
  125. unsigned last_loop = nloops - 1;
  126. unsigned last_rank = size - 1;
  127. for (loop = 0; loop < nloops; loop++)
  128. {
  129. int tag = loop*size + rank;
  130. if (loop == 0 && rank == 0)
  131. {
  132. starpu_data_acquire(token_handle, STARPU_W);
  133. token = 0;
  134. fprintf(stdout, "Start with token value %d\n", token);
  135. starpu_data_release(token_handle);
  136. }
  137. else
  138. {
  139. starpu_mpi_irecv_detached(token_handle, (rank+size-1)%size, tag, MPI_COMM_WORLD, NULL, NULL);
  140. }
  141. increment_token();
  142. if (loop == last_loop && rank == last_rank)
  143. {
  144. starpu_data_acquire(token_handle, STARPU_R);
  145. fprintf(stdout, "Finished: token value %d\n", token);
  146. starpu_data_release(token_handle);
  147. }
  148. else
  149. {
  150. starpu_mpi_isend_detached(token_handle, (rank+1)%size, tag+1, MPI_COMM_WORLD, NULL, NULL);
  151. }
  152. }
  153. starpu_task_wait_for_all();
  154. starpu_mpi_shutdown();
  155. if (rank == last_rank)
  156. {
  157. fprintf(stderr, "[%d] token = %d == %d * %d ?\n", rank, token, nloops, size);
  158. STARPU_ASSERT(token == nloops*size);
  159. }
  160. \endcode
  161. We have here replaced <c>MPI_Recv()</c> and <c>MPI_Send()</c> with starpu_mpi_irecv_detached()
  162. and starpu_mpi_isend_detached(), which just submit the communication to be
  163. performed. The implicit sequential consistency dependencies provide
  164. synchronization between mpi reception and emission and the corresponding tasks.
  165. The only remaining synchronization with starpu_data_acquire() is at
  166. the beginning and the end.
  167. \section MPIInitialization How to Initialize StarPU-MPI
  168. As seen in the previous example, one has to call starpu_mpi_init_conf() to
  169. initialize StarPU-MPI. The third parameter of the function indicates
  170. if MPI should be initialized by StarPU or if the application did it
  171. itself. If the application initializes MPI itself, it must call
  172. <c>MPI_Init_thread()</c> with <c>MPI_THREAD_SERIALIZED</c> or
  173. <c>MPI_THREAD_MULTIPLE</c>, since StarPU-MPI uses a separate thread to
  174. perform the communications. <c>MPI_THREAD_MULTIPLE</c> is necessary if
  175. the application also performs some MPI communications.
  176. \section PointToPointCommunication Point To Point Communication
  177. The standard point to point communications of MPI have been
  178. implemented. The semantic is similar to the MPI one, but adapted to
  179. the DSM provided by StarPU. A MPI request will only be submitted when
  180. the data is available in the main memory of the node submitting the
  181. request.
  182. There are two types of asynchronous communications: the classic
  183. asynchronous communications and the detached communications. The
  184. classic asynchronous communications (starpu_mpi_isend() and
  185. starpu_mpi_irecv()) need to be followed by a call to
  186. starpu_mpi_wait() or to starpu_mpi_test() to wait for or to
  187. test the completion of the communication. Waiting for or testing the
  188. completion of detached communications is not possible, this is done
  189. internally by StarPU-MPI, on completion, the resources are
  190. automatically released. This mechanism is similar to the pthread
  191. detach state attribute which determines whether a thread will be
  192. created in a joinable or a detached state.
  193. For send communications, data is acquired with the mode ::STARPU_R.
  194. When using the \c configure option
  195. \ref enable-mpi-pedantic-isend "--enable-mpi-pedantic-isend", the mode
  196. ::STARPU_RW is used to make sure there is no more than 1 concurrent
  197. \c MPI_Isend() call accessing a data.
  198. Internally, all communication are divided in 2 communications, a first
  199. message is used to exchange an envelope describing the data (i.e its
  200. tag and its size), the data itself is sent in a second message. All
  201. MPI communications submitted by StarPU uses a unique tag which has a
  202. default value, and can be accessed with the functions
  203. starpu_mpi_get_communication_tag() and
  204. starpu_mpi_set_communication_tag(). The matching of tags with
  205. corresponding requests is done within StarPU-MPI.
  206. For any userland communication, the call of the corresponding function
  207. (e.g starpu_mpi_isend()) will result in the creation of a StarPU-MPI
  208. request, the function starpu_data_acquire_cb() is then called to
  209. asynchronously request StarPU to fetch the data in main memory; when
  210. the data is ready and the corresponding buffer has already been
  211. received by MPI, it will be copied in the memory of the data,
  212. otherwise the request is stored in the <em>early requests list</em>. Sending
  213. requests are stored in the <em>ready requests list</em>.
  214. While requests need to be processed, the StarPU-MPI progression thread
  215. does the following:
  216. <ol>
  217. <li> it polls the <em>ready requests list</em>. For all the ready
  218. requests, the appropriate function is called to post the corresponding
  219. MPI call. For example, an initial call to starpu_mpi_isend() will
  220. result in a call to <c>MPI_Isend()</c>. If the request is marked as
  221. detached, the request will then be added in the <em>detached requests
  222. list</em>.
  223. </li>
  224. <li> it posts a <c>MPI_Irecv()</c> to retrieve a data envelope.
  225. </li>
  226. <li> it polls the <em>detached requests list</em>. For all the detached
  227. requests, it tests its completion of the MPI request by calling
  228. <c>MPI_Test()</c>. On completion, the data handle is released, and if a
  229. callback was defined, it is called.
  230. </li>
  231. <li> finally, it checks if a data envelope has been received. If so,
  232. if the data envelope matches a request in the <em>early requests list</em> (i.e
  233. the request has already been posted by the application), the
  234. corresponding MPI call is posted (similarly to the first step above).
  235. If the data envelope does not match any application request, a
  236. temporary handle is created to receive the data, a StarPU-MPI request
  237. is created and added into the <em>ready requests list</em>, and thus will be
  238. processed in the first step of the next loop.
  239. </li>
  240. </ol>
  241. \ref MPIPtpCommunication gives the list of all the
  242. point to point communications defined in StarPU-MPI.
  243. \section ExchangingUserDefinedDataInterface Exchanging User Defined Data Interface
  244. New data interfaces defined as explained in \ref DefiningANewDataInterface
  245. can also be used within StarPU-MPI and
  246. exchanged between nodes. Two functions needs to be defined through the
  247. type starpu_data_interface_ops. The function
  248. starpu_data_interface_ops::pack_data takes a handle and returns a
  249. contiguous memory buffer allocated with
  250. \code{.c}
  251. starpu_malloc_flags(ptr, size, 0)
  252. \endcode
  253. along with its size where data to be conveyed
  254. to another node should be copied. The reversed operation is
  255. implemented in the function starpu_data_interface_ops::unpack_data which
  256. takes a contiguous memory buffer and recreates the data handle.
  257. \code{.c}
  258. static int complex_pack_data(starpu_data_handle_t handle, unsigned node, void **ptr, ssize_t *count)
  259. {
  260. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  261. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, node);
  262. *count = complex_get_size(handle);
  263. starpu_malloc_flags(ptr, *count, 0);
  264. memcpy(*ptr, complex_interface->real, complex_interface->nx*sizeof(double));
  265. memcpy(*ptr+complex_interface->nx*sizeof(double), complex_interface->imaginary, complex_interface->nx*sizeof(double));
  266. return 0;
  267. }
  268. static int complex_unpack_data(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  269. {
  270. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  271. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, node);
  272. memcpy(complex_interface->real, ptr, complex_interface->nx*sizeof(double));
  273. memcpy(complex_interface->imaginary, ptr+complex_interface->nx*sizeof(double), complex_interface->nx*sizeof(double));
  274. return 0;
  275. }
  276. static struct starpu_data_interface_ops interface_complex_ops =
  277. {
  278. ...
  279. .pack_data = complex_pack_data,
  280. .unpack_data = complex_unpack_data
  281. };
  282. \endcode
  283. Instead of defining pack and unpack operations, users may want to attach a MPI type to their user defined data interface. The function starpu_mpi_datatype_register() allows to do so. This function takes 3 parameters: the data handle for which the MPI datatype is going to be defined, a function's pointer that will create the MPI datatype, and a function's pointer that will free the MPI datatype.
  284. \code{.c}
  285. starpu_data_interface handle;
  286. starpu_complex_data_register(&handle, STARPU_MAIN_RAM, real, imaginary, 2);
  287. starpu_mpi_datatype_register(handle, starpu_complex_interface_datatype_allocate, starpu_complex_interface_datatype_free);
  288. \endcode
  289. The functions to create and free the MPI datatype are defined as follows.
  290. \code{.c}
  291. void starpu_complex_interface_datatype_allocate(starpu_data_handle_t handle, MPI_Datatype *mpi_datatype)
  292. {
  293. int ret;
  294. int blocklengths[2];
  295. MPI_Aint displacements[2];
  296. MPI_Datatype types[2] = {MPI_DOUBLE, MPI_DOUBLE};
  297. struct starpu_complex_interface *complex_interface = (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  298. MPI_Get_address(complex_interface, displacements);
  299. MPI_Get_address(&complex_interface->imaginary, displacements+1);
  300. displacements[1] -= displacements[0];
  301. displacements[0] = 0;
  302. blocklengths[0] = complex_interface->nx;
  303. blocklengths[1] = complex_interface->nx;
  304. ret = MPI_Type_create_struct(2, blocklengths, displacements, types, mpi_datatype);
  305. STARPU_ASSERT_MSG(ret == MPI_SUCCESS, "MPI_Type_contiguous failed");
  306. ret = MPI_Type_commit(mpi_datatype);
  307. STARPU_ASSERT_MSG(ret == MPI_SUCCESS, "MPI_Type_commit failed");
  308. }
  309. void starpu_complex_interface_datatype_free(MPI_Datatype *mpi_datatype)
  310. {
  311. MPI_Type_free(mpi_datatype);
  312. }
  313. \endcode
  314. Note that it is important to make sure no communication is going to occur before the function starpu_mpi_datatype_register() is called. This would produce an undefined result as the data may be received before the function is called, and so the MPI datatype would not be known by the StarPU-MPI communication engine, and the data would be processed with the pack and unpack operations.
  315. \code{.c}
  316. starpu_data_interface handle;
  317. starpu_complex_data_register(&handle, STARPU_MAIN_RAM, real, imaginary, 2);
  318. starpu_mpi_datatype_register(handle, starpu_complex_interface_datatype_allocate, starpu_complex_interface_datatype_free);
  319. starpu_mpi_barrier(MPI_COMM_WORLD);
  320. \endcode
  321. \section MPIInsertTaskUtility MPI Insert Task Utility
  322. To save the programmer from having to explicit all communications, StarPU
  323. provides an "MPI Insert Task Utility". The principe is that the application
  324. decides a distribution of the data over the MPI nodes by allocating it and
  325. notifying StarPU of this decision, i.e. tell StarPU which MPI node "owns"
  326. which data. It also decides, for each handle, an MPI tag which will be used to
  327. exchange the content of the handle. All MPI nodes then process the whole task
  328. graph, and StarPU automatically determines which node actually execute which
  329. task, and trigger the required MPI transfers.
  330. The list of functions is described in \ref MPIInsertTask.
  331. Here an stencil example showing how to use starpu_mpi_task_insert(). One
  332. first needs to define a distribution function which specifies the
  333. locality of the data. Note that the data needs to be registered to MPI
  334. by calling starpu_mpi_data_register(). This function allows to set
  335. the distribution information and the MPI tag which should be used when
  336. communicating the data. It also allows to automatically clear the MPI
  337. communication cache when unregistering the data.
  338. \code{.c}
  339. /* Returns the MPI node number where data is */
  340. int my_distrib(int x, int y, int nb_nodes)
  341. {
  342. /* Block distrib */
  343. return ((int)(x / sqrt(nb_nodes) + (y / sqrt(nb_nodes)) * sqrt(nb_nodes))) % nb_nodes;
  344. // /* Other examples useful for other kinds of computations */
  345. // /* / distrib */
  346. // return (x+y) % nb_nodes;
  347. // /* Block cyclic distrib */
  348. // unsigned side = sqrt(nb_nodes);
  349. // return x % side + (y % side) * size;
  350. }
  351. \endcode
  352. Now the data can be registered within StarPU. Data which are not
  353. owned but will be needed for computations can be registered through
  354. the lazy allocation mechanism, i.e. with a <c>home_node</c> set to <c>-1</c>.
  355. StarPU will automatically allocate the memory when it is used for the
  356. first time.
  357. One can note an optimization here (the <c>else if</c> test): we only register
  358. data which will be needed by the tasks that we will execute.
  359. \code{.c}
  360. unsigned matrix[X][Y];
  361. starpu_data_handle_t data_handles[X][Y];
  362. for(x = 0; x < X; x++)
  363. {
  364. for (y = 0; y < Y; y++)
  365. {
  366. int mpi_rank = my_distrib(x, y, size);
  367. if (mpi_rank == my_rank)
  368. /* Owning data */
  369. starpu_variable_data_register(&data_handles[x][y], STARPU_MAIN_RAM, (uintptr_t)&(matrix[x][y]), sizeof(unsigned));
  370. else if (my_rank == my_distrib(x+1, y, size) || my_rank == my_distrib(x-1, y, size)
  371. || my_rank == my_distrib(x, y+1, size) || my_rank == my_distrib(x, y-1, size))
  372. /* I don't own this index, but will need it for my computations */
  373. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(unsigned));
  374. else
  375. /* I know it's useless to allocate anything for this */
  376. data_handles[x][y] = NULL;
  377. if (data_handles[x][y])
  378. {
  379. starpu_mpi_data_register(data_handles[x][y], x*X+y, mpi_rank);
  380. }
  381. }
  382. }
  383. \endcode
  384. Now starpu_mpi_task_insert() can be called for the different
  385. steps of the application.
  386. \code{.c}
  387. for(loop=0 ; loop<niter; loop++)
  388. for (x = 1; x < X-1; x++)
  389. for (y = 1; y < Y-1; y++)
  390. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl,
  391. STARPU_RW, data_handles[x][y],
  392. STARPU_R, data_handles[x-1][y],
  393. STARPU_R, data_handles[x+1][y],
  394. STARPU_R, data_handles[x][y-1],
  395. STARPU_R, data_handles[x][y+1],
  396. 0);
  397. starpu_task_wait_for_all();
  398. \endcode
  399. I.e. all MPI nodes process the whole task graph, but as mentioned above, for
  400. each task, only the MPI node which owns the data being written to (here,
  401. <c>data_handles[x][y]</c>) will actually run the task. The other MPI nodes will
  402. automatically send the required data.
  403. To tune the placement of tasks among MPI nodes, one can use
  404. ::STARPU_EXECUTE_ON_NODE or ::STARPU_EXECUTE_ON_DATA to specify an explicit
  405. node, or the node of a given data (e.g. one of the parameters), or use
  406. starpu_mpi_node_selection_register_policy() and ::STARPU_NODE_SELECTION_POLICY
  407. to provide a dynamic policy.
  408. A function starpu_mpi_task_build() is also provided with the aim to
  409. only construct the task structure. All MPI nodes need to call the
  410. function, which posts the required send/recv on the various nodes which have to.
  411. Only the node which is to execute the task will then return a
  412. valid task structure, others will return <c>NULL</c>. This node must submit the task.
  413. All nodes then need to call the function starpu_mpi_task_post_build() -- with the same
  414. list of arguments as starpu_mpi_task_build() -- to post all the
  415. necessary data communications meant to happen after the task execution.
  416. \code{.c}
  417. struct starpu_task *task;
  418. task = starpu_mpi_task_build(MPI_COMM_WORLD, &cl,
  419. STARPU_RW, data_handles[0],
  420. STARPU_R, data_handles[1],
  421. 0);
  422. if (task) starpu_task_submit(task);
  423. starpu_mpi_task_post_build(MPI_COMM_WORLD, &cl,
  424. STARPU_RW, data_handles[0],
  425. STARPU_R, data_handles[1],
  426. 0);
  427. \endcode
  428. \section MPIInsertPruning Pruning MPI Task Insertion
  429. Making all MPI nodes process the whole graph can be a concern with a growing
  430. number of nodes. To avoid this, the
  431. application can prune the task for loops according to the data distribution,
  432. so as to only submit tasks on nodes which have to care about them (either to
  433. execute them, or to send the required data).
  434. A way to do some of this quite easily can be to just add an <c>if</c> like this:
  435. \code{.c}
  436. for(loop=0 ; loop<niter; loop++)
  437. for (x = 1; x < X-1; x++)
  438. for (y = 1; y < Y-1; y++)
  439. if (my_distrib(x,y,size) == my_rank
  440. || my_distrib(x-1,y,size) == my_rank
  441. || my_distrib(x+1,y,size) == my_rank
  442. || my_distrib(x,y-1,size) == my_rank
  443. || my_distrib(x,y+1,size) == my_rank)
  444. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl,
  445. STARPU_RW, data_handles[x][y],
  446. STARPU_R, data_handles[x-1][y],
  447. STARPU_R, data_handles[x+1][y],
  448. STARPU_R, data_handles[x][y-1],
  449. STARPU_R, data_handles[x][y+1],
  450. 0);
  451. starpu_task_wait_for_all();
  452. \endcode
  453. This permits to drop the cost of function call argument passing and parsing.
  454. If the <c>my_distrib</c> function can be inlined by the compiler, the latter can
  455. improve the test.
  456. If the <c>size</c> can be made a compile-time constant, the compiler can
  457. considerably improve the test further.
  458. If the distribution function is not too complex and the compiler is very good,
  459. the latter can even optimize the <c>for</c> loops, thus dramatically reducing
  460. the cost of task submission.
  461. To estimate quickly how long task submission takes, and notably how much pruning
  462. saves, a quick and easy way is to measure the submission time of just one of the
  463. MPI nodes. This can be achieved by running the application on just one MPI node
  464. with the following environment variables:
  465. \code
  466. export STARPU_DISABLE_KERNELS=1
  467. export STARPU_MPI_FAKE_RANK=2
  468. export STARPU_MPI_FAKE_SIZE=1024
  469. \endcode
  470. Here we have disabled the kernel function call to skip the actual computation
  471. time and only keep submission time, and we have asked StarPU to fake running on
  472. MPI node 2 out of 1024 nodes.
  473. \section MPITemporaryData Temporary Data
  474. To be able to use starpu_mpi_task_insert(), one has to call
  475. starpu_mpi_data_register(), so that StarPU-MPI can know what it needs to do for
  476. each data. Parameters of starpu_mpi_data_register() are normally the same on all
  477. nodes for a given data, so that all nodes agree on which node owns the data, and
  478. which tag is used to transfer its value.
  479. It can however be useful to register e.g. some temporary data on just one node,
  480. without having to register a dumb handle on all nodes, while only one node will
  481. actually need to know about it. In this case, nodes which will not need the data
  482. can just pass \c NULL to starpu_mpi_task_insert():
  483. \code{.c}
  484. starpu_data_handle_t data0 = NULL;
  485. if (rank == 0)
  486. {
  487. starpu_variable_data_register(&data0, STARPU_MAIN_RAM, (uintptr_t) &val0, sizeof(val0));
  488. starpu_mpi_data_register(data0, 0, rank);
  489. }
  490. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl, STARPU_W, data0, 0); /* Executes on node 0 */
  491. \endcode
  492. Here, nodes whose rank is not \c 0 will simply not take care of the data, and consider it to be on another node.
  493. This can be mixed various way, for instance here node \c 1 determines that it does
  494. not have to care about \c data0, but knows that it should send the value of its
  495. \c data1 to node \c 0, which owns data and thus will need the value of \c data1 to execute the task:
  496. \code{.c}
  497. starpu_data_handle_t data0 = NULL, data1, data;
  498. if (rank == 0)
  499. {
  500. starpu_variable_data_register(&data0, STARPU_MAIN_RAM, (uintptr_t) &val0, sizeof(val0));
  501. starpu_mpi_data_register(data0, -1, rank);
  502. starpu_variable_data_register(&data1, -1, 0, sizeof(val1));
  503. starpu_variable_data_register(&data, STARPU_MAIN_RAM, (uintptr_t) &val, sizeof(val));
  504. }
  505. else if (rank == 1)
  506. {
  507. starpu_variable_data_register(&data1, STARPU_MAIN_RAM, (uintptr_t) &val1, sizeof(val1));
  508. starpu_variable_data_register(&data, -1, 0, sizeof(val));
  509. }
  510. starpu_mpi_data_register(data, 42, 0);
  511. starpu_mpi_data_register(data1, 43, 1);
  512. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl, STARPU_W, data, STARPU_R, data0, STARPU_R, data1, 0); /* Executes on node 0 */
  513. \endcode
  514. \section MPIPerNodeData Per-node Data
  515. Further than temporary data on just one node, one may want per-node data,
  516. to e.g. replicate some computation because that is less expensive than
  517. communicating the value over MPI:
  518. \code{.c}
  519. starpu_data_handle pernode, data0, data1;
  520. starpu_variable_data_register(&pernode, -1, 0, sizeof(val));
  521. starpu_mpi_data_register(pernode, -1, STARPU_MPI_PER_NODE);
  522. /* Normal data: one on node0, one on node1 */
  523. if (rank == 0)
  524. {
  525. starpu_variable_data_register(&data0, STARPU_MAIN_RAM, (uintptr_t) &val0, sizeof(val0));
  526. starpu_variable_data_register(&data1, -1, 0, sizeof(val1));
  527. }
  528. else if (rank == 1)
  529. {
  530. starpu_variable_data_register(&data0, -1, 0, sizeof(val1));
  531. starpu_variable_data_register(&data1, STARPU_MAIN_RAM, (uintptr_t) &val1, sizeof(val1));
  532. }
  533. starpu_mpi_data_register(data0, 42, 0);
  534. starpu_mpi_data_register(data1, 43, 1);
  535. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl, STARPU_W, pernode, 0); /* Will be replicated on all nodes */
  536. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl2, STARPU_RW, data0, STARPU_R, pernode); /* Will execute on node 0, using its own pernode*/
  537. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl2, STARPU_RW, data1, STARPU_R, pernode); /* Will execute on node 1, using its own pernode*/
  538. \endcode
  539. One can turn a normal data into pernode data, by first broadcasting it to all nodes:
  540. \code{.c}
  541. starpu_data_handle data;
  542. starpu_variable_data_register(&data, -1, 0, sizeof(val));
  543. starpu_mpi_data_register(data, 42, 0);
  544. /* Compute some value */
  545. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl, STARPU_W, data, 0); /* Node 0 computes it */
  546. /* Get it on all nodes */
  547. starpu_mpi_get_data_on_all_nodes_detached(MPI_COMM_WORLD, data);
  548. /* And turn it per-node */
  549. starpu_mpi_data_set_rank(data, STARPU_MPI_PER_NODE);
  550. \endcode
  551. The data can then be used just like pernode above.
  552. \section MPIPriorities Priorities
  553. All send functions have a <c>_prio</c> variant which takes an additional
  554. priority parameter, which allows to make StarPU-MPI change the order of MPI
  555. requests before submitting them to MPI. The default priority is \c 0.
  556. When using the starpu_mpi_task_insert() helper, ::STARPU_PRIORITY defines both the
  557. task priority and the MPI requests priority.
  558. To test how much MPI priorities have a good effect on performance, you can
  559. set the environment variable \ref STARPU_MPI_PRIORITIES to \c 0 to disable the use of
  560. priorities in StarPU-MPI.
  561. \section MPICache MPI Cache Support
  562. StarPU-MPI automatically optimizes duplicate data transmissions: if an MPI
  563. node \c B needs a piece of data \c D from MPI node \c A for several tasks, only one
  564. transmission of \c D will take place from \c A to \c B, and the value of \c D will be kept
  565. on \c B as long as no task modifies \c D.
  566. If a task modifies \c D, \c B will wait for all tasks which need the previous value of
  567. \c D, before invalidating the value of \c D. As a consequence, it releases the memory
  568. occupied by \c D. Whenever a task running on \c B needs the new value of \c D, allocation
  569. will take place again to receive it.
  570. Since tasks can be submitted dynamically, StarPU-MPI can not know whether the
  571. current value of data \c D will again be used by a newly-submitted task before
  572. being modified by another newly-submitted task, so until a task is submitted to
  573. modify the current value, it can not decide by itself whether to flush the cache
  574. or not. The application can however explicitly tell StarPU-MPI to flush the
  575. cache by calling starpu_mpi_cache_flush() or starpu_mpi_cache_flush_all_data(),
  576. for instance in case the data will not be used at all any more (see for instance
  577. the cholesky example in <c>mpi/examples/matrix_decomposition</c>), or at least not in
  578. the close future. If a newly-submitted task actually needs the value again,
  579. another transmission of \c D will be initiated from \c A to \c B. A mere
  580. starpu_mpi_cache_flush_all_data() can for instance be added at the end of the whole
  581. algorithm, to express that no data will be reused after this (or at least that
  582. it is not interesting to keep them in cache). It may however be interesting to
  583. add fine-graph starpu_mpi_cache_flush() calls during the algorithm; the effect
  584. for the data deallocation will be the same, but it will additionally release some
  585. pressure from the StarPU-MPI cache hash table during task submission.
  586. One can determine whether a piece of is cached with starpu_mpi_cached_receive()
  587. and starpu_mpi_cached_send().
  588. The whole caching behavior can be disabled thanks to the \ref STARPU_MPI_CACHE
  589. environment variable. The variable \ref STARPU_MPI_CACHE_STATS can be set to <c>1</c>
  590. to enable the runtime to display messages when data are added or removed
  591. from the cache holding the received data.
  592. \section MPIMigration MPI Data Migration
  593. The application can dynamically change its mind about the data distribution, to
  594. balance the load over MPI nodes for instance. This can be done very simply by
  595. requesting an explicit move and then change the registered rank. For instance,
  596. we here switch to a new distribution function <c>my_distrib2</c>: we first
  597. register any data which wasn't registered already and will be needed, then
  598. migrate the data, and register the new location.
  599. \code{.c}
  600. for(x = 0; x < X; x++)
  601. {
  602. for (y = 0; y < Y; y++)
  603. {
  604. int mpi_rank = my_distrib2(x, y, size);
  605. if (!data_handles[x][y] && (mpi_rank == my_rank
  606. || my_rank == my_distrib(x+1, y, size) || my_rank == my_distrib(x-1, y, size)
  607. || my_rank == my_distrib(x, y+1, size) || my_rank == my_distrib(x, y-1, size)))
  608. /* Register newly-needed data */
  609. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(unsigned));
  610. if (data_handles[x][y])
  611. {
  612. /* Migrate the data */
  613. starpu_mpi_data_migrate(MPI_COMM_WORLD, data_handles[x][y], mpi_rank);
  614. }
  615. }
  616. }
  617. \endcode
  618. From then on, further tasks submissions will use the new data distribution,
  619. which will thus change both MPI communications and task assignments.
  620. Very importantly, since all nodes have to agree on which node owns which data
  621. so as to determine MPI communications and task assignments the same way, all
  622. nodes have to perform the same data migration, and at the same point among task
  623. submissions. It thus does not require a strict synchronization, just a clear
  624. separation of task submissions before and after the data redistribution.
  625. Before data unregistration, it has to be migrated back to its original home
  626. node (the value, at least), since that is where the user-provided buffer
  627. resides. Otherwise the unregistration will complain that it does not have the
  628. latest value on the original home node.
  629. \code{.c}
  630. for(x = 0; x < X; x++)
  631. {
  632. for (y = 0; y < Y; y++)
  633. {
  634. if (data_handles[x][y])
  635. {
  636. int mpi_rank = my_distrib(x, y, size);
  637. /* Get back data to original place where the user-provided buffer is. */
  638. starpu_mpi_get_data_on_node_detached(MPI_COMM_WORLD, data_handles[x][y], mpi_rank, NULL, NULL);
  639. /* And unregister it */
  640. starpu_data_unregister(data_handles[x][y]);
  641. }
  642. }
  643. }
  644. \endcode
  645. \section MPICollective MPI Collective Operations
  646. The functions are described in \ref MPICollectiveOperations.
  647. \code{.c}
  648. if (rank == root)
  649. {
  650. /* Allocate the vector */
  651. vector = malloc(nblocks * sizeof(float *));
  652. for(x=0 ; x<nblocks ; x++)
  653. {
  654. starpu_malloc((void **)&vector[x], block_size*sizeof(float));
  655. }
  656. }
  657. /* Allocate data handles and register data to StarPU */
  658. data_handles = malloc(nblocks*sizeof(starpu_data_handle_t *));
  659. for(x = 0; x < nblocks ; x++)
  660. {
  661. int mpi_rank = my_distrib(x, nodes);
  662. if (rank == root)
  663. {
  664. starpu_vector_data_register(&data_handles[x], STARPU_MAIN_RAM, (uintptr_t)vector[x], blocks_size, sizeof(float));
  665. }
  666. else if ((mpi_rank == rank) || ((rank == mpi_rank+1 || rank == mpi_rank-1)))
  667. {
  668. /* I own this index, or i will need it for my computations */
  669. starpu_vector_data_register(&data_handles[x], -1, (uintptr_t)NULL, block_size, sizeof(float));
  670. }
  671. else
  672. {
  673. /* I know it's useless to allocate anything for this */
  674. data_handles[x] = NULL;
  675. }
  676. if (data_handles[x])
  677. {
  678. starpu_mpi_data_register(data_handles[x], x*nblocks+y, mpi_rank);
  679. }
  680. }
  681. /* Scatter the matrix among the nodes */
  682. starpu_mpi_scatter_detached(data_handles, nblocks, root, MPI_COMM_WORLD, NULL, NULL, NULL, NULL);
  683. /* Calculation */
  684. for(x = 0; x < nblocks ; x++)
  685. {
  686. if (data_handles[x])
  687. {
  688. int owner = starpu_data_get_rank(data_handles[x]);
  689. if (owner == rank)
  690. {
  691. starpu_task_insert(&cl, STARPU_RW, data_handles[x], 0);
  692. }
  693. }
  694. }
  695. /* Gather the matrix on main node */
  696. starpu_mpi_gather_detached(data_handles, nblocks, 0, MPI_COMM_WORLD, NULL, NULL, NULL, NULL);
  697. \endcode
  698. Other collective operations would be easy to define, just ask starpu-devel for
  699. them!
  700. \section MPIDriver Make StarPU-MPI Progression Thread Execute Tasks
  701. The default behaviour of StarPU-MPI is to spawn an MPI thread to take care only
  702. of MPI communications in an active fashion (i.e the StarPU-MPI thread sleeps
  703. only when there is no active request submitted by the application), with the
  704. goal of being as reactive as possible to communications. Knowing that, users
  705. usually leave one free core for the MPI thread when starting a distributed
  706. execution with StarPU-MPI. However, this could result in a loss of performance
  707. for applications that does not require an extreme reactivity to MPI
  708. communications.
  709. The starpu_mpi_init_conf() routine allows the user to give the
  710. starpu_conf configuration structure of StarPU (usually given to the
  711. starpu_init() routine) to StarPU-MPI, so that StarPU-MPI reserves for its own
  712. use one of the CPU drivers of the current computing node, or one of the CPU
  713. cores, and then calls starpu_init() internally.
  714. This allows the MPI communication thread to call a StarPU CPU driver to run
  715. tasks when there is no active requests to take care of, and thus recover the
  716. computational power of the "lost" core. Since there is a trade-off between
  717. executing tasks and polling MPI requests, which is how much the application
  718. wants to lose in reactivity to MPI communications to get back the computing
  719. power of the core dedicated to the StarPU-MPI thread, there are two environment
  720. variables to pilot the behaviour of the MPI thread so that users can tune
  721. this trade-off depending of the behaviour of the application.
  722. The \ref STARPU_MPI_DRIVER_CALL_FREQUENCY environment variable sets how many times
  723. the MPI progression thread goes through the MPI_Test() loop on each active communication request
  724. (and thus try to make communications progress by going into the MPI layer)
  725. before executing tasks. The default value for this environment variable is 0,
  726. which means that the support for interleaving task execution and communication
  727. polling is deactivated, thus returning the MPI progression thread to its
  728. original behaviour.
  729. The \ref STARPU_MPI_DRIVER_TASK_FREQUENCY environment variable sets how many tasks
  730. are executed by the MPI communication thread before checking all active
  731. requests again. While this environment variable allows a better use of the core
  732. dedicated to StarPU-MPI for computations, it also decreases the reactivity of
  733. the MPI communication thread as much.
  734. \section MPIDebug Debugging MPI
  735. Communication trace will be enabled when the environment variable
  736. \ref STARPU_MPI_COMM is set to \c 1, and StarPU has been configured with the
  737. option \ref enable-verbose "--enable-verbose".
  738. Statistics will be enabled for the communication cache when the
  739. environment variable \ref STARPU_MPI_CACHE_STATS is set to \c 1. It
  740. prints messages on the standard output when data are added or removed
  741. from the received communication cache.
  742. When the environment variable \ref STARPU_COMM_STATS is set to \c 1,
  743. StarPU will display at the end of the execution for each node the
  744. volume and the bandwidth of data sent to all the other nodes.
  745. Here an example of such a trace.
  746. \verbatim
  747. [starpu_comm_stats][3] TOTAL: 476.000000 B 0.000454 MB 0.000098 B/s 0.000000 MB/s
  748. [starpu_comm_stats][3:0] 248.000000 B 0.000237 MB 0.000051 B/s 0.000000 MB/s
  749. [starpu_comm_stats][3:2] 50.000000 B 0.000217 MB 0.000047 B/s 0.000000 MB/s
  750. [starpu_comm_stats][2] TOTAL: 288.000000 B 0.000275 MB 0.000059 B/s 0.000000 MB/s
  751. [starpu_comm_stats][2:1] 70.000000 B 0.000103 MB 0.000022 B/s 0.000000 MB/s
  752. [starpu_comm_stats][2:3] 288.000000 B 0.000172 MB 0.000037 B/s 0.000000 MB/s
  753. [starpu_comm_stats][1] TOTAL: 188.000000 B 0.000179 MB 0.000038 B/s 0.000000 MB/s
  754. [starpu_comm_stats][1:0] 80.000000 B 0.000114 MB 0.000025 B/s 0.000000 MB/s
  755. [starpu_comm_stats][1:2] 188.000000 B 0.000065 MB 0.000014 B/s 0.000000 MB/s
  756. [starpu_comm_stats][0] TOTAL: 376.000000 B 0.000359 MB 0.000077 B/s 0.000000 MB/s
  757. [starpu_comm_stats][0:1] 376.000000 B 0.000141 MB 0.000030 B/s 0.000000 MB/s
  758. [starpu_comm_stats][0:3] 10.000000 B 0.000217 MB 0.000047 B/s 0.000000 MB/s
  759. \endverbatim
  760. These statistics can be plotted as heatmaps using StarPU tool <c>starpu_mpi_comm_matrix.py</c>, this will produce 2 PDF files, one plot for the bandwidth, and one plot for the data volume.
  761. \image latex trace_bw_heatmap.pdf "Bandwidth Heatmap" width=0.5\textwidth
  762. \image html trace_bw_heatmap.png "Bandwidth Heatmap"
  763. \image latex trace_volume_heatmap.pdf "Data Volume Heatmap" width=0.5\textwidth
  764. \image html trace_volume_heatmap.png "Data Bandwidth Heatmap"
  765. \section MPIExamples More MPI examples
  766. MPI examples are available in the StarPU source code in mpi/examples:
  767. <ul>
  768. <li>
  769. <c>comm</c> shows how to use communicators with StarPU-MPI
  770. </li>
  771. <li>
  772. <c>complex</c> is a simple example using a user-define data interface over
  773. MPI (complex numbers),
  774. </li>
  775. <li>
  776. <c>stencil5</c> is a simple stencil example using starpu_mpi_task_insert(),
  777. </li>
  778. <li>
  779. <c>matrix_decomposition</c> is a cholesky decomposition example using
  780. starpu_mpi_task_insert(). The non-distributed version can check for
  781. <algorithm correctness in 1-node configuration, the distributed version uses
  782. exactly the same source code, to be used over MPI,
  783. </li>
  784. <li>
  785. <c>mpi_lu</c> is an LU decomposition example, provided in three versions:
  786. <c>plu_example</c> uses explicit MPI data transfers, <c>plu_implicit_example</c>
  787. uses implicit MPI data transfers, <c>plu_outofcore_example</c> uses implicit MPI
  788. data transfers and supports data matrices which do not fit in memory (out-of-core).
  789. </li>
  790. </ul>
  791. \section MPIImplementation Notes about the Implementation
  792. StarPU-MPI is implemented directly on top of MPI.
  793. Since the release 1.3.0, an implementation on top of NewMadeleine, an
  794. optimizing communication library for high-performance networks, is
  795. also provided. To use it, one needs to install NewMadeleine (see
  796. http://pm2.gforge.inria.fr/newmadeleine/) and enable the \c configure
  797. option \ref enable-nmad "--enable-nmad".
  798. Both implementations provide the same public API.
  799. \section MPIMasterSlave MPI Master Slave Support
  800. StarPU provides an other way to execute applications across many
  801. nodes. The Master Slave support permits to use remote cores without
  802. thinking about data distribution. This support can be activated with
  803. the \c configure option \ref enable-mpi-master-slave
  804. "--enable-mpi-master-slave". However, you should not activate both MPI
  805. support and MPI Master-Slave support.
  806. The existing kernels for CPU devices can be used as such. They only have to be
  807. exposed through the name of the function in the \ref starpu_codelet::cpu_funcs_name field.
  808. Functions have to be globally-visible (i.e. not static) for StarPU to
  809. be able to look them up, and <c>-rdynamic</c> must be passed to gcc (or
  810. <c>-export-dynamic</c> to ld) so that symbols of the main program are visible.
  811. Optionally, you can choose the use of another function on slaves thanks to
  812. the field \ref starpu_codelet::mpi_ms_funcs.
  813. By default, one core is dedicated on the master node to manage the
  814. entire set of slaves. If the implementation of MPI you are using has a
  815. good multiple threads support, you can use the \c configure option
  816. \ref with-mpi-master-slave-multiple-thread "--with-mpi-master-slave-multiple-thread"
  817. to dedicate one core per slave.
  818. Choosing the number of cores on each slave device is done by setting
  819. the environment variable \ref STARPU_NMPIMSTHREADS "STARPU_NMPIMSTHREADS=\<number\>"
  820. with <c>\<number\></c> being the requested number of cores. By default
  821. all the slave's cores are used.
  822. Setting the number of slaves nodes is done by changing the <c>-n</c>
  823. parameter when executing the application with mpirun or mpiexec.
  824. The master node is by default the node with the MPI rank equal to 0.
  825. To select another node, use the environment variable \ref
  826. STARPU_MPI_MASTER_NODE "STARPU_MPI_MASTER_NODE=\<number\>" with
  827. <c>\<number\></c> being the requested MPI rank node.
  828. */