123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- #ifndef BOOST_MPI_CARTESIAN_COMMUNICATOR_HPP
- #define BOOST_MPI_CARTESIAN_COMMUNICATOR_HPP
- #include <boost/mpi/communicator.hpp>
- #include <vector>
- #include <utility>
- #include <iostream>
- #include <utility>
- #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
- #include <initializer_list>
- #endif
- #include <boost/shared_array.hpp>
- #include <boost/assert.hpp>
- #include <boost/foreach.hpp>
- namespace boost { namespace mpi {
- struct cartesian_dimension {
-
- int size;
-
- bool periodic;
-
- cartesian_dimension(int sz = 0, bool p = false) : size(sz), periodic(p) {}
-
- private:
- friend class boost::serialization::access;
- template<class Archive>
- void serialize(Archive & ar, const unsigned int version)
- {
- ar & size & periodic;
- }
- };
- template <>
- struct is_mpi_datatype<cartesian_dimension> : mpl::true_ { };
- inline
- bool
- operator==(cartesian_dimension const& d1, cartesian_dimension const& d2) {
- return &d1 == &d2 || (d1.size == d2.size && d1.periodic == d2.periodic);
- }
- inline
- bool
- operator!=(cartesian_dimension const& d1, cartesian_dimension const& d2) {
- return !(d1 == d2);
- }
- std::ostream& operator<<(std::ostream& out, cartesian_dimension const& d);
- class BOOST_MPI_DECL cartesian_topology
- : private std::vector<cartesian_dimension> {
- friend class cartesian_communicator;
- typedef std::vector<cartesian_dimension> super;
- public:
-
- using super::operator[];
-
- using super::size;
- using super::begin;
- using super::end;
- using super::swap;
- #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
- cartesian_topology() = delete;
- #endif
- #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
- cartesian_topology(cartesian_topology const&) = default;
- cartesian_topology& operator=(cartesian_topology const&) = default;
-
-
- #if !defined(BOOST_NO_CXX11_DEFAULTED_MOVES)
- cartesian_topology(cartesian_topology&& other) : super(other) {}
- cartesian_topology& operator=(cartesian_topology&& other) {
- stl().swap(other.stl());
- return *this;
- }
- #endif
- ~cartesian_topology() = default;
- #endif
-
- cartesian_topology(int ndim)
- : super(ndim) {}
-
-
- cartesian_topology(std::vector<cartesian_dimension> const& dims)
- : super(dims) {}
-
-
- template<class InitArr>
- explicit cartesian_topology(InitArr dims)
- : super(0) {
- BOOST_FOREACH(cartesian_dimension const& d, dims) {
- push_back(d);
- }
- }
- #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
-
-
- explicit cartesian_topology(std::initializer_list<cartesian_dimension> dims)
- : super(dims) {}
- #endif
-
-
- template<int NDIM>
- explicit cartesian_topology(cartesian_dimension (&dims)[NDIM])
- : super(dims, dims+NDIM) {}
-
-
- template<class DimRg, class PerRg>
- cartesian_topology(DimRg const& dim_rg, PerRg const& period_rg)
- : super(0) {
- BOOST_FOREACH(int d, dim_rg) {
- super::push_back(cartesian_dimension(d));
- }
- super::iterator it = begin();
- BOOST_FOREACH(bool p, period_rg) {
- if (it < end()) {
- it->periodic = p;
- } else {
- push_back(cartesian_dimension(0,p));
- }
- ++it;
- }
- }
-
-
- template<class DimIter, class PerIter>
- cartesian_topology(DimIter dit, PerIter pit, int n)
- : super(n) {
- for(int i = 0; i < n; ++i) {
- (*this)[i] = cartesian_dimension(*dit++, *pit++);
- }
- }
-
-
- std::vector<cartesian_dimension>& stl() { return *this; }
-
- std::vector<cartesian_dimension> const& stl() const{ return *this; }
-
- void split(std::vector<int>& dims, std::vector<bool>& periodics) const;
- };
- inline
- bool
- operator==(cartesian_topology const& t1, cartesian_topology const& t2) {
- return t1.stl() == t2.stl();
- }
- inline
- bool
- operator!=(cartesian_topology const& t1, cartesian_topology const& t2) {
- return t1.stl() != t2.stl();
- }
- std::ostream& operator<<(std::ostream& out, cartesian_topology const& t);
- class BOOST_MPI_DECL cartesian_communicator : public communicator
- {
- friend class communicator;
-
- explicit cartesian_communicator(const shared_ptr<MPI_Comm>& comm_ptr)
- : communicator()
- {
- this->comm_ptr = comm_ptr;
- BOOST_ASSERT(has_cartesian_topology());
- }
- public:
-
- cartesian_communicator(const MPI_Comm& comm, comm_create_kind kind)
- : communicator(comm, kind)
- {
- BOOST_ASSERT(has_cartesian_topology());
- }
-
- cartesian_communicator(const communicator& comm,
- const cartesian_topology& dims,
- bool reorder = false);
-
-
- cartesian_communicator(const cartesian_communicator& comm,
- const std::vector<int>& keep );
-
- using communicator::rank;
-
- int ndims() const;
-
-
- int rank(const std::vector<int>& coords) const;
-
- std::pair<int, int> shifted_ranks(int dim, int disp) const;
-
- std::vector<int> coordinates(int rk) const;
-
- void topology( cartesian_topology& dims, std::vector<int>& coords ) const;
-
- cartesian_topology topology() const;
- };
- std::vector<int>& cartesian_dimensions(int nb_proc, std::vector<int>& dims);
- } }
- #endif
|