410_mpi_support.doxy 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2017 CNRS
  4. * Copyright (C) 2011-2013,2017 Inria
  5. * Copyright (C) 2009-2011,2013-2017 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_init(NULL);
  120. starpu_mpi_init(&argc, &argv, 1);
  121. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  122. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  123. starpu_vector_data_register(&token_handle, STARPU_MAIN_RAM, (uintptr_t)&token, 1, sizeof(unsigned));
  124. unsigned nloops = NITER;
  125. unsigned loop;
  126. unsigned last_loop = nloops - 1;
  127. unsigned last_rank = size - 1;
  128. for (loop = 0; loop < nloops; loop++)
  129. {
  130. int tag = loop*size + rank;
  131. if (loop == 0 && rank == 0)
  132. {
  133. starpu_data_acquire(token_handle, STARPU_W);
  134. token = 0;
  135. fprintf(stdout, "Start with token value %d\n", token);
  136. starpu_data_release(token_handle);
  137. }
  138. else
  139. {
  140. starpu_mpi_irecv_detached(token_handle, (rank+size-1)%size, tag,
  141. 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,
  153. MPI_COMM_WORLD, NULL, NULL);
  154. }
  155. }
  156. starpu_task_wait_for_all();
  157. starpu_mpi_shutdown();
  158. starpu_shutdown();
  159. if (rank == last_rank)
  160. {
  161. fprintf(stderr, "[%d] token = %d == %d * %d ?\n", rank, token, nloops, size);
  162. STARPU_ASSERT(token == nloops*size);
  163. }
  164. \endcode
  165. We have here replaced <c>MPI_Recv()</c> and <c>MPI_Send()</c> with starpu_mpi_irecv_detached()
  166. and starpu_mpi_isend_detached(), which just submit the communication to be
  167. performed. The implicit sequential consistency dependencies provide
  168. synchronization between mpi reception and emission and the corresponding tasks.
  169. The only remaining synchronization with starpu_data_acquire() is at
  170. the beginning and the end.
  171. \section MPIInitialization How to Initialize StarPU-MPI
  172. As seen in the previous example, one has to call starpu_mpi_init() to
  173. initialize StarPU-MPI. The third parameter of the function indicates
  174. if MPI should be initialized by StarPU or if the application will do
  175. it itself. If the application initializes MPI itself, it must call
  176. <c>MPI_Init_thread()</c> with <c>MPI_THREAD_SERIALIZED</c> or
  177. <c>MPI_THREAD_MULTIPLE</c>, since StarPU-MPI uses a separate thread to
  178. perform the communications. <c>MPI_THREAD_MULTIPLE</c> is necessary if
  179. the application also performs some MPI communications.
  180. \section PointToPointCommunication Point To Point Communication
  181. The standard point to point communications of MPI have been
  182. implemented. The semantic is similar to the MPI one, but adapted to
  183. the DSM provided by StarPU. A MPI request will only be submitted when
  184. the data is available in the main memory of the node submitting the
  185. request.
  186. There are two types of asynchronous communications: the classic
  187. asynchronous communications and the detached communications. The
  188. classic asynchronous communications (starpu_mpi_isend() and
  189. starpu_mpi_irecv()) need to be followed by a call to
  190. starpu_mpi_wait() or to starpu_mpi_test() to wait for or to
  191. test the completion of the communication. Waiting for or testing the
  192. completion of detached communications is not possible, this is done
  193. internally by StarPU-MPI, on completion, the resources are
  194. automatically released. This mechanism is similar to the pthread
  195. detach state attribute which determines whether a thread will be
  196. created in a joinable or a detached state.
  197. For send communications, data is acquired with the mode ::STARPU_R.
  198. When using the configure option
  199. \ref enable-mpi-pedantic-isend "--enable-mpi-pedantic-isend", the mode
  200. ::STARPU_RW is used to make sure there is no more than 1 concurrent
  201. MPI_Isend call accessing a data.
  202. Internally, all communication are divided in 2 communications, a first
  203. message is used to exchange an envelope describing the data (i.e its
  204. tag and its size), the data itself is sent in a second message. All
  205. MPI communications submitted by StarPU uses a unique tag which has a
  206. default value, and can be accessed with the functions
  207. starpu_mpi_get_communication_tag() and
  208. starpu_mpi_set_communication_tag(). The matching of tags with
  209. corresponding requests is done within StarPU-MPI.
  210. For any userland communication, the call of the corresponding function
  211. (e.g starpu_mpi_isend()) will result in the creation of a StarPU-MPI
  212. request, the function starpu_data_acquire_cb() is then called to
  213. asynchronously request StarPU to fetch the data in main memory; when
  214. the data is ready and the corresponding buffer has already been
  215. received by MPI, it will be copied in the memory of the data,
  216. otherwise the request is stored in the <em>early requests list</em>. Sending
  217. requests are stored in the <em>ready requests list</em>.
  218. While requests need to be processed, the StarPU-MPI progression thread
  219. does the following:
  220. <ol>
  221. <li> it polls the <em>ready requests list</em>. For all the ready
  222. requests, the appropriate function is called to post the corresponding
  223. MPI call. For example, an initial call to starpu_mpi_isend() will
  224. result in a call to <c>MPI_Isend()</c>. If the request is marked as
  225. detached, the request will then be added in the <em>detached requests
  226. list</em>.
  227. </li>
  228. <li> it posts a <c>MPI_Irecv()</c> to retrieve a data envelope.
  229. </li>
  230. <li> it polls the <em>detached requests list</em>. For all the detached
  231. requests, it tests its completion of the MPI request by calling
  232. <c>MPI_Test()</c>. On completion, the data handle is released, and if a
  233. callback was defined, it is called.
  234. </li>
  235. <li> finally, it checks if a data envelope has been received. If so,
  236. if the data envelope matches a request in the <em>early requests list</em> (i.e
  237. the request has already been posted by the application), the
  238. corresponding MPI call is posted (similarly to the first step above).
  239. If the data envelope does not match any application request, a
  240. temporary handle is created to receive the data, a StarPU-MPI request
  241. is created and added into the <em>ready requests list</em>, and thus will be
  242. processed in the first step of the next loop.
  243. </li>
  244. </ol>
  245. \ref MPIPtpCommunication gives the list of all the
  246. point to point communications defined in StarPU-MPI.
  247. \section ExchangingUserDefinedDataInterface Exchanging User Defined Data Interface
  248. New data interfaces defined as explained in \ref DefiningANewDataInterface
  249. can also be used within StarPU-MPI and
  250. exchanged between nodes. Two functions needs to be defined through the
  251. type starpu_data_interface_ops. The function
  252. starpu_data_interface_ops::pack_data takes a handle and returns a
  253. contiguous memory buffer allocated with
  254. \code{.c}
  255. starpu_malloc_flags(ptr, size, 0)
  256. \endcode
  257. along with its size where data to be conveyed
  258. to another node should be copied. The reversed operation is
  259. implemented in the function starpu_data_interface_ops::unpack_data which
  260. takes a contiguous memory buffer and recreates the data handle.
  261. \code{.c}
  262. static int complex_pack_data(starpu_data_handle_t handle, unsigned node, void **ptr, ssize_t *count)
  263. {
  264. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  265. struct starpu_complex_interface *complex_interface =
  266. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, node);
  267. *count = complex_get_size(handle);
  268. starpu_malloc_flags(ptr, *count, 0);
  269. memcpy(*ptr, complex_interface->real, complex_interface->nx*sizeof(double));
  270. memcpy(*ptr+complex_interface->nx*sizeof(double), complex_interface->imaginary,
  271. complex_interface->nx*sizeof(double));
  272. return 0;
  273. }
  274. static int complex_unpack_data(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  275. {
  276. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  277. struct starpu_complex_interface *complex_interface =
  278. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, node);
  279. memcpy(complex_interface->real, ptr, complex_interface->nx*sizeof(double));
  280. memcpy(complex_interface->imaginary, ptr+complex_interface->nx*sizeof(double),
  281. complex_interface->nx*sizeof(double));
  282. return 0;
  283. }
  284. static struct starpu_data_interface_ops interface_complex_ops =
  285. {
  286. ...
  287. .pack_data = complex_pack_data,
  288. .unpack_data = complex_unpack_data
  289. };
  290. \endcode
  291. 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.
  292. \code{.c}
  293. starpu_data_interface handle;
  294. starpu_complex_data_register(&handle, STARPU_MAIN_RAM, real, imaginary, 2);
  295. starpu_mpi_datatype_register(handle, starpu_complex_interface_datatype_allocate, starpu_complex_interface_datatype_free);
  296. \endcode
  297. The functions to create and free the MPI datatype are defined as follows.
  298. \code{.c}
  299. void starpu_complex_interface_datatype_allocate(starpu_data_handle_t handle, MPI_Datatype *mpi_datatype)
  300. {
  301. int ret;
  302. int blocklengths[2];
  303. MPI_Aint displacements[2];
  304. MPI_Datatype types[2] = {MPI_DOUBLE, MPI_DOUBLE};
  305. struct starpu_complex_interface *complex_interface =
  306. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  307. MPI_Address(complex_interface, displacements);
  308. MPI_Address(&complex_interface->imaginary, displacements+1);
  309. displacements[1] -= displacements[0];
  310. displacements[0] = 0;
  311. blocklengths[0] = complex_interface->nx;
  312. blocklengths[1] = complex_interface->nx;
  313. ret = MPI_Type_create_struct(2, blocklengths, displacements, types, mpi_datatype);
  314. STARPU_ASSERT_MSG(ret == MPI_SUCCESS, "MPI_Type_contiguous failed");
  315. ret = MPI_Type_commit(mpi_datatype);
  316. STARPU_ASSERT_MSG(ret == MPI_SUCCESS, "MPI_Type_commit failed");
  317. }
  318. void starpu_complex_interface_datatype_free(MPI_Datatype *mpi_datatype)
  319. {
  320. MPI_Type_free(mpi_datatype);
  321. }
  322. \endcode
  323. Note that it is important to make sure no communication is going to occur before the function starpu_mpi_datatype_register() is called. That 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.
  324. \code{.c}
  325. starpu_data_interface handle;
  326. starpu_complex_data_register(&handle, STARPU_MAIN_RAM, real, imaginary, 2);
  327. starpu_mpi_datatype_register(handle, starpu_complex_interface_datatype_allocate, starpu_complex_interface_datatype_free);
  328. starpu_mpi_barrier(MPI_COMM_WORLD);
  329. \endcode
  330. \section MPIInsertTaskUtility MPI Insert Task Utility
  331. To save the programmer from having to explicit all communications, StarPU
  332. provides an "MPI Insert Task Utility". The principe is that the application
  333. decides a distribution of the data over the MPI nodes by allocating it and
  334. notifying StarPU of that decision, i.e. tell StarPU which MPI node "owns"
  335. which data. It also decides, for each handle, an MPI tag which will be used to
  336. exchange the content of the handle. All MPI nodes then process the whole task
  337. graph, and StarPU automatically determines which node actually execute which
  338. task, and trigger the required MPI transfers.
  339. The list of functions is described in \ref MPIInsertTask.
  340. Here an stencil example showing how to use starpu_mpi_task_insert(). One
  341. first needs to define a distribution function which specifies the
  342. locality of the data. Note that the data needs to be registered to MPI
  343. by calling starpu_mpi_data_register(). This function allows to set
  344. the distribution information and the MPI tag which should be used when
  345. communicating the data. It also allows to automatically clear the MPI
  346. communication cache when unregistering the data.
  347. \code{.c}
  348. /* Returns the MPI node number where data is */
  349. int my_distrib(int x, int y, int nb_nodes)
  350. {
  351. /* Block distrib */
  352. return ((int)(x / sqrt(nb_nodes) + (y / sqrt(nb_nodes)) * sqrt(nb_nodes))) % nb_nodes;
  353. // /* Other examples useful for other kinds of computations */
  354. // /* / distrib */
  355. // return (x+y) % nb_nodes;
  356. // /* Block cyclic distrib */
  357. // unsigned side = sqrt(nb_nodes);
  358. // return x % side + (y % side) * size;
  359. }
  360. \endcode
  361. Now the data can be registered within StarPU. Data which are not
  362. owned but will be needed for computations can be registered through
  363. the lazy allocation mechanism, i.e. with a <c>home_node</c> set to <c>-1</c>.
  364. StarPU will automatically allocate the memory when it is used for the
  365. first time.
  366. One can note an optimization here (the <c>else if</c> test): we only register
  367. data which will be needed by the tasks that we will execute.
  368. \code{.c}
  369. unsigned matrix[X][Y];
  370. starpu_data_handle_t data_handles[X][Y];
  371. for(x = 0; x < X; x++)
  372. {
  373. for (y = 0; y < Y; y++)
  374. {
  375. int mpi_rank = my_distrib(x, y, size);
  376. if (mpi_rank == my_rank)
  377. /* Owning data */
  378. starpu_variable_data_register(&data_handles[x][y], STARPU_MAIN_RAM,
  379. (uintptr_t)&(matrix[x][y]), sizeof(unsigned));
  380. else if (my_rank == my_distrib(x+1, y, size) || my_rank == my_distrib(x-1, y, size)
  381. || my_rank == my_distrib(x, y+1, size) || my_rank == my_distrib(x, y-1, size))
  382. /* I don't own that index, but will need it for my computations */
  383. starpu_variable_data_register(&data_handles[x][y], -1,
  384. (uintptr_t)NULL, sizeof(unsigned));
  385. else
  386. /* I know it's useless to allocate anything for this */
  387. data_handles[x][y] = NULL;
  388. if (data_handles[x][y])
  389. {
  390. starpu_mpi_data_register(data_handles[x][y], x*X+y, mpi_rank);
  391. }
  392. }
  393. }
  394. \endcode
  395. Now starpu_mpi_task_insert() can be called for the different
  396. steps of the application.
  397. \code{.c}
  398. for(loop=0 ; loop<niter; loop++)
  399. for (x = 1; x < X-1; x++)
  400. for (y = 1; y < Y-1; y++)
  401. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl,
  402. STARPU_RW, data_handles[x][y],
  403. STARPU_R, data_handles[x-1][y],
  404. STARPU_R, data_handles[x+1][y],
  405. STARPU_R, data_handles[x][y-1],
  406. STARPU_R, data_handles[x][y+1],
  407. 0);
  408. starpu_task_wait_for_all();
  409. \endcode
  410. I.e. all MPI nodes process the whole task graph, but as mentioned above, for
  411. each task, only the MPI node which owns the data being written to (here,
  412. <c>data_handles[x][y]</c>) will actually run the task. The other MPI nodes will
  413. automatically send the required data.
  414. This can be a concern with a growing number of nodes. To avoid this, the
  415. application can prune the task for loops according to the data distribution,
  416. so as to only submit tasks on nodes which have to care about them (either to
  417. execute them, or to send the required data).
  418. A way to do some of this quite easily can be to just add an <c>if</c> like this:
  419. \code{.c}
  420. for(loop=0 ; loop<niter; loop++)
  421. for (x = 1; x < X-1; x++)
  422. for (y = 1; y < Y-1; y++)
  423. if (my_distrib(x,y,size) == my_rank
  424. || my_distrib(x-1,y,size) == my_rank
  425. || my_distrib(x+1,y,size) == my_rank
  426. || my_distrib(x,y-1,size) == my_rank
  427. || my_distrib(x,y+1,size) == my_rank)
  428. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl,
  429. STARPU_RW, data_handles[x][y],
  430. STARPU_R, data_handles[x-1][y],
  431. STARPU_R, data_handles[x+1][y],
  432. STARPU_R, data_handles[x][y-1],
  433. STARPU_R, data_handles[x][y+1],
  434. 0);
  435. starpu_task_wait_for_all();
  436. \endcode
  437. This permits to drop the cost of function call argument passing and parsing.
  438. If the <c>my_distrib</c> function can be inlined by the compiler, the latter can
  439. improve the test.
  440. If the <c>size</c> can be made a compile-time constant, the compiler can
  441. considerably improve the test further.
  442. If the distribution function is not too complex and the compiler is very good,
  443. the latter can even optimize the <c>for</c> loops, thus dramatically reducing
  444. the cost of task submission.
  445. To estimate quickly how long task submission takes, and notably how much pruning
  446. saves, a quick and easy way is to measure the submission time of just one of the
  447. MPI nodes. This can be achieved by running the application on just one MPI node
  448. with the following environment variables:
  449. \code
  450. export STARPU_DISABLE_KERNELS=1
  451. export STARPU_MPI_FAKE_RANK=2
  452. export STARPU_MPI_FAKE_SIZE=1024
  453. \endcode
  454. Here we have disabled the kernel function call to skip the actual computation
  455. time and only keep submission time, and we have asked StarPU to fake running on
  456. MPI node 2 out of 1024 nodes.
  457. To tune the placement of tasks among MPI nodes, one can use
  458. ::STARPU_EXECUTE_ON_NODE or ::STARPU_EXECUTE_ON_DATA to specify an explicit
  459. node, or the node of a given data (e.g. one of the parameters), or use
  460. starpu_mpi_node_selection_register_policy() and ::STARPU_NODE_SELECTION_POLICY
  461. to provide a dynamic policy.
  462. A function starpu_mpi_task_build() is also provided with the aim to
  463. only construct the task structure. All MPI nodes need to call the
  464. function, only the node which is to execute the task will return a
  465. valid task structure, others will return <c>NULL</c>. That node must submit that task.
  466. All nodes then need to call the function starpu_mpi_task_post_build() -- with the same
  467. list of arguments as starpu_mpi_task_build() -- to post all the
  468. necessary data communications.
  469. \code{.c}
  470. struct starpu_task *task;
  471. task = starpu_mpi_task_build(MPI_COMM_WORLD, &cl,
  472. STARPU_RW, data_handles[0],
  473. STARPU_R, data_handles[1],
  474. 0);
  475. if (task) starpu_task_submit(task);
  476. starpu_mpi_task_post_build(MPI_COMM_WORLD, &cl,
  477. STARPU_RW, data_handles[0],
  478. STARPU_R, data_handles[1],
  479. 0);
  480. \endcode
  481. \section MPITemporaryData Temporary Data
  482. To be able to use starpu_mpi_task_insert(), one has to call
  483. starpu_mpi_data_register(), so that StarPU-MPI can know what it needs to do for
  484. each data. Parameters of starpu_mpi_data_register() are normally the same on all
  485. nodes for a given data, so that all nodes agree on which node owns the data, and
  486. which tag is used to transfer its value.
  487. It can however be useful to register e.g. some temporary data on just one node,
  488. without having to register a dumb handle on all nodes, while only one node will
  489. actually need to know about it. In that case, nodes which will not need the data
  490. can just pass NULL to starpu_mpi_task_insert():
  491. \code{.c}
  492. starpu_data_handle_t data0 = NULL;
  493. if (rank == 0) {
  494. starpu_variable_data_register(&data0, STARPU_MAIN_RAM, (uintptr_t) &val0, sizeof(val0));
  495. starpu_mpi_data_register(data0, 0, rank);
  496. }
  497. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl, STARPU_W, data0, 0); /* Executes on node 0 */
  498. \endcode
  499. Here, nodes whose rank is not 0 will simply not take care of the data, and consider it to be on another node.
  500. This can be mixed various way, for instance here node 1 determines that it does
  501. not have to care about data0, but knows that it should send the value of its
  502. data1 to node 0, which owns data and thus will need the value of data1 to execute the task:
  503. \code{.c}
  504. starpu_data_handle_t data0 = NULL, data1, data;
  505. if (rank == 0) {
  506. starpu_variable_data_register(&data0, STARPU_MAIN_RAM, (uintptr_t) &val0, sizeof(val0));
  507. starpu_mpi_data_register(data0, -1, rank);
  508. starpu_variable_data_register(&data1, -1, 0, sizeof(val1));
  509. starpu_variable_data_register(&data, STARPU_MAIN_RAM, (uintptr_t) &val, sizeof(val));
  510. } else if (rank == 1) {
  511. starpu_variable_data_register(&data1, STARPU_MAIN_RAM, (uintptr_t) &val1, sizeof(val1));
  512. starpu_variable_data_register(&data, -1, 0, sizeof(val));
  513. }
  514. starpu_mpi_data_register(data, 42, 0);
  515. starpu_mpi_data_register(data1, 43, 1);
  516. starpu_mpi_task_insert(MPI_COMM_WORLD, &cl, STARPU_W, data, STARPU_R, data0, STARPU_R, data1, 0); /* Executes on node 0 */
  517. \endcode
  518. \section MPIPerNodeData Per-node Data
  519. Further than temporary data on just one node, one may want per-node data,
  520. to e.g. replicate some computation because that is less expensive than
  521. communicating the value over MPI:
  522. \code{.c}
  523. starpu_data_handle pernode, data0, data1;
  524. starpu_variable_data_register(&pernode, -1, 0, sizeof(val));
  525. starpu_mpi_data_register(pernode, -1, STARPU_MPI_PER_NODE);
  526. /* Normal data: one on node0, one on node1 */
  527. if (rank == 0) {
  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. } else if (rank == 1) {
  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 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 STARPU_MPI_PRIORITIES to 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 B needs a piece of data D from MPI node A for several tasks, only one
  565. transmission of D will take place from A to B, and the value of D will be kept
  566. on B as long as no task modifies D.
  567. If a task modifies D, B will wait for all tasks which need the previous value of
  568. D, before invalidating the value of D. As a consequence, it releases the memory
  569. occupied by D. Whenever a task running on B needs the new value of 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 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 D will be initiated from A to 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 that (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 that 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,
  611. (uintptr_t)NULL, sizeof(unsigned));
  612. if (data_handles[x][y])
  613. {
  614. /* Migrate the data */
  615. starpu_mpi_data_migrate(MPI_COMM_WORLD, data_handles[x][y], mpi_rank);
  616. }
  617. }
  618. }
  619. \endcode
  620. From then on, further tasks submissions will use the new data distribution,
  621. which will thus change both MPI communications and task assignments.
  622. Very importantly, since all nodes have to agree on which node owns which data
  623. so as to determine MPI communications and task assignments the same way, all
  624. nodes have to perform the same data migration, and at the same point among task
  625. submissions. It thus does not require a strict synchronization, just a clear
  626. separation of task submissions before and after the data redistribution.
  627. Before data unregistration, it has to be migrated back to its original home
  628. node (the value, at least), since that is where the user-provided buffer
  629. resides. Otherwise the unregistration will complain that it does not have the
  630. latest value on the original home node.
  631. \code{.c}
  632. for(x = 0; x < X; x++)
  633. {
  634. for (y = 0; y < Y; y++)
  635. {
  636. if (data_handles[x][y])
  637. {
  638. int mpi_rank = my_distrib(x, y, size);
  639. /* Get back data to original place where the user-provided buffer is. */
  640. starpu_mpi_get_data_on_node_detached(MPI_COMM_WORLD, data_handles[x][y], mpi_rank, NULL, NULL);
  641. /* And unregister it */
  642. starpu_data_unregister(data_handles[x][y]);
  643. }
  644. }
  645. }
  646. \endcode
  647. \section MPICollective MPI Collective Operations
  648. The functions are described in \ref MPICollectiveOperations.
  649. \code{.c}
  650. if (rank == root)
  651. {
  652. /* Allocate the vector */
  653. vector = malloc(nblocks * sizeof(float *));
  654. for(x=0 ; x<nblocks ; x++)
  655. {
  656. starpu_malloc((void **)&vector[x], block_size*sizeof(float));
  657. }
  658. }
  659. /* Allocate data handles and register data to StarPU */
  660. data_handles = malloc(nblocks*sizeof(starpu_data_handle_t *));
  661. for(x = 0; x < nblocks ; x++)
  662. {
  663. int mpi_rank = my_distrib(x, nodes);
  664. if (rank == root)
  665. {
  666. starpu_vector_data_register(&data_handles[x], STARPU_MAIN_RAM, (uintptr_t)vector[x],
  667. blocks_size, sizeof(float));
  668. }
  669. else if ((mpi_rank == rank) || ((rank == mpi_rank+1 || rank == mpi_rank-1)))
  670. {
  671. /* I own that index, or i will need it for my computations */
  672. starpu_vector_data_register(&data_handles[x], -1, (uintptr_t)NULL,
  673. block_size, sizeof(float));
  674. }
  675. else
  676. {
  677. /* I know it's useless to allocate anything for this */
  678. data_handles[x] = NULL;
  679. }
  680. if (data_handles[x])
  681. {
  682. starpu_mpi_data_register(data_handles[x], x*nblocks+y, mpi_rank);
  683. }
  684. }
  685. /* Scatter the matrix among the nodes */
  686. starpu_mpi_scatter_detached(data_handles, nblocks, root, MPI_COMM_WORLD);
  687. /* Calculation */
  688. for(x = 0; x < nblocks ; x++)
  689. {
  690. if (data_handles[x])
  691. {
  692. int owner = starpu_data_get_rank(data_handles[x]);
  693. if (owner == rank)
  694. {
  695. starpu_task_insert(&cl, STARPU_RW, data_handles[x], 0);
  696. }
  697. }
  698. }
  699. /* Gather the matrix on main node */
  700. starpu_mpi_gather_detached(data_handles, nblocks, 0, MPI_COMM_WORLD);
  701. \endcode
  702. Other collective operations would be easy to define, just ask starpu-devel for
  703. them!
  704. \section MPIDebug Debugging MPI
  705. Communication trace will be enabled when the environment variable
  706. \ref STARPU_MPI_COMM is set to 1, and StarPU has been configured with the
  707. option \ref enable-verbose "--enable-verbose".
  708. Statistics will be enabled for the communication cache when the
  709. environment variable \ref STARPU_MPI_CACHE_STATS is set to 1. It
  710. prints messages on the standard output when data are added or removed
  711. from the received communication cache.
  712. \section MPIExamples More MPI examples
  713. MPI examples are available in the StarPU source code in mpi/examples:
  714. <ul>
  715. <li>
  716. <c>comm</c> shows how to use communicators with StarPU-MPI
  717. </li>
  718. <li>
  719. <c>complex</c> is a simple example using a user-define data interface over
  720. MPI (complex numbers),
  721. </li>
  722. <li>
  723. <c>stencil5</c> is a simple stencil example using starpu_mpi_task_insert(),
  724. </li>
  725. <li>
  726. <c>matrix_decomposition</c> is a cholesky decomposition example using
  727. starpu_mpi_task_insert(). The non-distributed version can check for
  728. <algorithm correctness in 1-node configuration, the distributed version uses
  729. exactly the same source code, to be used over MPI,
  730. </li>
  731. <li>
  732. <c>mpi_lu</c> is an LU decomposition example, provided in three versions:
  733. <c>plu_example</c> uses explicit MPI data transfers, <c>plu_implicit_example</c>
  734. uses implicit MPI data transfers, <c>plu_outofcore_example</c> uses implicit MPI
  735. data transfers and supports data matrices which do not fit in memory (out-of-core).
  736. </li>
  737. </ul>
  738. \section MPIImplementation Notes about the Implementation
  739. StarPU-MPI is implemented directly on top of MPI.
  740. Since the release 1.3.0, an implementation on top of NewMadeleine, an
  741. optimizing communication library for high-performance networks, is
  742. also provided. To use it, one needs to install NewMadeleine (see
  743. http://pm2.gforge.inria.fr/newmadeleine/) and enable the configure
  744. option \ref enable-nmad "--enable-nmad".
  745. Both implementations provide the same public API.
  746. \section MPIMasterSlave MPI Master Slave Support
  747. StarPU provides an other way to execute applications across many
  748. nodes. The Master Slave support permits to use remote cores without
  749. thinking about data distribution. This support can be activated with
  750. the configure option \ref enable-mpi-master-slave
  751. "--enable-mpi-master-slave". However, you should not activate both MPI
  752. support and MPI Master-Slave support.
  753. The existing kernels for CPU devices can be used as such. They only have to be
  754. exposed through the name of the function in the \ref starpu_codelet::cpu_funcs_name field.
  755. Functions have to be globally-visible (i.e. not static) for StarPU to
  756. be able to look them up, and <c>-rdynamic</c> must be passed to gcc (or
  757. <c>-export-dynamic</c> to ld) so that symbols of the main program are visible.
  758. Optionally, you can choose the use of another function on slaves thanks to
  759. the field \ref starpu_codelet::mpi_ms_funcs.
  760. By default, one core is dedicated on the master node to manage the
  761. entire set of slaves. If the implementation of MPI you are using has a
  762. good multiple threads support, you can use the configure option
  763. \ref with-mpi-master-slave-multiple-thread "--with-mpi-master-slave-multiple-thread"
  764. to dedicate one core per slave.
  765. Choosing the number of cores on each slave device is done by setting
  766. the environment variable \ref STARPU_NMPIMSTHREADS "STARPU_NMPIMSTHREADS=\<number\>"
  767. with <c>\<number\></c> being the requested number of cores. By default
  768. all the slave's cores are used.
  769. Setting the number of slaves nodes is done by changing the <c>-n</c>
  770. parameter when executing the application with mpirun or mpiexec.
  771. The master node is by default the node with the MPI rank equal to 0.
  772. To select another node, use the environment variable \ref
  773. STARPU_MPI_MASTER_NODE "STARPU_MPI_MASTER_NODE=\<number\>" with
  774. <c>\<number\></c> being the requested MPI rank node.
  775. */