distributed_dfs_test.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //=======================================================================
  2. // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
  3. // Copyright 2004 Douglas Gregor
  4. // Copyright (C) 2006-2008
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. #include <boost/graph/use_mpi.hpp>
  10. #include <boost/config.hpp>
  11. #include <boost/throw_exception.hpp>
  12. #include <boost/serialization/vector.hpp>
  13. #include <boost/graph/depth_first_search.hpp>
  14. #include <boost/graph/distributed/mpi_process_group.hpp>
  15. #include <boost/graph/distributed/adjacency_list.hpp>
  16. #include <boost/test/minimal.hpp>
  17. #include <iostream>
  18. #ifdef BOOST_NO_EXCEPTIONS
  19. void
  20. boost::throw_exception(std::exception const& ex)
  21. {
  22. std::cout << ex.what() << std::endl;
  23. abort();
  24. }
  25. #endif
  26. using namespace boost;
  27. using boost::graph::distributed::mpi_process_group;
  28. // Set up the vertex names
  29. enum vertex_id_t { u, v, w, x, y, z, N };
  30. char vertex_names[] = { 'u', 'v', 'w', 'x', 'y', 'z' };
  31. template<typename Vertex, typename Graph>
  32. char get_vertex_name(Vertex v, const Graph& g)
  33. {
  34. return vertex_names[g.distribution().global(owner(v), local(v))];
  35. }
  36. struct printing_dfs_visitor
  37. {
  38. template<typename Vertex, typename Graph>
  39. void initialize_vertex(Vertex v, const Graph& g)
  40. {
  41. vertex_event("initialize_vertex", v, g);
  42. }
  43. template<typename Vertex, typename Graph>
  44. void start_vertex(Vertex v, const Graph& g)
  45. {
  46. vertex_event("start_vertex", v, g);
  47. }
  48. template<typename Vertex, typename Graph>
  49. void discover_vertex(Vertex v, const Graph& g)
  50. {
  51. vertex_event("discover_vertex", v, g);
  52. }
  53. template<typename Edge, typename Graph>
  54. void examine_edge(Edge e, const Graph& g)
  55. {
  56. edge_event("examine_edge", e, g);
  57. }
  58. template<typename Edge, typename Graph>
  59. void tree_edge(Edge e, const Graph& g)
  60. {
  61. edge_event("tree_edge", e, g);
  62. }
  63. template<typename Edge, typename Graph>
  64. void back_edge(Edge e, const Graph& g)
  65. {
  66. edge_event("back_edge", e, g);
  67. }
  68. template<typename Edge, typename Graph>
  69. void forward_or_cross_edge(Edge e, const Graph& g)
  70. {
  71. edge_event("forward_or_cross_edge", e, g);
  72. }
  73. template<typename Vertex, typename Graph>
  74. void finish_vertex(Vertex v, const Graph& g)
  75. {
  76. vertex_event("finish_vertex", v, g);
  77. }
  78. private:
  79. template<typename Vertex, typename Graph>
  80. void vertex_event(const char* name, Vertex v, const Graph& g)
  81. {
  82. std::cerr << "#" << process_id(g.process_group()) << ": " << name << "("
  83. << get_vertex_name(v, g) << ": " << local(v) << "@" << owner(v)
  84. << ")\n";
  85. }
  86. template<typename Edge, typename Graph>
  87. void edge_event(const char* name, Edge e, const Graph& g)
  88. {
  89. std::cerr << "#" << process_id(g.process_group()) << ": " << name << "("
  90. << get_vertex_name(source(e, g), g) << ": "
  91. << local(source(e, g)) << "@" << owner(source(e, g)) << ", "
  92. << get_vertex_name(target(e, g), g) << ": "
  93. << local(target(e, g)) << "@" << owner(target(e, g)) << ")\n";
  94. }
  95. };
  96. void
  97. test_distributed_dfs()
  98. {
  99. typedef adjacency_list<listS,
  100. distributedS<mpi_process_group, vecS>,
  101. undirectedS,
  102. // Vertex properties
  103. property<vertex_color_t, default_color_type> >
  104. Graph;
  105. typedef graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  106. // Specify the edges in the graph
  107. typedef std::pair<int, int> E;
  108. E edge_array[] = { E(u, v), E(u, w), E(u, x), E(x, v), E(y, x),
  109. E(v, y), E(w, y), E(w, z), E(z, z) };
  110. Graph g(edge_array, edge_array + sizeof(edge_array) / sizeof(E), N);
  111. std::vector<vertex_descriptor> parent(num_vertices(g));
  112. std::vector<vertex_descriptor> explore(num_vertices(g));
  113. boost::graph::tsin_depth_first_visit
  114. (g,
  115. vertex(u, g),
  116. printing_dfs_visitor(),
  117. get(vertex_color, g),
  118. make_iterator_property_map(parent.begin(), get(vertex_index, g)),
  119. make_iterator_property_map(explore.begin(), get(vertex_index, g)),
  120. get(vertex_index, g));
  121. #if 0
  122. std::size_t correct_parents1[N] = {u, u, y, y, v, w};
  123. std::size_t correct_parents2[N] = {u, u, y, v, x, w};
  124. #endif
  125. for (std::size_t i = 0; i < N; ++i) {
  126. vertex_descriptor v = vertex(i, g);
  127. if (owner(v) == process_id(g.process_group())) {
  128. std::cout << "parent(" << get_vertex_name(v, g) << ") = "
  129. << get_vertex_name(parent[v.local], g) << std::endl;
  130. }
  131. }
  132. if (false) {
  133. depth_first_visit(g, vertex(u, g), printing_dfs_visitor());
  134. }
  135. }
  136. int
  137. test_main(int argc, char* argv[])
  138. {
  139. mpi::environment env(argc, argv);
  140. test_distributed_dfs();
  141. return 0;
  142. }