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/serialization/vector.hpp>
14 #include <boost/graph/depth_first_search.hpp>
15 #include <boost/graph/distributed/mpi_process_group.hpp>
16 #include <boost/graph/distributed/adjacency_list.hpp>
17 #include <boost/test/minimal.hpp>
20 #ifdef BOOST_NO_EXCEPTIONS
22 boost::throw_exception(std::exception
const& ex
)
24 std::cout
<< ex
.what() << std::endl
;
29 using namespace boost
;
30 using boost::graph::distributed::mpi_process_group
;
32 // Set up the vertex names
33 enum vertex_id_t
{ u
, v
, w
, x
, y
, z
, N
};
34 char vertex_names
[] = { 'u', 'v', 'w', 'x', 'y', 'z' };
36 template<typename Vertex
, typename Graph
>
37 char get_vertex_name(Vertex v
, const Graph
& g
)
39 return vertex_names
[g
.distribution().global(owner(v
), local(v
))];
42 struct printing_dfs_visitor
44 template<typename Vertex
, typename Graph
>
45 void initialize_vertex(Vertex v
, const Graph
& g
)
47 vertex_event("initialize_vertex", v
, g
);
50 template<typename Vertex
, typename Graph
>
51 void start_vertex(Vertex v
, const Graph
& g
)
53 vertex_event("start_vertex", v
, g
);
56 template<typename Vertex
, typename Graph
>
57 void discover_vertex(Vertex v
, const Graph
& g
)
59 vertex_event("discover_vertex", v
, g
);
62 template<typename Edge
, typename Graph
>
63 void examine_edge(Edge e
, const Graph
& g
)
65 edge_event("examine_edge", e
, g
);
68 template<typename Edge
, typename Graph
>
69 void tree_edge(Edge e
, const Graph
& g
)
71 edge_event("tree_edge", e
, g
);
74 template<typename Edge
, typename Graph
>
75 void back_edge(Edge e
, const Graph
& g
)
77 edge_event("back_edge", e
, g
);
80 template<typename Edge
, typename Graph
>
81 void forward_or_cross_edge(Edge e
, const Graph
& g
)
83 edge_event("forward_or_cross_edge", e
, g
);
86 template<typename Vertex
, typename Graph
>
87 void finish_vertex(Vertex v
, const Graph
& g
)
89 vertex_event("finish_vertex", v
, g
);
93 template<typename Vertex
, typename Graph
>
94 void vertex_event(const char* name
, Vertex v
, const Graph
& g
)
96 std::cerr
<< "#" << process_id(g
.process_group()) << ": " << name
<< "("
97 << get_vertex_name(v
, g
) << ": " << local(v
) << "@" << owner(v
)
101 template<typename Edge
, typename Graph
>
102 void edge_event(const char* name
, Edge e
, const Graph
& g
)
104 std::cerr
<< "#" << process_id(g
.process_group()) << ": " << name
<< "("
105 << get_vertex_name(source(e
, g
), g
) << ": "
106 << local(source(e
, g
)) << "@" << owner(source(e
, g
)) << ", "
107 << get_vertex_name(target(e
, g
), g
) << ": "
108 << local(target(e
, g
)) << "@" << owner(target(e
, g
)) << ")\n";
114 test_distributed_dfs()
116 typedef adjacency_list
<listS
,
117 distributedS
<mpi_process_group
, vecS
>,
120 property
<vertex_color_t
, default_color_type
> >
122 typedef graph_traits
<Graph
>::vertex_descriptor vertex_descriptor
;
124 // Specify the edges in the graph
125 typedef std::pair
<int, int> E
;
126 E edge_array
[] = { E(u
, v
), E(u
, w
), E(u
, x
), E(x
, v
), E(y
, x
),
127 E(v
, y
), E(w
, y
), E(w
, z
), E(z
, z
) };
128 Graph
g(edge_array
, edge_array
+ sizeof(edge_array
) / sizeof(E
), N
);
130 std::vector
<vertex_descriptor
> parent(num_vertices(g
));
131 std::vector
<vertex_descriptor
> explore(num_vertices(g
));
133 boost::graph::tsin_depth_first_visit
136 printing_dfs_visitor(),
137 get(vertex_color
, g
),
138 make_iterator_property_map(parent
.begin(), get(vertex_index
, g
)),
139 make_iterator_property_map(explore
.begin(), get(vertex_index
, g
)),
140 get(vertex_index
, g
));
143 std::size_t correct_parents1
[N
] = {u
, u
, y
, y
, v
, w
};
144 std::size_t correct_parents2
[N
] = {u
, u
, y
, v
, x
, w
};
147 for (std::size_t i
= 0; i
< N
; ++i
) {
148 vertex_descriptor v
= vertex(i
, g
);
149 if (owner(v
) == process_id(g
.process_group())) {
150 std::cout
<< "parent(" << get_vertex_name(v
, g
) << ") = "
151 << get_vertex_name(parent
[v
.local
], g
) << std::endl
;
157 depth_first_visit(g
, vertex(u
, g
), printing_dfs_visitor());
162 test_main(int argc
, char* argv
[])
164 mpi::environment
env(argc
, argv
);
165 test_distributed_dfs();