st_connected.hpp 6.0 KB

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