1 //=======================================================================
2 // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
3 // Copyright 2004 Douglas Gregor
4 // Copyright (C) 2006-2008
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //=======================================================================
10 #include <boost/graph/use_mpi.hpp>
11 #include <boost/config.hpp>
12 #include <boost/throw_exception.hpp>
13 #include <boost/graph/depth_first_search.hpp>
14 #include <boost/graph/distributed/mpi_process_group.hpp>
15 #include <boost/graph/distributed/adjacency_list.hpp>
16 #include <boost/test/minimal.hpp>
19 #ifdef BOOST_NO_EXCEPTIONS
21 boost::throw_exception(std::exception const& ex)
23 std::cout << ex.what() << std::endl;
28 using namespace boost;
29 using boost::graph::distributed::mpi_process_group;
31 // Set up the vertex names
32 enum vertex_id_t { u, v, w, x, y, z, N };
33 char vertex_names[] = { 'u', 'v', 'w', 'x', 'y', 'z' };
35 template<typename Vertex, typename Graph>
36 char get_vertex_name(Vertex v, const Graph& g)
38 return vertex_names[g.distribution().global(owner(v), local(v))];
41 struct printing_dfs_visitor
43 template<typename Vertex, typename Graph>
44 void initialize_vertex(Vertex v, const Graph& g)
46 vertex_event("initialize_vertex", v, g);
49 template<typename Vertex, typename Graph>
50 void start_vertex(Vertex v, const Graph& g)
52 vertex_event("start_vertex", v, g);
55 template<typename Vertex, typename Graph>
56 void discover_vertex(Vertex v, const Graph& g)
58 vertex_event("discover_vertex", v, g);
61 template<typename Edge, typename Graph>
62 void examine_edge(Edge e, const Graph& g)
64 edge_event("examine_edge", e, g);
67 template<typename Edge, typename Graph>
68 void tree_edge(Edge e, const Graph& g)
70 edge_event("tree_edge", e, g);
73 template<typename Edge, typename Graph>
74 void back_edge(Edge e, const Graph& g)
76 edge_event("back_edge", e, g);
79 template<typename Edge, typename Graph>
80 void forward_or_cross_edge(Edge e, const Graph& g)
82 edge_event("forward_or_cross_edge", e, g);
85 template<typename Vertex, typename Graph>
86 void finish_vertex(Vertex v, const Graph& g)
88 vertex_event("finish_vertex", v, g);
92 template<typename Vertex, typename Graph>
93 void vertex_event(const char* name, Vertex v, const Graph& g)
95 std::cerr << "#" << process_id(g.process_group()) << ": " << name << "("
96 << get_vertex_name(v, g) << ": " << local(v) << "@" << owner(v)
100 template<typename Edge, typename Graph>
101 void edge_event(const char* name, Edge e, const Graph& g)
103 std::cerr << "#" << process_id(g.process_group()) << ": " << name << "("
104 << get_vertex_name(source(e, g), g) << ": "
105 << local(source(e, g)) << "@" << owner(source(e, g)) << ", "
106 << get_vertex_name(target(e, g), g) << ": "
107 << local(target(e, g)) << "@" << owner(target(e, g)) << ")\n";
113 test_distributed_dfs()
115 typedef adjacency_list<listS,
116 distributedS<mpi_process_group, vecS>,
119 property<vertex_color_t, default_color_type> >
121 typedef graph_traits<Graph>::vertex_descriptor vertex_descriptor;
123 // Specify the edges in the graph
124 typedef std::pair<int, int> E;
125 E edge_array[] = { E(u, v), E(u, w), E(u, x), E(x, v), E(y, x),
126 E(v, y), E(w, y), E(w, z), E(z, z) };
127 Graph g(edge_array, edge_array + sizeof(edge_array) / sizeof(E), N);
129 std::vector<vertex_descriptor> parent(num_vertices(g));
130 std::vector<vertex_descriptor> explore(num_vertices(g));
132 boost::graph::tsin_depth_first_visit
135 printing_dfs_visitor(),
136 get(vertex_color, g),
137 make_iterator_property_map(parent.begin(), get(vertex_index, g)),
138 make_iterator_property_map(explore.begin(), get(vertex_index, g)),
139 get(vertex_index, g));
142 std::size_t correct_parents1[N] = {u, u, y, y, v, w};
143 std::size_t correct_parents2[N] = {u, u, y, v, x, w};
146 for (std::size_t i = 0; i < N; ++i) {
147 vertex_descriptor v = vertex(i, g);
148 if (owner(v) == process_id(g.process_group())) {
149 std::cout << "parent(" << get_vertex_name(v, g) << ") = "
150 << get_vertex_name(parent[v.local], g) << std::endl;
156 depth_first_visit(g, vertex(u, g), printing_dfs_visitor());
161 test_main(int argc, char* argv[])
163 mpi::environment env(argc, argv);
164 test_distributed_dfs();