dijkstra_shortest_paths.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright (C) 2004-2006 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. #ifndef BOOST_GRAPH_PARALLEL_DIJKSTRA_HPP
  8. #define BOOST_GRAPH_PARALLEL_DIJKSTRA_HPP
  9. #ifndef BOOST_GRAPH_USE_MPI
  10. #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
  11. #endif
  12. #include <boost/graph/dijkstra_shortest_paths.hpp>
  13. #include <boost/graph/overloading.hpp>
  14. #include <boost/graph/distributed/concepts.hpp>
  15. #include <boost/graph/parallel/properties.hpp>
  16. #include <boost/graph/distributed/crauser_et_al_shortest_paths.hpp>
  17. #include <boost/graph/distributed/eager_dijkstra_shortest_paths.hpp>
  18. namespace boost {
  19. namespace graph { namespace detail {
  20. template<typename Lookahead>
  21. struct parallel_dijkstra_impl2
  22. {
  23. template<typename DistributedGraph, typename DijkstraVisitor,
  24. typename PredecessorMap, typename DistanceMap,
  25. typename WeightMap, typename IndexMap, typename ColorMap,
  26. typename Compare, typename Combine, typename DistInf,
  27. typename DistZero>
  28. static void
  29. run(const DistributedGraph& g,
  30. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  31. PredecessorMap predecessor, DistanceMap distance,
  32. typename property_traits<DistanceMap>::value_type lookahead,
  33. WeightMap weight, IndexMap index_map, ColorMap color_map,
  34. Compare compare, Combine combine, DistInf inf, DistZero zero,
  35. DijkstraVisitor vis)
  36. {
  37. eager_dijkstra_shortest_paths(g, s, predecessor, distance, lookahead,
  38. weight, index_map, color_map, compare,
  39. combine, inf, zero, vis);
  40. }
  41. };
  42. template<>
  43. struct parallel_dijkstra_impl2< ::boost::param_not_found >
  44. {
  45. template<typename DistributedGraph, typename DijkstraVisitor,
  46. typename PredecessorMap, typename DistanceMap,
  47. typename WeightMap, typename IndexMap, typename ColorMap,
  48. typename Compare, typename Combine, typename DistInf,
  49. typename DistZero>
  50. static void
  51. run(const DistributedGraph& g,
  52. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  53. PredecessorMap predecessor, DistanceMap distance,
  54. ::boost::param_not_found,
  55. WeightMap weight, IndexMap index_map, ColorMap color_map,
  56. Compare compare, Combine combine, DistInf inf, DistZero zero,
  57. DijkstraVisitor vis)
  58. {
  59. crauser_et_al_shortest_paths(g, s, predecessor, distance, weight,
  60. index_map, color_map, compare, combine,
  61. inf, zero, vis);
  62. }
  63. };
  64. template<typename ColorMap>
  65. struct parallel_dijkstra_impl
  66. {
  67. template<typename DistributedGraph, typename DijkstraVisitor,
  68. typename PredecessorMap, typename DistanceMap,
  69. typename Lookahead, typename WeightMap, typename IndexMap,
  70. typename Compare, typename Combine,
  71. typename DistInf, typename DistZero>
  72. static void
  73. run(const DistributedGraph& g,
  74. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  75. PredecessorMap predecessor, DistanceMap distance,
  76. Lookahead lookahead,
  77. WeightMap weight, IndexMap index_map, ColorMap color_map,
  78. Compare compare, Combine combine, DistInf inf, DistZero zero,
  79. DijkstraVisitor vis)
  80. {
  81. graph::detail::parallel_dijkstra_impl2<Lookahead>
  82. ::run(g, s, predecessor, distance, lookahead, weight, index_map,
  83. color_map, compare, combine, inf, zero, vis);
  84. }
  85. };
  86. template<>
  87. struct parallel_dijkstra_impl< ::boost::param_not_found >
  88. {
  89. private:
  90. template<typename DistributedGraph, typename DijkstraVisitor,
  91. typename PredecessorMap, typename DistanceMap,
  92. typename Lookahead, typename WeightMap, typename IndexMap,
  93. typename ColorMap, typename Compare, typename Combine,
  94. typename DistInf, typename DistZero>
  95. static void
  96. run_impl(const DistributedGraph& g,
  97. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  98. PredecessorMap predecessor, DistanceMap distance,
  99. Lookahead lookahead, WeightMap weight, IndexMap index_map,
  100. ColorMap color_map, Compare compare, Combine combine,
  101. DistInf inf, DistZero zero, DijkstraVisitor vis)
  102. {
  103. BGL_FORALL_VERTICES_T(u, g, DistributedGraph)
  104. BGL_FORALL_OUTEDGES_T(u, e, g, DistributedGraph)
  105. local_put(color_map, target(e, g), white_color);
  106. graph::detail::parallel_dijkstra_impl2<Lookahead>
  107. ::run(g, s, predecessor, distance, lookahead, weight, index_map,
  108. color_map, compare, combine, inf, zero, vis);
  109. }
  110. public:
  111. template<typename DistributedGraph, typename DijkstraVisitor,
  112. typename PredecessorMap, typename DistanceMap,
  113. typename Lookahead, typename WeightMap, typename IndexMap,
  114. typename Compare, typename Combine,
  115. typename DistInf, typename DistZero>
  116. static void
  117. run(const DistributedGraph& g,
  118. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  119. PredecessorMap predecessor, DistanceMap distance,
  120. Lookahead lookahead, WeightMap weight, IndexMap index_map,
  121. ::boost::param_not_found,
  122. Compare compare, Combine combine, DistInf inf, DistZero zero,
  123. DijkstraVisitor vis)
  124. {
  125. typedef typename graph_traits<DistributedGraph>::vertices_size_type
  126. vertices_size_type;
  127. vertices_size_type n = num_vertices(g);
  128. std::vector<default_color_type> colors(n, white_color);
  129. run_impl(g, s, predecessor, distance, lookahead, weight, index_map,
  130. make_iterator_property_map(colors.begin(), index_map),
  131. compare, combine, inf, zero, vis);
  132. }
  133. };
  134. } } // end namespace graph::detail
  135. /** Dijkstra's single-source shortest paths algorithm for distributed
  136. * graphs.
  137. *
  138. * Also implements the heuristics of:
  139. *
  140. * Andreas Crauser, Kurt Mehlhorn, Ulrich Meyer, and Peter
  141. * Sanders. A Parallelization of Dijkstra's Shortest Path
  142. * Algorithm. In Lubos Brim, Jozef Gruska, and Jiri Zlatuska,
  143. * editors, Mathematical Foundations of Computer Science (MFCS),
  144. * volume 1450 of Lecture Notes in Computer Science, pages
  145. * 722--731, 1998. Springer.
  146. */
  147. template<typename DistributedGraph, typename DijkstraVisitor,
  148. typename PredecessorMap, typename DistanceMap,
  149. typename WeightMap, typename IndexMap, typename Compare,
  150. typename Combine, typename DistInf, typename DistZero,
  151. typename T, typename Tag, typename Base>
  152. inline
  153. void
  154. dijkstra_shortest_paths
  155. (const DistributedGraph& g,
  156. typename graph_traits<DistributedGraph>::vertex_descriptor s,
  157. PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
  158. IndexMap index_map,
  159. Compare compare, Combine combine, DistInf inf, DistZero zero,
  160. DijkstraVisitor vis,
  161. const bgl_named_params<T, Tag, Base>& params
  162. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(DistributedGraph,distributed_graph_tag))
  163. {
  164. typedef typename graph_traits<DistributedGraph>::vertices_size_type
  165. vertices_size_type;
  166. // Build a distributed property map for vertex colors, if we need it
  167. bool use_default_color_map
  168. = is_default_param(get_param(params, vertex_color));
  169. vertices_size_type n = use_default_color_map? num_vertices(g) : 1;
  170. std::vector<default_color_type> color(n, white_color);
  171. typedef iterator_property_map<std::vector<default_color_type>::iterator,
  172. IndexMap> DefColorMap;
  173. DefColorMap color_map(color.begin(), index_map);
  174. typedef typename get_param_type< vertex_color_t, bgl_named_params<T, Tag, Base> >::type color_map_type;
  175. graph::detail::parallel_dijkstra_impl<color_map_type>
  176. ::run(g, s, predecessor, distance,
  177. get_param(params, lookahead_t()),
  178. weight, index_map,
  179. get_param(params, vertex_color),
  180. compare, combine, inf, zero, vis);
  181. }
  182. } // end namespace boost
  183. #endif // BOOST_GRAPH_PARALLEL_DIJKSTRA_HPP