410_mpi_support.doxy 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*
  2. * This file is part of the StarPU Handbook.
  3. * Copyright (C) 2009--2011 Universit@'e de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016 CNRS
  5. * Copyright (C) 2011, 2012, 2017 INRIA
  6. * See the file version.doxy for copying conditions.
  7. */
  8. /*! \page MPISupport MPI Support
  9. The integration of MPI transfers within task parallelism is done in a
  10. very natural way by the means of asynchronous interactions between the
  11. application and StarPU. This is implemented in a separate <c>libstarpumpi</c> library
  12. which basically provides "StarPU" equivalents of <c>MPI_*</c> functions, where
  13. <c>void *</c> buffers are replaced with ::starpu_data_handle_t, and all
  14. GPU-RAM-NIC transfers are handled efficiently by StarPU-MPI. The user has to
  15. use the usual <c>mpirun</c> command of the MPI implementation to start StarPU on
  16. the different MPI nodes.
  17. An MPI Insert Task function provides an even more seamless transition to a
  18. distributed application, by automatically issuing all required data transfers
  19. according to the task graph and an application-provided distribution.
  20. \section ExampleDocumentation Example used in this documentation
  21. The example below will be used as the base for this documentation. It
  22. initializes a token on node 0, and the token is passed from node to node,
  23. incremented by one on each step. The code is not using StarPU yet.
  24. \code{.c}
  25. for (loop = 0; loop < nloops; loop++) {
  26. int tag = loop*size + rank;
  27. if (loop == 0 && rank == 0)
  28. {
  29. token = 0;
  30. fprintf(stdout, "Start with token value %d\n", token);
  31. }
  32. else
  33. {
  34. MPI_Recv(&token, 1, MPI_INT, (rank+size-1)%size, tag, MPI_COMM_WORLD);
  35. }
  36. token++;
  37. if (loop == last_loop && rank == last_rank)
  38. {
  39. fprintf(stdout, "Finished: token value %d\n", token);
  40. }
  41. else
  42. {
  43. MPI_Send(&token, 1, MPI_INT, (rank+1)%size, tag+1, MPI_COMM_WORLD);
  44. }
  45. }
  46. \endcode
  47. \section NotUsingMPISupport About not using the MPI support
  48. Although StarPU provides MPI support, the application programmer may want to
  49. keep his MPI communications as they are for a start, and only delegate task
  50. execution to StarPU. This is possible by just using starpu_data_acquire(), for
  51. instance:
  52. \code{.c}
  53. for (loop = 0; loop < nloops; loop++) {
  54. int tag = loop*size + rank;
  55. /* Acquire the data to be able to write to it */
  56. starpu_data_acquire(token_handle, STARPU_W);
  57. if (loop == 0 && rank == 0)
  58. {
  59. token = 0;
  60. fprintf(stdout, "Start with token value %d\n", token);
  61. }
  62. else
  63. {
  64. MPI_Recv(&token, 1, MPI_INT, (rank+size-1)%size, tag, MPI_COMM_WORLD);
  65. }
  66. starpu_data_release(token_handle);
  67. /* Task delegation to StarPU to increment the token. The execution might
  68. * be performed on a CPU, a GPU, etc. */
  69. increment_token();
  70. /* Acquire the update data to be able to read from it */
  71. starpu_data_acquire(token_handle, STARPU_R);
  72. if (loop == last_loop && rank == last_rank)
  73. {
  74. fprintf(stdout, "Finished: token value %d\n", token);
  75. }
  76. else
  77. {
  78. MPI_Send(&token, 1, MPI_INT, (rank+1)%size, tag+1, MPI_COMM_WORLD);
  79. }
  80. starpu_data_release(token_handle);
  81. }
  82. \endcode
  83. In that case, <c>libstarpumpi</c> is not needed. One can also use <c>MPI_Isend()</c> and
  84. <c>MPI_Irecv()</c>, by calling starpu_data_release() after <c>MPI_Wait()</c> or <c>MPI_Test()</c>
  85. have notified completion.
  86. It is however better to use <c>libstarpumpi</c>, to save the application from having to
  87. synchronize with starpu_data_acquire(), and instead just submit all tasks and
  88. communications asynchronously, and wait for the overall completion.
  89. \section SimpleExample Simple Example
  90. The flags required to compile or link against the MPI layer are
  91. accessible with the following commands:
  92. \verbatim
  93. $ pkg-config --cflags starpumpi-1.3 # options for the compiler
  94. $ pkg-config --libs starpumpi-1.3 # options for the linker
  95. \endverbatim
  96. \code{.c}
  97. void increment_token(void)
  98. {
  99. struct starpu_task *task = starpu_task_create();
  100. task->cl = &increment_cl;
  101. task->handles[0] = token_handle;
  102. starpu_task_submit(task);
  103. }
  104. int main(int argc, char **argv)
  105. {
  106. int rank, size;
  107. starpu_init(NULL);
  108. starpu_mpi_initialize_extended(&rank, &size);
  109. starpu_vector_data_register(&token_handle, STARPU_MAIN_RAM, (uintptr_t)&token, 1, sizeof(unsigned));
  110. unsigned nloops = NITER;
  111. unsigned loop;
  112. unsigned last_loop = nloops - 1;
  113. unsigned last_rank = size - 1;
  114. for (loop = 0; loop < nloops; loop++) {
  115. int tag = loop*size + rank;
  116. if (loop == 0 && rank == 0)
  117. {
  118. starpu_data_acquire(token_handle, STARPU_W);
  119. token = 0;
  120. fprintf(stdout, "Start with token value %d\n", token);
  121. starpu_data_release(token_handle);
  122. }
  123. else
  124. {
  125. starpu_mpi_irecv_detached(token_handle, (rank+size-1)%size, tag,
  126. MPI_COMM_WORLD, NULL, NULL);
  127. }
  128. increment_token();
  129. if (loop == last_loop && rank == last_rank)
  130. {
  131. starpu_data_acquire(token_handle, STARPU_R);
  132. fprintf(stdout, "Finished: token value %d\n", token);
  133. starpu_data_release(token_handle);
  134. }
  135. else
  136. {
  137. starpu_mpi_isend_detached(token_handle, (rank+1)%size, tag+1,
  138. MPI_COMM_WORLD, NULL, NULL);
  139. }
  140. }
  141. starpu_task_wait_for_all();
  142. starpu_mpi_shutdown();
  143. starpu_shutdown();
  144. if (rank == last_rank)
  145. {
  146. fprintf(stderr, "[%d] token = %d == %d * %d ?\n", rank, token, nloops, size);
  147. STARPU_ASSERT(token == nloops*size);
  148. }
  149. \endcode
  150. We have here replaced <c>MPI_Recv()</c> and <c>MPI_Send()</c> with starpu_mpi_irecv_detached()
  151. and starpu_mpi_isend_detached(), which just submit the communication to be
  152. performed. The only remaining synchronization with starpu_data_acquire() is at
  153. the beginning and the end.
  154. \section PointToPointCommunication Point To Point Communication
  155. The standard point to point communications of MPI have been
  156. implemented. The semantic is similar to the MPI one, but adapted to
  157. the DSM provided by StarPU. A MPI request will only be submitted when
  158. the data is available in the main memory of the node submitting the
  159. request.
  160. There are two types of asynchronous communications: the classic
  161. asynchronous communications and the detached communications. The
  162. classic asynchronous communications (starpu_mpi_isend() and
  163. starpu_mpi_irecv()) need to be followed by a call to
  164. starpu_mpi_wait() or to starpu_mpi_test() to wait for or to
  165. test the completion of the communication. Waiting for or testing the
  166. completion of detached communications is not possible, this is done
  167. internally by StarPU-MPI, on completion, the resources are
  168. automatically released. This mechanism is similar to the pthread
  169. detach state attribute which determines whether a thread will be
  170. created in a joinable or a detached state.
  171. Internally, all communication are divided in 2 communications, a first
  172. message is used to exchange an envelope describing the data (i.e its
  173. tag and its size), the data itself is sent in a second message. All
  174. MPI communications submitted by StarPU uses a unique tag which has a
  175. default value, and can be accessed with the functions
  176. starpu_mpi_get_communication_tag() and
  177. starpu_mpi_set_communication_tag(). The matching of tags with
  178. corresponding requests is done within StarPU-MPI.
  179. For any userland communication, the call of the corresponding function
  180. (e.g starpu_mpi_isend()) will result in the creation of a StarPU-MPI
  181. request, the function starpu_data_acquire_cb() is then called to
  182. asynchronously request StarPU to fetch the data in main memory; when
  183. the data is ready and the corresponding buffer has already been
  184. received by MPI, it will be copied in the memory of the data,
  185. otherwise the request is stored in the <em>early requests list</em>. Sending
  186. requests are stored in the <em>ready requests list</em>.
  187. While requests need to be processed, the StarPU-MPI progression thread
  188. does the following:
  189. <ol>
  190. <li> it polls the <em>ready requests list</em>. For all the ready
  191. requests, the appropriate function is called to post the corresponding
  192. MPI call. For example, an initial call to starpu_mpi_isend() will
  193. result in a call to <c>MPI_Isend()</c>. If the request is marked as
  194. detached, the request will then be added in the <em>detached requests
  195. list</em>.
  196. </li>
  197. <li> it posts a <c>MPI_Irecv()</c> to retrieve a data envelope.
  198. </li>
  199. <li> it polls the <em>detached requests list</em>. For all the detached
  200. requests, it tests its completion of the MPI request by calling
  201. <c>MPI_Test()</c>. On completion, the data handle is released, and if a
  202. callback was defined, it is called.
  203. </li>
  204. <li> finally, it checks if a data envelope has been received. If so,
  205. if the data envelope matches a request in the <em>early requests list</em> (i.e
  206. the request has already been posted by the application), the
  207. corresponding MPI call is posted (similarly to the first step above).
  208. If the data envelope does not match any application request, a
  209. temporary handle is created to receive the data, a StarPU-MPI request
  210. is created and added into the <em>ready requests list</em>, and thus will be
  211. processed in the first step of the next loop.
  212. </li>
  213. </ol>
  214. \ref MPIPtpCommunication gives the list of all the
  215. point to point communications defined in StarPU-MPI.
  216. \section ExchangingUserDefinedDataInterface Exchanging User Defined Data Interface
  217. New data interfaces defined as explained in \ref DefiningANewDataInterface
  218. can also be used within StarPU-MPI and
  219. exchanged between nodes. Two functions needs to be defined through the
  220. type starpu_data_interface_ops. The function
  221. starpu_data_interface_ops::pack_data takes a handle and returns a
  222. contiguous memory buffer allocated with
  223. \code{.c}
  224. starpu_malloc_flags(ptr, size, 0)
  225. \endcode
  226. along with its size where data to be conveyed
  227. to another node should be copied. The reversed operation is
  228. implemented in the function starpu_data_interface_ops::unpack_data which
  229. takes a contiguous memory buffer and recreates the data handle.
  230. \code{.c}
  231. static int complex_pack_data(starpu_data_handle_t handle, unsigned node, void **ptr, ssize_t *count)
  232. {
  233. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  234. struct starpu_complex_interface *complex_interface =
  235. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, node);
  236. *count = complex_get_size(handle);
  237. starpu_malloc_flags(ptr, *count, 0);
  238. memcpy(*ptr, complex_interface->real, complex_interface->nx*sizeof(double));
  239. memcpy(*ptr+complex_interface->nx*sizeof(double), complex_interface->imaginary,
  240. complex_interface->nx*sizeof(double));
  241. return 0;
  242. }
  243. static int complex_unpack_data(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  244. {
  245. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  246. struct starpu_complex_interface *complex_interface =
  247. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, node);
  248. memcpy(complex_interface->real, ptr, complex_interface->nx*sizeof(double));
  249. memcpy(complex_interface->imaginary, ptr+complex_interface->nx*sizeof(double),
  250. complex_interface->nx*sizeof(double));
  251. return 0;
  252. }
  253. static struct starpu_data_interface_ops interface_complex_ops =
  254. {
  255. ...
  256. .pack_data = complex_pack_data,
  257. .unpack_data = complex_unpack_data
  258. };
  259. \endcode
  260. 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.
  261. \code{.c}
  262. starpu_data_interface handle;
  263. starpu_complex_data_register(&handle, STARPU_MAIN_RAM, real, imaginary, 2);
  264. starpu_mpi_datatype_register(handle, starpu_complex_interface_datatype_allocate, starpu_complex_interface_datatype_free);
  265. \endcode
  266. The functions to create and free the MPI datatype are defined as follows.
  267. \code{.c}
  268. void starpu_complex_interface_datatype_allocate(starpu_data_handle_t handle, MPI_Datatype *mpi_datatype)
  269. {
  270. int ret;
  271. int blocklengths[2];
  272. MPI_Aint displacements[2];
  273. MPI_Datatype types[2] = {MPI_DOUBLE, MPI_DOUBLE};
  274. struct starpu_complex_interface *complex_interface =
  275. (struct starpu_complex_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  276. MPI_Address(complex_interface, displacements);
  277. MPI_Address(&complex_interface->imaginary, displacements+1);
  278. displacements[1] -= displacements[0];
  279. displacements[0] = 0;
  280. blocklengths[0] = complex_interface->nx;
  281. blocklengths[1] = complex_interface->nx;
  282. ret = MPI_Type_create_struct(2, blocklengths, displacements, types, mpi_datatype);
  283. STARPU_ASSERT_MSG(ret == MPI_SUCCESS, "MPI_Type_contiguous failed");
  284. ret = MPI_Type_commit(mpi_datatype);
  285. STARPU_ASSERT_MSG(ret == MPI_SUCCESS, "MPI_Type_commit failed");
  286. }
  287. void starpu_complex_interface_datatype_free(MPI_Datatype *mpi_datatype)
  288. {
  289. MPI_Type_free(mpi_datatype);
  290. }
  291. \endcode
  292. 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.
  293. \code{.c}
  294. starpu_data_interface handle;
  295. starpu_complex_data_register(&handle, STARPU_MAIN_RAM, real, imaginary, 2);
  296. starpu_mpi_datatype_register(handle, starpu_complex_interface_datatype_allocate, starpu_complex_interface_datatype_free);
  297. starpu_mpi_barrier(MPI_COMM_WORLD);
  298. \endcode
  299. \section MPIInsertTaskUtility MPI Insert Task Utility
  300. To save the programmer from having to explicit all communications, StarPU
  301. provides an "MPI Insert Task Utility". The principe is that the application
  302. decides a distribution of the data over the MPI nodes by allocating it and
  303. notifying StarPU of that decision, i.e. tell StarPU which MPI node "owns"
  304. which data. It also decides, for each handle, an MPI tag which will be used to
  305. exchange the content of the handle. All MPI nodes then process the whole task
  306. graph, and StarPU automatically determines which node actually execute which
  307. task, and trigger the required MPI transfers.
  308. The list of functions is described in \ref MPIInsertTask.
  309. Here an stencil example showing how to use starpu_mpi_task_insert(). One
  310. first needs to define a distribution function which specifies the
  311. locality of the data. Note that the data needs to be registered to MPI
  312. by calling starpu_mpi_data_register(). This function allows to set
  313. the distribution information and the MPI tag which should be used when
  314. communicating the data. It also allows to automatically clear the MPI
  315. communication cache when unregistering the data.
  316. \code{.c}
  317. /* Returns the MPI node number where data is */
  318. int my_distrib(int x, int y, int nb_nodes) {
  319. /* Block distrib */
  320. return ((int)(x / sqrt(nb_nodes) + (y / sqrt(nb_nodes)) * sqrt(nb_nodes))) % nb_nodes;
  321. // /* Other examples useful for other kinds of computations */
  322. // /* / distrib */
  323. // return (x+y) % nb_nodes;
  324. // /* Block cyclic distrib */
  325. // unsigned side = sqrt(nb_nodes);
  326. // return x % side + (y % side) * size;
  327. }
  328. \endcode
  329. Now the data can be registered within StarPU. Data which are not
  330. owned but will be needed for computations can be registered through
  331. the lazy allocation mechanism, i.e. with a <c>home_node</c> set to <c>-1</c>.
  332. StarPU will automatically allocate the memory when it is used for the
  333. first time.
  334. One can note an optimization here (the <c>else if</c> test): we only register
  335. data which will be needed by the tasks that we will execute.
  336. \code{.c}
  337. unsigned matrix[X][Y];
  338. starpu_data_handle_t data_handles[X][Y];
  339. for(x = 0; x < X; x++) {
  340. for (y = 0; y < Y; y++) {
  341. int mpi_rank = my_distrib(x, y, size);
  342. if (mpi_rank == my_rank)
  343. /* Owning data */
  344. starpu_variable_data_register(&data_handles[x][y], STARPU_MAIN_RAM,
  345. (uintptr_t)&(matrix[x][y]), sizeof(unsigned));
  346. else if (my_rank == my_distrib(x+1, y, size) || my_rank == my_distrib(x-1, y, size)
  347. || my_rank == my_distrib(x, y+1, size) || my_rank == my_distrib(x, y-1, size))
  348. /* I don't own that index, but will need it for my computations */
  349. starpu_variable_data_register(&data_handles[x][y], -1,
  350. (uintptr_t)NULL, sizeof(unsigned));
  351. else
  352. /* I know it's useless to allocate anything for this */
  353. data_handles[x][y] = NULL;
  354. if (data_handles[x][y]) {
  355. starpu_mpi_data_register(data_handles[x][y], x*X+y, mpi_rank);
  356. }
  357. }
  358. }
  359. \endcode
  360. Now starpu_mpi_task_insert() can be called for the different
  361. steps of the application.
  362. \code{.c}
  363. for(loop=0 ; loop<niter; loop++)
  364. for (x = 1; x < X-1; x++)
  365. for (y = 1; y < Y-1; y++)
  366. starpu_mpi_task_insert(MPI_COMM_WORLD, &stencil5_cl,
  367. STARPU_RW, data_handles[x][y],
  368. STARPU_R, data_handles[x-1][y],
  369. STARPU_R, data_handles[x+1][y],
  370. STARPU_R, data_handles[x][y-1],
  371. STARPU_R, data_handles[x][y+1],
  372. 0);
  373. starpu_task_wait_for_all();
  374. \endcode
  375. I.e. all MPI nodes process the whole task graph, but as mentioned above, for
  376. each task, only the MPI node which owns the data being written to (here,
  377. <c>data_handles[x][y]</c>) will actually run the task. The other MPI nodes will
  378. automatically send the required data.
  379. This can be a concern with a growing number of nodes. To avoid this, the
  380. application can prune the task for loops according to the data distribution,
  381. so as to only submit tasks on nodes which have to care about them (either to
  382. execute them, or to send the required data).
  383. A way to do some of this quite easily can be to just add an <c>if</c> like this:
  384. \code{.c}
  385. for(loop=0 ; loop<niter; loop++)
  386. for (x = 1; x < X-1; x++)
  387. for (y = 1; y < Y-1; y++)
  388. if (my_distrib(x,y,size) == my_rank
  389. || my_distrib(x-1,y,size) == my_rank
  390. || my_distrib(x+1,y,size) == my_rank
  391. || my_distrib(x,y-1,size) == my_rank
  392. || my_distrib(x,y+1,size) == my_rank)
  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. This permits to drop the cost of function call argument passing and parsing.
  403. If the <c>my_distrib</c> function can be inlined by the compiler, the latter can
  404. improve the test.
  405. If the <c>size</c> can be made a compile-time constant, the compiler can
  406. considerably improve the test further.
  407. If the distribution function is not too complex and the compiler is very good,
  408. the latter can even optimize the <c>for</c> loops, thus dramatically reducing
  409. the cost of task submission.
  410. To estimate quickly how long task submission takes, and notably how much pruning
  411. saves, a quick and easy way is to measure the submission time of just one of the
  412. MPI nodes. This can be achieved by running the application on just one MPI node
  413. with the following environment variables:
  414. \code
  415. export STARPU_DISABLE_KERNELS=1
  416. export STARPU_MPI_FAKE_RANK=2
  417. export STARPU_MPI_FAKE_SIZE=1024
  418. \endcode
  419. Here we have disabled the kernel function call to skip the actual computation
  420. time and only keep submission time, and we have asked StarPU to fake running on
  421. MPI node 2 out of 1024 nodes.
  422. A function starpu_mpi_task_build() is also provided with the aim to
  423. only construct the task structure. All MPI nodes need to call the
  424. function, only the node which is to execute the task will return a
  425. valid task structure, others will return <c>NULL</c>. That node must submit that task.
  426. All nodes then need to call the function starpu_mpi_task_post_build() -- with the same
  427. list of arguments as starpu_mpi_task_build() -- to post all the
  428. necessary data communications.
  429. \code{.c}
  430. struct starpu_task *task;
  431. task = starpu_mpi_task_build(MPI_COMM_WORLD, &cl,
  432. STARPU_RW, data_handles[0],
  433. STARPU_R, data_handles[1],
  434. 0);
  435. if (task) starpu_task_submit(task);
  436. starpu_mpi_task_post_build(MPI_COMM_WORLD, &cl,
  437. STARPU_RW, data_handles[0],
  438. STARPU_R, data_handles[1],
  439. 0);
  440. \endcode
  441. \section MPICache MPI cache support
  442. StarPU-MPI automatically optimizes duplicate data transmissions: if an MPI
  443. node B needs a piece of data D from MPI node A for several tasks, only one
  444. transmission of D will take place from A to B, and the value of D will be kept
  445. on B as long as no task modifies D.
  446. If a task modifies D, B will wait for all tasks which need the previous value of
  447. D, before invalidating the value of D. As a consequence, it releases the memory
  448. occupied by D. Whenever a task running on B needs the new value of D, allocation
  449. will take place again to receive it.
  450. Since tasks can be submitted dynamically, StarPU-MPI can not know whether the
  451. current value of data D will again be used by a newly-submitted task before
  452. being modified by another newly-submitted task, so until a task is submitted to
  453. modify the current value, it can not decide by itself whether to flush the cache
  454. or not. The application can however explicitly tell StarPU-MPI to flush the
  455. cache by calling starpu_mpi_cache_flush() or starpu_mpi_cache_flush_all_data(),
  456. for instance in case the data will not be used at all any more (see for instance
  457. the cholesky example in <c>mpi/examples/matrix_decomposition</c>), or at least not in
  458. the close future. If a newly-submitted task actually needs the value again,
  459. another transmission of D will be initiated from A to B. A mere
  460. starpu_mpi_cache_flush_all_data() can for instance be added at the end of the whole
  461. algorithm, to express that no data will be reused after that (or at least that
  462. it is not interesting to keep them in cache). It may however be interesting to
  463. add fine-graph starpu_mpi_cache_flush() calls during the algorithm; the effect
  464. for the data deallocation will be the same, but it will additionally release some
  465. pressure from the StarPU-MPI cache hash table during task submission.
  466. The whole caching behavior can be disabled thanks to the \ref STARPU_MPI_CACHE
  467. environment variable. The variable \ref STARPU_MPI_CACHE_STATS can be set to <c>1</c>
  468. to enable the runtime to display messages when data are added or removed
  469. from the cache holding the received data.
  470. \section MPIMigration MPI Data migration
  471. The application can dynamically change its mind about the data distribution, to
  472. balance the load over MPI nodes for instance. This can be done very simply by
  473. requesting an explicit move and then change the registered rank. For instance,
  474. we here switch to a new distribution function <c>my_distrib2</c>: we first
  475. register any data that wasn't registered already and will be needed, then
  476. migrate the data, and register the new location.
  477. \code{.c}
  478. for(x = 0; x < X; x++) {
  479. for (y = 0; y < Y; y++) {
  480. int mpi_rank = my_distrib2(x, y, size);
  481. if (!data_handles[x][y] && (mpi_rank == my_rank
  482. || my_rank == my_distrib(x+1, y, size) || my_rank == my_distrib(x-1, y, size)
  483. || my_rank == my_distrib(x, y+1, size) || my_rank == my_distrib(x, y-1, size)))
  484. /* Register newly-needed data */
  485. starpu_variable_data_register(&data_handles[x][y], -1,
  486. (uintptr_t)NULL, sizeof(unsigned));
  487. if (data_handles[x][y]) {
  488. /* Migrate the data */
  489. starpu_mpi_data_migrate(MPI_COMM_WORLD, data_handles[x][y], mpi_rank);
  490. }
  491. }
  492. }
  493. \endcode
  494. From then on, further tasks submissions will use the new data distribution,
  495. which will thus change both MPI communications and task assignments.
  496. Very importantly, since all nodes have to agree on which node owns which data
  497. so as to determine MPI communications and task assignments the same way, all
  498. nodes have to perform the same data migration, and at the same point among task
  499. submissions. It thus does not require a strict synchronization, just a clear
  500. separation of task submissions before and after the data redistribution.
  501. Before data unregistration, it has to be migrated back to its original home
  502. node (the value, at least), since that is where the user-provided buffer
  503. resides. Otherwise the unregistration will complain that it does not have the
  504. latest value on the original home node.
  505. \code{.c}
  506. for(x = 0; x < X; x++) {
  507. for (y = 0; y < Y; y++) {
  508. if (data_handles[x][y]) {
  509. int mpi_rank = my_distrib(x, y, size);
  510. /* Get back data to original place where the user-provided buffer is. */
  511. starpu_mpi_get_data_on_node_detached(MPI_COMM_WORLD, data_handles[x][y], mpi_rank, NULL, NULL);
  512. /* And unregister it */
  513. starpu_data_unregister(data_handles[x][y]);
  514. }
  515. }
  516. }
  517. \endcode
  518. \section MPICollective MPI Collective Operations
  519. The functions are described in \ref MPICollectiveOperations.
  520. \code{.c}
  521. if (rank == root)
  522. {
  523. /* Allocate the vector */
  524. vector = malloc(nblocks * sizeof(float *));
  525. for(x=0 ; x<nblocks ; x++)
  526. {
  527. starpu_malloc((void **)&vector[x], block_size*sizeof(float));
  528. }
  529. }
  530. /* Allocate data handles and register data to StarPU */
  531. data_handles = malloc(nblocks*sizeof(starpu_data_handle_t *));
  532. for(x = 0; x < nblocks ; x++)
  533. {
  534. int mpi_rank = my_distrib(x, nodes);
  535. if (rank == root) {
  536. starpu_vector_data_register(&data_handles[x], STARPU_MAIN_RAM, (uintptr_t)vector[x],
  537. blocks_size, sizeof(float));
  538. }
  539. else if ((mpi_rank == rank) || ((rank == mpi_rank+1 || rank == mpi_rank-1))) {
  540. /* I own that index, or i will need it for my computations */
  541. starpu_vector_data_register(&data_handles[x], -1, (uintptr_t)NULL,
  542. block_size, sizeof(float));
  543. }
  544. else {
  545. /* I know it's useless to allocate anything for this */
  546. data_handles[x] = NULL;
  547. }
  548. if (data_handles[x]) {
  549. starpu_mpi_data_register(data_handles[x], x*nblocks+y, mpi_rank);
  550. }
  551. }
  552. /* Scatter the matrix among the nodes */
  553. starpu_mpi_scatter_detached(data_handles, nblocks, root, MPI_COMM_WORLD);
  554. /* Calculation */
  555. for(x = 0; x < nblocks ; x++) {
  556. if (data_handles[x]) {
  557. int owner = starpu_data_get_rank(data_handles[x]);
  558. if (owner == rank) {
  559. starpu_task_insert(&cl, STARPU_RW, data_handles[x], 0);
  560. }
  561. }
  562. }
  563. /* Gather the matrix on main node */
  564. starpu_mpi_gather_detached(data_handles, nblocks, 0, MPI_COMM_WORLD);
  565. \endcode
  566. Other collective operations would be easy to define, just ask starpu-devel for
  567. them!
  568. \section MPIDebug Debugging MPI
  569. Communication trace will be enabled when the environment variable
  570. \ref STARPU_MPI_COMM is set to 1, and StarPU has been configured with the
  571. option \ref enable-verbose "--enable-verbose".
  572. Statistics will be enabled for the communication cache when the
  573. environment variable \ref STARPU_MPI_CACHE_STATS is set to 1. It
  574. prints messages on the standard output when data are added or removed
  575. from the received communication cache.
  576. \section MPIExamples More MPI examples
  577. MPI examples are available in the StarPU source code in mpi/examples:
  578. <ul>
  579. <li>
  580. <c>comm</c> shows how to use communicators with StarPU-MPI
  581. </li>
  582. <li>
  583. <c>complex</c> is a simple example using a user-define data interface over
  584. MPI (complex numbers),
  585. </li>
  586. <li>
  587. <c>stencil5</c> is a simple stencil example using starpu_mpi_task_insert(),
  588. </li>
  589. <li>
  590. <c>matrix_decomposition</c> is a cholesky decomposition example using
  591. starpu_mpi_task_insert(). The non-distributed version can check for
  592. <algorithm correctness in 1-node configuration, the distributed version uses
  593. exactly the same source code, to be used over MPI,
  594. </li>
  595. <li>
  596. <c>mpi_lu</c> is an LU decomposition example, provided in three versions:
  597. <c>plu_example</c> uses explicit MPI data transfers, <c>plu_implicit_example</c>
  598. uses implicit MPI data transfers, <c>plu_outofcore_example</c> uses implicit MPI
  599. data transfers and supports data matrices which do not fit in memory (out-of-core).
  600. </li>
  601. </ul>
  602. \section MPIMasterSlave MPI Master Slave Support
  603. StarPU includes an other way to execute the application across many nodes. The Master
  604. Slave support permits to use remote cores without thinking about data distribution. This
  605. support can be activated with the <c>--enable-mpi-master-slave</c>. However, you should not activate
  606. both MPI support and MPI Master-Slave support.
  607. If a codelet contains a kernel for CPU devices, it is automatically eligible to be executed
  608. on a MPI Slave device. However, you can decide to execute the codelet on a MPI Slave by filling
  609. the <c>mpi_ms_funcs</c> variable. The functions have to be globally-visible (i.e. not static ) for
  610. StarPU to be able to look them up, and -rdynamic must be passed to gcc (or -export-dynamic to ld)
  611. so that symbols of the main program are visible.
  612. By default, one core is dedicated on the master to manage the entire set of slaves. If MPI
  613. has a good multiple threads support, you can use <c>--with-mpi-master-slave-multiple-thread</c> to
  614. dedicate one core per slave.
  615. If you want to chose the number of cores on the slave device, use the <c>STARPU_NMPIMSTHREADS=\<number\></c>
  616. with <c>\<number\></c> is the number of cores wanted. The default value is all the slave's cores. To select
  617. the number of slaves nodes, change the <c>-n</c> parameter when executing the application with mpirun
  618. or mpiexec.
  619. The node chosen by default is the with the MPI rank 0. To modify this, use the environment variable
  620. <c>STARPU_MPI_MASTER_NODE=\<number\></c> with <c>\<number\></c> is the MPI rank wanted.
  621. */