410_mpi_support.doxy 37 KB

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