1 // Copyright 2004 The Trustees of Indiana University.
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // Authors: Douglas Gregor
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/distributed/dehne_gotz_min_spanning_tree.hpp>
15 #include <boost/graph/distributed/mpi_process_group.hpp>
16 #include <boost/graph/distributed/vertex_list_adaptor.hpp>
17 #include <boost/graph/parallel/distribution.hpp>
18 #include <boost/test/minimal.hpp>
19 #include <boost/graph/distributed/adjacency_list.hpp>
23 #ifdef BOOST_NO_EXCEPTIONS
25 boost::throw_exception(std::exception
const& ex
)
27 std::cout
<< ex
.what() << std::endl
;
32 using namespace boost
;
33 using boost::graph::distributed::mpi_process_group
;
35 template<typename Graph
, typename WeightMap
, typename InputIterator
>
37 total_weight(const Graph
& g
, WeightMap weight_map
,
38 InputIterator first
, InputIterator last
)
40 typedef typename graph_traits
<Graph
>::vertex_descriptor vertex_descriptor
;
43 while (first
!= last
) {
44 total_weight
+= get(weight_map
, *first
);
45 if (process_id(g
.process_group()) == 0) {
46 vertex_descriptor u
= source(*first
, g
);
47 vertex_descriptor v
= target(*first
, g
);
48 std::cout
<< "(" << g
.distribution().global(owner(u
), local(u
))
49 << ", " << g
.distribution().global(owner(v
), local(v
))
59 test_distributed_dense_boruvka()
61 typedef adjacency_list
<listS
,
62 distributedS
<mpi_process_group
, vecS
>,
67 property
<edge_weight_t
, int> > Graph
;
69 typedef graph_traits
<Graph
>::edge_descriptor edge_descriptor
;
71 typedef std::pair
<int, int> E
;
73 const int num_nodes
= 5;
74 E edge_array
[] = { E(0, 2), E(1, 3), E(1, 4), E(2, 1), E(2, 3),
75 E(3, 4), E(4, 0), E(4, 1)
77 int weights
[] = { 1, 1, 2, 7, 3, 1, 1, 1 };
78 std::size_t num_edges
= sizeof(edge_array
) / sizeof(E
);
80 Graph
g(edge_array
, edge_array
+ num_edges
, weights
, num_nodes
);
83 if (process_id(g
.process_group()) == 0)
84 std::cerr
<< "--Dense Boruvka--\n";
85 typedef property_map
<Graph
, edge_weight_t
>::type WeightMap
;
86 WeightMap weight_map
= get(edge_weight
, g
);
88 std::vector
<edge_descriptor
> mst_edges
;
89 dense_boruvka_minimum_spanning_tree(make_vertex_list_adaptor(g
),
91 std::back_inserter(mst_edges
));
92 int w
= total_weight(g
, weight_map
, mst_edges
.begin(), mst_edges
.end());
94 BOOST_CHECK(mst_edges
.size() == 4);
98 if (process_id(g
.process_group()) == 0)
99 std::cerr
<< "--Merge local MSTs--\n";
100 typedef property_map
<Graph
, edge_weight_t
>::type WeightMap
;
101 WeightMap weight_map
= get(edge_weight
, g
);
103 std::vector
<edge_descriptor
> mst_edges
;
104 merge_local_minimum_spanning_trees(make_vertex_list_adaptor(g
), weight_map
,
105 std::back_inserter(mst_edges
));
106 if (process_id(g
.process_group()) == 0) {
107 int w
= total_weight(g
, weight_map
, mst_edges
.begin(), mst_edges
.end());
109 BOOST_CHECK(mst_edges
.size() == 4);
114 if (process_id(g
.process_group()) == 0)
115 std::cerr
<< "--Boruvka then Merge--\n";
116 typedef property_map
<Graph
, edge_weight_t
>::type WeightMap
;
117 WeightMap weight_map
= get(edge_weight
, g
);
119 std::vector
<edge_descriptor
> mst_edges
;
120 boruvka_then_merge(make_vertex_list_adaptor(g
), weight_map
,
121 std::back_inserter(mst_edges
));
122 if (process_id(g
.process_group()) == 0) {
123 int w
= total_weight(g
, weight_map
, mst_edges
.begin(), mst_edges
.end());
125 BOOST_CHECK(mst_edges
.size() == 4);
130 if (process_id(g
.process_group()) == 0)
131 std::cerr
<< "--Boruvka mixed Merge--\n";
132 typedef property_map
<Graph
, edge_weight_t
>::type WeightMap
;
133 WeightMap weight_map
= get(edge_weight
, g
);
135 std::vector
<edge_descriptor
> mst_edges
;
136 boruvka_mixed_merge(make_vertex_list_adaptor(g
), weight_map
,
137 std::back_inserter(mst_edges
));
138 if (process_id(g
.process_group()) == 0) {
139 int w
= total_weight(g
, weight_map
, mst_edges
.begin(), mst_edges
.end());
141 BOOST_CHECK(mst_edges
.size() == 4);
146 int test_main(int argc
, char** argv
)
148 boost::mpi::environment
env(argc
, argv
);
149 test_distributed_dense_boruvka();