Chris@16
|
1 // Copyright (C) 2006 The Trustees of Indiana University.
|
Chris@16
|
2
|
Chris@16
|
3 // Use, modification and distribution is subject to the Boost Software
|
Chris@16
|
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
5 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6
|
Chris@16
|
7 // Authors: Douglas Gregor
|
Chris@16
|
8 // Andrew Lumsdaine
|
Chris@16
|
9 #ifndef BOOST_GRAPH_DISTRIBUTED_ST_CONNECTED_HPP
|
Chris@16
|
10 #define BOOST_GRAPH_DISTRIBUTED_ST_CONNECTED_HPP
|
Chris@16
|
11
|
Chris@16
|
12 #ifndef BOOST_GRAPH_USE_MPI
|
Chris@16
|
13 #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
|
Chris@16
|
14 #endif
|
Chris@16
|
15
|
Chris@16
|
16 #include <boost/graph/graph_traits.hpp>
|
Chris@16
|
17 #include <boost/graph/two_bit_color_map.hpp>
|
Chris@16
|
18 #include <boost/graph/distributed/queue.hpp>
|
Chris@16
|
19 #include <boost/pending/queue.hpp>
|
Chris@16
|
20 #include <boost/graph/iteration_macros.hpp>
|
Chris@16
|
21 #include <boost/graph/parallel/container_traits.hpp>
|
Chris@16
|
22 #include <boost/property_map/property_map.hpp>
|
Chris@16
|
23 #include <boost/graph/parallel/algorithm.hpp>
|
Chris@16
|
24 #include <utility>
|
Chris@16
|
25 #include <boost/optional.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 namespace boost { namespace graph { namespace distributed {
|
Chris@16
|
28
|
Chris@16
|
29 namespace detail {
|
Chris@16
|
30 struct pair_and_or
|
Chris@16
|
31 {
|
Chris@16
|
32 std::pair<bool, bool>
|
Chris@16
|
33 operator()(std::pair<bool, bool> x, std::pair<bool, bool> y) const
|
Chris@16
|
34 {
|
Chris@16
|
35 return std::pair<bool, bool>(x.first && y.first,
|
Chris@16
|
36 x.second || y.second);
|
Chris@16
|
37 }
|
Chris@16
|
38 };
|
Chris@16
|
39
|
Chris@16
|
40 } // end namespace detail
|
Chris@16
|
41
|
Chris@16
|
42 template<typename DistributedGraph, typename ColorMap, typename OwnerMap>
|
Chris@16
|
43 bool
|
Chris@16
|
44 st_connected(const DistributedGraph& g,
|
Chris@16
|
45 typename graph_traits<DistributedGraph>::vertex_descriptor s,
|
Chris@16
|
46 typename graph_traits<DistributedGraph>::vertex_descriptor t,
|
Chris@16
|
47 ColorMap color, OwnerMap owner)
|
Chris@16
|
48 {
|
Chris@16
|
49 using boost::graph::parallel::process_group;
|
Chris@16
|
50 using boost::graph::parallel::process_group_type;
|
Chris@16
|
51 using boost::parallel::all_reduce;
|
Chris@16
|
52
|
Chris@16
|
53 typedef typename property_traits<ColorMap>::value_type Color;
|
Chris@16
|
54 typedef color_traits<Color> ColorTraits;
|
Chris@16
|
55 typedef typename process_group_type<DistributedGraph>::type ProcessGroup;
|
Chris@16
|
56 typedef typename ProcessGroup::process_id_type ProcessID;
|
Chris@16
|
57 typedef typename graph_traits<DistributedGraph>::vertex_descriptor Vertex;
|
Chris@16
|
58
|
Chris@16
|
59 // Set all vertices to white (unvisited)
|
Chris@16
|
60 BGL_FORALL_VERTICES_T(v, g, DistributedGraph)
|
Chris@16
|
61 put(color, v, ColorTraits::white());
|
Chris@16
|
62
|
Chris@16
|
63 // "color" plays the role of a color map, with no synchronization.
|
Chris@16
|
64 set_property_map_role(vertex_color, color);
|
Chris@16
|
65 color.set_consistency_model(0);
|
Chris@16
|
66
|
Chris@16
|
67 // Vertices found from the source are grey
|
Chris@16
|
68 put(color, s, ColorTraits::gray());
|
Chris@16
|
69
|
Chris@16
|
70 // Vertices found from the target are green
|
Chris@16
|
71 put(color, t, ColorTraits::green());
|
Chris@16
|
72
|
Chris@16
|
73 ProcessGroup pg = process_group(g);
|
Chris@16
|
74 ProcessID rank = process_id(pg);
|
Chris@16
|
75
|
Chris@16
|
76 // Build a local queue
|
Chris@16
|
77 queue<Vertex> Q;
|
Chris@16
|
78 if (get(owner, s) == rank) Q.push(s);
|
Chris@16
|
79 if (get(owner, t) == rank) Q.push(t);
|
Chris@16
|
80
|
Chris@16
|
81 queue<Vertex> other_Q;
|
Chris@16
|
82
|
Chris@16
|
83 while (true) {
|
Chris@16
|
84 bool found = false;
|
Chris@16
|
85
|
Chris@16
|
86 // Process all vertices in the local queue
|
Chris@16
|
87 while (!found && !Q.empty()) {
|
Chris@16
|
88 Vertex u = Q.top(); Q.pop();
|
Chris@16
|
89 Color u_color = get(color, u);
|
Chris@16
|
90
|
Chris@16
|
91 BGL_FORALL_OUTEDGES_T(u, e, g, DistributedGraph) {
|
Chris@16
|
92 Vertex v = target(e, g);
|
Chris@16
|
93 Color v_color = get(color, v);
|
Chris@16
|
94 if (v_color == ColorTraits::white()) {
|
Chris@16
|
95 // We have not seen "v" before; mark it with the same color as u
|
Chris@16
|
96 Color u_color = get(color, u);
|
Chris@16
|
97 put(color, v, u_color);
|
Chris@16
|
98
|
Chris@16
|
99 // Either push v into the local queue or send it off to its
|
Chris@16
|
100 // owner.
|
Chris@16
|
101 ProcessID v_owner = get(owner, v);
|
Chris@16
|
102 if (v_owner == rank)
|
Chris@16
|
103 other_Q.push(v);
|
Chris@16
|
104 else
|
Chris@16
|
105 send(pg, v_owner, 0,
|
Chris@16
|
106 std::make_pair(v, u_color == ColorTraits::gray()));
|
Chris@16
|
107 } else if (v_color != ColorTraits::black() && u_color != v_color) {
|
Chris@16
|
108 // Colors have collided. We're done!
|
Chris@16
|
109 found = true;
|
Chris@16
|
110 break;
|
Chris@16
|
111 }
|
Chris@16
|
112 }
|
Chris@16
|
113
|
Chris@16
|
114 // u is done, so mark it black
|
Chris@16
|
115 put(color, u, ColorTraits::black());
|
Chris@16
|
116 }
|
Chris@16
|
117
|
Chris@16
|
118 // Ensure that all transmitted messages have been received.
|
Chris@16
|
119 synchronize(pg);
|
Chris@16
|
120
|
Chris@16
|
121 // Move all of the send-to-self values into the local Q.
|
Chris@16
|
122 other_Q.swap(Q);
|
Chris@16
|
123
|
Chris@16
|
124 if (!found) {
|
Chris@16
|
125 // Receive all messages
|
Chris@16
|
126 while (optional<std::pair<ProcessID, int> > msg = probe(pg)) {
|
Chris@16
|
127 std::pair<Vertex, bool> data;
|
Chris@16
|
128 receive(pg, msg->first, msg->second, data);
|
Chris@16
|
129
|
Chris@16
|
130 // Determine the colors of u and v, the source and target
|
Chris@16
|
131 // vertices (v is local).
|
Chris@16
|
132 Vertex v = data.first;
|
Chris@16
|
133 Color v_color = get(color, v);
|
Chris@16
|
134 Color u_color = data.second? ColorTraits::gray() : ColorTraits::green();
|
Chris@16
|
135 if (v_color == ColorTraits::white()) {
|
Chris@16
|
136 // v had no color before, so give it u's color and push it
|
Chris@16
|
137 // into the queue.
|
Chris@16
|
138 Q.push(v);
|
Chris@16
|
139 put(color, v, u_color);
|
Chris@16
|
140 } else if (v_color != ColorTraits::black() && u_color != v_color) {
|
Chris@16
|
141 // Colors have collided. We're done!
|
Chris@16
|
142 found = true;
|
Chris@16
|
143 break;
|
Chris@16
|
144 }
|
Chris@16
|
145 }
|
Chris@16
|
146 }
|
Chris@16
|
147
|
Chris@16
|
148 // Check if either all queues are empty or
|
Chris@16
|
149 std::pair<bool, bool> results = all_reduce(pg,
|
Chris@16
|
150 boost::parallel::detail::make_untracked_pair(Q.empty(), found),
|
Chris@16
|
151 detail::pair_and_or());
|
Chris@16
|
152
|
Chris@16
|
153 // If someone found the answer, we're done!
|
Chris@16
|
154 if (results.second)
|
Chris@16
|
155 return true;
|
Chris@16
|
156
|
Chris@16
|
157 // If all queues are empty, we're done.
|
Chris@16
|
158 if (results.first)
|
Chris@16
|
159 return false;
|
Chris@16
|
160 }
|
Chris@16
|
161 }
|
Chris@16
|
162
|
Chris@16
|
163 template<typename DistributedGraph, typename ColorMap>
|
Chris@16
|
164 inline bool
|
Chris@16
|
165 st_connected(const DistributedGraph& g,
|
Chris@16
|
166 typename graph_traits<DistributedGraph>::vertex_descriptor s,
|
Chris@16
|
167 typename graph_traits<DistributedGraph>::vertex_descriptor t,
|
Chris@16
|
168 ColorMap color)
|
Chris@16
|
169 {
|
Chris@16
|
170 return st_connected(g, s, t, color, get(vertex_owner, g));
|
Chris@16
|
171 }
|
Chris@16
|
172
|
Chris@16
|
173 template<typename DistributedGraph>
|
Chris@16
|
174 inline bool
|
Chris@16
|
175 st_connected(const DistributedGraph& g,
|
Chris@16
|
176 typename graph_traits<DistributedGraph>::vertex_descriptor s,
|
Chris@16
|
177 typename graph_traits<DistributedGraph>::vertex_descriptor t)
|
Chris@16
|
178 {
|
Chris@16
|
179 return st_connected(g, s, t,
|
Chris@16
|
180 make_two_bit_color_map(num_vertices(g),
|
Chris@16
|
181 get(vertex_index, g)));
|
Chris@16
|
182 }
|
Chris@16
|
183
|
Chris@16
|
184 } } } // end namespace boost::graph::distributed
|
Chris@16
|
185
|
Chris@16
|
186 #endif // BOOST_GRAPH_DISTRIBUTED_ST_CONNECTED_HPP
|