/Src/Dependencies/Boost/boost/graph/distributed/adjlist/redistribute.hpp

http://hadesmem.googlecode.com/ · C++ Header · 393 lines · 257 code · 65 blank · 71 comment · 48 complexity · 37df71f47d8f7e6ce8ea139f7bd195fd MD5 · raw file

  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: Douglas Gregor
  6. // Andrew Lumsdaine
  7. //
  8. // Implements redistribution of vertices for a distributed adjacency
  9. // list. This file should not be included by users. It will be
  10. // included by the distributed adjacency list header.
  11. //
  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. #include <boost/pending/container_traits.hpp>
  16. namespace boost { namespace detail { namespace parallel {
  17. /* This structure contains a (vertex or edge) descriptor that is being
  18. moved from one processor to another. It contains the properties for
  19. that descriptor (if any).
  20. */
  21. template<typename Descriptor, typename DescriptorProperty>
  22. struct redistributed_descriptor : maybe_store_property<DescriptorProperty>
  23. {
  24. typedef maybe_store_property<DescriptorProperty> inherited;
  25. redistributed_descriptor() { }
  26. redistributed_descriptor(const Descriptor& v, const DescriptorProperty& p)
  27. : inherited(p), descriptor(v) { }
  28. Descriptor descriptor;
  29. private:
  30. friend class boost::serialization::access;
  31. template<typename Archiver>
  32. void serialize(Archiver& ar, unsigned int /*version*/)
  33. {
  34. ar & boost::serialization::base_object<inherited>(*this)
  35. & unsafe_serialize(descriptor);
  36. }
  37. };
  38. /* Predicate that returns true if the target has migrated. */
  39. template<typename VertexProcessorMap, typename Graph>
  40. struct target_migrated_t
  41. {
  42. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  43. typedef typename graph_traits<Graph>::edge_descriptor Edge;
  44. target_migrated_t(VertexProcessorMap vertex_to_processor, const Graph& g)
  45. : vertex_to_processor(vertex_to_processor), g(g) { }
  46. bool operator()(Edge e) const
  47. {
  48. typedef global_descriptor<Vertex> DVertex;
  49. processor_id_type owner = get(edge_target_processor_id, g, e);
  50. return get(vertex_to_processor, DVertex(owner, target(e, g))) != owner;
  51. }
  52. private:
  53. VertexProcessorMap vertex_to_processor;
  54. const Graph& g;
  55. };
  56. template<typename VertexProcessorMap, typename Graph>
  57. inline target_migrated_t<VertexProcessorMap, Graph>
  58. target_migrated(VertexProcessorMap vertex_to_processor, const Graph& g)
  59. { return target_migrated_t<VertexProcessorMap, Graph>(vertex_to_processor, g); }
  60. /* Predicate that returns true if the source of an in-edge has migrated. */
  61. template<typename VertexProcessorMap, typename Graph>
  62. struct source_migrated_t
  63. {
  64. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  65. typedef typename graph_traits<Graph>::edge_descriptor Edge;
  66. source_migrated_t(VertexProcessorMap vertex_to_processor, const Graph& g)
  67. : vertex_to_processor(vertex_to_processor), g(g) { }
  68. bool operator()(stored_in_edge<Edge> e) const
  69. {
  70. return get(vertex_to_processor, DVertex(e.source_processor, source(e.e, g)))
  71. != e.source_processor;
  72. }
  73. private:
  74. VertexProcessorMap vertex_to_processor;
  75. const Graph& g;
  76. };
  77. template<typename VertexProcessorMap, typename Graph>
  78. inline source_migrated_t<VertexProcessorMap, Graph>
  79. source_migrated(VertexProcessorMap vertex_to_processor, const Graph& g)
  80. { return source_migrated_t<VertexProcessorMap, Graph>(vertex_to_processor, g); }
  81. /* Predicate that returns true if the target has migrated. */
  82. template<typename VertexProcessorMap, typename Graph>
  83. struct source_or_target_migrated_t
  84. {
  85. typedef typename graph_traits<Graph>::edge_descriptor Edge;
  86. source_or_target_migrated_t(VertexProcessorMap vertex_to_processor,
  87. const Graph& g)
  88. : vertex_to_processor(vertex_to_processor), g(g) { }
  89. bool operator()(Edge e) const
  90. {
  91. return get(vertex_to_processor, source(e, g)) != source(e, g).owner
  92. || get(vertex_to_processor, target(e, g)) != target(e, g).owner;
  93. }
  94. private:
  95. VertexProcessorMap vertex_to_processor;
  96. const Graph& g;
  97. };
  98. template<typename VertexProcessorMap, typename Graph>
  99. inline source_or_target_migrated_t<VertexProcessorMap, Graph>
  100. source_or_target_migrated(VertexProcessorMap vertex_to_processor,
  101. const Graph& g)
  102. {
  103. typedef source_or_target_migrated_t<VertexProcessorMap, Graph> result_type;
  104. return result_type(vertex_to_processor, g);
  105. }
  106. } } // end of namespace detail::parallel
  107. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  108. template<typename VertexProcessorMap>
  109. void
  110. PBGL_DISTRIB_ADJLIST_TYPE
  111. ::request_in_neighbors(vertex_descriptor v,
  112. VertexProcessorMap vertex_to_processor,
  113. bidirectionalS)
  114. {
  115. BGL_FORALL_INEDGES_T(v, e, *this, graph_type)
  116. request(vertex_to_processor, source(e, *this));
  117. }
  118. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  119. template<typename VertexProcessorMap>
  120. void
  121. PBGL_DISTRIB_ADJLIST_TYPE
  122. ::remove_migrated_in_edges(vertex_descriptor v,
  123. VertexProcessorMap vertex_to_processor,
  124. bidirectionalS)
  125. {
  126. graph_detail::erase_if(get(vertex_in_edges, base())[v.local],
  127. source_migrated(vertex_to_processor, base()));
  128. }
  129. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  130. template<typename VertexProcessorMap>
  131. void
  132. PBGL_DISTRIB_ADJLIST_TYPE
  133. ::redistribute(VertexProcessorMap vertex_to_processor)
  134. {
  135. using boost::parallel::inplace_all_to_all;
  136. // When we have stable descriptors, we only move those descriptors
  137. // that actually need to be moved. Otherwise, we essentially have to
  138. // regenerate the entire graph.
  139. const bool has_stable_descriptors =
  140. is_same<typename config_type::vertex_list_selector, listS>::value
  141. || is_same<typename config_type::vertex_list_selector, setS>::value
  142. || is_same<typename config_type::vertex_list_selector, multisetS>::value;
  143. typedef detail::parallel::redistributed_descriptor<vertex_descriptor,
  144. vertex_property_type>
  145. redistributed_vertex;
  146. typedef detail::parallel::redistributed_descriptor<edge_descriptor,
  147. edge_property_type>
  148. redistributed_edge;
  149. typedef std::pair<vertices_size_type, edges_size_type> num_relocated_pair;
  150. vertex_iterator vi, vi_end;
  151. edge_iterator ei, ei_end;
  152. process_group_type pg = process_group();
  153. // Initial synchronization makes sure that we have all of our ducks
  154. // in a row. We don't want any outstanding add/remove messages
  155. // coming in mid-redistribution!
  156. synchronize(process_group_);
  157. // We cannot cope with eviction of ghost cells
  158. vertex_to_processor.set_max_ghost_cells(0);
  159. process_id_type p = num_processes(pg);
  160. // Send vertices and edges to the processor where they will
  161. // actually reside. This requires O(|V| + |E|) communication
  162. std::vector<std::vector<redistributed_vertex> > redistributed_vertices(p);
  163. std::vector<std::vector<redistributed_edge> > redistributed_edges(p);
  164. // Build the sets of relocated vertices for each process and then do
  165. // an all-to-all transfer.
  166. for (boost::tie(vi, vi_end) = vertices(*this); vi != vi_end; ++vi) {
  167. if (!has_stable_descriptors
  168. || get(vertex_to_processor, *vi) != vi->owner) {
  169. redistributed_vertices[get(vertex_to_processor, *vi)]
  170. .push_back(redistributed_vertex(*vi, get(vertex_all_t(), base(),
  171. vi->local)));
  172. }
  173. // When our descriptors are stable, we need to determine which
  174. // adjacent descriptors are stable to determine which edges will
  175. // be removed.
  176. if (has_stable_descriptors) {
  177. BGL_FORALL_OUTEDGES_T(*vi, e, *this, graph_type)
  178. request(vertex_to_processor, target(e, *this));
  179. request_in_neighbors(*vi, vertex_to_processor, directed_selector());
  180. }
  181. }
  182. inplace_all_to_all(pg, redistributed_vertices);
  183. // If we have stable descriptors, we need to know where our neighbor
  184. // vertices are moving.
  185. if (has_stable_descriptors)
  186. synchronize(vertex_to_processor);
  187. // Build the sets of relocated edges for each process and then do
  188. // an all-to-all transfer.
  189. for (boost::tie(ei, ei_end) = edges(*this); ei != ei_end; ++ei) {
  190. vertex_descriptor src = source(*ei, *this);
  191. vertex_descriptor tgt = target(*ei, *this);
  192. if (!has_stable_descriptors
  193. || get(vertex_to_processor, src) != src.owner
  194. || get(vertex_to_processor, tgt) != tgt.owner)
  195. redistributed_edges[get(vertex_to_processor, source(*ei, *this))]
  196. .push_back(redistributed_edge(*ei, get(edge_all_t(), base(),
  197. ei->local)));
  198. }
  199. inplace_all_to_all(pg, redistributed_edges);
  200. // A mapping from old vertex descriptors to new vertex
  201. // descriptors. This is an STL map partly because I'm too lazy to
  202. // build a real property map (which is hard in the general case) but
  203. // also because it won't try to look in the graph itself, because
  204. // the keys are all vertex descriptors that have been invalidated.
  205. std::map<vertex_descriptor, vertex_descriptor> old_to_new_vertex_map;
  206. if (has_stable_descriptors) {
  207. // Clear out all vertices and edges that will have moved. There
  208. // are several stages to this.
  209. // First, eliminate all outgoing edges from the (local) vertices
  210. // that have been moved or whose targets have been moved.
  211. BGL_FORALL_VERTICES_T(v, *this, graph_type) {
  212. if (get(vertex_to_processor, v) != v.owner) {
  213. clear_out_edges(v.local, base());
  214. clear_in_edges_local(v, directed_selector());
  215. } else {
  216. remove_out_edge_if(v.local,
  217. target_migrated(vertex_to_processor, base()),
  218. base());
  219. remove_migrated_in_edges(v, vertex_to_processor, directed_selector());
  220. }
  221. }
  222. // Next, eliminate locally-stored edges that have migrated (for
  223. // undirected graphs).
  224. graph_detail::erase_if(local_edges_,
  225. source_or_target_migrated(vertex_to_processor, *this));
  226. // Eliminate vertices that have migrated
  227. for (boost::tie(vi, vi_end) = vertices(*this); vi != vi_end; /* in loop */) {
  228. if (get(vertex_to_processor, *vi) != vi->owner)
  229. remove_vertex((*vi++).local, base());
  230. else {
  231. // Add the identity relation for vertices that have not migrated
  232. old_to_new_vertex_map[*vi] = *vi;
  233. ++vi;
  234. }
  235. }
  236. } else {
  237. // Clear out the local graph: the entire graph is in transit
  238. clear();
  239. }
  240. // Add the new vertices to the graph. When we do so, update the old
  241. // -> new vertex mapping both locally and for the owner of the "old"
  242. // vertex.
  243. {
  244. typedef std::pair<vertex_descriptor, vertex_descriptor> mapping_pair;
  245. std::vector<std::vector<mapping_pair> > mappings(p);
  246. for (process_id_type src = 0; src < p; ++src) {
  247. for (typename std::vector<redistributed_vertex>::iterator vi =
  248. redistributed_vertices[src].begin();
  249. vi != redistributed_vertices[src].end(); ++vi) {
  250. vertex_descriptor new_vertex =
  251. add_vertex(vi->get_property(), *this);
  252. old_to_new_vertex_map[vi->descriptor] = new_vertex;
  253. mappings[vi->descriptor.owner].push_back(mapping_pair(vi->descriptor,
  254. new_vertex));
  255. }
  256. redistributed_vertices[src].clear();
  257. }
  258. inplace_all_to_all(pg, mappings);
  259. // Add the mappings we were sent into the old->new map.
  260. for (process_id_type src = 0; src < p; ++src)
  261. old_to_new_vertex_map.insert(mappings[src].begin(), mappings[src].end());
  262. }
  263. // Get old->new vertex mappings for all of the vertices we need to
  264. // know about.
  265. // TBD: An optimization here might involve sending the
  266. // request-response pairs without an explicit request step (for
  267. // bidirectional and undirected graphs). However, it may not matter
  268. // all that much given the cost of redistribution.
  269. {
  270. std::vector<std::vector<vertex_descriptor> > vertex_map_requests(p);
  271. std::vector<std::vector<vertex_descriptor> > vertex_map_responses(p);
  272. // We need to know about all of the vertices incident on edges
  273. // that have been relocated to this processor. Tell each processor
  274. // what each other processor needs to know.
  275. for (process_id_type src = 0; src < p; ++src)
  276. for (typename std::vector<redistributed_edge>::iterator ei =
  277. redistributed_edges[src].begin();
  278. ei != redistributed_edges[src].end(); ++ei) {
  279. vertex_descriptor need_vertex = target(ei->descriptor, *this);
  280. if (old_to_new_vertex_map.find(need_vertex)
  281. == old_to_new_vertex_map.end())
  282. {
  283. old_to_new_vertex_map[need_vertex] = need_vertex;
  284. vertex_map_requests[need_vertex.owner].push_back(need_vertex);
  285. }
  286. }
  287. inplace_all_to_all(pg,
  288. vertex_map_requests,
  289. vertex_map_responses);
  290. // Process the requests made for vertices we own. Then perform yet
  291. // another all-to-all swap. This one matches the requests we've
  292. // made to the responses we were given.
  293. for (process_id_type src = 0; src < p; ++src)
  294. for (typename std::vector<vertex_descriptor>::iterator vi =
  295. vertex_map_responses[src].begin();
  296. vi != vertex_map_responses[src].end(); ++vi)
  297. *vi = old_to_new_vertex_map[*vi];
  298. inplace_all_to_all(pg, vertex_map_responses);
  299. // Matching the requests to the responses, update the old->new
  300. // vertex map for all of the vertices we will need to know.
  301. for (process_id_type src = 0; src < p; ++src) {
  302. typedef typename std::vector<vertex_descriptor>::size_type size_type;
  303. for (size_type i = 0; i < vertex_map_requests[src].size(); ++i) {
  304. old_to_new_vertex_map[vertex_map_requests[src][i]] =
  305. vertex_map_responses[src][i];
  306. }
  307. }
  308. }
  309. // Add edges to the graph by mapping the source and target.
  310. for (process_id_type src = 0; src < p; ++src) {
  311. for (typename std::vector<redistributed_edge>::iterator ei =
  312. redistributed_edges[src].begin();
  313. ei != redistributed_edges[src].end(); ++ei) {
  314. add_edge(old_to_new_vertex_map[source(ei->descriptor, *this)],
  315. old_to_new_vertex_map[target(ei->descriptor, *this)],
  316. ei->get_property(),
  317. *this);
  318. }
  319. redistributed_edges[src].clear();
  320. }
  321. // Be sure that edge-addition messages are received now, completing
  322. // the graph.
  323. synchronize(process_group_);
  324. this->distribution().clear();
  325. detail::parallel::maybe_initialize_vertex_indices(vertices(base()),
  326. get(vertex_index, base()));
  327. }
  328. } // end namespace boost