breadth_first_search.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2004 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Douglas Gregor
  6. // Andrew Lumsdaine
  7. #ifndef BOOST_GRAPH_PARALLEL_BFS_HPP
  8. #define BOOST_GRAPH_PARALLEL_BFS_HPP
  9. #ifndef BOOST_GRAPH_USE_MPI
  10. #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
  11. #endif
  12. #include <boost/graph/breadth_first_search.hpp>
  13. #include <boost/graph/overloading.hpp>
  14. #include <boost/graph/distributed/concepts.hpp>
  15. #include <boost/graph/distributed/detail/filtered_queue.hpp>
  16. #include <boost/graph/distributed/queue.hpp>
  17. #include <boost/dynamic_bitset.hpp>
  18. #include <boost/pending/queue.hpp>
  19. #include <boost/graph/parallel/properties.hpp>
  20. #include <boost/graph/parallel/container_traits.hpp>
  21. namespace boost {
  22. namespace detail {
  23. /** @brief A unary predicate that decides when to push into a
  24. * breadth-first search queue.
  25. *
  26. * This predicate stores a color map that is used to determine
  27. * when to push. If it is provided with a key for which the color
  28. * is white, it darkens the color to gray and returns true (so
  29. * that the value will be pushed appropriately); if the color is
  30. * not white, it returns false so that the vertex will be
  31. * ignored.
  32. */
  33. template<typename ColorMap>
  34. struct darken_and_push
  35. {
  36. typedef typename property_traits<ColorMap>::key_type argument_type;
  37. typedef bool result_type;
  38. explicit darken_and_push(const ColorMap& color) : color(color) { }
  39. bool operator()(const argument_type& value) const
  40. {
  41. typedef color_traits<typename property_traits<ColorMap>::value_type>
  42. Color;
  43. if (get(color, value) == Color::white()) {
  44. put(color, value, Color::gray());
  45. return true;
  46. } else {
  47. return false;
  48. }
  49. }
  50. ColorMap color;
  51. };
  52. template<typename IndexMap>
  53. struct has_not_been_seen
  54. {
  55. typedef bool result_type;
  56. has_not_been_seen() { }
  57. has_not_been_seen(std::size_t n, IndexMap index_map)
  58. : seen(n), index_map(index_map) {}
  59. template<typename Key>
  60. result_type operator()(Key key)
  61. {
  62. bool result = seen[get(index_map, key)];
  63. seen[get(index_map, key)] = true;
  64. return !result;
  65. }
  66. void swap(has_not_been_seen& other)
  67. {
  68. using std::swap;
  69. swap(seen, other.seen);
  70. swap(index_map, other.index_map);
  71. }
  72. private:
  73. dynamic_bitset<> seen;
  74. IndexMap index_map;
  75. };
  76. template<typename IndexMap>
  77. inline void
  78. swap(has_not_been_seen<IndexMap>& x, has_not_been_seen<IndexMap>& y)
  79. {
  80. x.swap(y);
  81. }
  82. template <class DistributedGraph, class ColorMap, class BFSVisitor,
  83. class BufferRef, class VertexIndexMap>
  84. inline void
  85. parallel_bfs_helper
  86. (DistributedGraph& g,
  87. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  88. ColorMap color,
  89. BFSVisitor vis,
  90. BufferRef Q,
  91. VertexIndexMap)
  92. {
  93. set_property_map_role(vertex_color, color);
  94. color.set_consistency_model(0);
  95. breadth_first_search(g, s, Q.ref, vis, color);
  96. }
  97. template <class DistributedGraph, class ColorMap, class BFSVisitor,
  98. class VertexIndexMap>
  99. void parallel_bfs_helper
  100. (DistributedGraph& g,
  101. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  102. ColorMap color,
  103. BFSVisitor vis,
  104. boost::param_not_found,
  105. VertexIndexMap vertex_index)
  106. {
  107. using boost::graph::parallel::process_group;
  108. typedef graph_traits<DistributedGraph> Traits;
  109. typedef typename Traits::vertex_descriptor Vertex;
  110. typedef typename boost::graph::parallel::process_group_type<DistributedGraph>::type
  111. process_group_type;
  112. set_property_map_role(vertex_color, color);
  113. color.set_consistency_model(0);
  114. // Buffer default
  115. typedef typename property_map<DistributedGraph, vertex_owner_t>
  116. ::const_type vertex_owner_map;
  117. typedef boost::graph::distributed::distributed_queue<
  118. process_group_type, vertex_owner_map, queue<Vertex>,
  119. detail::darken_and_push<ColorMap> > queue_t;
  120. queue_t Q(process_group(g),
  121. get(vertex_owner, g),
  122. detail::darken_and_push<ColorMap>(color));
  123. breadth_first_search(g, s, Q, vis, color);
  124. }
  125. template <class DistributedGraph, class ColorMap, class BFSVisitor,
  126. class P, class T, class R>
  127. void bfs_helper
  128. (DistributedGraph& g,
  129. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  130. ColorMap color,
  131. BFSVisitor vis,
  132. const bgl_named_params<P, T, R>& params,
  133. boost::mpl::true_)
  134. {
  135. parallel_bfs_helper
  136. (g, s, color, vis, get_param(params, buffer_param_t()),
  137. choose_const_pmap(get_param(params, vertex_index), g, vertex_index));
  138. }
  139. }
  140. }
  141. #endif // BOOST_GRAPH_PARALLEL_BFS_HPP