1 // Copyright Daniel Wallin 2007. Use, modification and distribution is
2 // subject to the Boost Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #ifndef BOOST_SHUFFLED_DISTRIBUTION_070923_HPP
6 #define BOOST_SHUFFLED_DISTRIBUTION_070923_HPP
8 #ifndef BOOST_GRAPH_USE_MPI
9 #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
12 # include <boost/assert.hpp>
13 # include <boost/iterator/counting_iterator.hpp>
16 namespace boost { namespace graph { namespace distributed {
18 template <class BaseDistribution>
19 struct shuffled_distribution : BaseDistribution
21 typedef std::size_t size_type;
23 template <class ProcessGroup>
24 shuffled_distribution(ProcessGroup const& pg, BaseDistribution const& base)
25 : BaseDistribution(base)
26 , n(num_processes(pg))
27 , mapping_(make_counting_iterator(size_type(0)), make_counting_iterator(n))
28 , reverse_mapping(mapping_)
31 std::vector<size_type> const& mapping() const
36 template <class InputIterator>
37 void assign_mapping(InputIterator first, InputIterator last)
39 mapping_.assign(first, last);
40 BOOST_ASSERT(mapping_.size() == n);
41 reverse_mapping.resize(mapping_.size());
43 for (std::vector<size_t>::iterator i(mapping_.begin());
44 i != mapping_.end(); ++i)
46 reverse_mapping[*i] = i - mapping_.begin();
50 BaseDistribution& base()
55 BaseDistribution const& base() const
60 template <class ProcessID>
61 size_type block_size(ProcessID id, size_type n) const
63 return base().block_size(reverse_mapping[id], n);
67 size_type operator()(T const& value) const
69 return mapping_[base()(value)];
72 template <class ProcessID>
73 size_type start(ProcessID id) const
75 return base().start(reverse_mapping[id]);
78 size_type local(size_type i) const
80 return base().local(i);
83 size_type global(size_type i) const
85 return base().global(i);
88 template <class ProcessID>
89 size_type global(ProcessID id, size_type n) const
91 return base().global(reverse_mapping[id], n);
94 template <class Archive>
95 void serialize(Archive& ar, unsigned long /*version*/)
97 ar & serialization::make_nvp("base", base());
107 std::vector<size_type> mapping_;
108 std::vector<size_type> reverse_mapping;
111 }}} // namespace boost::graph::distributed
113 #endif // BOOST_SHUFFLED_DISTRIBUTION_070923_HPP