distributed_graph_utility.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (C) 2005-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: Peter Gottschling
  6. // Douglas Gregor
  7. // Andrew Lumsdaine
  8. #include <boost/graph/iteration_macros.hpp>
  9. #include <boost/property_map/parallel/global_index_map.hpp>
  10. #ifndef BOOST_GRAPH_DISTRIBUTED_GRAPH_UTILITY_INCLUDE
  11. #define BOOST_GRAPH_DISTRIBUTED_GRAPH_UTILITY_INCLUDE
  12. #ifndef BOOST_GRAPH_USE_MPI
  13. #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
  14. #endif
  15. namespace boost { namespace graph {
  16. template <class Property, class Graph>
  17. void property_on_inedges(Property p, const Graph& g)
  18. {
  19. BGL_FORALL_VERTICES_T(u, g, Graph)
  20. BGL_FORALL_INEDGES_T(u, e, g, Graph)
  21. request(p, e);
  22. synchronize(p);
  23. }
  24. // For reverse graphs
  25. template <class Property, class Graph>
  26. void property_on_outedges(Property p, const Graph& g)
  27. {
  28. BGL_FORALL_VERTICES_T(u, g, Graph)
  29. BGL_FORALL_OUTEDGES_T(u, e, g, Graph)
  30. request(p, e);
  31. synchronize(p);
  32. }
  33. template <class Property, class Graph>
  34. void property_on_successors(Property p, const Graph& g)
  35. {
  36. BGL_FORALL_VERTICES_T(u, g, Graph)
  37. BGL_FORALL_OUTEDGES_T(u, e, g, Graph)
  38. request(p, target(e, g));
  39. synchronize(p);
  40. }
  41. template <class Property, class Graph>
  42. void property_on_predecessors(Property p, const Graph& g)
  43. {
  44. BGL_FORALL_VERTICES_T(u, g, Graph)
  45. BGL_FORALL_INEDGES_T(u, e, g, Graph)
  46. request(p, source(e, g));
  47. synchronize(p);
  48. }
  49. // Like successors and predecessors but saves one synchronize (and a call)
  50. template <class Property, class Graph>
  51. void property_on_adjacents(Property p, const Graph& g)
  52. {
  53. BGL_FORALL_VERTICES_T(u, g, Graph) {
  54. BGL_FORALL_OUTEDGES_T(u, e, g, Graph)
  55. request(p, target(e, g));
  56. BGL_FORALL_INEDGES_T(u, e, g, Graph)
  57. request(p, source(e, g));
  58. }
  59. synchronize(p);
  60. }
  61. template <class PropertyIn, class PropertyOut, class Graph>
  62. void copy_vertex_property(PropertyIn p_in, PropertyOut p_out, Graph& g)
  63. {
  64. BGL_FORALL_VERTICES_T(u, g, Graph)
  65. put(p_out, u, get(p_in, g));
  66. }
  67. template <class PropertyIn, class PropertyOut, class Graph>
  68. void copy_edge_property(PropertyIn p_in, PropertyOut p_out, Graph& g)
  69. {
  70. BGL_FORALL_EDGES_T(e, g, Graph)
  71. put(p_out, e, get(p_in, g));
  72. }
  73. namespace distributed {
  74. // Define global_index<Graph> global(graph);
  75. // Then global(v) returns global index of v
  76. template <typename Graph>
  77. struct global_index
  78. {
  79. typedef typename property_map<Graph, vertex_index_t>::const_type
  80. VertexIndexMap;
  81. typedef typename property_map<Graph, vertex_global_t>::const_type
  82. VertexGlobalMap;
  83. explicit global_index(Graph const& g)
  84. : global_index_map(process_group(g), num_vertices(g), get(vertex_index, g),
  85. get(vertex_global, g)) {}
  86. int operator() (typename graph_traits<Graph>::vertex_descriptor v)
  87. { return get(global_index_map, v); }
  88. protected:
  89. boost::parallel::global_index_map<VertexIndexMap, VertexGlobalMap>
  90. global_index_map;
  91. };
  92. template<typename T>
  93. struct additive_reducer {
  94. BOOST_STATIC_CONSTANT(bool, non_default_resolver = true);
  95. template<typename K>
  96. T operator()(const K&) const { return T(0); }
  97. template<typename K>
  98. T operator()(const K&, const T& local, const T& remote) const { return local + remote; }
  99. };
  100. template <typename T>
  101. struct choose_min_reducer {
  102. BOOST_STATIC_CONSTANT(bool, non_default_resolver = true);
  103. template<typename K>
  104. T operator()(const K&) const { return (std::numeric_limits<T>::max)(); }
  105. template<typename K>
  106. T operator()(const K&, const T& x, const T& y) const
  107. { return x < y ? x : y; }
  108. };
  109. // To use a property map syntactically like a function
  110. template <typename PropertyMap>
  111. struct property_map_reader
  112. {
  113. explicit property_map_reader(PropertyMap pm) : pm(pm) {}
  114. template <typename T>
  115. typename PropertyMap::value_type
  116. operator() (const T& v)
  117. {
  118. return get(pm, v);
  119. }
  120. private:
  121. PropertyMap pm;
  122. };
  123. } // namespace distributed
  124. }} // namespace boost::graph
  125. #endif // BOOST_GRAPH_DISTRIBUTED_GRAPH_UTILITY_INCLUDE