410_mpi_support.doxy 41 KB

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