]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/graph_parallel/doc/html/breadth_first_search.html
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / graph_parallel / doc / html / breadth_first_search.html
1 <?xml version="1.0" encoding="utf-8" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 <meta name="generator" content="Docutils 0.6: http://docutils.sourceforge.net/" />
7 <title>Parallel BGL Breadth-First Search</title>
8 <link rel="stylesheet" href="../../../../rst.css" type="text/css" />
9 </head>
10 <body>
11 <div class="document" id="logo-breadth-first-search">
12 <h1 class="title"><a class="reference external" href="http://www.osl.iu.edu/research/pbgl"><img align="middle" alt="Parallel BGL" class="align-middle" src="pbgl-logo.png" /></a> Breadth-First Search</h1>
13
14 <!-- Copyright (C) 2004-2008 The Trustees of Indiana University.
15 Use, modification and distribution is subject to the Boost Software
16 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
17 http://www.boost.org/LICENSE_1_0.txt) -->
18 <pre class="literal-block">
19 // named parameter version
20 template &lt;class Graph, class P, class T, class R&gt;
21 void breadth_first_search(Graph&amp; G,
22 typename graph_traits&lt;Graph&gt;::vertex_descriptor s,
23 const bgl_named_params&lt;P, T, R&gt;&amp; params);
24
25 // non-named parameter version
26 template &lt;class Graph, class Buffer, class BFSVisitor,
27 class ColorMap&gt;
28 void breadth_first_search(const Graph&amp; g,
29 typename graph_traits&lt;Graph&gt;::vertex_descriptor s,
30 Buffer&amp; Q, BFSVisitor vis, ColorMap color);
31 </pre>
32 <p>The <tt class="docutils literal"><span class="pre">breadth_first_search()</span></tt> function performs a distributed breadth-first
33 traversal of a directed or undirected graph. The distributed BFS is
34 syntactically equivalent to its <a class="reference external" href="http://www.boost.org/libs/graph/doc/breadth_first_search.html">sequential counterpart</a>, which
35 provides additional background and discussion. Differences in
36 semantics are highlighted here, and we refer the reader to the
37 documentation of the <a class="reference external" href="http://www.boost.org/libs/graph/doc/breadth_first_search.html">sequential breadth-first search</a> for the
38 remainder of the details.</p>
39 <p>This distributed breadth-first search algorithm implements a
40 <em>level-synchronized</em> breadth-first search, meaning that all vertices
41 in a given level of the BFS tree will be processed (potentially in
42 parallel) before any vertices from a successive level in the tree are
43 processed. Distributed breadth-first search visitors should account
44 for this behavior, a topic discussed further in <a class="reference internal" href="#visitor-event-points">Visitor Event
45 Points</a>.</p>
46 <div class="contents topic" id="contents">
47 <p class="topic-title first">Contents</p>
48 <ul class="simple">
49 <li><a class="reference internal" href="#where-defined" id="id1">Where Defined</a></li>
50 <li><a class="reference internal" href="#parameter-defaults" id="id2">Parameter Defaults</a></li>
51 <li><a class="reference internal" href="#complexity" id="id3">Complexity</a></li>
52 <li><a class="reference internal" href="#visitor-event-points" id="id4">Visitor Event Points</a><ul>
53 <li><a class="reference internal" href="#making-visitors-safe-for-distributed-bfs" id="id5">Making Visitors Safe for Distributed BFS</a></li>
54 <li><a class="reference internal" href="#distributed-bfs-visitor-example" id="id6">Distributed BFS Visitor Example</a></li>
55 </ul>
56 </li>
57 <li><a class="reference internal" href="#performance" id="id7">Performance</a></li>
58 </ul>
59 </div>
60 <div class="section" id="where-defined">
61 <h1><a class="toc-backref" href="#id1">Where Defined</a></h1>
62 <p>&lt;<tt class="docutils literal"><span class="pre">boost/graph/breadth_first_search.hpp</span></tt>&gt;</p>
63 </div>
64 <div class="section" id="parameter-defaults">
65 <h1><a class="toc-backref" href="#id2">Parameter Defaults</a></h1>
66 <p>All parameters of the <a class="reference external" href="http://www.boost.org/libs/graph/doc/breadth_first_search.html">sequential breadth-first search</a> are supported
67 and have essentially the same meaning. Only differences are documented
68 here.</p>
69 <dl class="docutils">
70 <dt>IN: <tt class="docutils literal"><span class="pre">Graph&amp;</span> <span class="pre">g</span></tt></dt>
71 <dd>The graph type must be a model of <a class="reference external" href="DistributedGraph.html">Distributed Graph</a>.</dd>
72 <dt>IN: <tt class="docutils literal"><span class="pre">vertex_descriptor</span> <span class="pre">s</span></tt></dt>
73 <dd>The start vertex must be the same in every process.</dd>
74 <dt>IN: <tt class="docutils literal"><span class="pre">visitor(BFSVisitor</span> <span class="pre">vis)</span></tt></dt>
75 <dd>The visitor must be a distributed BFS visitor. The suble differences
76 between sequential and distributed BFS visitors are discussed in the
77 section <a class="reference internal" href="#visitor-event-points">Visitor Event Points</a>.</dd>
78 <dt>UTIL/OUT: <tt class="docutils literal"><span class="pre">color_map(ColorMap</span> <span class="pre">color)</span></tt></dt>
79 <dd>The color map must be a <a class="reference external" href="distributed_property_map.html">Distributed Property Map</a> with the same
80 process group as the graph <tt class="docutils literal"><span class="pre">g</span></tt> whose colors must monotonically
81 darken (white -&gt; gray -&gt; black). The default value is a distributed
82 <tt class="docutils literal"><span class="pre">iterator_property_map</span></tt> created from a <tt class="docutils literal"><span class="pre">std::vector</span></tt> of
83 <tt class="docutils literal"><span class="pre">default_color_type</span></tt>.</dd>
84 <dt>UTIL: <tt class="docutils literal"><span class="pre">buffer(Buffer&amp;</span> <span class="pre">Q)</span></tt></dt>
85 <dd><p class="first">The queue must be a distributed queue that passes vertices to their
86 owning process. If already-visited vertices should not be visited
87 again (as is typical for BFS), the queue must filter duplicates
88 itself. The queue controls synchronization within the algorithm: its
89 <tt class="docutils literal"><span class="pre">empty()</span></tt> method must not return <tt class="docutils literal"><span class="pre">true</span></tt> until all local queues
90 are empty.</p>
91 <dl class="last docutils">
92 <dt><strong>Default:</strong> A <tt class="docutils literal"><span class="pre">distributed_queue</span></tt> of a <tt class="docutils literal"><span class="pre">filtered_queue</span></tt> over a</dt>
93 <dd>standard <tt class="docutils literal"><span class="pre">boost::queue</span></tt>. This queue filters out duplicate
94 vertices and distributes vertices appropriately.</dd>
95 </dl>
96 </dd>
97 </dl>
98 </div>
99 <div class="section" id="complexity">
100 <h1><a class="toc-backref" href="#id3">Complexity</a></h1>
101 <p>This algorithm performs <em>O(V + E)</em> work in <em>d + 1</em> BSP supersteps,
102 where <em>d</em> is the diameter of the connected component being
103 searched. Over all supersteps, <em>O(E)</em> messages of constant size will
104 be transmitted.</p>
105 </div>
106 <div class="section" id="visitor-event-points">
107 <h1><a class="toc-backref" href="#id4">Visitor Event Points</a></h1>
108 <p>The <a class="reference external" href="http://www.boost.org/libs/graph/doc/BFSVisitor.html">BFS Visitor</a> concept defines 9 event points that will be
109 triggered by the <a class="reference external" href="http://www.boost.org/libs/graph/doc/breadth_first_search.html">sequential breadth-first search</a>. The distributed
110 BFS retains these nine event points, but the sequence of events
111 triggered and the process in which each event occurs will change
112 depending on the distribution of the graph.</p>
113 <dl class="docutils">
114 <dt><tt class="docutils literal"><span class="pre">initialize_vertex(s,</span> <span class="pre">g)</span></tt></dt>
115 <dd>This will be invoked by every process for each local vertex.</dd>
116 <dt><tt class="docutils literal"><span class="pre">discover_vertex(u,</span> <span class="pre">g)</span></tt></dt>
117 <dd>This will be invoked each time a process discovers a new vertex
118 <tt class="docutils literal"><span class="pre">u</span></tt>. Due to incomplete information in distributed property maps,
119 this event may be triggered many times for the same vertex <tt class="docutils literal"><span class="pre">u</span></tt>.</dd>
120 <dt><tt class="docutils literal"><span class="pre">examine_vertex(u,</span> <span class="pre">g)</span></tt></dt>
121 <dd>This will be invoked by the process owning the vertex <tt class="docutils literal"><span class="pre">u</span></tt>. If the
122 distributed queue prevents duplicates, it will be invoked only
123 once for a particular vertex <tt class="docutils literal"><span class="pre">u</span></tt>.</dd>
124 <dt><tt class="docutils literal"><span class="pre">examine_edge(e,</span> <span class="pre">g)</span></tt></dt>
125 <dd>This will be invoked by the process owning the source vertex of
126 <tt class="docutils literal"><span class="pre">e</span></tt>. If the distributed queue prevents duplicates, it will be
127 invoked only once for a particular edge <tt class="docutils literal"><span class="pre">e</span></tt>.</dd>
128 <dt><tt class="docutils literal"><span class="pre">tree_edge(e,</span> <span class="pre">g)</span></tt></dt>
129 <dd>Similar to <tt class="docutils literal"><span class="pre">examine_edge</span></tt>, this will be invoked by the process
130 owning the source vertex and may be invoked only once. Unlike the
131 sequential BFS, this event may be triggered even when the target has
132 already been discovered (but by a different process). Thus, some
133 <tt class="docutils literal"><span class="pre">non_tree_edge</span></tt> events in a sequential BFS may become
134 <tt class="docutils literal"><span class="pre">tree_edge</span></tt> in a distributed BFS.</dd>
135 <dt><tt class="docutils literal"><span class="pre">non_tree_edge(e,</span> <span class="pre">g)</span></tt></dt>
136 <dd>Some <tt class="docutils literal"><span class="pre">non_tree_edge</span></tt> events in a sequential BFS may become
137 <tt class="docutils literal"><span class="pre">tree_edge</span></tt> events in a distributed BFS. See the description of
138 <tt class="docutils literal"><span class="pre">tree_edge</span></tt> for additional details.</dd>
139 <dt><tt class="docutils literal"><span class="pre">gray_target(e,</span> <span class="pre">g)</span></tt></dt>
140 <dd>As with <tt class="docutils literal"><span class="pre">tree_edge</span></tt> not knowing when another process has already
141 discovered a vertex, <tt class="docutils literal"><span class="pre">gray_target</span></tt> events may occur in a
142 distributed BFS when <tt class="docutils literal"><span class="pre">black_target</span></tt> events may occur in a
143 sequential BFS, due to a lack of information on a given
144 processor. The source of edge <tt class="docutils literal"><span class="pre">e</span></tt> will be local to the process
145 executing this event.</dd>
146 <dt><tt class="docutils literal"><span class="pre">black_target(e,</span> <span class="pre">g)</span></tt></dt>
147 <dd>See documentation for <tt class="docutils literal"><span class="pre">gray_target</span></tt></dd>
148 <dt><tt class="docutils literal"><span class="pre">finish_vertex(e,</span> <span class="pre">g)</span></tt></dt>
149 <dd>See documentation for <tt class="docutils literal"><span class="pre">examine_vertex</span></tt>.</dd>
150 </dl>
151 <div class="section" id="making-visitors-safe-for-distributed-bfs">
152 <h2><a class="toc-backref" href="#id5">Making Visitors Safe for Distributed BFS</a></h2>
153 <p>The three most important things to remember when updating an existing
154 BFS visitor for distributed BFS or writing a new distributed BFS
155 visitor are:</p>
156 <ol class="arabic simple">
157 <li>Be sure that all state is either entirely local or in a
158 distributed data structure (most likely a property map!) using
159 the same process group as the graph.</li>
160 <li>Be sure that the visitor doesn't require precise event sequences
161 that cannot be guaranteed by distributed BFS, e.g., requiring
162 <tt class="docutils literal"><span class="pre">tree_edge</span></tt> and <tt class="docutils literal"><span class="pre">non_tree_edge</span></tt> events to be completely
163 distinct.</li>
164 <li>Be sure that the visitor can operate on incomplete
165 information. This often includes using an appropriate reduction
166 operation in a <a class="reference external" href="distributed_property_map.html">distributed property map</a> and verifying that
167 results written are &quot;better&quot; that what was previously written.</li>
168 </ol>
169 </div>
170 <div class="section" id="distributed-bfs-visitor-example">
171 <h2><a class="toc-backref" href="#id6">Distributed BFS Visitor Example</a></h2>
172 <p>To illustrate the differences between sequential and distributed BFS
173 visitors, we consider a BFS visitor that places the distance from the
174 source vertex to every other vertex in a property map. The sequential
175 visitor is very simple:</p>
176 <pre class="literal-block">
177 template&lt;typename DistanceMap&gt;
178 struct bfs_discovery_visitor : bfs_visitor&lt;&gt;
179 {
180 bfs_discovery_visitor(DistanceMap distance) : distance(distance) {}
181
182 template&lt;typename Edge, typename Graph&gt;
183 void tree_edge(Edge e, const Graph&amp; g)
184 {
185 std::size_t new_distance = get(distance, source(e, g)) + 1;
186 put(distance, target(e, g), new_distance);
187 }
188
189 private:
190 DistanceMap distance;
191 };
192 </pre>
193 <p>To revisit this code for distributed BFS, we consider the three points
194 in the section <a class="reference internal" href="#making-visitors-safe-for-distributed-bfs">Making Visitors Safe for Distributed BFS</a>:</p>
195 <ol class="arabic">
196 <li><p class="first">The distance map will need to become distributed, because the
197 distance to each vertex should be stored in the process owning the
198 vertex. This is actually a requirement on the user to provide such
199 a distributed property map, although in many cases the property map
200 will automatically be distributed and no syntactic changes will be
201 required.</p>
202 </li>
203 <li><p class="first">This visitor <em>does</em> require a precise sequence of events that may
204 change with a distributed BFS. The extraneous <tt class="docutils literal"><span class="pre">tree_edge</span></tt> events
205 that may occur could result in attempts to put distances into the
206 distance map multiple times for a single vertex. We therefore need
207 to consider bullet #3.</p>
208 </li>
209 <li><p class="first">Since multiple distance values may be written for each vertex, we
210 must always choose the best value we can find to update the
211 distance map. The distributed property map <tt class="docutils literal"><span class="pre">distance</span></tt> needs to
212 resolve distances to the smallest distance it has seen. For
213 instance, process 0 may find vertex <tt class="docutils literal"><span class="pre">u</span></tt> at level 3 but process 1
214 finds it at level 5: the distance must remain at 3. To do this, we
215 state that the property map's <em>role</em> is as a distance map, which
216 introduces an appropriate reduction operation:</p>
217 <pre class="literal-block">
218 set_property_map_role(vertex_distance, distance);
219 </pre>
220 </li>
221 </ol>
222 <p>The resulting distributed BFS visitor (which also applies, with no
223 changes, in the sequential BFS) is very similar to our original
224 sequential BFS visitor. Note the single-line difference in the
225 constructor:</p>
226 <pre class="literal-block">
227 template&lt;typename DistanceMap&gt;
228 struct bfs_discovery_visitor : bfs_visitor&lt;&gt;
229 {
230 bfs_discovery_visitor(DistanceMap distance) : distance(distance)
231 {
232 set_property_map_role(vertex_distance, distance);
233 }
234
235 template&lt;typename Edge, typename Graph&gt;
236 void tree_edge(Edge e, const Graph&amp; g)
237 {
238 std::size_t new_distance = get(distance, source(e, g)) + 1;
239 put(distance, target(e, g), new_distance);
240 }
241
242 private:
243 DistanceMap distance;
244 };
245 </pre>
246 </div>
247 </div>
248 <div class="section" id="performance">
249 <h1><a class="toc-backref" href="#id7">Performance</a></h1>
250 <p>The performance of Breadth-First Search is illustrated by the
251 following charts. Scaling and performance is reasonable for all of the
252 graphs we have tried.</p>
253 <img align="left" alt="chart_php_generator_ER_SF_SW_dataset_TimeSparse_columns_4.png" class="align-left" src="chart_php_generator_ER_SF_SW_dataset_TimeSparse_columns_4.png" />
254 <img alt="chart_php_generator_ER_SF_SW_dataset_TimeSparse_columns_4_speedup_1.png" src="chart_php_generator_ER_SF_SW_dataset_TimeSparse_columns_4_speedup_1.png" />
255 <img align="left" alt="chart_php_generator_ER_SF_SW_dataset_TimeDense_columns_4.png" class="align-left" src="chart_php_generator_ER_SF_SW_dataset_TimeDense_columns_4.png" />
256 <img alt="chart_php_generator_ER_SF_SW_dataset_TimeDense_columns_4_speedup_1.png" src="chart_php_generator_ER_SF_SW_dataset_TimeDense_columns_4_speedup_1.png" />
257 <hr class="docutils" />
258 <p>Copyright (C) 2004 The Trustees of Indiana University.</p>
259 <p>Authors: Douglas Gregor and Andrew Lumsdaine</p>
260 </div>
261 </div>
262 <div class="footer">
263 <hr class="footer" />
264 Generated on: 2009-05-31 00:21 UTC.
265 Generated by <a class="reference external" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
266
267 </div>
268 </body>
269 </html>