123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- #ifndef BOOST_GRAPH_DISTRIBUTED_ST_CONNECTED_HPP
- #define BOOST_GRAPH_DISTRIBUTED_ST_CONNECTED_HPP
- #ifndef BOOST_GRAPH_USE_MPI
- #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
- #endif
- #include <boost/graph/graph_traits.hpp>
- #include <boost/graph/two_bit_color_map.hpp>
- #include <boost/graph/distributed/queue.hpp>
- #include <boost/pending/queue.hpp>
- #include <boost/graph/iteration_macros.hpp>
- #include <boost/graph/parallel/container_traits.hpp>
- #include <boost/property_map/property_map.hpp>
- #include <boost/property_map/parallel/parallel_property_maps.hpp>
- #include <boost/graph/parallel/algorithm.hpp>
- #include <utility>
- #include <boost/optional.hpp>
- namespace boost { namespace graph { namespace distributed {
- namespace detail {
- struct pair_and_or
- {
- std::pair<bool, bool>
- operator()(std::pair<bool, bool> x, std::pair<bool, bool> y) const
- {
- return std::pair<bool, bool>(x.first && y.first,
- x.second || y.second);
- }
- };
- }
- template<typename DistributedGraph, typename ColorMap, typename OwnerMap>
- bool
- st_connected(const DistributedGraph& g,
- typename graph_traits<DistributedGraph>::vertex_descriptor s,
- typename graph_traits<DistributedGraph>::vertex_descriptor t,
- ColorMap color, OwnerMap owner)
- {
- using boost::graph::parallel::process_group;
- using boost::graph::parallel::process_group_type;
- using boost::parallel::all_reduce;
- typedef typename property_traits<ColorMap>::value_type Color;
- typedef color_traits<Color> ColorTraits;
- typedef typename process_group_type<DistributedGraph>::type ProcessGroup;
- typedef typename ProcessGroup::process_id_type ProcessID;
- typedef typename graph_traits<DistributedGraph>::vertex_descriptor Vertex;
-
- BGL_FORALL_VERTICES_T(v, g, DistributedGraph)
- put(color, v, ColorTraits::white());
-
- set_property_map_role(vertex_color, color);
- color.set_consistency_model(0);
-
- put(color, s, ColorTraits::gray());
-
- put(color, t, ColorTraits::green());
- ProcessGroup pg = process_group(g);
- ProcessID rank = process_id(pg);
-
- queue<Vertex> Q;
- if (get(owner, s) == rank) Q.push(s);
- if (get(owner, t) == rank) Q.push(t);
- queue<Vertex> other_Q;
- while (true) {
- bool found = false;
-
- while (!found && !Q.empty()) {
- Vertex u = Q.top(); Q.pop();
- Color u_color = get(color, u);
- BGL_FORALL_OUTEDGES_T(u, e, g, DistributedGraph) {
- Vertex v = target(e, g);
- Color v_color = get(color, v);
- if (v_color == ColorTraits::white()) {
-
- Color u_color = get(color, u);
- put(color, v, u_color);
-
-
- ProcessID v_owner = get(owner, v);
- if (v_owner == rank)
- other_Q.push(v);
- else
- send(pg, v_owner, 0,
- std::make_pair(v, u_color == ColorTraits::gray()));
- } else if (v_color != ColorTraits::black() && u_color != v_color) {
-
- found = true;
- break;
- }
- }
-
- put(color, u, ColorTraits::black());
- }
-
- synchronize(pg);
-
- other_Q.swap(Q);
- if (!found) {
-
- while (optional<std::pair<ProcessID, int> > msg = probe(pg)) {
- std::pair<Vertex, bool> data;
- receive(pg, msg->first, msg->second, data);
-
-
-
- Vertex v = data.first;
- Color v_color = get(color, v);
- Color u_color = data.second? ColorTraits::gray() : ColorTraits::green();
- if (v_color == ColorTraits::white()) {
-
-
- Q.push(v);
- put(color, v, u_color);
- } else if (v_color != ColorTraits::black() && u_color != v_color) {
-
- found = true;
- break;
- }
- }
- }
-
- std::pair<bool, bool> results = all_reduce(pg,
- boost::parallel::detail::make_untracked_pair(Q.empty(), found),
- detail::pair_and_or());
-
- if (results.second)
- return true;
-
- if (results.first)
- return false;
- }
- }
- template<typename DistributedGraph, typename ColorMap>
- inline bool
- st_connected(const DistributedGraph& g,
- typename graph_traits<DistributedGraph>::vertex_descriptor s,
- typename graph_traits<DistributedGraph>::vertex_descriptor t,
- ColorMap color)
- {
- return st_connected(g, s, t, color, get(vertex_owner, g));
- }
- template<typename DistributedGraph>
- inline bool
- st_connected(const DistributedGraph& g,
- typename graph_traits<DistributedGraph>::vertex_descriptor s,
- typename graph_traits<DistributedGraph>::vertex_descriptor t)
- {
- return st_connected(g, s, t,
- make_two_bit_color_map(num_vertices(g),
- get(vertex_index, g)));
- }
- } } }
- #endif
|