depth_first_search.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Copyright (C) 2004-2008 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_DISTRIBUTED_DFS_HPP
  8. #define BOOST_GRAPH_DISTRIBUTED_DFS_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/graph_traits.hpp>
  13. #include <boost/property_map/property_map.hpp>
  14. #include <boost/graph/overloading.hpp>
  15. #include <boost/graph/properties.hpp>
  16. #include <boost/graph/distributed/concepts.hpp>
  17. #include <boost/static_assert.hpp>
  18. #include <boost/assert.hpp>
  19. #include <boost/graph/parallel/process_group.hpp>
  20. #include <boost/graph/parallel/container_traits.hpp>
  21. namespace boost {
  22. namespace graph { namespace distributed { namespace detail {
  23. template<typename DistributedGraph, typename ColorMap, typename ParentMap,
  24. typename ExploreMap, typename VertexIndexMap, typename DFSVisitor>
  25. class parallel_dfs
  26. {
  27. typedef typename graph_traits<DistributedGraph>::vertex_iterator
  28. vertex_iterator;
  29. typedef typename graph_traits<DistributedGraph>::vertex_descriptor
  30. vertex_descriptor;
  31. typedef typename graph_traits<DistributedGraph>::out_edge_iterator
  32. out_edge_iterator;
  33. typedef typename boost::graph::parallel::process_group_type<DistributedGraph>
  34. ::type process_group_type;
  35. typedef typename process_group_type::process_id_type process_id_type;
  36. /**
  37. * The first vertex in the pair is the local node (i) and the
  38. * second vertex in the pair is the (possibly remote) node (j).
  39. */
  40. typedef boost::parallel::detail::untracked_pair<vertex_descriptor, vertex_descriptor> vertex_pair;
  41. typedef typename property_traits<ColorMap>::value_type color_type;
  42. typedef color_traits<color_type> Color;
  43. // Message types
  44. enum { discover_msg = 10, return_msg = 50, visited_msg = 100 , done_msg = 150};
  45. public:
  46. parallel_dfs(const DistributedGraph& g, ColorMap color,
  47. ParentMap parent, ExploreMap explore,
  48. VertexIndexMap index_map, DFSVisitor vis)
  49. : g(g), color(color), parent(parent), explore(explore),
  50. index_map(index_map), vis(vis), pg(process_group(g)),
  51. owner(get(vertex_owner, g)), next_out_edge(num_vertices(g))
  52. { }
  53. void run(vertex_descriptor s)
  54. {
  55. vertex_iterator vi, vi_end;
  56. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
  57. put(color, *vi, Color::white());
  58. put(parent, *vi, *vi);
  59. put(explore, *vi, *vi);
  60. next_out_edge[get(index_map, *vi)] = out_edges(*vi, g).first;
  61. vis.initialize_vertex(*vi, g);
  62. }
  63. vis.start_vertex(s, g);
  64. if (get(owner, s) == process_id(pg)) {
  65. send_oob(pg, get(owner, s), discover_msg, vertex_pair(s, s));
  66. }
  67. bool done = false;
  68. while (!done) {
  69. std::pair<process_id_type, int> msg = *pg.poll(true);
  70. switch (msg.second) {
  71. case discover_msg:
  72. {
  73. vertex_pair p;
  74. receive_oob(pg, msg.first, msg.second, p);
  75. if (p.first != p.second) {
  76. // delete j from nomessage(j)
  77. if (get(color, p.second) != Color::black())
  78. local_put(color, p.second, Color::gray());
  79. if (recover(p)) break;
  80. }
  81. if (get(color, p.first) == Color::white()) {
  82. put(color, p.first, Color::gray());
  83. put(parent, p.first, p.second);
  84. vis.discover_vertex(p.first, g);
  85. if (shift_center_of_activity(p.first)) break;
  86. out_edge_iterator ei, ei_end;
  87. for (boost::tie(ei,ei_end) = out_edges(p.first, g); ei != ei_end; ++ei)
  88. {
  89. // Notify everyone who may not know that the source
  90. // vertex has been visited. They can then mark the
  91. // corresponding color map entry gray.
  92. if (get(parent, p.first) != target(*ei, g)
  93. && get(explore, p.first) != target(*ei, g)) {
  94. vertex_pair visit(target(*ei, g), p.first);
  95. send_oob(pg, get(owner, target(*ei, g)), visited_msg, visit);
  96. }
  97. }
  98. }
  99. }
  100. break;
  101. case visited_msg:
  102. {
  103. vertex_pair p;
  104. receive_oob(pg, msg.first, msg.second, p);
  105. // delete j from nomessage(j)
  106. if (get(color, p.second) != Color::black())
  107. local_put(color, p.second, Color::gray());
  108. recover(p);
  109. }
  110. break;
  111. case return_msg:
  112. {
  113. vertex_pair p;
  114. receive_oob(pg, msg.first, msg.second, p);
  115. // delete j from nomessage(i)
  116. local_put(color, p.second, Color::black());
  117. shift_center_of_activity(p.first);
  118. }
  119. break;
  120. case done_msg:
  121. {
  122. receive_oob(pg, msg.first, msg.second, done);
  123. // Propagate done message downward in tree
  124. done = true;
  125. process_id_type id = process_id(pg);
  126. process_id_type left = 2*id + 1;
  127. process_id_type right = left + 1;
  128. if (left < num_processes(pg))
  129. send_oob(pg, left, done_msg, done);
  130. if (right < num_processes(pg))
  131. send_oob(pg, right, done_msg, done);
  132. }
  133. break;
  134. default:
  135. BOOST_ASSERT(false);
  136. }
  137. }
  138. }
  139. private:
  140. bool recover(const vertex_pair& p)
  141. {
  142. if (get(explore, p.first) == p.second) {
  143. return shift_center_of_activity(p.first);
  144. }
  145. else
  146. return false;
  147. }
  148. bool shift_center_of_activity(vertex_descriptor i)
  149. {
  150. for (out_edge_iterator ei = next_out_edge[get(index_map, i)],
  151. ei_end = out_edges(i, g).second;
  152. ei != ei_end; ++ei) {
  153. vis.examine_edge(*ei, g);
  154. vertex_descriptor k = target(*ei, g);
  155. color_type target_color = get(color, k);
  156. if (target_color == Color::black()) vis.forward_or_cross_edge(*ei, g);
  157. else if (target_color == Color::gray()) vis.back_edge(*ei, g);
  158. else {
  159. put(explore, i, k);
  160. vis.tree_edge(*ei, g);
  161. vertex_pair p(k, i);
  162. send_oob(pg, get(owner, k), discover_msg, p);
  163. next_out_edge[get(index_map, i)] = ++ei;
  164. return false;
  165. }
  166. }
  167. next_out_edge[get(index_map, i)] = out_edges(i, g).second;
  168. put(explore, i, i);
  169. put(color, i, Color::black());
  170. vis.finish_vertex(i, g);
  171. if (get(parent, i) == i) {
  172. send_oob(pg, 0, done_msg, true);
  173. return true;
  174. }
  175. else {
  176. vertex_pair ret(get(parent, i), i);
  177. send_oob(pg, get(owner, ret.first), return_msg, ret);
  178. }
  179. return false;
  180. }
  181. const DistributedGraph& g;
  182. ColorMap color;
  183. ParentMap parent;
  184. ExploreMap explore;
  185. VertexIndexMap index_map;
  186. DFSVisitor vis;
  187. process_group_type pg;
  188. typename property_map<DistributedGraph, vertex_owner_t>::const_type owner;
  189. std::vector<out_edge_iterator> next_out_edge;
  190. };
  191. } // end namespace detail
  192. template<typename DistributedGraph, typename ColorMap, typename ParentMap,
  193. typename ExploreMap, typename VertexIndexMap, typename DFSVisitor>
  194. void
  195. tsin_depth_first_visit
  196. (const DistributedGraph& g,
  197. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  198. DFSVisitor vis, ColorMap color, ParentMap parent, ExploreMap explore,
  199. VertexIndexMap index_map)
  200. {
  201. typedef typename graph_traits<DistributedGraph>::directed_category
  202. directed_category;
  203. BOOST_STATIC_ASSERT(
  204. (is_convertible<directed_category, undirected_tag>::value));
  205. set_property_map_role(vertex_color, color);
  206. graph::distributed::detail::parallel_dfs
  207. <DistributedGraph, ColorMap, ParentMap, ExploreMap, VertexIndexMap,
  208. DFSVisitor> do_dfs(g, color, parent, explore, index_map, vis);
  209. do_dfs.run(s);
  210. using boost::graph::parallel::process_group;
  211. synchronize(process_group(g));
  212. }
  213. template<typename DistributedGraph, typename DFSVisitor,
  214. typename VertexIndexMap>
  215. void
  216. tsin_depth_first_visit
  217. (const DistributedGraph& g,
  218. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  219. DFSVisitor vis,
  220. VertexIndexMap index_map)
  221. {
  222. typedef typename graph_traits<DistributedGraph>::vertex_descriptor
  223. vertex_descriptor;
  224. std::vector<default_color_type> colors(num_vertices(g));
  225. std::vector<vertex_descriptor> parent(num_vertices(g));
  226. std::vector<vertex_descriptor> explore(num_vertices(g));
  227. tsin_depth_first_visit
  228. (g, s,
  229. vis,
  230. make_iterator_property_map(colors.begin(), index_map),
  231. make_iterator_property_map(parent.begin(), index_map),
  232. make_iterator_property_map(explore.begin(), index_map),
  233. index_map);
  234. }
  235. template<typename DistributedGraph, typename DFSVisitor,
  236. typename VertexIndexMap>
  237. void
  238. tsin_depth_first_visit
  239. (const DistributedGraph& g,
  240. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  241. DFSVisitor vis)
  242. {
  243. tsin_depth_first_visit(g, s, vis, get(vertex_index, g));
  244. }
  245. } // end namespace distributed
  246. using distributed::tsin_depth_first_visit;
  247. } // end namespace graph
  248. template<typename DistributedGraph, typename DFSVisitor>
  249. void
  250. depth_first_visit
  251. (const DistributedGraph& g,
  252. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  253. DFSVisitor vis)
  254. {
  255. graph::tsin_depth_first_visit(g, s, vis, get(vertex_index, g));
  256. }
  257. } // end namespace boost
  258. #endif // BOOST_GRAPH_DISTRIBUTED_DFS_HPP