cartesian_communicator.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // Copyright Alain Miniussi 2014.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Alain Miniussi
  6. /** @file cartesian_communicator.hpp
  7. *
  8. * This header defines facilities to support MPI communicators with
  9. * cartesian topologies.
  10. * If known at compiled time, the dimension of the implied grid
  11. * can be statically enforced, through the templatized communicator
  12. * class. Otherwise, a non template, dynamic, base class is provided.
  13. *
  14. */
  15. #ifndef BOOST_MPI_CARTESIAN_COMMUNICATOR_HPP
  16. #define BOOST_MPI_CARTESIAN_COMMUNICATOR_HPP
  17. #include <boost/mpi/communicator.hpp>
  18. #include <vector>
  19. #include <utility>
  20. #include <iostream>
  21. #include <utility>
  22. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  23. #include <initializer_list>
  24. #endif // BOOST_NO_CXX11_HDR_INITIALIZER_LIST
  25. // Headers required to implement cartesian topologies
  26. #include <boost/shared_array.hpp>
  27. #include <boost/assert.hpp>
  28. #include <boost/foreach.hpp>
  29. namespace boost { namespace mpi {
  30. /**
  31. * @brief Specify the size and periodicity of the grid in a single dimension.
  32. *
  33. * POD lightweight object.
  34. */
  35. struct cartesian_dimension {
  36. /** The size of the grid n this dimension. */
  37. int size;
  38. /** Is the grid periodic in this dimension. */
  39. bool periodic;
  40. cartesian_dimension(int sz = 0, bool p = false) : size(sz), periodic(p) {}
  41. private:
  42. friend class boost::serialization::access;
  43. template<class Archive>
  44. void serialize(Archive & ar, const unsigned int version)
  45. {
  46. ar & size & periodic;
  47. }
  48. };
  49. template <>
  50. struct is_mpi_datatype<cartesian_dimension> : mpl::true_ { };
  51. /**
  52. * @brief Test if the dimensions values are identical.
  53. */
  54. inline
  55. bool
  56. operator==(cartesian_dimension const& d1, cartesian_dimension const& d2) {
  57. return &d1 == &d2 || (d1.size == d2.size && d1.periodic == d2.periodic);
  58. }
  59. /**
  60. * @brief Test if the dimension values are different.
  61. */
  62. inline
  63. bool
  64. operator!=(cartesian_dimension const& d1, cartesian_dimension const& d2) {
  65. return !(d1 == d2);
  66. }
  67. /**
  68. * @brief Pretty printing of a cartesian dimension (size, periodic)
  69. */
  70. std::ostream& operator<<(std::ostream& out, cartesian_dimension const& d);
  71. /**
  72. * @brief Describe the topology of a cartesian grid.
  73. *
  74. * Behave mostly like a sequence of @c cartesian_dimension with the notable
  75. * exception that its size is fixed.
  76. * This is a lightweight object, so that any constructor that could be considered
  77. * missing could be replaced with a function (move constructor provided when supported).
  78. */
  79. class BOOST_MPI_DECL cartesian_topology
  80. : private std::vector<cartesian_dimension> {
  81. friend class cartesian_communicator;
  82. typedef std::vector<cartesian_dimension> super;
  83. public:
  84. /**
  85. * Retrieve a specific dimension.
  86. */
  87. using super::operator[];
  88. /**
  89. * @brief Topology dimentionality.
  90. */
  91. using super::size;
  92. using super::begin;
  93. using super::end;
  94. using super::swap;
  95. #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
  96. cartesian_topology() = delete;
  97. #endif
  98. #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
  99. cartesian_topology(cartesian_topology const&) = default;
  100. cartesian_topology& operator=(cartesian_topology const&) = default;
  101. // There is apparently no macro for checking the support of move constructor.
  102. // Assume that defaulted function is close enough.
  103. #if !defined(BOOST_NO_CXX11_DEFAULTED_MOVES)
  104. cartesian_topology(cartesian_topology&& other) : super(other) {}
  105. cartesian_topology& operator=(cartesian_topology&& other) {
  106. stl().swap(other.stl());
  107. return *this;
  108. }
  109. #endif
  110. ~cartesian_topology() = default;
  111. #endif
  112. /**
  113. * @brief Create a N dimension space.
  114. * Each dimension is initialized as non periodic of size 0.
  115. */
  116. cartesian_topology(int ndim)
  117. : super(ndim) {}
  118. /**
  119. * @brief Use the provided dimensions specification as initial values.
  120. */
  121. cartesian_topology(std::vector<cartesian_dimension> const& dims)
  122. : super(dims) {}
  123. /**
  124. * @brief Use dimensions specification provided in the sequence container as initial values.
  125. * #param dims must be a sequence container.
  126. */
  127. template<class InitArr>
  128. explicit cartesian_topology(InitArr dims)
  129. : super(0) {
  130. BOOST_FOREACH(cartesian_dimension const& d, dims) {
  131. push_back(d);
  132. }
  133. }
  134. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  135. /**
  136. * @brief Use dimensions specification provided in the initialization list as initial values.
  137. * #param dims can be of the form { dim_1, false}, .... {dim_n, true}
  138. */
  139. explicit cartesian_topology(std::initializer_list<cartesian_dimension> dims)
  140. : super(dims) {}
  141. #endif
  142. /**
  143. * @brief Use dimensions specification provided in the array.
  144. * #param dims can be of the form { dim_1, false}, .... {dim_n, true}
  145. */
  146. template<int NDIM>
  147. explicit cartesian_topology(cartesian_dimension (&dims)[NDIM])
  148. : super(dims, dims+NDIM) {}
  149. /**
  150. * @brief Use dimensions specification provided in the input ranges
  151. * The ranges do not need to be the same size. If the sizes are different,
  152. * the missing values will be complete with zeros of the dim and assumed non periodic.
  153. * @param dim_rg the dimensions, values must convert to integers.
  154. * @param period_rg the periodicities, values must convert to booleans.
  155. * #param dims can be of the form { dim_1, false}, .... {dim_n, true}
  156. */
  157. template<class DimRg, class PerRg>
  158. cartesian_topology(DimRg const& dim_rg, PerRg const& period_rg)
  159. : super(0) {
  160. BOOST_FOREACH(int d, dim_rg) {
  161. super::push_back(cartesian_dimension(d));
  162. }
  163. super::iterator it = begin();
  164. BOOST_FOREACH(bool p, period_rg) {
  165. if (it < end()) {
  166. it->periodic = p;
  167. } else {
  168. push_back(cartesian_dimension(0,p));
  169. }
  170. ++it;
  171. }
  172. }
  173. /**
  174. * @brief Iterator based initializer.
  175. * Will use the first n iterated values.
  176. * Both iterators can be single pass.
  177. * @param dit dimension iterator, value must convert to integer type.
  178. * @param pit periodicity iterator, value must convert to booleans..
  179. */
  180. template<class DimIter, class PerIter>
  181. cartesian_topology(DimIter dit, PerIter pit, int n)
  182. : super(n) {
  183. for(int i = 0; i < n; ++i) {
  184. (*this)[i] = cartesian_dimension(*dit++, *pit++);
  185. }
  186. }
  187. /**
  188. * Export as an stl sequence.
  189. */
  190. std::vector<cartesian_dimension>& stl() { return *this; }
  191. /**
  192. * Export as an stl sequence.
  193. */
  194. std::vector<cartesian_dimension> const& stl() const{ return *this; }
  195. /**
  196. * Split the topology in two sequences of sizes and periodicities.
  197. */
  198. void split(std::vector<int>& dims, std::vector<bool>& periodics) const;
  199. };
  200. inline
  201. bool
  202. operator==(cartesian_topology const& t1, cartesian_topology const& t2) {
  203. return t1.stl() == t2.stl();
  204. }
  205. inline
  206. bool
  207. operator!=(cartesian_topology const& t1, cartesian_topology const& t2) {
  208. return t1.stl() != t2.stl();
  209. }
  210. /**
  211. * @brief Pretty printing of a cartesian topology
  212. */
  213. std::ostream& operator<<(std::ostream& out, cartesian_topology const& t);
  214. /**
  215. * @brief An MPI communicator with a cartesian topology.
  216. *
  217. * A @c cartesian_communicator is a communicator whose topology is
  218. * expressed as a grid. Cartesian communicators have the same
  219. * functionality as (intra)communicators, but also allow one to query
  220. * the relationships among processes and the properties of the grid.
  221. */
  222. class BOOST_MPI_DECL cartesian_communicator : public communicator
  223. {
  224. friend class communicator;
  225. /**
  226. * INTERNAL ONLY
  227. *
  228. * Construct a cartesian communicator given a shared pointer to the
  229. * underlying MPI_Comm (which must have a cartesian topology).
  230. * This operation is used for "casting" from a communicator to
  231. * a cartesian communicator.
  232. */
  233. explicit cartesian_communicator(const shared_ptr<MPI_Comm>& comm_ptr)
  234. : communicator()
  235. {
  236. this->comm_ptr = comm_ptr;
  237. BOOST_ASSERT(has_cartesian_topology());
  238. }
  239. public:
  240. /**
  241. * Build a new Boost.MPI cartesian communicator based on the MPI
  242. * communicator @p comm with cartesian topology.
  243. *
  244. * @p comm may be any valid MPI communicator. If @p comm is
  245. * MPI_COMM_NULL, an empty communicator (that cannot be used for
  246. * communication) is created and the @p kind parameter is
  247. * ignored. Otherwise, the @p kind parameter determines how the
  248. * Boost.MPI communicator will be related to @p comm:
  249. *
  250. * - If @p kind is @c comm_duplicate, duplicate @c comm to create
  251. * a new communicator. This new communicator will be freed when
  252. * the Boost.MPI communicator (and all copies of it) is
  253. * destroyed. This option is only permitted if the underlying MPI
  254. * implementation supports MPI 2.0; duplication of
  255. * intercommunicators is not available in MPI 1.x.
  256. *
  257. * - If @p kind is @c comm_take_ownership, take ownership of @c
  258. * comm. It will be freed automatically when all of the Boost.MPI
  259. * communicators go out of scope.
  260. *
  261. * - If @p kind is @c comm_attach, this Boost.MPI communicator
  262. * will reference the existing MPI communicator @p comm but will
  263. * not free @p comm when the Boost.MPI communicator goes out of
  264. * scope. This option should only be used when the communicator is
  265. * managed by the user.
  266. */
  267. cartesian_communicator(const MPI_Comm& comm, comm_create_kind kind)
  268. : communicator(comm, kind)
  269. {
  270. BOOST_ASSERT(has_cartesian_topology());
  271. }
  272. /**
  273. * Create a new communicator whose topology is described by the
  274. * given cartesian. The indices of the vertices in the cartesian will be
  275. * assumed to be the ranks of the processes within the
  276. * communicator. There may be fewer vertices in the cartesian than
  277. * there are processes in the communicator; in this case, the
  278. * resulting communicator will be a NULL communicator.
  279. *
  280. * @param comm The communicator that the new, cartesian communicator
  281. * will be based on.
  282. *
  283. * @param dims the cartesian dimension of the new communicator. The size indicate
  284. * the number of dimension. Some dimensions be set to zero, in which case
  285. * the corresponding dimension value is left to the system.
  286. *
  287. * @param reorder Whether MPI is permitted to re-order the process
  288. * ranks within the returned communicator, to better optimize
  289. * communication. If false, the ranks of each process in the
  290. * returned process will match precisely the rank of that process
  291. * within the original communicator.
  292. */
  293. cartesian_communicator(const communicator& comm,
  294. const cartesian_topology& dims,
  295. bool reorder = false);
  296. /**
  297. * Create a new cartesian communicator whose topology is a subset of
  298. * an existing cartesian cimmunicator.
  299. * @param comm the original communicator.
  300. * @param keep and array containiing the dimension to keep from the existing
  301. * communicator.
  302. */
  303. cartesian_communicator(const cartesian_communicator& comm,
  304. const std::vector<int>& keep );
  305. using communicator::rank;
  306. /**
  307. * Retrive the number of dimension of the underlying toppology.
  308. */
  309. int ndims() const;
  310. /**
  311. * Return the rank of the process at the given coordinates.
  312. * @param coords the coordinates. the size must match the communicator's topology.
  313. */
  314. int rank(const std::vector<int>& coords) const;
  315. /**
  316. * Return the rank of the source and target destination process through a shift.
  317. * @param dim the dimension in which the shift takes place. 0 <= dim <= ndim().
  318. * @param disp the shift displacement, can be positive (upward) or negative (downward).
  319. */
  320. std::pair<int, int> shifted_ranks(int dim, int disp) const;
  321. /**
  322. * Provides the coordinates of the process with the given rank.
  323. * @param rk the ranks in this communicator.
  324. * @returns the coordinates.
  325. */
  326. std::vector<int> coordinates(int rk) const;
  327. /**
  328. * Retrieve the topology and coordinates of this process in the grid.
  329. *
  330. */
  331. void topology( cartesian_topology& dims, std::vector<int>& coords ) const;
  332. /**
  333. * Retrieve the topology of the grid.
  334. *
  335. */
  336. cartesian_topology topology() const;
  337. };
  338. /**
  339. * Given en number of processes, and a partially filled sequence
  340. * of dimension, try to complete the dimension sequence.
  341. * @param nb_proc the numer of mpi processes.fill a sequence of dimension.
  342. * @param dims a sequence of positive or null dimensions. Non zero dimension
  343. * will be left untouched.
  344. */
  345. std::vector<int>& cartesian_dimensions(int nb_proc, std::vector<int>& dims);
  346. } } // end namespace boost::mpi
  347. #endif // BOOST_MPI_CARTESIAN_COMMUNICATOR_HPP