1 // Copyright (C) 2005-2006 Douglas Gregor <doug.gregor -at- gmail.com>.
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // Message Passing Interface 1.1 -- Section 4. MPI Collectives
9 /** @file collectives.hpp
11 * This header contains MPI collective operations, which implement
12 * various parallel algorithms that require the coordination of all
13 * processes within a communicator. The header @c collectives_fwd.hpp
14 * provides forward declarations for each of these operations. To
15 * include only specific collective algorithms, use the headers @c
16 * boost/mpi/collectives/algorithm_name.hpp.
18 #ifndef BOOST_MPI_COLLECTIVES_HPP
19 #define BOOST_MPI_COLLECTIVES_HPP
21 #include <boost/mpi/communicator.hpp>
22 #include <boost/mpi/inplace.hpp>
25 namespace boost { namespace mpi {
27 * @brief Gather the values stored at every process into vectors of
28 * values from each process.
30 * @c all_gather is a collective algorithm that collects the values
31 * stored at each process into a vector of values indexed by the
32 * process number they came from. The type @c T of the values may be
33 * any type that is serializable or has an associated MPI data type.
35 * When the type @c T has an associated MPI data type, this routine
36 * invokes @c MPI_Allgather to gather the values.
38 * @param comm The communicator over which the all-gather will
41 * @param in_value The value to be transmitted by each process. To
42 * gather an array of values, @c in_values points to the @c n local
43 * values to be transmitted.
45 * @param out_values A vector or pointer to storage that will be
46 * populated with the values from each process, indexed by the
47 * process ID number. If it is a vector, the vector will be resized
52 all_gather(const communicator& comm, const T& in_value,
53 std::vector<T>& out_values);
60 all_gather(const communicator& comm, const T& in_value, T* out_values);
67 all_gather(const communicator& comm, const T* in_values, int n,
68 std::vector<T>& out_values);
75 all_gather(const communicator& comm, const T* in_values, int n, T* out_values);
78 * @brief Combine the values stored by each process into a single
79 * value available to all processes.
81 * @c all_reduce is a collective algorithm that combines the values
82 * stored by each process into a single value available to all
83 * processes. The values are combined in a user-defined way,
84 * specified via a function object. The type @c T of the values may
85 * be any type that is serializable or has an associated MPI data
86 * type. One can think of this operation as a @c all_gather, followed
87 * by an @c std::accumulate() over the gather values and using the
90 * When the type @c T has an associated MPI data type, this routine
91 * invokes @c MPI_Allreduce to perform the reduction. If possible,
92 * built-in MPI operations will be used; otherwise, @c all_reduce()
93 * will create a custom MPI_Op for the call to MPI_Allreduce.
95 * @param comm The communicator over which the reduction will
97 * @param value The local value to be combined with the local
98 * values of every other process. For reducing arrays, @c in_values
99 * is a pointer to the local values to be reduced and @c n is the
100 * number of values to reduce. See @c reduce for more information.
102 * If wrapped in a @c inplace_t object, combine the usage of both
103 * input and $c out_value and the local value will be overwritten
104 * (a convenience function @c inplace is provided for the wrapping).
106 * @param out_value Will receive the result of the reduction
107 * operation. If this parameter is omitted, the outgoing value will
108 * instead be returned.
110 * @param op The binary operation that combines two values of type
111 * @c T and returns a third value of type @c T. For types @c T that has
112 * ssociated MPI data types, @c op will either be translated into
113 * an @c MPI_Op (via @c MPI_Op_create) or, if possible, mapped
114 * directly to a built-in MPI operation. See @c is_mpi_op in the @c
115 * operations.hpp header for more details on this mapping. For any
116 * non-built-in operation, commutativity will be determined by the
117 * @c is_commmutative trait (also in @c operations.hpp): users are
118 * encouraged to mark commutative operations as such, because it
119 * gives the implementation additional lattitude to optimize the
120 * reduction operation.
122 * @param n Indicated the size of the buffers of array type.
123 * @returns If no @p out_value parameter is supplied, returns the
124 * result of the reduction operation.
126 template<typename T, typename Op>
128 all_reduce(const communicator& comm, const T* value, int n, T* out_value,
133 template<typename T, typename Op>
135 all_reduce(const communicator& comm, const T& value, T& out_value, Op op);
139 template<typename T, typename Op>
140 T all_reduce(const communicator& comm, const T& value, Op op);
145 template<typename T, typename Op>
147 all_reduce(const communicator& comm, inplace_t<T*> value, int n,
152 template<typename T, typename Op>
154 all_reduce(const communicator& comm, inplace_t<T> value, Op op);
157 * @brief Send data from every process to every other process.
159 * @c all_to_all is a collective algorithm that transmits @c p values
160 * from every process to every other process. On process i, jth value
161 * of the @p in_values vector is sent to process j and placed in the
162 * ith position of the @p out_values vector in process @p j. The type
163 * @c T of the values may be any type that is serializable or has an
164 * associated MPI data type. If @c n is provided, then arrays of @p n
165 * values will be transferred from one process to another.
167 * When the type @c T has an associated MPI data type, this routine
168 * invokes @c MPI_Alltoall to scatter the values.
170 * @param comm The communicator over which the all-to-all
171 * communication will occur.
173 * @param in_values A vector or pointer to storage that contains
174 * the values to send to each process, indexed by the process ID
177 * @param out_values A vector or pointer to storage that will be
178 * updated to contain the values received from other processes. The
179 * jth value in @p out_values will come from the procss with rank j.
183 all_to_all(const communicator& comm, const std::vector<T>& in_values,
184 std::vector<T>& out_values);
190 void all_to_all(const communicator& comm, const T* in_values, T* out_values);
197 all_to_all(const communicator& comm, const std::vector<T>& in_values, int n,
198 std::vector<T>& out_values);
205 all_to_all(const communicator& comm, const T* in_values, int n, T* out_values);
208 * @brief Broadcast a value from a root process to all other
211 * @c broadcast is a collective algorithm that transfers a value from
212 * an arbitrary @p root process to every other process that is part of
213 * the given communicator. The @c broadcast algorithm can transmit any
214 * Serializable value, values that have associated MPI data types,
215 * packed archives, skeletons, and the content of skeletons; see the
216 * @c send primitive for communicators for a complete list. The type
217 * @c T shall be the same for all processes that are a part of the
218 * communicator @p comm, unless packed archives are being transferred:
219 * with packed archives, the root sends a @c packed_oarchive or @c
220 * packed_skeleton_oarchive whereas the other processes receive a
221 * @c packed_iarchive or @c packed_skeleton_iarchve, respectively.
223 * When the type @c T has an associated MPI data type, this routine
224 * invokes @c MPI_Bcast to perform the broadcast.
226 * @param comm The communicator over which the broadcast will
229 * @param value The value (or values, if @p n is provided) to be
230 * transmitted (if the rank of @p comm is equal to @p root) or
231 * received (if the rank of @p comm is not equal to @p root). When
232 * the @p value is a @c skeleton_proxy, only the skeleton of the
233 * object will be broadcast. In this case, the @p root will build a
234 * skeleton from the object help in the proxy and all of the
235 * non-roots will reshape the objects held in their proxies based on
236 * the skeleton sent from the root.
238 * @param n When supplied, the number of values that the pointer @p
239 * values points to, for broadcasting an array of values. The value
240 * of @p n must be the same for all processes in @p comm.
242 * @param root The rank/process ID of the process that will be
243 * transmitting the value.
246 void broadcast(const communicator& comm, T& value, int root);
252 void broadcast(const communicator& comm, T* values, int n, int root);
258 void broadcast(const communicator& comm, skeleton_proxy<T>& value, int root);
265 broadcast(const communicator& comm, const skeleton_proxy<T>& value, int root);
268 * @brief Gather the values stored at every process into a vector at
271 * @c gather is a collective algorithm that collects the values
272 * stored at each process into a vector of values at the @p root
273 * process. This vector is indexed by the process number that the
274 * value came from. The type @c T of the values may be any type that
275 * is serializable or has an associated MPI data type.
277 * When the type @c T has an associated MPI data type, this routine
278 * invokes @c MPI_Gather to gather the values.
280 * @param comm The communicator over which the gather will occur.
282 * @param in_value The value to be transmitted by each process. For
283 * gathering arrays of values, @c in_values points to storage for
284 * @c n*comm.size() values.
286 * @param out_values A vector or pointer to storage that will be
287 * populated with the values from each process, indexed by the
288 * process ID number. If it is a vector, it will be resized
289 * accordingly. For non-root processes, this parameter may be
290 * omitted. If it is still provided, however, it will be unchanged.
292 * @param root The process ID number that will collect the
293 * values. This value must be the same on all processes.
297 gather(const communicator& comm, const T& in_value, std::vector<T>& out_values,
305 gather(const communicator& comm, const T& in_value, T* out_values, int root);
311 void gather(const communicator& comm, const T& in_value, int root);
318 gather(const communicator& comm, const T* in_values, int n,
319 std::vector<T>& out_values, int root);
326 gather(const communicator& comm, const T* in_values, int n, T* out_values,
333 void gather(const communicator& comm, const T* in_values, int n, int root);
336 * @brief Similar to boost::mpi::gather with the difference that the number
337 * of values to be send by non-root processes can vary.
339 * @param comm The communicator over which the gather will occur.
341 * @param in_values The array of values to be transmitted by each process.
343 * @param in_size For each non-root process this specifies the size
346 * @param out_values A pointer to storage that will be populated with
347 * the values from each process. For non-root processes, this parameter
348 * may be omitted. If it is still provided, however, it will be unchanged.
350 * @param sizes A vector containing the number of elements each non-root
353 * @param displs A vector such that the i-th entry specifies the
354 * displacement (relative to @p out_values) from which to take the ingoing
355 * data at the @p root process. Overloaded versions for which @p displs is
356 * omitted assume that the data is to be placed contiguously at the root process.
358 * @param root The process ID number that will collect the
359 * values. This value must be the same on all processes.
363 gatherv(const communicator& comm, const std::vector<T>& in_values,
364 T* out_values, const std::vector<int>& sizes, const std::vector<int>& displs,
372 gatherv(const communicator& comm, const T* in_values, int in_size,
373 T* out_values, const std::vector<int>& sizes, const std::vector<int>& displs,
380 void gatherv(const communicator& comm, const std::vector<T>& in_values, int root);
386 void gatherv(const communicator& comm, const T* in_values, int in_size, int root);
393 gatherv(const communicator& comm, const T* in_values, int in_size,
394 T* out_values, const std::vector<int>& sizes, int root);
401 gatherv(const communicator& comm, const std::vector<T>& in_values,
402 T* out_values, const std::vector<int>& sizes, int root);
405 * @brief Scatter the values stored at the root to all processes
406 * within the communicator.
408 * @c scatter is a collective algorithm that scatters the values
409 * stored in the @p root process (inside a vector) to all of the
410 * processes in the communicator. The vector @p out_values (only
411 * significant at the @p root) is indexed by the process number to
412 * which the corresponding value will be sent. The type @c T of the
413 * values may be any type that is serializable or has an associated
416 * When the type @c T has an associated MPI data type, this routine
417 * invokes @c MPI_Scatter to scatter the values.
419 * @param comm The communicator over which the scatter will occur.
421 * @param in_values A vector or pointer to storage that will contain
422 * the values to send to each process, indexed by the process rank.
423 * For non-root processes, this parameter may be omitted. If it is
424 * still provided, however, it will be unchanged.
426 * @param out_value The value received by each process. When
427 * scattering an array of values, @p out_values points to the @p n
428 * values that will be received by each process.
430 * @param root The process ID number that will scatter the
431 * values. This value must be the same on all processes.
435 scatter(const communicator& comm, const std::vector<T>& in_values, T& out_value,
443 scatter(const communicator& comm, const T* in_values, T& out_value, int root);
449 void scatter(const communicator& comm, T& out_value, int root);
456 scatter(const communicator& comm, const std::vector<T>& in_values,
457 T* out_values, int n, int root);
464 scatter(const communicator& comm, const T* in_values, T* out_values, int n,
471 void scatter(const communicator& comm, T* out_values, int n, int root);
474 * @brief Similar to boost::mpi::scatter with the difference that the number
475 * of values stored at the root process does not need to be a multiple of
476 * the communicator's size.
478 * @param comm The communicator over which the scatter will occur.
480 * @param in_values A vector or pointer to storage that will contain
481 * the values to send to each process, indexed by the process rank.
482 * For non-root processes, this parameter may be omitted. If it is
483 * still provided, however, it will be unchanged.
485 * @param sizes A vector containing the number of elements each non-root
486 * process will receive.
488 * @param displs A vector such that the i-th entry specifies the
489 * displacement (relative to @p in_values) from which to take the outgoing
490 * data to process i. Overloaded versions for which @p displs is omitted
491 * assume that the data is contiguous at the @p root process.
493 * @param out_values The array of values received by each process.
495 * @param out_size For each non-root process this will contain the size
498 * @param root The process ID number that will scatter the
499 * values. This value must be the same on all processes.
503 scatterv(const communicator& comm, const std::vector<T>& in_values,
504 const std::vector<int>& sizes, const std::vector<int>& displs,
505 T* out_values, int out_size, int root);
512 scatterv(const communicator& comm, const T* in_values,
513 const std::vector<int>& sizes, const std::vector<int>& displs,
514 T* out_values, int out_size, int root);
520 void scatterv(const communicator& comm, T* out_values, int out_size, int root);
527 scatterv(const communicator& comm, const T* in_values,
528 const std::vector<int>& sizes, T* out_values, int root);
535 scatterv(const communicator& comm, const std::vector<T>& in_values,
536 const std::vector<int>& sizes, T* out_values, int root);
539 * @brief Combine the values stored by each process into a single
542 * @c reduce is a collective algorithm that combines the values
543 * stored by each process into a single value at the @c root. The
544 * values can be combined arbitrarily, specified via a function
545 * object. The type @c T of the values may be any type that is
546 * serializable or has an associated MPI data type. One can think of
547 * this operation as a @c gather to the @p root, followed by an @c
548 * std::accumulate() over the gathered values and using the operation
551 * When the type @c T has an associated MPI data type, this routine
552 * invokes @c MPI_Reduce to perform the reduction. If possible,
553 * built-in MPI operations will be used; otherwise, @c reduce() will
554 * create a custom MPI_Op for the call to MPI_Reduce.
556 * @param comm The communicator over which the reduction will
559 * @param in_value The local value to be combined with the local
560 * values of every other process. For reducing arrays, @c in_values
561 * contains a pointer to the local values. In this case, @c n is
562 * the number of values that will be reduced. Reduction occurs
563 * independently for each of the @p n values referenced by @p
564 * in_values, e.g., calling reduce on an array of @p n values is
565 * like calling @c reduce @p n separate times, one for each
566 * location in @p in_values and @p out_values.
568 * @param out_value Will receive the result of the reduction
569 * operation, but only for the @p root process. Non-root processes
570 * may omit if parameter; if they choose to supply the parameter,
571 * it will be unchanged. For reducing arrays, @c out_values
572 * contains a pointer to the storage for the output values.
574 * @param op The binary operation that combines two values of type
575 * @c T into a third value of type @c T. For types @c T that has
576 * ssociated MPI data types, @c op will either be translated into
577 * an @c MPI_Op (via @c MPI_Op_create) or, if possible, mapped
578 * directly to a built-in MPI operation. See @c is_mpi_op in the @c
579 * operations.hpp header for more details on this mapping. For any
580 * non-built-in operation, commutativity will be determined by the
581 * @c is_commmutative trait (also in @c operations.hpp): users are
582 * encouraged to mark commutative operations as such, because it
583 * gives the implementation additional lattitude to optimize the
584 * reduction operation.
586 * @param root The process ID number that will receive the final,
587 * combined value. This value must be the same on all processes.
589 template<typename T, typename Op>
591 reduce(const communicator& comm, const T& in_value, T& out_value, Op op,
597 template<typename T, typename Op>
598 void reduce(const communicator& comm, const T& in_value, Op op, int root);
603 template<typename T, typename Op>
605 reduce(const communicator& comm, const T* in_values, int n, T* out_values,
611 template<typename T, typename Op>
613 reduce(const communicator& comm, const T* in_values, int n, Op op, int root);
616 * @brief Compute a prefix reduction of values from all processes in
619 * @c scan is a collective algorithm that combines the values stored
620 * by each process with the values of all processes with a smaller
621 * rank. The values can be arbitrarily combined, specified via a
622 * function object @p op. The type @c T of the values may be any type
623 * that is serializable or has an associated MPI data type. One can
624 * think of this operation as a @c gather to some process, followed
625 * by an @c std::prefix_sum() over the gathered values using the
626 * operation @c op. The ith process returns the ith value emitted by
627 * @c std::prefix_sum().
629 * When the type @c T has an associated MPI data type, this routine
630 * invokes @c MPI_Scan to perform the reduction. If possible,
631 * built-in MPI operations will be used; otherwise, @c scan() will
632 * create a custom @c MPI_Op for the call to MPI_Scan.
634 * @param comm The communicator over which the prefix reduction
637 * @param in_value The local value to be combined with the local
638 * values of other processes. For the array variant, the @c
639 * in_values parameter points to the @c n local values that will be
642 * @param out_value If provided, the ith process will receive the
643 * value @c op(in_value[0], op(in_value[1], op(..., in_value[i])
644 * ... )). For the array variant, @c out_values contains a pointer
645 * to storage for the @c n output values. The prefix reduction
646 * occurs independently for each of the @p n values referenced by
647 * @p in_values, e.g., calling scan on an array of @p n values is
648 * like calling @c scan @p n separate times, one for each location
649 * in @p in_values and @p out_values.
651 * @param op The binary operation that combines two values of type
652 * @c T into a third value of type @c T. For types @c T that has
653 * ssociated MPI data types, @c op will either be translated into
654 * an @c MPI_Op (via @c MPI_Op_create) or, if possible, mapped
655 * directly to a built-in MPI operation. See @c is_mpi_op in the @c
656 * operations.hpp header for more details on this mapping. For any
657 * non-built-in operation, commutativity will be determined by the
658 * @c is_commmutative trait (also in @c operations.hpp).
660 * @returns If no @p out_value parameter is provided, returns the
661 * result of prefix reduction.
663 template<typename T, typename Op>
665 scan(const communicator& comm, const T& in_value, T& out_value, Op op);
670 template<typename T, typename Op>
672 scan(const communicator& comm, const T& in_value, Op op);
677 template<typename T, typename Op>
679 scan(const communicator& comm, const T* in_values, int n, T* out_values, Op op);
681 } } // end namespace boost::mpi
682 #endif // BOOST_MPI_COLLECTIVES_HPP
684 #ifndef BOOST_MPI_COLLECTIVES_FORWARD_ONLY
685 // Include implementations of each of the collectives
686 # include <boost/mpi/collectives/all_gather.hpp>
687 # include <boost/mpi/collectives/all_reduce.hpp>
688 # include <boost/mpi/collectives/all_to_all.hpp>
689 # include <boost/mpi/collectives/broadcast.hpp>
690 # include <boost/mpi/collectives/gather.hpp>
691 # include <boost/mpi/collectives/gatherv.hpp>
692 # include <boost/mpi/collectives/scatter.hpp>
693 # include <boost/mpi/collectives/scatterv.hpp>
694 # include <boost/mpi/collectives/reduce.hpp>
695 # include <boost/mpi/collectives/scan.hpp>