named_vertices_test.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (C) 2007 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/graph/use_mpi.hpp>
  6. #include <boost/config.hpp>
  7. #include <boost/throw_exception.hpp>
  8. #include <boost/graph/distributed/adjacency_list.hpp>
  9. #include <boost/graph/distributed/mpi_process_group.hpp>
  10. #include <boost/graph/iteration_macros.hpp>
  11. #include <boost/test/minimal.hpp>
  12. #include <string>
  13. #include <iostream>
  14. #ifdef BOOST_NO_EXCEPTIONS
  15. void
  16. boost::throw_exception(std::exception const& ex)
  17. {
  18. std::cout << ex.what() << std::endl;
  19. abort();
  20. }
  21. #endif
  22. using namespace boost;
  23. using boost::graph::distributed::mpi_process_group;
  24. /// City structure to be attached to each vertex
  25. struct City {
  26. City() {}
  27. City(const std::string& name, int population = -1)
  28. : name(name), population(population) { }
  29. template<typename Archiver>
  30. void serialize(Archiver& ar, const unsigned int /*version*/)
  31. {
  32. ar & name & population;
  33. }
  34. std::string name;
  35. int population;
  36. };
  37. namespace boost { namespace graph {
  38. /// Use the City name as a key for indexing cities in a graph
  39. template<>
  40. struct internal_vertex_name<City>
  41. {
  42. typedef multi_index::member<City, std::string, &City::name> type;
  43. };
  44. /// Allow the graph to build cities given only their names (filling in
  45. /// the defaults for fields).
  46. template<>
  47. struct internal_vertex_constructor<City>
  48. {
  49. typedef vertex_from_name<City> type;
  50. };
  51. } } // end namespace boost::graph
  52. /// Our road map, where each of the vertices are cities
  53. typedef boost::adjacency_list<vecS, distributedS<mpi_process_group, vecS>,
  54. bidirectionalS, City> RoadMap;
  55. typedef graph_traits<RoadMap>::vertex_descriptor Vertex;
  56. int test_main(int argc, char** argv)
  57. {
  58. boost::mpi::environment env(argc, argv);
  59. RoadMap map;
  60. int rank = process_id(mpi_process_group());
  61. bool i_am_root = rank == 0;
  62. /// Create vertices for Bloomington, Indianapolis, Chicago. Everyone will
  63. /// try to do this, but only one of each vertex will be added.
  64. Vertex bloomington = add_vertex(City("Bloomington", 69291), map);
  65. Vertex chicago = add_vertex(City("Chicago", 9500000), map);
  66. Vertex indianapolis = add_vertex(City("Indianapolis", 791926), map);
  67. BGL_FORALL_VERTICES(city, map, RoadMap)
  68. std::cout << rank << ": " << map[city].name << ", population "
  69. << map[city].population << std::endl;
  70. BOOST_CHECK(*find_vertex("Bloomington", map) == bloomington);
  71. BOOST_CHECK(*find_vertex("Indianapolis", map) == indianapolis);
  72. BOOST_CHECK(*find_vertex("Chicago", map) == chicago);
  73. if (i_am_root) {
  74. add_edge(bloomington, "Indianapolis", map);
  75. add_edge("Indianapolis", chicago, map);
  76. add_edge("Indianapolis", "Cincinnati", map);
  77. }
  78. synchronize(map);
  79. {
  80. property_map<RoadMap, std::string City::*>::type
  81. city_name = get(&City::name, map);
  82. BGL_FORALL_EDGES(road, map, RoadMap)
  83. std::cout << rank << ": " << get(city_name, source(road, map)) << " -> "
  84. << get(city_name, target(road, map)) << std::endl;
  85. synchronize(map);
  86. }
  87. // Make sure the vertex for "Cincinnati" was created implicitly
  88. Vertex cincinnati = *find_vertex("Cincinnati", map);
  89. if (get(vertex_owner, map, cincinnati)
  90. == process_id(map.process_group()))
  91. BOOST_CHECK(map[cincinnati].population == -1);
  92. synchronize(map);
  93. return 0;
  94. }