410_mpi_support.doxy 42 KB

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