1 // Copyright (C) 2007 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 #include <boost/graph/use_mpi.hpp>
8 #include <boost/config.hpp>
9 #include <boost/throw_exception.hpp>
10 #include <boost/graph/distributed/adjacency_list.hpp>
11 #include <boost/graph/distributed/mpi_process_group.hpp>
12 #include <boost/graph/iteration_macros.hpp>
13 #include <boost/test/minimal.hpp>
17 #ifdef BOOST_NO_EXCEPTIONS
19 boost::throw_exception(std::exception
const& ex
)
21 std::cout
<< ex
.what() << std::endl
;
26 using namespace boost
;
27 using boost::graph::distributed::mpi_process_group
;
29 /// City structure to be attached to each vertex
33 City(const std::string
& name
, int population
= -1)
34 : name(name
), population(population
) { }
36 template<typename Archiver
>
37 void serialize(Archiver
& ar
, const unsigned int /*version*/)
39 ar
& name
& population
;
46 namespace boost
{ namespace graph
{
48 /// Use the City name as a key for indexing cities in a graph
50 struct internal_vertex_name
<City
>
52 typedef multi_index::member
<City
, std::string
, &City::name
> type
;
55 /// Allow the graph to build cities given only their names (filling in
56 /// the defaults for fields).
58 struct internal_vertex_constructor
<City
>
60 typedef vertex_from_name
<City
> type
;
63 } } // end namespace boost::graph
65 /// Our road map, where each of the vertices are cities
66 typedef boost::adjacency_list
<vecS
, distributedS
<mpi_process_group
, vecS
>,
67 bidirectionalS
, City
> RoadMap
;
68 typedef graph_traits
<RoadMap
>::vertex_descriptor Vertex
;
70 int test_main(int argc
, char** argv
)
72 boost::mpi::environment
env(argc
, argv
);
76 int rank
= process_id(mpi_process_group());
77 bool i_am_root
= rank
== 0;
79 /// Create vertices for Bloomington, Indianapolis, Chicago. Everyone will
80 /// try to do this, but only one of each vertex will be added.
81 Vertex bloomington
= add_vertex(City("Bloomington", 69291), map
);
82 Vertex chicago
= add_vertex(City("Chicago", 9500000), map
);
83 Vertex indianapolis
= add_vertex(City("Indianapolis", 791926), map
);
85 BGL_FORALL_VERTICES(city
, map
, RoadMap
)
86 std::cout
<< rank
<< ": " << map
[city
].name
<< ", population "
87 << map
[city
].population
<< std::endl
;
89 BOOST_CHECK(*find_vertex("Bloomington", map
) == bloomington
);
90 BOOST_CHECK(*find_vertex("Indianapolis", map
) == indianapolis
);
91 BOOST_CHECK(*find_vertex("Chicago", map
) == chicago
);
94 add_edge(bloomington
, "Indianapolis", map
);
95 add_edge("Indianapolis", chicago
, map
);
96 add_edge("Indianapolis", "Cincinnati", map
);
102 property_map
<RoadMap
, std::string
City::*>::type
103 city_name
= get(&City::name
, map
);
105 BGL_FORALL_EDGES(road
, map
, RoadMap
)
106 std::cout
<< rank
<< ": " << get(city_name
, source(road
, map
)) << " -> "
107 << get(city_name
, target(road
, map
)) << std::endl
;
112 // Make sure the vertex for "Cincinnati" was created implicitly
113 Vertex cincinnati
= *find_vertex("Cincinnati", map
);
114 if (get(vertex_owner
, map
, cincinnati
)
115 == process_id(map
.process_group()))
116 BOOST_CHECK(map
[cincinnati
].population
== -1);