distributed_mst_test.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2004 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. // Authors: Douglas Gregor
  6. // Andrew Lumsdaine
  7. #include <boost/graph/use_mpi.hpp>
  8. #include <boost/config.hpp>
  9. #include <boost/throw_exception.hpp>
  10. #include <boost/serialization/vector.hpp>
  11. #include <boost/graph/distributed/dehne_gotz_min_spanning_tree.hpp>
  12. #include <boost/graph/distributed/mpi_process_group.hpp>
  13. #include <boost/graph/distributed/vertex_list_adaptor.hpp>
  14. #include <boost/graph/parallel/distribution.hpp>
  15. #include <boost/test/minimal.hpp>
  16. #include <boost/graph/distributed/adjacency_list.hpp>
  17. #include <iostream>
  18. #include <cstdlib>
  19. #ifdef BOOST_NO_EXCEPTIONS
  20. void
  21. boost::throw_exception(std::exception const& ex)
  22. {
  23. std::cout << ex.what() << std::endl;
  24. abort();
  25. }
  26. #endif
  27. using namespace boost;
  28. using boost::graph::distributed::mpi_process_group;
  29. template<typename Graph, typename WeightMap, typename InputIterator>
  30. int
  31. total_weight(const Graph& g, WeightMap weight_map,
  32. InputIterator first, InputIterator last)
  33. {
  34. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  35. int total_weight = 0;
  36. while (first != last) {
  37. total_weight += get(weight_map, *first);
  38. if (process_id(g.process_group()) == 0) {
  39. vertex_descriptor u = source(*first, g);
  40. vertex_descriptor v = target(*first, g);
  41. std::cout << "(" << g.distribution().global(owner(u), local(u))
  42. << ", " << g.distribution().global(owner(v), local(v))
  43. << ")\n";
  44. }
  45. ++first;
  46. }
  47. return total_weight;
  48. }
  49. void
  50. test_distributed_dense_boruvka()
  51. {
  52. typedef adjacency_list<listS,
  53. distributedS<mpi_process_group, vecS>,
  54. undirectedS,
  55. // Vertex properties
  56. no_property,
  57. // Edge properties
  58. property<edge_weight_t, int> > Graph;
  59. typedef graph_traits<Graph>::edge_descriptor edge_descriptor;
  60. typedef std::pair<int, int> E;
  61. const int num_nodes = 5;
  62. E edge_array[] = { E(0, 2), E(1, 3), E(1, 4), E(2, 1), E(2, 3),
  63. E(3, 4), E(4, 0), E(4, 1)
  64. };
  65. int weights[] = { 1, 1, 2, 7, 3, 1, 1, 1 };
  66. std::size_t num_edges = sizeof(edge_array) / sizeof(E);
  67. Graph g(edge_array, edge_array + num_edges, weights, num_nodes);
  68. {
  69. if (process_id(g.process_group()) == 0)
  70. std::cerr << "--Dense Boruvka--\n";
  71. typedef property_map<Graph, edge_weight_t>::type WeightMap;
  72. WeightMap weight_map = get(edge_weight, g);
  73. std::vector<edge_descriptor> mst_edges;
  74. dense_boruvka_minimum_spanning_tree(make_vertex_list_adaptor(g),
  75. weight_map,
  76. std::back_inserter(mst_edges));
  77. int w = total_weight(g, weight_map, mst_edges.begin(), mst_edges.end());
  78. BOOST_CHECK(w == 4);
  79. BOOST_CHECK(mst_edges.size() == 4);
  80. }
  81. {
  82. if (process_id(g.process_group()) == 0)
  83. std::cerr << "--Merge local MSTs--\n";
  84. typedef property_map<Graph, edge_weight_t>::type WeightMap;
  85. WeightMap weight_map = get(edge_weight, g);
  86. std::vector<edge_descriptor> mst_edges;
  87. merge_local_minimum_spanning_trees(make_vertex_list_adaptor(g), weight_map,
  88. std::back_inserter(mst_edges));
  89. if (process_id(g.process_group()) == 0) {
  90. int w = total_weight(g, weight_map, mst_edges.begin(), mst_edges.end());
  91. BOOST_CHECK(w == 4);
  92. BOOST_CHECK(mst_edges.size() == 4);
  93. }
  94. }
  95. {
  96. if (process_id(g.process_group()) == 0)
  97. std::cerr << "--Boruvka then Merge--\n";
  98. typedef property_map<Graph, edge_weight_t>::type WeightMap;
  99. WeightMap weight_map = get(edge_weight, g);
  100. std::vector<edge_descriptor> mst_edges;
  101. boruvka_then_merge(make_vertex_list_adaptor(g), weight_map,
  102. std::back_inserter(mst_edges));
  103. if (process_id(g.process_group()) == 0) {
  104. int w = total_weight(g, weight_map, mst_edges.begin(), mst_edges.end());
  105. BOOST_CHECK(w == 4);
  106. BOOST_CHECK(mst_edges.size() == 4);
  107. }
  108. }
  109. {
  110. if (process_id(g.process_group()) == 0)
  111. std::cerr << "--Boruvka mixed Merge--\n";
  112. typedef property_map<Graph, edge_weight_t>::type WeightMap;
  113. WeightMap weight_map = get(edge_weight, g);
  114. std::vector<edge_descriptor> mst_edges;
  115. boruvka_mixed_merge(make_vertex_list_adaptor(g), weight_map,
  116. std::back_inserter(mst_edges));
  117. if (process_id(g.process_group()) == 0) {
  118. int w = total_weight(g, weight_map, mst_edges.begin(), mst_edges.end());
  119. BOOST_CHECK(w == 4);
  120. BOOST_CHECK(mst_edges.size() == 4);
  121. }
  122. }
  123. }
  124. int test_main(int argc, char** argv)
  125. {
  126. boost::mpi::environment env(argc, argv);
  127. test_distributed_dense_boruvka();
  128. return 0;
  129. }