connected_components.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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: Nick Edmonds
  6. // Douglas Gregor
  7. // Andrew Lumsdaine
  8. #ifndef BOOST_GRAPH_PARALLEL_CC_HPP
  9. #define BOOST_GRAPH_PARALLEL_CC_HPP
  10. #ifndef BOOST_GRAPH_USE_MPI
  11. #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
  12. #endif
  13. #include <boost/detail/is_sorted.hpp>
  14. #include <boost/assert.hpp>
  15. #include <boost/property_map/property_map.hpp>
  16. #include <boost/property_map/parallel/caching_property_map.hpp>
  17. #include <boost/graph/parallel/algorithm.hpp>
  18. #include <boost/pending/indirect_cmp.hpp>
  19. #include <boost/graph/graph_traits.hpp>
  20. #include <boost/graph/overloading.hpp>
  21. #include <boost/graph/distributed/concepts.hpp>
  22. #include <boost/graph/parallel/properties.hpp>
  23. #include <boost/graph/distributed/local_subgraph.hpp>
  24. #include <boost/graph/connected_components.hpp>
  25. #include <boost/graph/named_function_params.hpp>
  26. #include <boost/graph/parallel/process_group.hpp>
  27. #include <boost/optional.hpp>
  28. #include <functional>
  29. #include <algorithm>
  30. #include <vector>
  31. #include <list>
  32. #include <boost/graph/parallel/container_traits.hpp>
  33. #include <boost/graph/iteration_macros.hpp>
  34. #define PBGL_IN_PLACE_MERGE /* In place merge instead of sorting */
  35. //#define PBGL_SORT_ASSERT /* Assert sorted for in place merge */
  36. /* Explicit sychronization in pointer doubling step? */
  37. #define PBGL_EXPLICIT_SYNCH
  38. //#define PBGL_CONSTRUCT_METAGRAPH
  39. #ifdef PBGL_CONSTRUCT_METAGRAPH
  40. # define MAX_VERTICES_IN_METAGRAPH 10000
  41. #endif
  42. namespace boost { namespace graph { namespace distributed {
  43. namespace cc_detail {
  44. enum connected_components_message {
  45. edges_msg, req_parents_msg, parents_msg, root_adj_msg
  46. };
  47. template <typename Vertex>
  48. struct metaVertex {
  49. metaVertex() {}
  50. metaVertex(const Vertex& v) : name(v) {}
  51. template<typename Archiver>
  52. void serialize(Archiver& ar, const unsigned int /*version*/)
  53. {
  54. ar & name;
  55. }
  56. Vertex name;
  57. };
  58. #ifdef PBGL_CONSTRUCT_METAGRAPH
  59. // Build meta-graph on result of local connected components
  60. template <typename Graph, typename ParentMap, typename RootIterator,
  61. typename AdjacencyMap>
  62. void
  63. build_local_metagraph(const Graph& g, ParentMap p, RootIterator r,
  64. RootIterator r_end, AdjacencyMap& adj)
  65. {
  66. // TODO: Static assert that AdjacencyMap::value_type is std::vector<vertex_descriptor>
  67. typedef typename boost::graph::parallel::process_group_type<Graph>::type
  68. process_group_type;
  69. typedef typename process_group_type::process_id_type process_id_type;
  70. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  71. BOOST_STATIC_ASSERT((is_same<typename AdjacencyMap::mapped_type,
  72. std::vector<vertex_descriptor> >::value));
  73. using boost::graph::parallel::process_group;
  74. process_group_type pg = process_group(g);
  75. process_id_type id = process_id(pg);
  76. if (id != 0) {
  77. // Send component roots and their associated edges to P0
  78. for ( ; r != r_end; ++r ) {
  79. std::vector<vertex_descriptor> adjs(1, *r); // Root
  80. adjs.reserve(adjs.size() + adj[*r].size());
  81. for (typename std::vector<vertex_descriptor>::iterator iter = adj[*r].begin();
  82. iter != adj[*r].end(); ++iter)
  83. adjs.push_back(get(p, *iter)); // Adjacencies
  84. send(pg, 0, root_adj_msg, adjs);
  85. }
  86. }
  87. synchronize(pg);
  88. if (id == 0) {
  89. typedef metaVertex<vertex_descriptor> VertexProperties;
  90. typedef boost::adjacency_list<vecS, vecS, undirectedS,
  91. VertexProperties> metaGraph;
  92. typedef typename graph_traits<metaGraph>::vertex_descriptor
  93. meta_vertex_descriptor;
  94. std::map<vertex_descriptor, meta_vertex_descriptor> vertex_map;
  95. std::vector<std::pair<vertex_descriptor, vertex_descriptor> > edges;
  96. // Receive remote roots and edges
  97. while (optional<std::pair<process_id_type, int> > m = probe(pg)) {
  98. BOOST_ASSERT(m->second == root_adj_msg);
  99. std::vector<vertex_descriptor> adjs;
  100. receive(pg, m->first, m->second, adjs);
  101. vertex_map[adjs[0]] = graph_traits<metaGraph>::null_vertex();
  102. for (typename std::vector<vertex_descriptor>::iterator iter
  103. = ++adjs.begin(); iter != adjs.end(); ++iter)
  104. edges.push_back(std::make_pair(adjs[0], *iter));
  105. }
  106. // Add local roots and edges
  107. for ( ; r != r_end; ++r ) {
  108. vertex_map[*r] = graph_traits<metaGraph>::null_vertex();
  109. edges.reserve(edges.size() + adj[*r].size());
  110. for (typename std::vector<vertex_descriptor>::iterator iter = adj[*r].begin();
  111. iter != adj[*r].end(); ++iter)
  112. edges.push_back(std::make_pair(*r, get(p, *iter)));
  113. }
  114. // Build local meta-graph
  115. metaGraph mg;
  116. // Add vertices with property to map back to distributed graph vertex
  117. for (typename std::map<vertex_descriptor, meta_vertex_descriptor>::iterator
  118. iter = vertex_map.begin(); iter != vertex_map.end(); ++iter)
  119. vertex_map[iter->first]
  120. = add_vertex(metaVertex<vertex_descriptor>(iter->first), mg);
  121. // Build meta-vertex map
  122. typename property_map<metaGraph, vertex_descriptor VertexProperties::*>::type
  123. metaVertexMap = get(&VertexProperties::name, mg);
  124. typename std::vector<std::pair<vertex_descriptor, vertex_descriptor> >
  125. ::iterator edge_iter = edges.begin();
  126. for ( ; edge_iter != edges.end(); ++edge_iter)
  127. add_edge(vertex_map[edge_iter->first], vertex_map[edge_iter->second], mg);
  128. edges.clear();
  129. // Call connected_components on it
  130. typedef typename property_map<metaGraph, vertex_index_t>::type
  131. meta_index_map_type;
  132. meta_index_map_type meta_index = get(vertex_index, mg);
  133. std::vector<std::size_t> mg_component_vec(num_vertices(mg));
  134. typedef iterator_property_map<std::vector<std::size_t>::iterator,
  135. meta_index_map_type>
  136. meta_components_map_type;
  137. meta_components_map_type mg_component(mg_component_vec.begin(),
  138. meta_index);
  139. std::size_t num_comp = connected_components(mg, mg_component);
  140. // Update Parent pointers
  141. std::vector<meta_vertex_descriptor> roots(num_comp, graph_traits<metaGraph>::null_vertex());
  142. BGL_FORALL_VERTICES_T(v, mg, metaGraph) {
  143. size_t component = get(mg_component, v);
  144. if (roots[component] == graph_traits<metaGraph>::null_vertex() ||
  145. get(meta_index, v) < get(meta_index, roots[component]))
  146. roots[component] = v;
  147. }
  148. // Set all the local parent pointers
  149. BGL_FORALL_VERTICES_T(v, mg, metaGraph) {
  150. // Problem in value being put (3rd parameter)
  151. put(p, get(metaVertexMap, v), get(metaVertexMap, roots[get(mg_component, v)]));
  152. }
  153. }
  154. synchronize(p);
  155. }
  156. #endif
  157. /* Function object used to remove internal vertices and vertices >
  158. the current vertex from the adjacent vertex lists at each
  159. root */
  160. template <typename Vertex, typename ParentMap>
  161. class cull_adjacency_list
  162. {
  163. public:
  164. cull_adjacency_list(const Vertex v, const ParentMap p) : v(v), p(p) {}
  165. bool operator() (const Vertex x) { return (get(p, x) == v || x == v); }
  166. private:
  167. const Vertex v;
  168. const ParentMap p;
  169. };
  170. /* Comparison operator used to choose targets for hooking s.t. vertices
  171. that are hooked to are evenly distributed across processors */
  172. template <typename OwnerMap, typename LocalMap>
  173. class hashed_vertex_compare
  174. {
  175. public:
  176. hashed_vertex_compare (const OwnerMap& o, const LocalMap& l)
  177. : owner(o), local(l) { }
  178. template <typename Vertex>
  179. bool operator() (const Vertex x, const Vertex y)
  180. {
  181. if (get(local, x) < get(local, y))
  182. return true;
  183. else if (get(local, x) == get(local, y))
  184. return (get(owner, x) < get(owner, y));
  185. return false;
  186. }
  187. private:
  188. OwnerMap owner;
  189. LocalMap local;
  190. };
  191. #ifdef PBGL_EXPLICIT_SYNCH
  192. template <typename Graph, typename ParentMap, typename VertexList>
  193. void
  194. request_parent_map_entries(const Graph& g, ParentMap p,
  195. std::vector<VertexList>& parent_requests)
  196. {
  197. typedef typename boost::graph::parallel::process_group_type<Graph>
  198. ::type process_group_type;
  199. typedef typename process_group_type::process_id_type process_id_type;
  200. typedef typename graph_traits<Graph>::vertex_descriptor
  201. vertex_descriptor;
  202. process_group_type pg = process_group(g);
  203. /*
  204. This should probably be send_oob_with_reply, especially when Dave
  205. finishes prefetch-batching
  206. */
  207. // Send root requests
  208. for (process_id_type i = 0; i < num_processes(pg); ++i) {
  209. if (!parent_requests[i].empty()) {
  210. std::vector<vertex_descriptor> reqs(parent_requests[i].begin(),
  211. parent_requests[i].end());
  212. send(pg, i, req_parents_msg, reqs);
  213. }
  214. }
  215. synchronize(pg);
  216. // Receive root requests and reply to them
  217. while (optional<std::pair<process_id_type, int> > m = probe(pg)) {
  218. std::vector<vertex_descriptor> requests;
  219. receive(pg, m->first, m->second, requests);
  220. for (std::size_t i = 0; i < requests.size(); ++i)
  221. requests[i] = get(p, requests[i]);
  222. send(pg, m->first, parents_msg, requests);
  223. }
  224. synchronize(pg);
  225. // Receive requested parents
  226. std::vector<vertex_descriptor> responses;
  227. for (process_id_type i = 0; i < num_processes(pg); ++i) {
  228. if (!parent_requests[i].empty()) {
  229. receive(pg, i, parents_msg, responses);
  230. std::size_t parent_idx = 0;
  231. for (typename VertexList::iterator v = parent_requests[i].begin();
  232. v != parent_requests[i].end(); ++v, ++parent_idx)
  233. put(p, *v, responses[parent_idx]);
  234. }
  235. }
  236. }
  237. #endif
  238. template<typename DistributedGraph, typename ParentMap>
  239. void
  240. parallel_connected_components(DistributedGraph& g, ParentMap p)
  241. {
  242. using boost::connected_components;
  243. typedef typename graph_traits<DistributedGraph>::adjacency_iterator
  244. adjacency_iterator;
  245. typedef typename graph_traits<DistributedGraph>::vertex_descriptor
  246. vertex_descriptor;
  247. typedef typename boost::graph::parallel::process_group_type<DistributedGraph>
  248. ::type process_group_type;
  249. typedef typename process_group_type::process_id_type process_id_type;
  250. using boost::graph::parallel::process_group;
  251. process_group_type pg = process_group(g);
  252. process_id_type id = process_id(pg);
  253. // TODO (NGE): Should old_roots, roots, and completed_roots be std::list
  254. adjacency_iterator av1, av2;
  255. std::vector<vertex_descriptor> old_roots;
  256. typename std::vector<vertex_descriptor>::iterator liter;
  257. typename std::vector<vertex_descriptor>::iterator aliter;
  258. typename std::map<vertex_descriptor,
  259. std::vector<vertex_descriptor> > adj;
  260. typedef typename property_map<DistributedGraph, vertex_owner_t>::const_type
  261. OwnerMap;
  262. OwnerMap owner = get(vertex_owner, g);
  263. typedef typename property_map<DistributedGraph, vertex_local_t>::const_type
  264. LocalMap;
  265. LocalMap local = get(vertex_local, g);
  266. // We need to hold on to all of the parent pointers
  267. p.set_max_ghost_cells(0);
  268. //
  269. // STAGE 1 : Compute local components
  270. //
  271. local_subgraph<const DistributedGraph> ls(g);
  272. typedef typename property_map<local_subgraph<const DistributedGraph>,
  273. vertex_index_t>::type local_index_map_type;
  274. local_index_map_type local_index = get(vertex_index, ls);
  275. // Compute local connected components
  276. std::vector<std::size_t> ls_components_vec(num_vertices(ls));
  277. typedef iterator_property_map<std::vector<std::size_t>::iterator,
  278. local_index_map_type>
  279. ls_components_map_type;
  280. ls_components_map_type ls_component(ls_components_vec.begin(),
  281. local_index);
  282. std::size_t num_comp = connected_components(ls, ls_component);
  283. std::vector<vertex_descriptor>
  284. roots(num_comp, graph_traits<DistributedGraph>::null_vertex());
  285. BGL_FORALL_VERTICES_T(v, g, DistributedGraph) {
  286. size_t component = get(ls_component, v);
  287. if (roots[component] == graph_traits<DistributedGraph>::null_vertex() ||
  288. get(local_index, v) < get(local_index, roots[component]))
  289. roots[component] = v;
  290. }
  291. // Set all the local parent pointers
  292. BGL_FORALL_VERTICES_T(v, g, DistributedGraph) {
  293. put(p, v, roots[get(ls_component, v)]);
  294. }
  295. if (num_processes(pg) == 1) return;
  296. // Build adjacency list for all roots
  297. BGL_FORALL_VERTICES_T(v, g, DistributedGraph) {
  298. std::vector<vertex_descriptor>& my_adj = adj[get(p, v)];
  299. for (boost::tie(av1, av2) = adjacent_vertices(v, g);
  300. av1 != av2; ++av1) {
  301. if (get(owner, *av1) != id) my_adj.push_back(*av1);
  302. }
  303. }
  304. // For all vertices adjacent to a local vertex get p(v)
  305. for ( liter = roots.begin(); liter != roots.end(); ++liter ) {
  306. std::vector<vertex_descriptor>& my_adj = adj[*liter];
  307. for ( aliter = my_adj.begin(); aliter != my_adj.end(); ++aliter )
  308. request(p, *aliter);
  309. }
  310. synchronize(p);
  311. // Update adjacency list at root to make sure all adjacent
  312. // vertices are roots of remote components
  313. for ( liter = roots.begin(); liter != roots.end(); ++liter )
  314. {
  315. std::vector<vertex_descriptor>& my_adj = adj[*liter];
  316. for ( aliter = my_adj.begin(); aliter != my_adj.end(); ++aliter )
  317. *aliter = get(p, *aliter);
  318. my_adj.erase
  319. (std::remove_if(my_adj.begin(), my_adj.end(),
  320. cull_adjacency_list<vertex_descriptor,
  321. ParentMap>(*liter, p) ),
  322. my_adj.end());
  323. // This sort needs to be here to make sure the initial
  324. // adjacency list is sorted
  325. std::sort(my_adj.begin(), my_adj.end(), std::less<vertex_descriptor>());
  326. my_adj.erase(std::unique(my_adj.begin(), my_adj.end()), my_adj.end());
  327. }
  328. // Get p(v) for the new adjacent roots
  329. p.clear();
  330. for ( liter = roots.begin(); liter != roots.end(); ++liter ) {
  331. std::vector<vertex_descriptor>& my_adj = adj[*liter];
  332. for ( aliter = my_adj.begin(); aliter != my_adj.end(); ++aliter )
  333. request(p, *aliter);
  334. }
  335. #ifdef PBGL_EXPLICIT_SYNCH
  336. synchronize(p);
  337. #endif
  338. // Lastly, remove roots with no adjacent vertices, this is
  339. // unnecessary but will speed up sparse graphs
  340. for ( liter = roots.begin(); liter != roots.end(); /*in loop*/)
  341. {
  342. if ( adj[*liter].empty() )
  343. liter = roots.erase(liter);
  344. else
  345. ++liter;
  346. }
  347. #ifdef PBGL_CONSTRUCT_METAGRAPH
  348. /* TODO: If the number of roots is sufficiently small, we can
  349. use a 'problem folding' approach like we do in MST
  350. to gather all the roots and their adjacencies on one proc
  351. and solve for the connected components of the meta-graph */
  352. using boost::parallel::all_reduce;
  353. std::size_t num_roots = all_reduce(pg, roots.size(), std::plus<std::size_t>());
  354. if (num_roots < MAX_VERTICES_IN_METAGRAPH) {
  355. build_local_metagraph(g, p, roots.begin(), roots.end(), adj);
  356. // For each vertex in g, p(v) = p(p(v)), assign parent of leaf
  357. // vertices from first step to final parent
  358. BGL_FORALL_VERTICES_T(v, g, DistributedGraph) {
  359. put(p, v, get(p, get(p, v)));
  360. }
  361. synchronize(p);
  362. return;
  363. }
  364. #endif
  365. //
  366. // Parallel Phase
  367. //
  368. std::vector<vertex_descriptor> completed_roots;
  369. hashed_vertex_compare<OwnerMap, LocalMap> v_compare(owner, local);
  370. bool any_hooked;
  371. vertex_descriptor new_root;
  372. std::size_t steps = 0;
  373. do {
  374. ++steps;
  375. // Pull in new parents for hooking phase
  376. synchronize(p);
  377. //
  378. // Hooking
  379. //
  380. bool hooked = false;
  381. completed_roots.clear();
  382. for ( liter = roots.begin(); liter != roots.end(); )
  383. {
  384. new_root = graph_traits<DistributedGraph>::null_vertex();
  385. std::vector<vertex_descriptor>& my_adj = adj[*liter];
  386. for ( aliter = my_adj.begin(); aliter != my_adj.end(); ++aliter )
  387. // try to hook to better adjacent vertex
  388. if ( v_compare( get(p, *aliter), *liter ) )
  389. new_root = get(p, *aliter);
  390. if ( new_root != graph_traits<DistributedGraph>::null_vertex() )
  391. {
  392. hooked = true;
  393. put(p, *liter, new_root);
  394. old_roots.push_back(*liter);
  395. completed_roots.push_back(*liter);
  396. liter = roots.erase(liter);
  397. }
  398. else
  399. ++liter;
  400. }
  401. //
  402. // Pointer jumping, perform until new roots determined
  403. //
  404. // TODO: Implement cycle reduction rules to reduce this from
  405. // O(n) to O(log n) [n = cycle length]
  406. bool all_done;
  407. std::size_t parent_root_count;
  408. std::size_t double_steps = 0;
  409. do {
  410. ++double_steps;
  411. #ifndef PBGL_EXPLICIT_SYNCH
  412. // Get p(p(v)) for all old roots, and p(v) for all current roots
  413. for ( liter = old_roots.begin(); liter != old_roots.end(); ++liter )
  414. request(p, get(p, *liter));
  415. synchronize(p);
  416. #else
  417. // Build root requests
  418. typedef std::set<vertex_descriptor> VertexSet;
  419. std::vector<VertexSet> parent_requests(num_processes(pg));
  420. for ( liter = old_roots.begin(); liter != old_roots.end(); ++liter )
  421. {
  422. vertex_descriptor p1 = *liter;
  423. if (get(owner, p1) != id) parent_requests[get(owner, p1)].insert(p1);
  424. vertex_descriptor p2 = get(p, p1);
  425. if (get(owner, p2) != id) parent_requests[get(owner, p2)].insert(p2);
  426. }
  427. request_parent_map_entries(g, p, parent_requests);
  428. #endif
  429. // Perform a pointer jumping step on all old roots
  430. for ( liter = old_roots.begin(); liter != old_roots.end(); ++liter )
  431. put(p, *liter, get(p, get(p, *liter)));
  432. // make sure the parent of all old roots is itself a root
  433. parent_root_count = 0;
  434. for ( liter = old_roots.begin(); liter != old_roots.end(); ++liter )
  435. if ( get(p, *liter) == get(p, get(p, *liter)) )
  436. parent_root_count++;
  437. bool done = parent_root_count == old_roots.size();
  438. all_reduce(pg, &done, &done+1, &all_done,
  439. std::logical_and<bool>());
  440. } while ( !all_done );
  441. #ifdef PARALLEL_BGL_DEBUG
  442. if (id == 0) std::cerr << double_steps << " doubling steps.\n";
  443. #endif
  444. //
  445. // Add adjacent vertices of just completed roots to adjacent
  446. // vertex list at new parent
  447. //
  448. typename std::vector<vertex_descriptor> outgoing_edges;
  449. for ( liter = completed_roots.begin(); liter != completed_roots.end();
  450. ++liter )
  451. {
  452. vertex_descriptor new_parent = get(p, *liter);
  453. if ( get(owner, new_parent) == id )
  454. {
  455. std::vector<vertex_descriptor>& my_adj = adj[new_parent];
  456. my_adj.reserve(my_adj.size() + adj[*liter].size());
  457. my_adj.insert( my_adj.end(),
  458. adj[*liter].begin(), adj[*liter].end() );
  459. #ifdef PBGL_IN_PLACE_MERGE
  460. #ifdef PBGL_SORT_ASSERT
  461. BOOST_ASSERT(::boost::detail::is_sorted(my_adj.begin(),
  462. my_adj.end() - adj[*liter].size(),
  463. std::less<vertex_descriptor>()));
  464. BOOST_ASSERT(::boost::detail::is_sorted(my_adj.end() - adj[*liter].size(),
  465. my_adj.end(),
  466. std::less<vertex_descriptor>()));
  467. #endif
  468. std::inplace_merge(my_adj.begin(),
  469. my_adj.end() - adj[*liter].size(),
  470. my_adj.end(),
  471. std::less<vertex_descriptor>());
  472. #endif
  473. }
  474. else if ( adj[*liter].begin() != adj[*liter].end() )
  475. {
  476. outgoing_edges.clear();
  477. outgoing_edges.reserve(adj[*liter].size() + 1);
  478. // First element is the destination of the adjacency list
  479. outgoing_edges.push_back(new_parent);
  480. outgoing_edges.insert(outgoing_edges.end(),
  481. adj[*liter].begin(), adj[*liter].end() );
  482. send(pg, get(owner, new_parent), edges_msg, outgoing_edges);
  483. adj[*liter].clear();
  484. }
  485. }
  486. synchronize(pg);
  487. // Receive edges sent by remote nodes and add them to the
  488. // indicated vertex's adjacency list
  489. while (optional<std::pair<process_id_type, int> > m
  490. = probe(pg))
  491. {
  492. std::vector<vertex_descriptor> incoming_edges;
  493. receive(pg, m->first, edges_msg, incoming_edges);
  494. typename std::vector<vertex_descriptor>::iterator aviter
  495. = incoming_edges.begin();
  496. ++aviter;
  497. std::vector<vertex_descriptor>& my_adj = adj[incoming_edges[0]];
  498. my_adj.reserve(my_adj.size() + incoming_edges.size() - 1);
  499. my_adj.insert( my_adj.end(), aviter, incoming_edges.end() );
  500. #ifdef PBGL_IN_PLACE_MERGE
  501. std::size_t num_incoming_edges = incoming_edges.size();
  502. #ifdef PBGL_SORT_ASSERT
  503. BOOST_ASSERT(::boost::detail::is_sorted(my_adj.begin(),
  504. my_adj.end() - (num_incoming_edges-1),
  505. std::less<vertex_descriptor>()));
  506. BOOST_ASSERT(::boost::detail::is_sorted(my_adj.end() - (num_incoming_edges-1),
  507. my_adj.end(),
  508. std::less<vertex_descriptor>()));
  509. #endif
  510. std::inplace_merge(my_adj.begin(),
  511. my_adj.end() - (num_incoming_edges - 1),
  512. my_adj.end(),
  513. std::less<vertex_descriptor>());
  514. #endif
  515. }
  516. // Remove any adjacent vertices that are in the same component
  517. // as a root from that root's list
  518. for ( liter = roots.begin(); liter != roots.end(); ++liter )
  519. {
  520. // We can probably get away without sorting and removing
  521. // duplicates Though sorting *may* cause root
  522. // determination to occur faster by choosing the root with
  523. // the most potential to hook to at each step
  524. std::vector<vertex_descriptor>& my_adj = adj[*liter];
  525. my_adj.erase
  526. (std::remove_if(my_adj.begin(), my_adj.end(),
  527. cull_adjacency_list<vertex_descriptor,
  528. ParentMap>(*liter, p) ),
  529. my_adj.end());
  530. #ifndef PBGL_IN_PLACE_MERGE
  531. std::sort(my_adj.begin(), my_adj.end(),
  532. std::less<vertex_descriptor>() );
  533. #endif
  534. my_adj.erase(std::unique(my_adj.begin(), my_adj.end()), my_adj.end());
  535. }
  536. // Reduce result of empty root list test
  537. all_reduce(pg, &hooked, &hooked+1, &any_hooked,
  538. std::logical_or<bool>());
  539. } while ( any_hooked );
  540. #ifdef PARALLEL_BGL_DEBUG
  541. if (id == 0) std::cerr << steps << " iterations.\n";
  542. #endif
  543. //
  544. // Finalize
  545. //
  546. // For each vertex in g, p(v) = p(p(v)), assign parent of leaf
  547. // vertices from first step to final parent
  548. BGL_FORALL_VERTICES_T(v, g, DistributedGraph) {
  549. put(p, v, get(p, get(p, v)));
  550. }
  551. synchronize(p);
  552. }
  553. } // end namespace cc_detail
  554. template<typename Graph, typename ParentMap, typename ComponentMap>
  555. typename property_traits<ComponentMap>::value_type
  556. number_components_from_parents(const Graph& g, ParentMap p, ComponentMap c)
  557. {
  558. typedef typename graph_traits<Graph>::vertex_descriptor
  559. vertex_descriptor;
  560. typedef typename boost::graph::parallel::process_group_type<Graph>::type
  561. process_group_type;
  562. typedef typename property_traits<ComponentMap>::value_type
  563. ComponentMapType;
  564. process_group_type pg = process_group(g);
  565. /* Build list of roots */
  566. std::vector<vertex_descriptor> my_roots, all_roots;
  567. BGL_FORALL_VERTICES_T(v, g, Graph) {
  568. if( std::find( my_roots.begin(), my_roots.end(), get(p, v) )
  569. == my_roots.end() )
  570. my_roots.push_back( get(p, v) );
  571. }
  572. all_gather(pg, my_roots.begin(), my_roots.end(), all_roots);
  573. /* Number components */
  574. std::map<vertex_descriptor, ComponentMapType> comp_numbers;
  575. ComponentMapType c_num = 0;
  576. // Compute component numbers
  577. for (std::size_t i = 0; i < all_roots.size(); i++ )
  578. if ( comp_numbers.count(all_roots[i]) == 0 )
  579. comp_numbers[all_roots[i]] = c_num++;
  580. // Broadcast component numbers
  581. BGL_FORALL_VERTICES_T(v, g, Graph) {
  582. put( c, v, comp_numbers[get(p, v)] );
  583. }
  584. // Broadcast number of components
  585. if (process_id(pg) == 0) {
  586. typedef typename process_group_type::process_size_type
  587. process_size_type;
  588. for (process_size_type dest = 1, n = num_processes(pg);
  589. dest != n; ++dest)
  590. send(pg, dest, 0, c_num);
  591. }
  592. synchronize(pg);
  593. if (process_id(pg) != 0) receive(pg, 0, 0, c_num);
  594. synchronize(c);
  595. return c_num;
  596. }
  597. template<typename Graph, typename ParentMap>
  598. int
  599. number_components_from_parents(const Graph& g, ParentMap p,
  600. dummy_property_map)
  601. {
  602. using boost::parallel::all_reduce;
  603. // Count local roots.
  604. int num_roots = 0;
  605. BGL_FORALL_VERTICES_T(v, g, Graph)
  606. if (get(p, v) == v) ++num_roots;
  607. return all_reduce(g.process_group(), num_roots, std::plus<int>());
  608. }
  609. template<typename Graph, typename ComponentMap, typename ParentMap>
  610. typename property_traits<ComponentMap>::value_type
  611. connected_components
  612. (const Graph& g, ComponentMap c, ParentMap p
  613. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph, distributed_graph_tag))
  614. {
  615. cc_detail::parallel_connected_components(g, p);
  616. return number_components_from_parents(g, p, c);
  617. }
  618. /* Construct ParentMap by default */
  619. template<typename Graph, typename ComponentMap>
  620. typename property_traits<ComponentMap>::value_type
  621. connected_components
  622. ( const Graph& g, ComponentMap c
  623. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph, distributed_graph_tag) )
  624. {
  625. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  626. std::vector<vertex_descriptor> x(num_vertices(g));
  627. return connected_components
  628. (g, c,
  629. make_iterator_property_map(x.begin(), get(vertex_index, g)));
  630. }
  631. } // end namespace distributed
  632. using distributed::connected_components;
  633. } // end namespace graph
  634. using graph::distributed::connected_components;
  635. } // end namespace boost
  636. #endif // BOOST_GRAPH_PARALLEL_CC_HPP