1 // Copyright (C) 2007 Trustees of Indiana University
3 // Authors: Douglas Gregor
6 // Use, modification and distribution is subject to the Boost Software
7 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
12 * This header defines the @c group class, which allows one to
13 * manipulate and query groups of processes.
15 #ifndef BOOST_MPI_GROUP_HPP
16 #define BOOST_MPI_GROUP_HPP
18 #include <boost/mpi/exception.hpp>
19 #include <boost/mpi/detail/antiques.hpp>
20 #include <boost/shared_ptr.hpp>
21 #include <boost/optional.hpp>
24 namespace boost { namespace mpi {
27 * @brief A @c group is a representation of a subset of the processes
28 * within a @c communicator.
30 * The @c group class allows one to create arbitrary subsets of the
31 * processes within a communicator. One can compute the union,
32 * intersection, or difference of two groups, or create new groups by
33 * specifically including or excluding certain processes. Given a
34 * group, one can create a new communicator containing only the
35 * processes in that group.
37 class BOOST_MPI_DECL group
41 * @brief Constructs an empty group.
43 group() : group_ptr() { }
46 * @brief Constructs a group from an @c MPI_Group.
48 * This routine allows one to construct a Boost.MPI @c group from a
49 * C @c MPI_Group. The @c group object can (optionally) adopt the @c
50 * MPI_Group, after which point the @c group object becomes
51 * responsible for freeing the @c MPI_Group when the last copy of @c
54 * @param in_group The @c MPI_Group used to construct this @c group.
56 * @param adopt Whether the @c group should adopt the @c
57 * MPI_Group. When true, the @c group object (or one of its copies)
58 * will free the group (via @c MPI_Comm_free) when the last copy is
59 * destroyed. Otherwise, the user is responsible for calling @c
62 group(const MPI_Group& in_group, bool adopt);
65 * @brief Determine the rank of the calling process in the group.
67 * This routine is equivalent to @c MPI_Group_rank.
69 * @returns The rank of the calling process in the group, which will
70 * be a value in [0, size()). If the calling process is not in the
71 * group, returns an empty value.
73 optional<int> rank() const;
76 * @brief Determine the number of processes in the group.
78 * This routine is equivalent to @c MPI_Group_size.
80 * @returns The number of processes in the group.
85 * @brief Translates the ranks from one group into the ranks of the
86 * same processes in another group.
88 * This routine translates each of the integer rank values in the
89 * iterator range @c [first, last) from the current group into rank
90 * values of the corresponding processes in @p to_group. The
91 * corresponding rank values are written via the output iterator @c
92 * out. When there is no correspondence between a rank in the
93 * current group and a rank in @c to_group, the value @c
94 * MPI_UNDEFINED is written to the output iterator.
96 * @param first Beginning of the iterator range of ranks in the
99 * @param last Past the end of the iterator range of ranks in the
102 * @param to_group The group that we are translating ranks to.
104 * @param out The output iterator to which the translated ranks will
107 * @returns the output iterator, which points one step past the last
110 template<typename InputIterator, typename OutputIterator>
111 OutputIterator translate_ranks(InputIterator first, InputIterator last,
112 const group& to_group, OutputIterator out);
115 * @brief Determines whether the group is non-empty.
117 * @returns True if the group is not empty, false if it is empty.
119 operator bool() const { return (bool)group_ptr; }
122 * @brief Retrieves the underlying @c MPI_Group associated with this
125 * @returns The @c MPI_Group handle manipulated by this object. If
126 * this object represents the empty group, returns @c
129 operator MPI_Group() const
134 return MPI_GROUP_EMPTY;
138 * @brief Creates a new group including a subset of the processes
139 * in the current group.
141 * This routine creates a new @c group which includes only those
142 * processes in the current group that are listed in the integer
143 * iterator range @c [first, last). Equivalent to @c
146 * @c first The beginning of the iterator range of ranks to include.
148 * @c last Past the end of the iterator range of ranks to include.
150 * @returns A new group containing those processes with ranks @c
151 * [first, last) in the current group.
153 template<typename InputIterator>
154 group include(InputIterator first, InputIterator last);
157 * @brief Creates a new group from all of the processes in the
158 * current group, exluding a specific subset of the processes.
160 * This routine creates a new @c group which includes all of the
161 * processes in the current group except those whose ranks are
162 * listed in the integer iterator range @c [first,
163 * last). Equivalent to @c MPI_Group_excl.
165 * @c first The beginning of the iterator range of ranks to exclude.
167 * @c last Past the end of the iterator range of ranks to exclude.
169 * @returns A new group containing all of the processes in the
170 * current group except those processes with ranks @c [first, last)
171 * in the current group.
173 template<typename InputIterator>
174 group exclude(InputIterator first, InputIterator last);
181 * Function object that frees an MPI group and deletes the
182 * memory associated with it. Intended to be used as a deleter with
187 void operator()(MPI_Group* comm) const
190 BOOST_MPI_CHECK_RESULT(MPI_Finalized, (&finalized));
192 BOOST_MPI_CHECK_RESULT(MPI_Group_free, (comm));
198 * The underlying MPI group. This is a shared pointer, so the actual
199 * MPI group which will be shared among all related instances of the
200 * @c group class. When there are no more such instances, the group
201 * will be automatically freed.
203 shared_ptr<MPI_Group> group_ptr;
207 * @brief Determines whether two process groups are identical.
209 * Equivalent to calling @c MPI_Group_compare and checking whether the
210 * result is @c MPI_IDENT.
212 * @returns True when the two process groups contain the same
213 * processes in the same order.
215 BOOST_MPI_DECL bool operator==(const group& g1, const group& g2);
218 * @brief Determines whether two process groups are not identical.
220 * Equivalent to calling @c MPI_Group_compare and checking whether the
221 * result is not @c MPI_IDENT.
223 * @returns False when the two process groups contain the same
224 * processes in the same order.
226 inline bool operator!=(const group& g1, const group& g2)
232 * @brief Computes the union of two process groups.
234 * This routine returns a new @c group that contains all processes
235 * that are either in group @c g1 or in group @c g2 (or both). The
236 * processes that are in @c g1 will be first in the resulting group,
237 * followed by the processes from @c g2 (but not also in @c
238 * g1). Equivalent to @c MPI_Group_union.
240 BOOST_MPI_DECL group operator|(const group& g1, const group& g2);
243 * @brief Computes the intersection of two process groups.
245 * This routine returns a new @c group that contains all processes
246 * that are in group @c g1 and in group @c g2, ordered in the same way
247 * as @c g1. Equivalent to @c MPI_Group_intersection.
249 BOOST_MPI_DECL group operator&(const group& g1, const group& g2);
252 * @brief Computes the difference between two process groups.
254 * This routine returns a new @c group that contains all processes
255 * that are in group @c g1 but not in group @c g2, ordered in the same way
256 * as @c g1. Equivalent to @c MPI_Group_difference.
258 BOOST_MPI_DECL group operator-(const group& g1, const group& g2);
260 /************************************************************************
261 * Implementation details *
262 ************************************************************************/
263 template<typename InputIterator, typename OutputIterator>
265 group::translate_ranks(InputIterator first, InputIterator last,
266 const group& to_group, OutputIterator out)
268 std::vector<int> in_array(first, last);
269 if (in_array.empty())
272 std::vector<int> out_array(in_array.size());
273 BOOST_MPI_CHECK_RESULT(MPI_Group_translate_ranks,
276 detail::c_data(in_array),
278 detail::c_data(out_array)));
280 for (std::vector<int>::size_type i = 0, n = out_array.size(); i < n; ++i)
281 *out++ = out_array[i];
288 * Specialization of translate_ranks that handles the one case where
289 * we can avoid any memory allocation or copying.
293 group::translate_ranks(int* first, int* last, const group& to_group, int* out);
295 template<typename InputIterator>
296 group group::include(InputIterator first, InputIterator last)
301 std::vector<int> ranks(first, last);
303 BOOST_MPI_CHECK_RESULT(MPI_Group_incl,
304 ((MPI_Group)*this, ranks.size(), detail::c_data(ranks), &result));
305 return group(result, /*adopt=*/true);
311 * Specialization of group::include that handles the one case where we
312 * can avoid any memory allocation or copying before creating the
315 template<> BOOST_MPI_DECL group group::include(int* first, int* last);
317 template<typename InputIterator>
318 group group::exclude(InputIterator first, InputIterator last)
323 std::vector<int> ranks(first, last);
325 BOOST_MPI_CHECK_RESULT(MPI_Group_excl,
326 ((MPI_Group)*this, ranks.size(), detail::c_data(ranks), &result));
327 return group(result, /*adopt=*/true);
333 * Specialization of group::exclude that handles the one case where we
334 * can avoid any memory allocation or copying before creating the
337 template<> BOOST_MPI_DECL group group::exclude(int* first, int* last);
339 } } // end namespace boost::mpi
341 #endif // BOOST_MPI_GROUP_HPP