named_vertices_hash_test.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright (C) 2007-2008 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/mpl/print.hpp>
  9. #include <boost/graph/distributed/adjacency_list.hpp>
  10. #include <boost/graph/distributed/mpi_process_group.hpp>
  11. #include <boost/graph/iteration_macros.hpp>
  12. #include <boost/test/minimal.hpp>
  13. #include <string>
  14. #include <iostream>
  15. #ifdef BOOST_NO_EXCEPTIONS
  16. void
  17. boost::throw_exception(std::exception const& ex)
  18. {
  19. std::cout << ex.what() << std::endl;
  20. abort();
  21. }
  22. #endif
  23. using namespace boost;
  24. using boost::graph::distributed::mpi_process_group;
  25. /// City structure to be attached to each vertex
  26. struct City {
  27. City() {}
  28. City(const std::string& name, int population = -1)
  29. : name(name), population(population) { }
  30. template<typename Archiver>
  31. void serialize(Archiver& ar, const unsigned int /*version*/)
  32. {
  33. ar & name & population;
  34. }
  35. std::string name;
  36. int population;
  37. };
  38. /// Our distribution function
  39. template<typename T>
  40. struct named_vertices_hashed_distribution
  41. {
  42. template<typename ProcessGroup>
  43. named_vertices_hashed_distribution(const ProcessGroup& pg,
  44. std::size_t /*num_vertices*/ = 0)
  45. : n(num_processes(pg)) { }
  46. int operator()(const T& value) const
  47. {
  48. return hasher(value) % n;
  49. }
  50. std::size_t n;
  51. boost::hash<T> hasher;
  52. };
  53. typedef named_vertices_hashed_distribution<std::string> hasher_type;
  54. namespace boost { namespace graph {
  55. /// Use the City name as a key for indexing cities in a graph
  56. template<>
  57. struct internal_vertex_name<City>
  58. {
  59. typedef multi_index::member<City, std::string, &City::name> type;
  60. };
  61. /// Allow the graph to build cities given only their names (filling in
  62. /// the defaults for fields).
  63. template<>
  64. struct internal_vertex_constructor<City>
  65. {
  66. typedef vertex_from_name<City> type;
  67. };
  68. // namespace distributed
  69. // {
  70. // /// Use the City name as the source for the distribution hasher
  71. // ///
  72. // /// This is currently needed in addition to the specification of this
  73. // /// hasher functor as the 3rd template parameter to the distributedS
  74. // /// template.
  75. // template<>
  76. // struct internal_vertex_name_distribution<City>
  77. // {
  78. // typedef named_vertices_hashed_distribution<std::string> type;
  79. // };
  80. // }
  81. } } // end namespace boost::graph
  82. /// Our road map, where each of the vertices are cities
  83. typedef boost::adjacency_list<vecS,
  84. distributedS<mpi_process_group, vecS, hasher_type>,
  85. bidirectionalS, City> RoadMap;
  86. typedef graph_traits<RoadMap>::vertex_descriptor Vertex;
  87. int test_main(int argc, char** argv)
  88. {
  89. boost::mpi::environment env(argc, argv);
  90. mpi_process_group pg;
  91. RoadMap map; // (pg, hasher_type(pg));
  92. int rank = process_id(pg);
  93. bool i_am_root = rank == 0;
  94. /// Create vertices for Bloomington, Indianapolis, Chicago. Everyone will
  95. /// try to do this, but only one of each vertex will be added.
  96. Vertex bloomington = add_vertex(City("Bloomington", 69291), map);
  97. Vertex chicago = add_vertex(City("Chicago", 9500000), map);
  98. Vertex indianapolis = add_vertex(City("Indianapolis", 791926), map);
  99. BGL_FORALL_VERTICES(city, map, RoadMap)
  100. std::cout << rank << ": " << map[city].name << ", population "
  101. << map[city].population << std::endl;
  102. BOOST_CHECK(*find_vertex("Bloomington", map) == bloomington);
  103. BOOST_CHECK(*find_vertex("Indianapolis", map) == indianapolis);
  104. BOOST_CHECK(*find_vertex("Chicago", map) == chicago);
  105. if (i_am_root) {
  106. add_edge(bloomington, "Indianapolis", map);
  107. add_edge("Indianapolis", chicago, map);
  108. add_edge("Indianapolis", "Cincinnati", map);
  109. }
  110. synchronize(map);
  111. {
  112. property_map<RoadMap, std::string City::*>::type
  113. city_name = get(&City::name, map);
  114. BGL_FORALL_EDGES(road, map, RoadMap)
  115. std::cout << rank << ": " << get(city_name, source(road, map)) << " -> "
  116. << get(city_name, target(road, map)) << std::endl;
  117. synchronize(map);
  118. }
  119. // Make sure the vertex for "Cincinnati" was created implicitly
  120. Vertex cincinnati = *find_vertex("Cincinnati", map);
  121. if (get(get(vertex_owner, map), cincinnati)
  122. == process_id(map.process_group()))
  123. BOOST_CHECK(map[cincinnati].population == -1);
  124. synchronize(map);
  125. return 0;
  126. }