st_connected.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright (C) 2006 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_ST_CONNECTED_HPP
  8. #define BOOST_GRAPH_DISTRIBUTED_ST_CONNECTED_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/graph/two_bit_color_map.hpp>
  14. #include <boost/graph/distributed/queue.hpp>
  15. #include <boost/pending/queue.hpp>
  16. #include <boost/graph/iteration_macros.hpp>
  17. #include <boost/graph/parallel/container_traits.hpp>
  18. #include <boost/property_map/property_map.hpp>
  19. #include <boost/property_map/parallel/parallel_property_maps.hpp>
  20. #include <boost/graph/parallel/algorithm.hpp>
  21. #include <utility>
  22. #include <boost/optional.hpp>
  23. namespace boost { namespace graph { namespace distributed {
  24. namespace detail {
  25. struct pair_and_or
  26. {
  27. std::pair<bool, bool>
  28. operator()(std::pair<bool, bool> x, std::pair<bool, bool> y) const
  29. {
  30. return std::pair<bool, bool>(x.first && y.first,
  31. x.second || y.second);
  32. }
  33. };
  34. } // end namespace detail
  35. template<typename DistributedGraph, typename ColorMap, typename OwnerMap>
  36. bool
  37. st_connected(const DistributedGraph& g,
  38. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  39. typename graph_traits<DistributedGraph>::vertex_descriptor t,
  40. ColorMap color, OwnerMap owner)
  41. {
  42. using boost::graph::parallel::process_group;
  43. using boost::graph::parallel::process_group_type;
  44. using boost::parallel::all_reduce;
  45. typedef typename property_traits<ColorMap>::value_type Color;
  46. typedef color_traits<Color> ColorTraits;
  47. typedef typename process_group_type<DistributedGraph>::type ProcessGroup;
  48. typedef typename ProcessGroup::process_id_type ProcessID;
  49. typedef typename graph_traits<DistributedGraph>::vertex_descriptor Vertex;
  50. // Set all vertices to white (unvisited)
  51. BGL_FORALL_VERTICES_T(v, g, DistributedGraph)
  52. put(color, v, ColorTraits::white());
  53. // "color" plays the role of a color map, with no synchronization.
  54. set_property_map_role(vertex_color, color);
  55. color.set_consistency_model(0);
  56. // Vertices found from the source are grey
  57. put(color, s, ColorTraits::gray());
  58. // Vertices found from the target are green
  59. put(color, t, ColorTraits::green());
  60. ProcessGroup pg = process_group(g);
  61. ProcessID rank = process_id(pg);
  62. // Build a local queue
  63. queue<Vertex> Q;
  64. if (get(owner, s) == rank) Q.push(s);
  65. if (get(owner, t) == rank) Q.push(t);
  66. queue<Vertex> other_Q;
  67. while (true) {
  68. bool found = false;
  69. // Process all vertices in the local queue
  70. while (!found && !Q.empty()) {
  71. Vertex u = Q.top(); Q.pop();
  72. Color u_color = get(color, u);
  73. BGL_FORALL_OUTEDGES_T(u, e, g, DistributedGraph) {
  74. Vertex v = target(e, g);
  75. Color v_color = get(color, v);
  76. if (v_color == ColorTraits::white()) {
  77. // We have not seen "v" before; mark it with the same color as u
  78. Color u_color = get(color, u);
  79. put(color, v, u_color);
  80. // Either push v into the local queue or send it off to its
  81. // owner.
  82. ProcessID v_owner = get(owner, v);
  83. if (v_owner == rank)
  84. other_Q.push(v);
  85. else
  86. send(pg, v_owner, 0,
  87. std::make_pair(v, u_color == ColorTraits::gray()));
  88. } else if (v_color != ColorTraits::black() && u_color != v_color) {
  89. // Colors have collided. We're done!
  90. found = true;
  91. break;
  92. }
  93. }
  94. // u is done, so mark it black
  95. put(color, u, ColorTraits::black());
  96. }
  97. // Ensure that all transmitted messages have been received.
  98. synchronize(pg);
  99. // Move all of the send-to-self values into the local Q.
  100. other_Q.swap(Q);
  101. if (!found) {
  102. // Receive all messages
  103. while (optional<std::pair<ProcessID, int> > msg = probe(pg)) {
  104. std::pair<Vertex, bool> data;
  105. receive(pg, msg->first, msg->second, data);
  106. // Determine the colors of u and v, the source and target
  107. // vertices (v is local).
  108. Vertex v = data.first;
  109. Color v_color = get(color, v);
  110. Color u_color = data.second? ColorTraits::gray() : ColorTraits::green();
  111. if (v_color == ColorTraits::white()) {
  112. // v had no color before, so give it u's color and push it
  113. // into the queue.
  114. Q.push(v);
  115. put(color, v, u_color);
  116. } else if (v_color != ColorTraits::black() && u_color != v_color) {
  117. // Colors have collided. We're done!
  118. found = true;
  119. break;
  120. }
  121. }
  122. }
  123. // Check if either all queues are empty or
  124. std::pair<bool, bool> results = all_reduce(pg,
  125. boost::parallel::detail::make_untracked_pair(Q.empty(), found),
  126. detail::pair_and_or());
  127. // If someone found the answer, we're done!
  128. if (results.second)
  129. return true;
  130. // If all queues are empty, we're done.
  131. if (results.first)
  132. return false;
  133. }
  134. }
  135. template<typename DistributedGraph, typename ColorMap>
  136. inline bool
  137. st_connected(const DistributedGraph& g,
  138. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  139. typename graph_traits<DistributedGraph>::vertex_descriptor t,
  140. ColorMap color)
  141. {
  142. return st_connected(g, s, t, color, get(vertex_owner, g));
  143. }
  144. template<typename DistributedGraph>
  145. inline bool
  146. st_connected(const DistributedGraph& g,
  147. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  148. typename graph_traits<DistributedGraph>::vertex_descriptor t)
  149. {
  150. return st_connected(g, s, t,
  151. make_two_bit_color_map(num_vertices(g),
  152. get(vertex_index, g)));
  153. }
  154. } } } // end namespace boost::graph::distributed
  155. #endif // BOOST_GRAPH_DISTRIBUTED_ST_CONNECTED_HPP