shuffled_distribution.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifndef BOOST_SHUFFLED_DISTRIBUTION_070923_HPP
  5. #define BOOST_SHUFFLED_DISTRIBUTION_070923_HPP
  6. #ifndef BOOST_GRAPH_USE_MPI
  7. #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
  8. #endif
  9. # include <boost/assert.hpp>
  10. # include <boost/iterator/counting_iterator.hpp>
  11. # include <vector>
  12. namespace boost { namespace graph { namespace distributed {
  13. template <class BaseDistribution>
  14. struct shuffled_distribution : BaseDistribution
  15. {
  16. typedef std::size_t size_type;
  17. template <class ProcessGroup>
  18. shuffled_distribution(ProcessGroup const& pg, BaseDistribution const& base)
  19. : BaseDistribution(base)
  20. , n(num_processes(pg))
  21. , mapping_(make_counting_iterator(size_type(0)), make_counting_iterator(n))
  22. , reverse_mapping(mapping_)
  23. {}
  24. std::vector<size_type> const& mapping() const
  25. {
  26. return mapping_;
  27. }
  28. template <class InputIterator>
  29. void assign_mapping(InputIterator first, InputIterator last)
  30. {
  31. mapping_.assign(first, last);
  32. BOOST_ASSERT(mapping_.size() == n);
  33. reverse_mapping.resize(mapping_.size());
  34. for (std::vector<size_t>::iterator i(mapping_.begin());
  35. i != mapping_.end(); ++i)
  36. {
  37. reverse_mapping[*i] = i - mapping_.begin();
  38. }
  39. }
  40. BaseDistribution& base()
  41. {
  42. return *this;
  43. }
  44. BaseDistribution const& base() const
  45. {
  46. return *this;
  47. }
  48. template <class ProcessID>
  49. size_type block_size(ProcessID id, size_type n) const
  50. {
  51. return base().block_size(reverse_mapping[id], n);
  52. }
  53. template <class T>
  54. size_type operator()(T const& value) const
  55. {
  56. return mapping_[base()(value)];
  57. }
  58. template <class ProcessID>
  59. size_type start(ProcessID id) const
  60. {
  61. return base().start(reverse_mapping[id]);
  62. }
  63. size_type local(size_type i) const
  64. {
  65. return base().local(i);
  66. }
  67. size_type global(size_type i) const
  68. {
  69. return base().global(i);
  70. }
  71. template <class ProcessID>
  72. size_type global(ProcessID id, size_type n) const
  73. {
  74. return base().global(reverse_mapping[id], n);
  75. }
  76. template <class Archive>
  77. void serialize(Archive& ar, unsigned long /*version*/)
  78. {
  79. ar & serialization::make_nvp("base", base());
  80. }
  81. void clear()
  82. {
  83. base().clear();
  84. }
  85. private:
  86. size_type n;
  87. std::vector<size_type> mapping_;
  88. std::vector<size_type> reverse_mapping;
  89. };
  90. }}} // namespace boost::graph::distributed
  91. #endif // BOOST_SHUFFLED_DISTRIBUTION_070923_HPP