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) 4 5 #ifndef BOOST_SHUFFLED_DISTRIBUTION_070923_HPP 6 #define BOOST_SHUFFLED_DISTRIBUTION_070923_HPP 7 8 #ifndef BOOST_GRAPH_USE_MPI 9 #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included" 10 #endif 11 12 # include <boost/assert.hpp> 13 # include <boost/iterator/counting_iterator.hpp> 14 # include <vector> 15 16 namespace boost { namespace graph { namespace distributed { 17 18 template <class BaseDistribution> 19 struct shuffled_distribution : BaseDistribution 20 { 21 typedef std::size_t size_type; 22 23 template <class ProcessGroup> shuffled_distributionboost::graph::distributed::shuffled_distribution24 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_) 29 {} 30 mappingboost::graph::distributed::shuffled_distribution31 std::vector<size_type> const& mapping() const 32 { 33 return mapping_; 34 } 35 36 template <class InputIterator> assign_mappingboost::graph::distributed::shuffled_distribution37 void assign_mapping(InputIterator first, InputIterator last) 38 { 39 mapping_.assign(first, last); 40 BOOST_ASSERT(mapping_.size() == n); 41 reverse_mapping.resize(mapping_.size()); 42 43 for (std::vector<size_t>::iterator i(mapping_.begin()); 44 i != mapping_.end(); ++i) 45 { 46 reverse_mapping[*i] = i - mapping_.begin(); 47 } 48 } 49 baseboost::graph::distributed::shuffled_distribution50 BaseDistribution& base() 51 { 52 return *this; 53 } 54 baseboost::graph::distributed::shuffled_distribution55 BaseDistribution const& base() const 56 { 57 return *this; 58 } 59 60 template <class ProcessID> block_sizeboost::graph::distributed::shuffled_distribution61 size_type block_size(ProcessID id, size_type n) const 62 { 63 return base().block_size(reverse_mapping[id], n); 64 } 65 66 template <class T> operator ()boost::graph::distributed::shuffled_distribution67 size_type operator()(T const& value) const 68 { 69 return mapping_[base()(value)]; 70 } 71 72 template <class ProcessID> startboost::graph::distributed::shuffled_distribution73 size_type start(ProcessID id) const 74 { 75 return base().start(reverse_mapping[id]); 76 } 77 localboost::graph::distributed::shuffled_distribution78 size_type local(size_type i) const 79 { 80 return base().local(i); 81 } 82 globalboost::graph::distributed::shuffled_distribution83 size_type global(size_type i) const 84 { 85 return base().global(i); 86 } 87 88 template <class ProcessID> globalboost::graph::distributed::shuffled_distribution89 size_type global(ProcessID id, size_type n) const 90 { 91 return base().global(reverse_mapping[id], n); 92 } 93 94 template <class Archive> serializeboost::graph::distributed::shuffled_distribution95 void serialize(Archive& ar, unsigned long /*version*/) 96 { 97 ar & serialization::make_nvp("base", base()); 98 } 99 clearboost::graph::distributed::shuffled_distribution100 void clear() 101 { 102 base().clear(); 103 } 104 105 private: 106 size_type n; 107 std::vector<size_type> mapping_; 108 std::vector<size_type> reverse_mapping; 109 }; 110 111 }}} // namespace boost::graph::distributed 112 113 #endif // BOOST_SHUFFLED_DISTRIBUTION_070923_HPP 114 115