collectives.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // Copyright (C) 2005-2006 Douglas Gregor <doug.gregor -at- gmail.com>.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Message Passing Interface 1.1 -- Section 4. MPI Collectives
  6. /** @file collectives.hpp
  7. *
  8. * This header contains MPI collective operations, which implement
  9. * various parallel algorithms that require the coordination of all
  10. * processes within a communicator. The header @c collectives_fwd.hpp
  11. * provides forward declarations for each of these operations. To
  12. * include only specific collective algorithms, use the headers @c
  13. * boost/mpi/collectives/algorithm_name.hpp.
  14. */
  15. #ifndef BOOST_MPI_COLLECTIVES_HPP
  16. #define BOOST_MPI_COLLECTIVES_HPP
  17. #include <boost/mpi/communicator.hpp>
  18. #include <boost/mpi/inplace.hpp>
  19. #include <vector>
  20. namespace boost { namespace mpi {
  21. /**
  22. * @brief Gather the values stored at every process into vectors of
  23. * values from each process.
  24. *
  25. * @c all_gather is a collective algorithm that collects the values
  26. * stored at each process into a vector of values indexed by the
  27. * process number they came from. The type @c T of the values may be
  28. * any type that is serializable or has an associated MPI data type.
  29. *
  30. * When the type @c T has an associated MPI data type, this routine
  31. * invokes @c MPI_Allgather to gather the values.
  32. *
  33. * @param comm The communicator over which the all-gather will
  34. * occur.
  35. *
  36. * @param in_value The value to be transmitted by each process. To
  37. * gather an array of values, @c in_values points to the @c n local
  38. * values to be transmitted.
  39. *
  40. * @param out_values A vector or pointer to storage that will be
  41. * populated with the values from each process, indexed by the
  42. * process ID number. If it is a vector, the vector will be resized
  43. * accordingly.
  44. */
  45. template<typename T>
  46. void
  47. all_gather(const communicator& comm, const T& in_value,
  48. std::vector<T>& out_values);
  49. /**
  50. * \overload
  51. */
  52. template<typename T>
  53. void
  54. all_gather(const communicator& comm, const T& in_value, T* out_values);
  55. /**
  56. * \overload
  57. */
  58. template<typename T>
  59. void
  60. all_gather(const communicator& comm, const T* in_values, int n,
  61. std::vector<T>& out_values);
  62. /**
  63. * \overload
  64. */
  65. template<typename T>
  66. void
  67. all_gather(const communicator& comm, const T* in_values, int n, T* out_values);
  68. /**
  69. * @brief Combine the values stored by each process into a single
  70. * value available to all processes.
  71. *
  72. * @c all_reduce is a collective algorithm that combines the values
  73. * stored by each process into a single value available to all
  74. * processes. The values are combined in a user-defined way,
  75. * specified via a function object. The type @c T of the values may
  76. * be any type that is serializable or has an associated MPI data
  77. * type. One can think of this operation as a @c all_gather, followed
  78. * by an @c std::accumulate() over the gather values and using the
  79. * operation @c op.
  80. *
  81. * When the type @c T has an associated MPI data type, this routine
  82. * invokes @c MPI_Allreduce to perform the reduction. If possible,
  83. * built-in MPI operations will be used; otherwise, @c all_reduce()
  84. * will create a custom MPI_Op for the call to MPI_Allreduce.
  85. *
  86. * @param comm The communicator over which the reduction will
  87. * occur.
  88. * @param value The local value to be combined with the local
  89. * values of every other process. For reducing arrays, @c in_values
  90. * is a pointer to the local values to be reduced and @c n is the
  91. * number of values to reduce. See @c reduce for more information.
  92. *
  93. * If wrapped in a @c inplace_t object, combine the usage of both
  94. * input and $c out_value and the local value will be overwritten
  95. * (a convenience function @c inplace is provided for the wrapping).
  96. *
  97. * @param out_value Will receive the result of the reduction
  98. * operation. If this parameter is omitted, the outgoing value will
  99. * instead be returned.
  100. *
  101. * @param op The binary operation that combines two values of type
  102. * @c T and returns a third value of type @c T. For types @c T that has
  103. * ssociated MPI data types, @c op will either be translated into
  104. * an @c MPI_Op (via @c MPI_Op_create) or, if possible, mapped
  105. * directly to a built-in MPI operation. See @c is_mpi_op in the @c
  106. * operations.hpp header for more details on this mapping. For any
  107. * non-built-in operation, commutativity will be determined by the
  108. * @c is_commmutative trait (also in @c operations.hpp): users are
  109. * encouraged to mark commutative operations as such, because it
  110. * gives the implementation additional lattitude to optimize the
  111. * reduction operation.
  112. *
  113. * @param n Indicated the size of the buffers of array type.
  114. * @returns If no @p out_value parameter is supplied, returns the
  115. * result of the reduction operation.
  116. */
  117. template<typename T, typename Op>
  118. void
  119. all_reduce(const communicator& comm, const T* value, int n, T* out_value,
  120. Op op);
  121. /**
  122. * \overload
  123. */
  124. template<typename T, typename Op>
  125. void
  126. all_reduce(const communicator& comm, const T& value, T& out_value, Op op);
  127. /**
  128. * \overload
  129. */
  130. template<typename T, typename Op>
  131. T all_reduce(const communicator& comm, const T& value, Op op);
  132. /**
  133. * \overload
  134. */
  135. template<typename T, typename Op>
  136. void
  137. all_reduce(const communicator& comm, inplace_t<T*> value, int n,
  138. Op op);
  139. /**
  140. * \overload
  141. */
  142. template<typename T, typename Op>
  143. void
  144. all_reduce(const communicator& comm, inplace_t<T> value, Op op);
  145. /**
  146. * @brief Send data from every process to every other process.
  147. *
  148. * @c all_to_all is a collective algorithm that transmits @c p values
  149. * from every process to every other process. On process i, jth value
  150. * of the @p in_values vector is sent to process j and placed in the
  151. * ith position of the @p out_values vector in process @p j. The type
  152. * @c T of the values may be any type that is serializable or has an
  153. * associated MPI data type. If @c n is provided, then arrays of @p n
  154. * values will be transferred from one process to another.
  155. *
  156. * When the type @c T has an associated MPI data type, this routine
  157. * invokes @c MPI_Alltoall to scatter the values.
  158. *
  159. * @param comm The communicator over which the all-to-all
  160. * communication will occur.
  161. *
  162. * @param in_values A vector or pointer to storage that contains
  163. * the values to send to each process, indexed by the process ID
  164. * number.
  165. *
  166. * @param out_values A vector or pointer to storage that will be
  167. * updated to contain the values received from other processes. The
  168. * jth value in @p out_values will come from the procss with rank j.
  169. */
  170. template<typename T>
  171. void
  172. all_to_all(const communicator& comm, const std::vector<T>& in_values,
  173. std::vector<T>& out_values);
  174. /**
  175. * \overload
  176. */
  177. template<typename T>
  178. void all_to_all(const communicator& comm, const T* in_values, T* out_values);
  179. /**
  180. * \overload
  181. */
  182. template<typename T>
  183. void
  184. all_to_all(const communicator& comm, const std::vector<T>& in_values, int n,
  185. std::vector<T>& out_values);
  186. /**
  187. * \overload
  188. */
  189. template<typename T>
  190. void
  191. all_to_all(const communicator& comm, const T* in_values, int n, T* out_values);
  192. /**
  193. * @brief Broadcast a value from a root process to all other
  194. * processes.
  195. *
  196. * @c broadcast is a collective algorithm that transfers a value from
  197. * an arbitrary @p root process to every other process that is part of
  198. * the given communicator. The @c broadcast algorithm can transmit any
  199. * Serializable value, values that have associated MPI data types,
  200. * packed archives, skeletons, and the content of skeletons; see the
  201. * @c send primitive for communicators for a complete list. The type
  202. * @c T shall be the same for all processes that are a part of the
  203. * communicator @p comm, unless packed archives are being transferred:
  204. * with packed archives, the root sends a @c packed_oarchive or @c
  205. * packed_skeleton_oarchive whereas the other processes receive a
  206. * @c packed_iarchive or @c packed_skeleton_iarchve, respectively.
  207. *
  208. * When the type @c T has an associated MPI data type, this routine
  209. * invokes @c MPI_Bcast to perform the broadcast.
  210. *
  211. * @param comm The communicator over which the broadcast will
  212. * occur.
  213. *
  214. * @param value The value (or values, if @p n is provided) to be
  215. * transmitted (if the rank of @p comm is equal to @p root) or
  216. * received (if the rank of @p comm is not equal to @p root). When
  217. * the @p value is a @c skeleton_proxy, only the skeleton of the
  218. * object will be broadcast. In this case, the @p root will build a
  219. * skeleton from the object help in the proxy and all of the
  220. * non-roots will reshape the objects held in their proxies based on
  221. * the skeleton sent from the root.
  222. *
  223. * @param n When supplied, the number of values that the pointer @p
  224. * values points to, for broadcasting an array of values. The value
  225. * of @p n must be the same for all processes in @p comm.
  226. *
  227. * @param root The rank/process ID of the process that will be
  228. * transmitting the value.
  229. */
  230. template<typename T>
  231. void broadcast(const communicator& comm, T& value, int root);
  232. /**
  233. * \overload
  234. */
  235. template<typename T>
  236. void broadcast(const communicator& comm, T* values, int n, int root);
  237. /**
  238. * \overload
  239. */
  240. template<typename T>
  241. void broadcast(const communicator& comm, skeleton_proxy<T>& value, int root);
  242. /**
  243. * \overload
  244. */
  245. template<typename T>
  246. void
  247. broadcast(const communicator& comm, const skeleton_proxy<T>& value, int root);
  248. /**
  249. * @brief Gather the values stored at every process into a vector at
  250. * the root process.
  251. *
  252. * @c gather is a collective algorithm that collects the values
  253. * stored at each process into a vector of values at the @p root
  254. * process. This vector is indexed by the process number that the
  255. * value came from. The type @c T of the values may be any type that
  256. * is serializable or has an associated MPI data type.
  257. *
  258. * When the type @c T has an associated MPI data type, this routine
  259. * invokes @c MPI_Gather to gather the values.
  260. *
  261. * @param comm The communicator over which the gather will occur.
  262. *
  263. * @param in_value The value to be transmitted by each process. For
  264. * gathering arrays of values, @c in_values points to storage for
  265. * @c n*comm.size() values.
  266. *
  267. * @param out_values A vector or pointer to storage that will be
  268. * populated with the values from each process, indexed by the
  269. * process ID number. If it is a vector, it will be resized
  270. * accordingly. For non-root processes, this parameter may be
  271. * omitted. If it is still provided, however, it will be unchanged.
  272. *
  273. * @param root The process ID number that will collect the
  274. * values. This value must be the same on all processes.
  275. */
  276. template<typename T>
  277. void
  278. gather(const communicator& comm, const T& in_value, std::vector<T>& out_values,
  279. int root);
  280. /**
  281. * \overload
  282. */
  283. template<typename T>
  284. void
  285. gather(const communicator& comm, const T& in_value, T* out_values, int root);
  286. /**
  287. * \overload
  288. */
  289. template<typename T>
  290. void gather(const communicator& comm, const T& in_value, int root);
  291. /**
  292. * \overload
  293. */
  294. template<typename T>
  295. void
  296. gather(const communicator& comm, const T* in_values, int n,
  297. std::vector<T>& out_values, int root);
  298. /**
  299. * \overload
  300. */
  301. template<typename T>
  302. void
  303. gather(const communicator& comm, const T* in_values, int n, T* out_values,
  304. int root);
  305. /**
  306. * \overload
  307. */
  308. template<typename T>
  309. void gather(const communicator& comm, const T* in_values, int n, int root);
  310. /**
  311. * @brief Scatter the values stored at the root to all processes
  312. * within the communicator.
  313. *
  314. * @c scatter is a collective algorithm that scatters the values
  315. * stored in the @p root process (inside a vector) to all of the
  316. * processes in the communicator. The vector @p out_values (only
  317. * significant at the @p root) is indexed by the process number to
  318. * which the corresponding value will be sent. The type @c T of the
  319. * values may be any type that is serializable or has an associated
  320. * MPI data type.
  321. *
  322. * When the type @c T has an associated MPI data type, this routine
  323. * invokes @c MPI_Scatter to scatter the values.
  324. *
  325. * @param comm The communicator over which the gather will occur.
  326. *
  327. * @param in_values A vector or pointer to storage that will contain
  328. * the values to send to each process, indexed by the process rank.
  329. * For non-root processes, this parameter may be omitted. If it is
  330. * still provided, however, it will be unchanged.
  331. *
  332. * @param out_value The value received by each process. When
  333. * scattering an array of values, @p out_values points to the @p n
  334. * values that will be received by each process.
  335. *
  336. * @param root The process ID number that will scatter the
  337. * values. This value must be the same on all processes.
  338. */
  339. template<typename T>
  340. void
  341. scatter(const communicator& comm, const std::vector<T>& in_values, T& out_value,
  342. int root);
  343. /**
  344. * \overload
  345. */
  346. template<typename T>
  347. void
  348. scatter(const communicator& comm, const T* in_values, T& out_value, int root);
  349. /**
  350. * \overload
  351. */
  352. template<typename T>
  353. void scatter(const communicator& comm, T& out_value, int root);
  354. /**
  355. * \overload
  356. */
  357. template<typename T>
  358. void
  359. scatter(const communicator& comm, const std::vector<T>& in_values,
  360. T* out_values, int n, int root);
  361. /**
  362. * \overload
  363. */
  364. template<typename T>
  365. void
  366. scatter(const communicator& comm, const T* in_values, T* out_values, int n,
  367. int root);
  368. /**
  369. * \overload
  370. */
  371. template<typename T>
  372. void scatter(const communicator& comm, T* out_values, int n, int root);
  373. /**
  374. * @brief Combine the values stored by each process into a single
  375. * value at the root.
  376. *
  377. * @c reduce is a collective algorithm that combines the values
  378. * stored by each process into a single value at the @c root. The
  379. * values can be combined arbitrarily, specified via a function
  380. * object. The type @c T of the values may be any type that is
  381. * serializable or has an associated MPI data type. One can think of
  382. * this operation as a @c gather to the @p root, followed by an @c
  383. * std::accumulate() over the gathered values and using the operation
  384. * @c op.
  385. *
  386. * When the type @c T has an associated MPI data type, this routine
  387. * invokes @c MPI_Reduce to perform the reduction. If possible,
  388. * built-in MPI operations will be used; otherwise, @c reduce() will
  389. * create a custom MPI_Op for the call to MPI_Reduce.
  390. *
  391. * @param comm The communicator over which the reduction will
  392. * occur.
  393. *
  394. * @param in_value The local value to be combined with the local
  395. * values of every other process. For reducing arrays, @c in_values
  396. * contains a pointer to the local values. In this case, @c n is
  397. * the number of values that will be reduced. Reduction occurs
  398. * independently for each of the @p n values referenced by @p
  399. * in_values, e.g., calling reduce on an array of @p n values is
  400. * like calling @c reduce @p n separate times, one for each
  401. * location in @p in_values and @p out_values.
  402. *
  403. * @param out_value Will receive the result of the reduction
  404. * operation, but only for the @p root process. Non-root processes
  405. * may omit if parameter; if they choose to supply the parameter,
  406. * it will be unchanged. For reducing arrays, @c out_values
  407. * contains a pointer to the storage for the output values.
  408. *
  409. * @param op The binary operation that combines two values of type
  410. * @c T into a third value of type @c T. For types @c T that has
  411. * ssociated MPI data types, @c op will either be translated into
  412. * an @c MPI_Op (via @c MPI_Op_create) or, if possible, mapped
  413. * directly to a built-in MPI operation. See @c is_mpi_op in the @c
  414. * operations.hpp header for more details on this mapping. For any
  415. * non-built-in operation, commutativity will be determined by the
  416. * @c is_commmutative trait (also in @c operations.hpp): users are
  417. * encouraged to mark commutative operations as such, because it
  418. * gives the implementation additional lattitude to optimize the
  419. * reduction operation.
  420. *
  421. * @param root The process ID number that will receive the final,
  422. * combined value. This value must be the same on all processes.
  423. */
  424. template<typename T, typename Op>
  425. void
  426. reduce(const communicator& comm, const T& in_value, T& out_value, Op op,
  427. int root);
  428. /**
  429. * \overload
  430. */
  431. template<typename T, typename Op>
  432. void reduce(const communicator& comm, const T& in_value, Op op, int root);
  433. /**
  434. * \overload
  435. */
  436. template<typename T, typename Op>
  437. void
  438. reduce(const communicator& comm, const T* in_values, int n, T* out_values,
  439. Op op, int root);
  440. /**
  441. * \overload
  442. */
  443. template<typename T, typename Op>
  444. void
  445. reduce(const communicator& comm, const T* in_values, int n, Op op, int root);
  446. /**
  447. * @brief Compute a prefix reduction of values from all processes in
  448. * the communicator.
  449. *
  450. * @c scan is a collective algorithm that combines the values stored
  451. * by each process with the values of all processes with a smaller
  452. * rank. The values can be arbitrarily combined, specified via a
  453. * function object @p op. The type @c T of the values may be any type
  454. * that is serializable or has an associated MPI data type. One can
  455. * think of this operation as a @c gather to some process, followed
  456. * by an @c std::prefix_sum() over the gathered values using the
  457. * operation @c op. The ith process returns the ith value emitted by
  458. * @c std::prefix_sum().
  459. *
  460. * When the type @c T has an associated MPI data type, this routine
  461. * invokes @c MPI_Scan to perform the reduction. If possible,
  462. * built-in MPI operations will be used; otherwise, @c scan() will
  463. * create a custom @c MPI_Op for the call to MPI_Scan.
  464. *
  465. * @param comm The communicator over which the prefix reduction
  466. * will occur.
  467. *
  468. * @param in_value The local value to be combined with the local
  469. * values of other processes. For the array variant, the @c
  470. * in_values parameter points to the @c n local values that will be
  471. * combined.
  472. *
  473. * @param out_value If provided, the ith process will receive the
  474. * value @c op(in_value[0], op(in_value[1], op(..., in_value[i])
  475. * ... )). For the array variant, @c out_values contains a pointer
  476. * to storage for the @c n output values. The prefix reduction
  477. * occurs independently for each of the @p n values referenced by
  478. * @p in_values, e.g., calling scan on an array of @p n values is
  479. * like calling @c scan @p n separate times, one for each location
  480. * in @p in_values and @p out_values.
  481. *
  482. * @param op The binary operation that combines two values of type
  483. * @c T into a third value of type @c T. For types @c T that has
  484. * ssociated MPI data types, @c op will either be translated into
  485. * an @c MPI_Op (via @c MPI_Op_create) or, if possible, mapped
  486. * directly to a built-in MPI operation. See @c is_mpi_op in the @c
  487. * operations.hpp header for more details on this mapping. For any
  488. * non-built-in operation, commutativity will be determined by the
  489. * @c is_commmutative trait (also in @c operations.hpp).
  490. *
  491. * @returns If no @p out_value parameter is provided, returns the
  492. * result of prefix reduction.
  493. */
  494. template<typename T, typename Op>
  495. void
  496. scan(const communicator& comm, const T& in_value, T& out_value, Op op);
  497. /**
  498. * \overload
  499. */
  500. template<typename T, typename Op>
  501. T
  502. scan(const communicator& comm, const T& in_value, Op op);
  503. /**
  504. * \overload
  505. */
  506. template<typename T, typename Op>
  507. void
  508. scan(const communicator& comm, const T* in_values, int n, T* out_values, Op op);
  509. } } // end namespace boost::mpi
  510. #endif // BOOST_MPI_COLLECTIVES_HPP
  511. #ifndef BOOST_MPI_COLLECTIVES_FORWARD_ONLY
  512. // Include implementations of each of the collectives
  513. # include <boost/mpi/collectives/all_gather.hpp>
  514. # include <boost/mpi/collectives/all_reduce.hpp>
  515. # include <boost/mpi/collectives/all_to_all.hpp>
  516. # include <boost/mpi/collectives/broadcast.hpp>
  517. # include <boost/mpi/collectives/gather.hpp>
  518. # include <boost/mpi/collectives/scatter.hpp>
  519. # include <boost/mpi/collectives/reduce.hpp>
  520. # include <boost/mpi/collectives/scan.hpp>
  521. #endif