File indexing completed on 2024-11-15 09:12:55
0001
0002
0003
0004
0005 #ifndef BOOST_SHUFFLED_DISTRIBUTION_070923_HPP
0006 #define BOOST_SHUFFLED_DISTRIBUTION_070923_HPP
0007
0008 #ifndef BOOST_GRAPH_USE_MPI
0009 #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
0010 #endif
0011
0012 # include <boost/assert.hpp>
0013 # include <boost/iterator/counting_iterator.hpp>
0014 # include <vector>
0015
0016 namespace boost { namespace graph { namespace distributed {
0017
0018 template <class BaseDistribution>
0019 struct shuffled_distribution : BaseDistribution
0020 {
0021 typedef std::size_t size_type;
0022
0023 template <class ProcessGroup>
0024 shuffled_distribution(ProcessGroup const& pg, BaseDistribution const& base)
0025 : BaseDistribution(base)
0026 , n(num_processes(pg))
0027 , mapping_(make_counting_iterator(size_type(0)), make_counting_iterator(n))
0028 , reverse_mapping(mapping_)
0029 {}
0030
0031 std::vector<size_type> const& mapping() const
0032 {
0033 return mapping_;
0034 }
0035
0036 template <class InputIterator>
0037 void assign_mapping(InputIterator first, InputIterator last)
0038 {
0039 mapping_.assign(first, last);
0040 BOOST_ASSERT(mapping_.size() == n);
0041 reverse_mapping.resize(mapping_.size());
0042
0043 for (std::vector<size_t>::iterator i(mapping_.begin());
0044 i != mapping_.end(); ++i)
0045 {
0046 reverse_mapping[*i] = i - mapping_.begin();
0047 }
0048 }
0049
0050 BaseDistribution& base()
0051 {
0052 return *this;
0053 }
0054
0055 BaseDistribution const& base() const
0056 {
0057 return *this;
0058 }
0059
0060 template <class ProcessID>
0061 size_type block_size(ProcessID id, size_type n) const
0062 {
0063 return base().block_size(reverse_mapping[id], n);
0064 }
0065
0066 template <class T>
0067 size_type operator()(T const& value) const
0068 {
0069 return mapping_[base()(value)];
0070 }
0071
0072 template <class ProcessID>
0073 size_type start(ProcessID id) const
0074 {
0075 return base().start(reverse_mapping[id]);
0076 }
0077
0078 size_type local(size_type i) const
0079 {
0080 return base().local(i);
0081 }
0082
0083 size_type global(size_type i) const
0084 {
0085 return base().global(i);
0086 }
0087
0088 template <class ProcessID>
0089 size_type global(ProcessID id, size_type n) const
0090 {
0091 return base().global(reverse_mapping[id], n);
0092 }
0093
0094 template <class Archive>
0095 void serialize(Archive& ar, unsigned long )
0096 {
0097 ar & serialization::make_nvp("base", base());
0098 }
0099
0100 void clear()
0101 {
0102 base().clear();
0103 }
0104
0105 private:
0106 size_type n;
0107 std::vector<size_type> mapping_;
0108 std::vector<size_type> reverse_mapping;
0109 };
0110
0111 }}}
0112
0113 #endif
0114