page_rank.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright (C) 2004-2006 The Trustees of Indiana University.
  2. // Copyright (C) 2002 Brad King and Douglas Gregor
  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)
  6. // Authors: Douglas Gregor
  7. // Andrew Lumsdaine
  8. // Brian Barrett
  9. #ifndef BOOST_PARALLEL_GRAPH_PAGE_RANK_HPP
  10. #define BOOST_PARALLEL_GRAPH_PAGE_RANK_HPP
  11. #ifndef BOOST_GRAPH_USE_MPI
  12. #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
  13. #endif
  14. #include <boost/assert.hpp>
  15. #include <boost/graph/overloading.hpp>
  16. #include <boost/graph/page_rank.hpp>
  17. #include <boost/graph/distributed/concepts.hpp>
  18. #include <boost/property_map/parallel/distributed_property_map.hpp>
  19. #include <boost/property_map/parallel/caching_property_map.hpp>
  20. #include <boost/graph/parallel/algorithm.hpp>
  21. #include <boost/graph/parallel/container_traits.hpp>
  22. // #define WANT_MPI_ONESIDED 1
  23. namespace boost { namespace graph { namespace distributed {
  24. namespace detail {
  25. #ifdef WANT_MPI_ONESIDED
  26. template<typename Graph, typename RankMap, typename owner_map_t>
  27. void page_rank_step(const Graph& g, RankMap from_rank, MPI_Win to_win,
  28. typename property_traits<RankMap>::value_type damping,
  29. owner_map_t owner)
  30. {
  31. typedef typename property_traits<RankMap>::value_type rank_type;
  32. int me, ret;
  33. MPI_Comm_rank(MPI_COMM_WORLD, &me);
  34. // MPI_Accumulate is not required to store the value of the data
  35. // being sent, only the address. The value of the memory location
  36. // must not change until the end of the access epoch, meaning the
  37. // call to MPI_Fence. We therefore store the updated value back
  38. // into the from_rank map before the accumulate rather than using
  39. // a temporary. We're going to reset the values in the from_rank
  40. // before the end of page_rank_step() anyway, so this isn't a huge
  41. // deal. But MPI-2 One-sided is an abomination.
  42. BGL_FORALL_VERTICES_T(u, g, Graph) {
  43. put(from_rank, u, (damping * get(from_rank, u) / out_degree(u, g)));
  44. BGL_FORALL_ADJ_T(u, v, g, Graph) {
  45. ret = MPI_Accumulate(&(from_rank[u]),
  46. 1, MPI_DOUBLE,
  47. get(owner, v), local(v),
  48. 1, MPI_DOUBLE, MPI_SUM, to_win);
  49. BOOST_ASSERT(MPI_SUCCESS == ret);
  50. }
  51. }
  52. MPI_Win_fence(0, to_win);
  53. // Set new rank maps for the other map. Do this now to get around
  54. // the stupid synchronization rules of MPI-2 One-sided
  55. BGL_FORALL_VERTICES_T(v, g, Graph) put(from_rank, v, rank_type(1 - damping));
  56. }
  57. #endif
  58. template<typename T>
  59. struct rank_accumulate_reducer {
  60. BOOST_STATIC_CONSTANT(bool, non_default_resolver = true);
  61. template<typename K>
  62. T operator()(const K&) const { return T(0); }
  63. template<typename K>
  64. T operator()(const K&, const T& x, const T& y) const { return x + y; }
  65. };
  66. } // end namespace detail
  67. template<typename Graph, typename RankMap, typename Done, typename RankMap2>
  68. void
  69. page_rank_impl(const Graph& g, RankMap rank_map, Done done,
  70. typename property_traits<RankMap>::value_type damping,
  71. typename graph_traits<Graph>::vertices_size_type n,
  72. RankMap2 rank_map2)
  73. {
  74. typedef typename property_traits<RankMap>::value_type rank_type;
  75. int me;
  76. MPI_Comm_rank(MPI_COMM_WORLD, &me);
  77. typename property_map<Graph, vertex_owner_t>::const_type
  78. owner = get(vertex_owner, g);
  79. (void)owner;
  80. typedef typename boost::graph::parallel::process_group_type<Graph>
  81. ::type process_group_type;
  82. typedef typename process_group_type::process_id_type process_id_type;
  83. process_group_type pg = process_group(g);
  84. process_id_type id = process_id(pg);
  85. BOOST_ASSERT(me == id);
  86. rank_type initial_rank = rank_type(rank_type(1) / n);
  87. BGL_FORALL_VERTICES_T(v, g, Graph) put(rank_map, v, initial_rank);
  88. #ifdef WANT_MPI_ONESIDED
  89. BOOST_ASSERT(sizeof(rank_type) == sizeof(double));
  90. bool to_map_2 = true;
  91. MPI_Win win, win2;
  92. MPI_Win_create(&(rank_map[*(vertices(g).first)]),
  93. sizeof(double) * num_vertices(g),
  94. sizeof(double),
  95. MPI_INFO_NULL, MPI_COMM_WORLD, &win);
  96. MPI_Win_set_name(win, "rank_map_win");
  97. MPI_Win_create(&(rank_map2[*(vertices(g).first)]),
  98. sizeof(double) * num_vertices(g),
  99. sizeof(double),
  100. MPI_INFO_NULL, MPI_COMM_WORLD, &win2);
  101. MPI_Win_set_name(win, "rank_map2_win");
  102. // set initial rank maps for the first iteration...
  103. BGL_FORALL_VERTICES_T(v, g, Graph) put(rank_map2, v, rank_type(1 - damping));
  104. MPI_Win_fence(0, win);
  105. MPI_Win_fence(0, win2);
  106. while ((to_map_2 && !done(rank_map, g)) ||
  107. (!to_map_2 && !done(rank_map2, g))) {
  108. if (to_map_2) {
  109. graph::distributed::detail::page_rank_step(g, rank_map, win2, damping, owner);
  110. to_map_2 = false;
  111. } else {
  112. graph::distributed::detail::page_rank_step(g, rank_map2, win, damping, owner);
  113. to_map_2 = true;
  114. }
  115. }
  116. synchronize(boost::graph::parallel::process_group(g));
  117. MPI_Win_free(&win);
  118. MPI_Win_free(&win2);
  119. #else
  120. // The ranks accumulate after each step.
  121. rank_map.set_reduce(detail::rank_accumulate_reducer<rank_type>());
  122. rank_map2.set_reduce(detail::rank_accumulate_reducer<rank_type>());
  123. rank_map.set_consistency_model(boost::parallel::cm_flush | boost::parallel::cm_reset);
  124. rank_map2.set_consistency_model(boost::parallel::cm_flush | boost::parallel::cm_reset);
  125. bool to_map_2 = true;
  126. while ((to_map_2 && !done(rank_map, g)) ||
  127. (!to_map_2 && !done(rank_map2, g))) {
  128. /**
  129. * PageRank can implemented slightly more efficiently on a
  130. * bidirectional graph than on an incidence graph. However,
  131. * distributed PageRank requires that we have the rank of the
  132. * source vertex available locally, so we force the incidence
  133. * graph implementation, which pushes rank from source to
  134. * target.
  135. */
  136. typedef incidence_graph_tag category;
  137. if (to_map_2) {
  138. graph::detail::page_rank_step(g, rank_map, rank_map2, damping,
  139. category());
  140. to_map_2 = false;
  141. } else {
  142. graph::detail::page_rank_step(g, rank_map2, rank_map, damping,
  143. category());
  144. to_map_2 = true;
  145. }
  146. using boost::graph::parallel::process_group;
  147. synchronize(process_group(g));
  148. }
  149. rank_map.reset();
  150. #endif
  151. if (!to_map_2)
  152. BGL_FORALL_VERTICES_T(v, g, Graph) put(rank_map, v, get(rank_map2, v));
  153. }
  154. template<typename Graph, typename RankMap, typename Done, typename RankMap2>
  155. void
  156. page_rank(const Graph& g, RankMap rank_map, Done done,
  157. typename property_traits<RankMap>::value_type damping,
  158. typename graph_traits<Graph>::vertices_size_type n,
  159. RankMap2 rank_map2
  160. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph, distributed_graph_tag))
  161. {
  162. (page_rank_impl)(g, rank_map, done, damping, n, rank_map2);
  163. }
  164. template<typename MutableGraph>
  165. void
  166. remove_dangling_links(MutableGraph& g
  167. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(MutableGraph,
  168. distributed_graph_tag))
  169. {
  170. typename graph_traits<MutableGraph>::vertices_size_type old_n;
  171. do {
  172. old_n = num_vertices(g);
  173. typename graph_traits<MutableGraph>::vertex_iterator vi, vi_end;
  174. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; /* in loop */) {
  175. typename graph_traits<MutableGraph>::vertex_descriptor v = *vi++;
  176. if (out_degree(v, g) == 0) {
  177. clear_vertex(v, g);
  178. remove_vertex(v, g);
  179. }
  180. }
  181. } while (num_vertices(g) < old_n);
  182. }
  183. } // end namespace distributed
  184. using distributed::page_rank;
  185. using distributed::remove_dangling_links;
  186. } } // end namespace boost::graph
  187. #endif // BOOST_PARALLEL_GRAPH_PAGE_RANK_HPP