PageRenderTime 68ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/Src/Dependencies/Boost/boost/graph/distributed/adjacency_list.hpp

http://hadesmem.googlecode.com/
C++ Header | 3970 lines | 2664 code | 533 blank | 773 comment | 189 complexity | 3c4b75ee66cb03622c873a03e54057c9 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0, Apache-2.0, LGPL-3.0
  1. // Copyright (C) 2004-2006 The Trustees of Indiana University.
  2. // Copyright (C) 2007 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. #ifndef BOOST_GRAPH_DISTRIBUTED_ADJACENCY_LIST_HPP
  9. #define BOOST_GRAPH_DISTRIBUTED_ADJACENCY_LIST_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/graph/adjacency_list.hpp>
  14. #include <boost/graph/properties.hpp>
  15. #include <boost/graph/graph_traits.hpp>
  16. #include <boost/graph/iteration_macros.hpp>
  17. #include <boost/graph/distributed/concepts.hpp>
  18. #include <boost/iterator/transform_iterator.hpp>
  19. #include <boost/property_map/property_map.hpp>
  20. #include <boost/graph/adjacency_iterator.hpp>
  21. #include <boost/property_map/parallel/distributed_property_map.hpp>
  22. #include <boost/property_map/parallel/local_property_map.hpp>
  23. #include <boost/graph/parallel/detail/property_holders.hpp>
  24. #include <boost/mpl/if.hpp>
  25. #include <boost/type_traits/is_same.hpp>
  26. #include <boost/assert.hpp>
  27. #include <list>
  28. #include <algorithm>
  29. #include <boost/limits.hpp>
  30. #include <boost/graph/parallel/properties.hpp>
  31. #include <boost/graph/parallel/distribution.hpp>
  32. #include <boost/graph/parallel/algorithm.hpp>
  33. #include <boost/graph/distributed/selector.hpp>
  34. #include <boost/graph/parallel/process_group.hpp>
  35. // Callbacks
  36. #include <boost/function/function2.hpp>
  37. // Serialization
  38. #include <boost/serialization/base_object.hpp>
  39. #include <boost/mpi/datatype.hpp>
  40. #include <boost/pending/property_serialize.hpp>
  41. #include <boost/graph/distributed/unsafe_serialize.hpp>
  42. // Named vertices
  43. #include <boost/graph/distributed/named_graph.hpp>
  44. #include <boost/graph/distributed/shuffled_distribution.hpp>
  45. namespace boost {
  46. /// The type used to store an identifier that uniquely names a processor.
  47. // NGE: I doubt we'll be running on more than 32768 procs for the time being
  48. typedef /*int*/ short processor_id_type;
  49. // Tell which processor the target of an edge resides on (for
  50. // directed graphs) or which processor the other end point of the
  51. // edge resides on (for undirected graphs).
  52. enum edge_target_processor_id_t { edge_target_processor_id };
  53. BOOST_INSTALL_PROPERTY(edge, target_processor_id);
  54. // For undirected graphs, tells whether the edge is locally owned.
  55. enum edge_locally_owned_t { edge_locally_owned };
  56. BOOST_INSTALL_PROPERTY(edge, locally_owned);
  57. // For bidirectional graphs, stores the incoming edges.
  58. enum vertex_in_edges_t { vertex_in_edges };
  59. BOOST_INSTALL_PROPERTY(vertex, in_edges);
  60. /// Tag class for directed, distributed adjacency list
  61. struct directed_distributed_adj_list_tag
  62. : public virtual distributed_graph_tag,
  63. public virtual distributed_vertex_list_graph_tag,
  64. public virtual distributed_edge_list_graph_tag,
  65. public virtual incidence_graph_tag,
  66. public virtual adjacency_graph_tag {};
  67. /// Tag class for bidirectional, distributed adjacency list
  68. struct bidirectional_distributed_adj_list_tag
  69. : public virtual distributed_graph_tag,
  70. public virtual distributed_vertex_list_graph_tag,
  71. public virtual distributed_edge_list_graph_tag,
  72. public virtual incidence_graph_tag,
  73. public virtual adjacency_graph_tag,
  74. public virtual bidirectional_graph_tag {};
  75. /// Tag class for undirected, distributed adjacency list
  76. struct undirected_distributed_adj_list_tag
  77. : public virtual distributed_graph_tag,
  78. public virtual distributed_vertex_list_graph_tag,
  79. public virtual distributed_edge_list_graph_tag,
  80. public virtual incidence_graph_tag,
  81. public virtual adjacency_graph_tag,
  82. public virtual bidirectional_graph_tag {};
  83. namespace detail {
  84. template<typename Archiver, typename Directed, typename Vertex>
  85. void
  86. serialize(Archiver& ar, edge_base<Directed, Vertex>& e,
  87. const unsigned int /*version*/)
  88. {
  89. ar & unsafe_serialize(e.m_source)
  90. & unsafe_serialize(e.m_target);
  91. }
  92. template<typename Archiver, typename Directed, typename Vertex>
  93. void
  94. serialize(Archiver& ar, edge_desc_impl<Directed, Vertex>& e,
  95. const unsigned int /*version*/)
  96. {
  97. ar & boost::serialization::base_object<edge_base<Directed, Vertex> >(e)
  98. & unsafe_serialize(e.m_eproperty);
  99. }
  100. }
  101. namespace detail { namespace parallel {
  102. /**
  103. * A distributed vertex descriptor. These descriptors contain both
  104. * the ID of the processor that owns the vertex and a local vertex
  105. * descriptor that identifies the particular vertex for that
  106. * processor.
  107. */
  108. template<typename LocalDescriptor>
  109. struct global_descriptor
  110. {
  111. typedef LocalDescriptor local_descriptor_type;
  112. global_descriptor() : owner(), local() { }
  113. global_descriptor(processor_id_type owner, LocalDescriptor local)
  114. : owner(owner), local(local) { }
  115. processor_id_type owner;
  116. LocalDescriptor local;
  117. /**
  118. * A function object that, given a processor ID, generates
  119. * distributed vertex descriptors from local vertex
  120. * descriptors. This function object is used by the
  121. * vertex_iterator of the distributed adjacency list.
  122. */
  123. struct generator
  124. {
  125. typedef global_descriptor<LocalDescriptor> result_type;
  126. typedef LocalDescriptor argument_type;
  127. generator() {}
  128. generator(processor_id_type owner) : owner(owner) {}
  129. result_type operator()(argument_type v) const
  130. { return result_type(owner, v); }
  131. private:
  132. processor_id_type owner;
  133. };
  134. template<typename Archiver>
  135. void serialize(Archiver& ar, const unsigned int /*version*/)
  136. {
  137. ar & owner & unsafe_serialize(local);
  138. }
  139. };
  140. /// Determine the process that owns the given descriptor
  141. template<typename LocalDescriptor>
  142. inline processor_id_type owner(const global_descriptor<LocalDescriptor>& v)
  143. { return v.owner; }
  144. /// Determine the local portion of the given descriptor
  145. template<typename LocalDescriptor>
  146. inline LocalDescriptor local(const global_descriptor<LocalDescriptor>& v)
  147. { return v.local; }
  148. /// Compare distributed vertex descriptors for equality
  149. template<typename LocalDescriptor>
  150. inline bool
  151. operator==(const global_descriptor<LocalDescriptor>& u,
  152. const global_descriptor<LocalDescriptor>& v)
  153. {
  154. return u.owner == v.owner && u.local == v.local;
  155. }
  156. /// Compare distributed vertex descriptors for inequality
  157. template<typename LocalDescriptor>
  158. inline bool
  159. operator!=(const global_descriptor<LocalDescriptor>& u,
  160. const global_descriptor<LocalDescriptor>& v)
  161. { return !(u == v); }
  162. template<typename LocalDescriptor>
  163. inline bool
  164. operator<(const global_descriptor<LocalDescriptor>& u,
  165. const global_descriptor<LocalDescriptor>& v)
  166. {
  167. return (u.owner) < v.owner || (u.owner == v.owner && (u.local) < v.local);
  168. }
  169. template<typename LocalDescriptor>
  170. inline bool
  171. operator<=(const global_descriptor<LocalDescriptor>& u,
  172. const global_descriptor<LocalDescriptor>& v)
  173. {
  174. return u.owner <= v.owner || (u.owner == v.owner && u.local <= v.local);
  175. }
  176. template<typename LocalDescriptor>
  177. inline bool
  178. operator>(const global_descriptor<LocalDescriptor>& u,
  179. const global_descriptor<LocalDescriptor>& v)
  180. {
  181. return v < u;
  182. }
  183. template<typename LocalDescriptor>
  184. inline bool
  185. operator>=(const global_descriptor<LocalDescriptor>& u,
  186. const global_descriptor<LocalDescriptor>& v)
  187. {
  188. return v <= u;
  189. }
  190. // DPG TBD: Add <, <=, >=, > for global descriptors
  191. /**
  192. * A Readable Property Map that extracts a global descriptor pair
  193. * from a global_descriptor.
  194. */
  195. template<typename LocalDescriptor>
  196. struct global_descriptor_property_map
  197. {
  198. typedef std::pair<processor_id_type, LocalDescriptor> value_type;
  199. typedef value_type reference;
  200. typedef global_descriptor<LocalDescriptor> key_type;
  201. typedef readable_property_map_tag category;
  202. };
  203. template<typename LocalDescriptor>
  204. inline std::pair<processor_id_type, LocalDescriptor>
  205. get(global_descriptor_property_map<LocalDescriptor>,
  206. global_descriptor<LocalDescriptor> x)
  207. {
  208. return std::pair<processor_id_type, LocalDescriptor>(x.owner, x.local);
  209. }
  210. /**
  211. * A Readable Property Map that extracts the owner of a global
  212. * descriptor.
  213. */
  214. template<typename LocalDescriptor>
  215. struct owner_property_map
  216. {
  217. typedef processor_id_type value_type;
  218. typedef value_type reference;
  219. typedef global_descriptor<LocalDescriptor> key_type;
  220. typedef readable_property_map_tag category;
  221. };
  222. template<typename LocalDescriptor>
  223. inline processor_id_type
  224. get(owner_property_map<LocalDescriptor>,
  225. global_descriptor<LocalDescriptor> x)
  226. {
  227. return x.owner;
  228. }
  229. /**
  230. * A Readable Property Map that extracts the local descriptor from
  231. * a global descriptor.
  232. */
  233. template<typename LocalDescriptor>
  234. struct local_descriptor_property_map
  235. {
  236. typedef LocalDescriptor value_type;
  237. typedef value_type reference;
  238. typedef global_descriptor<LocalDescriptor> key_type;
  239. typedef readable_property_map_tag category;
  240. };
  241. template<typename LocalDescriptor>
  242. inline LocalDescriptor
  243. get(local_descriptor_property_map<LocalDescriptor>,
  244. global_descriptor<LocalDescriptor> x)
  245. {
  246. return x.local;
  247. }
  248. /**
  249. * Stores an incoming edge for a bidirectional distributed
  250. * adjacency list. The user does not see this type directly,
  251. * because it is just an implementation detail.
  252. */
  253. template<typename Edge>
  254. struct stored_in_edge
  255. {
  256. stored_in_edge(processor_id_type sp, Edge e)
  257. : source_processor(sp), e(e) {}
  258. processor_id_type source_processor;
  259. Edge e;
  260. };
  261. /**
  262. * A distributed edge descriptor. These descriptors contain the
  263. * underlying edge descriptor, the processor IDs for both the
  264. * source and the target of the edge, and a boolean flag that
  265. * indicates which of the processors actually owns the edge.
  266. */
  267. template<typename Edge>
  268. struct edge_descriptor
  269. {
  270. edge_descriptor(processor_id_type sp = processor_id_type(),
  271. processor_id_type tp = processor_id_type(),
  272. bool owns = false, Edge ld = Edge())
  273. : source_processor(sp), target_processor(tp),
  274. source_owns_edge(owns), local(ld) {}
  275. processor_id_type owner() const
  276. {
  277. return source_owns_edge? source_processor : target_processor;
  278. }
  279. /// The processor that the source vertex resides on
  280. processor_id_type source_processor;
  281. /// The processor that the target vertex resides on
  282. processor_id_type target_processor;
  283. /// True when the source processor owns the edge, false when the
  284. /// target processor owns the edge.
  285. bool source_owns_edge;
  286. /// The local edge descriptor.
  287. Edge local;
  288. /**
  289. * Function object that generates edge descriptors for the
  290. * out_edge_iterator of the given distributed adjacency list
  291. * from the edge descriptors of the underlying adjacency list.
  292. */
  293. template<typename Graph>
  294. class out_generator
  295. {
  296. typedef typename Graph::directed_selector directed_selector;
  297. public:
  298. typedef edge_descriptor<Edge> result_type;
  299. typedef Edge argument_type;
  300. out_generator() : g(0) {}
  301. explicit out_generator(const Graph& g) : g(&g) {}
  302. result_type operator()(argument_type e) const
  303. { return map(e, directed_selector()); }
  304. private:
  305. result_type map(argument_type e, directedS) const
  306. {
  307. return result_type(g->processor(),
  308. get(edge_target_processor_id, g->base(), e),
  309. true, e);
  310. }
  311. result_type map(argument_type e, bidirectionalS) const
  312. {
  313. return result_type(g->processor(),
  314. get(edge_target_processor_id, g->base(), e),
  315. true, e);
  316. }
  317. result_type map(argument_type e, undirectedS) const
  318. {
  319. return result_type(g->processor(),
  320. get(edge_target_processor_id, g->base(), e),
  321. get(edge_locally_owned, g->base(), e),
  322. e);
  323. }
  324. const Graph* g;
  325. };
  326. /**
  327. * Function object that generates edge descriptors for the
  328. * in_edge_iterator of the given distributed adjacency list
  329. * from the edge descriptors of the underlying adjacency list.
  330. */
  331. template<typename Graph>
  332. class in_generator
  333. {
  334. typedef typename Graph::directed_selector DirectedS;
  335. public:
  336. typedef typename boost::mpl::if_<is_same<DirectedS, bidirectionalS>,
  337. stored_in_edge<Edge>,
  338. Edge>::type argument_type;
  339. typedef edge_descriptor<Edge> result_type;
  340. in_generator() : g(0) {}
  341. explicit in_generator(const Graph& g) : g(&g) {}
  342. result_type operator()(argument_type e) const
  343. { return map(e, DirectedS()); }
  344. private:
  345. /**
  346. * For a bidirectional graph, we just generate the appropriate
  347. * edge. No tricks.
  348. */
  349. result_type map(argument_type e, bidirectionalS) const
  350. {
  351. return result_type(e.source_processor,
  352. g->processor(),
  353. true,
  354. e.e);
  355. }
  356. /**
  357. * For an undirected graph, we generate descriptors for the
  358. * incoming edges by swapping the source/target of the
  359. * underlying edge descriptor (a hack). The target processor
  360. * ID on the edge is actually the source processor for this
  361. * edge, and our processor is the target processor. If the
  362. * edge is locally owned, then it is owned by the target (us);
  363. * otherwise it is owned by the source.
  364. */
  365. result_type map(argument_type e, undirectedS) const
  366. {
  367. typename Graph::local_edge_descriptor local_edge(e);
  368. // TBD: This is a very, VERY lame hack that takes advantage
  369. // of our knowledge of the internals of the BGL
  370. // adjacency_list. There should be a cleaner way to handle
  371. // this...
  372. using std::swap;
  373. swap(local_edge.m_source, local_edge.m_target);
  374. return result_type(get(edge_target_processor_id, g->base(), e),
  375. g->processor(),
  376. !get(edge_locally_owned, g->base(), e),
  377. local_edge);
  378. }
  379. const Graph* g;
  380. };
  381. private:
  382. friend class boost::serialization::access;
  383. template<typename Archiver>
  384. void serialize(Archiver& ar, const unsigned int /*version*/)
  385. {
  386. ar
  387. & source_processor
  388. & target_processor
  389. & source_owns_edge
  390. & local;
  391. }
  392. };
  393. /// Determine the process that owns this edge
  394. template<typename Edge>
  395. inline processor_id_type
  396. owner(const edge_descriptor<Edge>& e)
  397. { return e.source_owns_edge? e.source_processor : e.target_processor; }
  398. /// Determine the local descriptor for this edge.
  399. template<typename Edge>
  400. inline Edge
  401. local(const edge_descriptor<Edge>& e)
  402. { return e.local; }
  403. /**
  404. * A Readable Property Map that extracts the owner and local
  405. * descriptor of an edge descriptor.
  406. */
  407. template<typename Edge>
  408. struct edge_global_property_map
  409. {
  410. typedef std::pair<processor_id_type, Edge> value_type;
  411. typedef value_type reference;
  412. typedef edge_descriptor<Edge> key_type;
  413. typedef readable_property_map_tag category;
  414. };
  415. template<typename Edge>
  416. inline std::pair<processor_id_type, Edge>
  417. get(edge_global_property_map<Edge>, const edge_descriptor<Edge>& e)
  418. {
  419. typedef std::pair<processor_id_type, Edge> result_type;
  420. return result_type(e.source_owns_edge? e.source_processor
  421. /* target owns edge*/: e.target_processor,
  422. e.local);
  423. }
  424. /**
  425. * A Readable Property Map that extracts the owner of an edge
  426. * descriptor.
  427. */
  428. template<typename Edge>
  429. struct edge_owner_property_map
  430. {
  431. typedef processor_id_type value_type;
  432. typedef value_type reference;
  433. typedef edge_descriptor<Edge> key_type;
  434. typedef readable_property_map_tag category;
  435. };
  436. template<typename Edge>
  437. inline processor_id_type
  438. get(edge_owner_property_map<Edge>, const edge_descriptor<Edge>& e)
  439. {
  440. return e.source_owns_edge? e.source_processor : e.target_processor;
  441. }
  442. /**
  443. * A Readable Property Map that extracts the local descriptor from
  444. * an edge descriptor.
  445. */
  446. template<typename Edge>
  447. struct edge_local_property_map
  448. {
  449. typedef Edge value_type;
  450. typedef value_type reference;
  451. typedef edge_descriptor<Edge> key_type;
  452. typedef readable_property_map_tag category;
  453. };
  454. template<typename Edge>
  455. inline Edge
  456. get(edge_local_property_map<Edge>,
  457. const edge_descriptor<Edge>& e)
  458. {
  459. return e.local;
  460. }
  461. /** Compare distributed edge descriptors for equality.
  462. *
  463. * \todo need edge_descriptor to know if it is undirected so we
  464. * can compare both ways.
  465. */
  466. template<typename Edge>
  467. inline bool
  468. operator==(const edge_descriptor<Edge>& e1,
  469. const edge_descriptor<Edge>& e2)
  470. {
  471. return (e1.source_processor == e2.source_processor
  472. && e1.target_processor == e2.target_processor
  473. && e1.local == e2.local);
  474. }
  475. /// Compare distributed edge descriptors for inequality.
  476. template<typename Edge>
  477. inline bool
  478. operator!=(const edge_descriptor<Edge>& e1,
  479. const edge_descriptor<Edge>& e2)
  480. { return !(e1 == e2); }
  481. /**
  482. * Configuration for the distributed adjacency list. We use this
  483. * parameter to store all of the configuration details for the
  484. * implementation of the distributed adjacency list, which allows us to
  485. * get at the distribution type in the maybe_named_graph.
  486. */
  487. template<typename OutEdgeListS, typename ProcessGroup,
  488. typename InVertexListS, typename InDistribution,
  489. typename DirectedS, typename VertexProperty,
  490. typename EdgeProperty, typename GraphProperty,
  491. typename EdgeListS>
  492. struct adjacency_list_config
  493. {
  494. typedef typename mpl::if_<is_same<InVertexListS, defaultS>,
  495. vecS, InVertexListS>::type
  496. VertexListS;
  497. /// Introduce the target processor ID property for each edge
  498. typedef property<edge_target_processor_id_t, processor_id_type,
  499. EdgeProperty> edge_property_with_id;
  500. /// For undirected graphs, introduce the locally-owned property for edges
  501. typedef typename boost::mpl::if_<is_same<DirectedS, undirectedS>,
  502. property<edge_locally_owned_t, bool,
  503. edge_property_with_id>,
  504. edge_property_with_id>::type
  505. base_edge_property_type;
  506. /// The edge descriptor type for the local subgraph
  507. typedef typename adjacency_list_traits<OutEdgeListS,
  508. VertexListS,
  509. directedS>::edge_descriptor
  510. local_edge_descriptor;
  511. /// For bidirectional graphs, the type of an incoming stored edge
  512. typedef stored_in_edge<local_edge_descriptor> bidir_stored_edge;
  513. /// The container type that will store incoming edges for a
  514. /// bidirectional graph.
  515. typedef typename container_gen<EdgeListS, bidir_stored_edge>::type
  516. in_edge_list_type;
  517. // Bidirectional graphs have an extra vertex property to store
  518. // the incoming edges.
  519. typedef typename boost::mpl::if_<is_same<DirectedS, bidirectionalS>,
  520. property<vertex_in_edges_t, in_edge_list_type,
  521. VertexProperty>,
  522. VertexProperty>::type
  523. base_vertex_property_type;
  524. // The type of the distributed adjacency list
  525. typedef adjacency_list<OutEdgeListS,
  526. distributedS<ProcessGroup,
  527. VertexListS,
  528. InDistribution>,
  529. DirectedS, VertexProperty, EdgeProperty,
  530. GraphProperty, EdgeListS>
  531. graph_type;
  532. // The type of the underlying adjacency list implementation
  533. typedef adjacency_list<OutEdgeListS, VertexListS, directedS,
  534. base_vertex_property_type,
  535. base_edge_property_type,
  536. GraphProperty,
  537. EdgeListS>
  538. inherited;
  539. typedef InDistribution in_distribution_type;
  540. typedef typename inherited::vertices_size_type vertices_size_type;
  541. typedef typename ::boost::graph::distributed::select_distribution<
  542. in_distribution_type, VertexProperty, vertices_size_type,
  543. ProcessGroup>::type
  544. base_distribution_type;
  545. typedef ::boost::graph::distributed::shuffled_distribution<
  546. base_distribution_type> distribution_type;
  547. typedef VertexProperty vertex_property_type;
  548. typedef EdgeProperty edge_property_type;
  549. typedef ProcessGroup process_group_type;
  550. typedef VertexListS vertex_list_selector;
  551. typedef OutEdgeListS out_edge_list_selector;
  552. typedef DirectedS directed_selector;
  553. typedef GraphProperty graph_property_type;
  554. typedef EdgeListS edge_list_selector;
  555. };
  556. // Maybe initialize the indices of each vertex
  557. template<typename IteratorPair, typename VertexIndexMap>
  558. void
  559. maybe_initialize_vertex_indices(IteratorPair p, VertexIndexMap to_index,
  560. read_write_property_map_tag)
  561. {
  562. typedef typename property_traits<VertexIndexMap>::value_type index_t;
  563. index_t next_index = 0;
  564. while (p.first != p.second)
  565. put(to_index, *p.first++, next_index++);
  566. }
  567. template<typename IteratorPair, typename VertexIndexMap>
  568. inline void
  569. maybe_initialize_vertex_indices(IteratorPair p, VertexIndexMap to_index,
  570. readable_property_map_tag)
  571. {
  572. // Do nothing
  573. }
  574. template<typename IteratorPair, typename VertexIndexMap>
  575. inline void
  576. maybe_initialize_vertex_indices(IteratorPair p, VertexIndexMap to_index)
  577. {
  578. typedef typename property_traits<VertexIndexMap>::category category;
  579. maybe_initialize_vertex_indices(p, to_index, category());
  580. }
  581. template<typename IteratorPair>
  582. inline void
  583. maybe_initialize_vertex_indices(IteratorPair p,
  584. ::boost::detail::error_property_not_found)
  585. { }
  586. /***********************************************************************
  587. * Message Payloads *
  588. ***********************************************************************/
  589. /**
  590. * Data stored with a msg_add_edge message, which requests the
  591. * remote addition of an edge.
  592. */
  593. template<typename Vertex, typename LocalVertex>
  594. struct msg_add_edge_data
  595. {
  596. msg_add_edge_data() { }
  597. msg_add_edge_data(Vertex source, Vertex target)
  598. : source(source.local), target(target) { }
  599. /// The source of the edge; the processor will be the
  600. /// receiving processor.
  601. LocalVertex source;
  602. /// The target of the edge.
  603. Vertex target;
  604. template<typename Archiver>
  605. void serialize(Archiver& ar, const unsigned int /*version*/)
  606. {
  607. ar & unsafe_serialize(source) & target;
  608. }
  609. };
  610. /**
  611. * Like @c msg_add_edge_data, but also includes a user-specified
  612. * property value to be attached to the edge.
  613. */
  614. template<typename Vertex, typename LocalVertex, typename EdgeProperty>
  615. struct msg_add_edge_with_property_data
  616. : msg_add_edge_data<Vertex, LocalVertex>,
  617. maybe_store_property<EdgeProperty>
  618. {
  619. private:
  620. typedef msg_add_edge_data<Vertex, LocalVertex> inherited_data;
  621. typedef maybe_store_property<EdgeProperty> inherited_property;
  622. public:
  623. msg_add_edge_with_property_data() { }
  624. msg_add_edge_with_property_data(Vertex source,
  625. Vertex target,
  626. const EdgeProperty& property)
  627. : inherited_data(source, target),
  628. inherited_property(property) { }
  629. template<typename Archiver>
  630. void serialize(Archiver& ar, const unsigned int /*version*/)
  631. {
  632. ar & boost::serialization::base_object<inherited_data>(*this)
  633. & boost::serialization::base_object<inherited_property>(*this);
  634. }
  635. };
  636. //------------------------------------------------------------------------
  637. // Distributed adjacency list property map details
  638. /**
  639. * Metafunction that extracts the given property from the given
  640. * distributed adjacency list type. This could be implemented much
  641. * more cleanly, but even newer versions of GCC (e.g., 3.2.3)
  642. * cannot properly handle partial specializations involving
  643. * enumerator types.
  644. */
  645. template<typename Property>
  646. struct get_adj_list_pmap
  647. {
  648. template<typename Graph>
  649. struct apply
  650. {
  651. typedef Graph graph_type;
  652. typedef typename graph_type::process_group_type process_group_type;
  653. typedef typename graph_type::inherited base_graph_type;
  654. typedef typename property_map<base_graph_type, Property>::type
  655. local_pmap;
  656. typedef typename property_map<base_graph_type, Property>::const_type
  657. local_const_pmap;
  658. typedef graph_traits<graph_type> traits;
  659. typedef typename graph_type::local_vertex_descriptor local_vertex;
  660. typedef typename property_traits<local_pmap>::key_type local_key_type;
  661. typedef typename property_traits<local_pmap>::value_type value_type;
  662. typedef typename property_map<Graph, vertex_global_t>::const_type
  663. vertex_global_map;
  664. typedef typename property_map<Graph, edge_global_t>::const_type
  665. edge_global_map;
  666. typedef typename mpl::if_c<(is_same<local_key_type,
  667. local_vertex>::value),
  668. vertex_global_map, edge_global_map>::type
  669. global_map;
  670. public:
  671. typedef ::boost::parallel::distributed_property_map<
  672. process_group_type, global_map, local_pmap> type;
  673. typedef ::boost::parallel::distributed_property_map<
  674. process_group_type, global_map, local_const_pmap> const_type;
  675. };
  676. };
  677. /**
  678. * The local vertex index property map is actually a mapping from
  679. * the local vertex descriptors to vertex indices.
  680. */
  681. template<>
  682. struct get_adj_list_pmap<vertex_local_index_t>
  683. {
  684. template<typename Graph>
  685. struct apply
  686. : ::boost::property_map<typename Graph::inherited, vertex_index_t>
  687. { };
  688. };
  689. /**
  690. * The vertex index property map maps from global descriptors
  691. * (e.g., the vertex descriptor of a distributed adjacency list)
  692. * to the underlying local index. It is not valid to use this
  693. * property map with nonlocal descriptors.
  694. */
  695. template<>
  696. struct get_adj_list_pmap<vertex_index_t>
  697. {
  698. template<typename Graph>
  699. struct apply
  700. {
  701. private:
  702. typedef typename property_map<Graph, vertex_global_t>::const_type
  703. global_map;
  704. typedef property_map<typename Graph::inherited, vertex_index_t> local;
  705. public:
  706. typedef local_property_map<typename Graph::process_group_type,
  707. global_map,
  708. typename local::type> type;
  709. typedef local_property_map<typename Graph::process_group_type,
  710. global_map,
  711. typename local::const_type> const_type;
  712. };
  713. };
  714. /**
  715. * The vertex owner property map maps from vertex descriptors to
  716. * the processor that owns the vertex.
  717. */
  718. template<>
  719. struct get_adj_list_pmap<vertex_global_t>
  720. {
  721. template<typename Graph>
  722. struct apply
  723. {
  724. private:
  725. typedef typename Graph::local_vertex_descriptor
  726. local_vertex_descriptor;
  727. public:
  728. typedef global_descriptor_property_map<local_vertex_descriptor> type;
  729. typedef type const_type;
  730. };
  731. };
  732. /**
  733. * The vertex owner property map maps from vertex descriptors to
  734. * the processor that owns the vertex.
  735. */
  736. template<>
  737. struct get_adj_list_pmap<vertex_owner_t>
  738. {
  739. template<typename Graph>
  740. struct apply
  741. {
  742. private:
  743. typedef typename Graph::local_vertex_descriptor
  744. local_vertex_descriptor;
  745. public:
  746. typedef owner_property_map<local_vertex_descriptor> type;
  747. typedef type const_type;
  748. };
  749. };
  750. /**
  751. * The vertex local property map maps from vertex descriptors to
  752. * the local descriptor for that vertex.
  753. */
  754. template<>
  755. struct get_adj_list_pmap<vertex_local_t>
  756. {
  757. template<typename Graph>
  758. struct apply
  759. {
  760. private:
  761. typedef typename Graph::local_vertex_descriptor
  762. local_vertex_descriptor;
  763. public:
  764. typedef local_descriptor_property_map<local_vertex_descriptor> type;
  765. typedef type const_type;
  766. };
  767. };
  768. /**
  769. * The edge global property map maps from edge descriptors to
  770. * a pair of the owning processor and local descriptor.
  771. */
  772. template<>
  773. struct get_adj_list_pmap<edge_global_t>
  774. {
  775. template<typename Graph>
  776. struct apply
  777. {
  778. private:
  779. typedef typename Graph::local_edge_descriptor
  780. local_edge_descriptor;
  781. public:
  782. typedef edge_global_property_map<local_edge_descriptor> type;
  783. typedef type const_type;
  784. };
  785. };
  786. /**
  787. * The edge owner property map maps from edge descriptors to
  788. * the processor that owns the edge.
  789. */
  790. template<>
  791. struct get_adj_list_pmap<edge_owner_t>
  792. {
  793. template<typename Graph>
  794. struct apply
  795. {
  796. private:
  797. typedef typename Graph::local_edge_descriptor
  798. local_edge_descriptor;
  799. public:
  800. typedef edge_owner_property_map<local_edge_descriptor> type;
  801. typedef type const_type;
  802. };
  803. };
  804. /**
  805. * The edge local property map maps from edge descriptors to
  806. * the local descriptor for that edge.
  807. */
  808. template<>
  809. struct get_adj_list_pmap<edge_local_t>
  810. {
  811. template<typename Graph>
  812. struct apply
  813. {
  814. private:
  815. typedef typename Graph::local_edge_descriptor
  816. local_edge_descriptor;
  817. public:
  818. typedef edge_local_property_map<local_edge_descriptor> type;
  819. typedef type const_type;
  820. };
  821. };
  822. //------------------------------------------------------------------------
  823. // Directed graphs do not have in edges, so this is a no-op
  824. template<typename Graph>
  825. inline void
  826. remove_in_edge(typename Graph::edge_descriptor, Graph&, directedS)
  827. { }
  828. // Bidirectional graphs have in edges stored in the
  829. // vertex_in_edges property.
  830. template<typename Graph>
  831. inline void
  832. remove_in_edge(typename Graph::edge_descriptor e, Graph& g, bidirectionalS)
  833. {
  834. typedef typename Graph::in_edge_list_type in_edge_list_type;
  835. in_edge_list_type& in_edges =
  836. get(vertex_in_edges, g.base())[target(e, g).local];
  837. typename in_edge_list_type::iterator i = in_edges.begin();
  838. while (i != in_edges.end()
  839. && !(i->source_processor == source(e, g).owner)
  840. && i->e == e.local)
  841. ++i;
  842. BOOST_ASSERT(i != in_edges.end());
  843. in_edges.erase(i);
  844. }
  845. // Undirected graphs have in edges stored as normal edges.
  846. template<typename Graph>
  847. inline void
  848. remove_in_edge(typename Graph::edge_descriptor e, Graph& g, undirectedS)
  849. {
  850. typedef typename Graph::inherited base_type;
  851. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  852. // TBD: can we make this more efficient?
  853. // Removing edge (v, u). v is local
  854. base_type& bg = g.base();
  855. vertex_descriptor u = source(e, g);
  856. vertex_descriptor v = target(e, g);
  857. if (v.owner != process_id(g.process_group())) {
  858. using std::swap;
  859. swap(u, v);
  860. }
  861. typename graph_traits<base_type>::out_edge_iterator ei, ei_end;
  862. for (boost::tie(ei, ei_end) = out_edges(v.local, bg); ei != ei_end; ++ei)
  863. {
  864. if (target(*ei, g.base()) == u.local
  865. // TBD: deal with parallel edges properly && *ei == e
  866. && get(edge_target_processor_id, bg, *ei) == u.owner) {
  867. remove_edge(ei, bg);
  868. return;
  869. }
  870. }
  871. if (v.owner == process_id(g.process_group())) {
  872. }
  873. }
  874. //------------------------------------------------------------------------
  875. // Lazy addition of edges
  876. // Work around the fact that an adjacency_list with vecS vertex
  877. // storage automatically adds edges when the descriptor is
  878. // out-of-range.
  879. template <class Graph, class Config, class Base>
  880. inline std::pair<typename Config::edge_descriptor, bool>
  881. add_local_edge(typename Config::vertex_descriptor u,
  882. typename Config::vertex_descriptor v,
  883. const typename Config::edge_property_type& p,
  884. vec_adj_list_impl<Graph, Config, Base>& g_)
  885. {
  886. adj_list_helper<Config, Base>& g = g_;
  887. return add_edge(u, v, p, g);
  888. }
  889. template <class Graph, class Config, class Base>
  890. inline std::pair<typename Config::edge_descriptor, bool>
  891. add_local_edge(typename Config::vertex_descriptor u,
  892. typename Config::vertex_descriptor v,
  893. const typename Config::edge_property_type& p,
  894. boost::adj_list_impl<Graph, Config, Base>& g)
  895. {
  896. return add_edge(u, v, p, g);
  897. }
  898. template <class EdgeProperty,class EdgeDescriptor>
  899. struct msg_nonlocal_edge_data
  900. : public detail::parallel::maybe_store_property<EdgeProperty>
  901. {
  902. typedef EdgeProperty edge_property_type;
  903. typedef EdgeDescriptor local_edge_descriptor;
  904. typedef detail::parallel::maybe_store_property<edge_property_type>
  905. inherited;
  906. msg_nonlocal_edge_data() {}
  907. msg_nonlocal_edge_data(local_edge_descriptor e,
  908. const edge_property_type& p)
  909. : inherited(p), e(e) { }
  910. local_edge_descriptor e;
  911. template<typename Archiver>
  912. void serialize(Archiver& ar, const unsigned int /*version*/)
  913. {
  914. ar & boost::serialization::base_object<inherited>(*this) & e;
  915. }
  916. };
  917. template <class EdgeDescriptor>
  918. struct msg_remove_edge_data
  919. {
  920. typedef EdgeDescriptor edge_descriptor;
  921. msg_remove_edge_data() {}
  922. explicit msg_remove_edge_data(edge_descriptor e) : e(e) {}
  923. edge_descriptor e;
  924. template<typename Archiver>
  925. void serialize(Archiver& ar, const unsigned int /*version*/)
  926. {
  927. ar & e;
  928. }
  929. };
  930. } } // end namespace detail::parallel
  931. /**
  932. * Adjacency list traits for a distributed adjacency list. Contains
  933. * the vertex and edge descriptors, the directed-ness, and the
  934. * parallel edges typedefs.
  935. */
  936. template<typename OutEdgeListS, typename ProcessGroup,
  937. typename InVertexListS, typename InDistribution, typename DirectedS>
  938. struct adjacency_list_traits<OutEdgeListS,
  939. distributedS<ProcessGroup,
  940. InVertexListS,
  941. InDistribution>,
  942. DirectedS>
  943. {
  944. private:
  945. typedef typename mpl::if_<is_same<InVertexListS, defaultS>,
  946. vecS,
  947. InVertexListS>::type VertexListS;
  948. typedef adjacency_list_traits<OutEdgeListS, VertexListS, directedS>
  949. base_type;
  950. public:
  951. typedef typename base_type::vertex_descriptor local_vertex_descriptor;
  952. typedef typename base_type::edge_descriptor local_edge_descriptor;
  953. typedef typename boost::mpl::if_<typename DirectedS::is_bidir_t,
  954. bidirectional_tag,
  955. typename boost::mpl::if_<typename DirectedS::is_directed_t,
  956. directed_tag, undirected_tag
  957. >::type
  958. >::type directed_category;
  959. typedef typename parallel_edge_traits<OutEdgeListS>::type
  960. edge_parallel_category;
  961. typedef detail::parallel::global_descriptor<local_vertex_descriptor>
  962. vertex_descriptor;
  963. typedef detail::parallel::edge_descriptor<local_edge_descriptor>
  964. edge_descriptor;
  965. };
  966. #define PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS \
  967. typename OutEdgeListS, typename ProcessGroup, typename InVertexListS, \
  968. typename InDistribution, typename DirectedS, typename VertexProperty, \
  969. typename EdgeProperty, typename GraphProperty, typename EdgeListS
  970. #define PBGL_DISTRIB_ADJLIST_TYPE \
  971. adjacency_list<OutEdgeListS, \
  972. distributedS<ProcessGroup, InVertexListS, InDistribution>, \
  973. DirectedS, VertexProperty, EdgeProperty, GraphProperty, \
  974. EdgeListS>
  975. #define PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG \
  976. typename OutEdgeListS, typename ProcessGroup, typename InVertexListS, \
  977. typename InDistribution, typename VertexProperty, \
  978. typename EdgeProperty, typename GraphProperty, typename EdgeListS
  979. #define PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(directed) \
  980. adjacency_list<OutEdgeListS, \
  981. distributedS<ProcessGroup, InVertexListS, InDistribution>, \
  982. directed, VertexProperty, EdgeProperty, GraphProperty, \
  983. EdgeListS>
  984. /** A distributed adjacency list.
  985. *
  986. * This class template partial specialization defines a distributed
  987. * (or "partitioned") adjacency list graph. The distributed
  988. * adjacency list is similar to the standard Boost Graph Library
  989. * adjacency list, which stores a list of vertices and for each
  990. * verted the list of edges outgoing from the vertex (and, in some
  991. * cases, also the edges incoming to the vertex). The distributed
  992. * adjacency list differs in that it partitions the graph into
  993. * several subgraphs that are then divided among different
  994. * processors (or nodes within a cluster). The distributed adjacency
  995. * list attempts to maintain a high degree of compatibility with the
  996. * standard, non-distributed adjacency list.
  997. *
  998. * The graph is partitioned by vertex, with each processor storing
  999. * all of the required information for a particular subset of the
  1000. * vertices, including vertex properties, outgoing edges, and (for
  1001. * bidirectional graphs) incoming edges. This information is
  1002. * accessible only on the processor that owns the vertex: for
  1003. * instance, if processor 0 owns vertex @c v, no other processor can
  1004. * directly access the properties of @c v or enumerate its outgoing
  1005. * edges.
  1006. *
  1007. * Edges in a graph may be entirely local (connecting two local
  1008. * vertices), but more often it is the case that edges are
  1009. * non-local, meaning that the two vertices they connect reside in
  1010. * different processes. Edge properties are stored with the
  1011. * originating vertex for directed and bidirectional graphs, and are
  1012. * therefore only accessible from the processor that owns the
  1013. * originating vertex. Other processors may query the source and
  1014. * target of the edge, but cannot access its properties. This is
  1015. * particularly interesting when accessing the incoming edges of a
  1016. * bidirectional graph, which are not guaranteed to be stored on the
  1017. * processor that is able to perform the iteration. For undirected
  1018. * graphs the situation is more complicated, since no vertex clearly
  1019. * owns the edges: the list of edges incident to a vertex may
  1020. * contain a mix of local and non-local edges.
  1021. *
  1022. * The distributed adjacency list is able to model several of the
  1023. * existing Graph concepts. It models the Graph concept because it
  1024. * exposes vertex and edge descriptors in the normal way; these
  1025. * descriptors model the GlobalDescriptor concept (because they have
  1026. * an owner and a local descriptor), and as such the distributed
  1027. * adjacency list models the DistributedGraph concept. The adjacency
  1028. * list also models the IncidenceGraph and AdjacencyGraph concepts,
  1029. * although this is only true so long as the domain of the valid
  1030. * expression arguments are restricted to vertices and edges stored
  1031. * locally. Likewise, bidirectional and undirected distributed
  1032. * adjacency lists model the BidirectionalGraph concept (vertex and
  1033. * edge domains must be respectived) and the distributed adjacency
  1034. * list models the MutableGraph concept (vertices and edges can only
  1035. * be added or removed locally). T he distributed adjacency list
  1036. * does not, however, model the VertexListGraph or EdgeListGraph
  1037. * concepts, because we can not efficiently enumerate all vertices
  1038. * or edges in the graph. Instead, the local subsets of vertices and
  1039. * edges can be enumerated (with the same syntax): the distributed
  1040. * adjacency list therefore models the DistributedVertexListGraph
  1041. * and DistributedEdgeListGraph concepts, because concurrent
  1042. * iteration over all of the vertices or edges stored on each
  1043. * processor will visit each vertex or edge.
  1044. *
  1045. * The distributed adjacency list is distinguished from the
  1046. * non-distributed version by the vertex list descriptor, which will
  1047. * be @c distributedS<ProcessGroup,VertexListS>. Here,
  1048. * the VertexListS type plays the same role as the VertexListS type
  1049. * in the non-distributed adjacency list: it allows one to select
  1050. * the data structure that will be used to store the local
  1051. * vertices. The ProcessGroup type, on the other hand, is unique to
  1052. * distributed data structures: it is the type that abstracts a
  1053. * group of cooperating processes, and it used for process
  1054. * identification, communication, and synchronization, among other
  1055. * things. Different process group types represent different
  1056. * communication mediums (e.g., MPI, PVM, TCP) or different models
  1057. * of communication (LogP, CGM, BSP, synchronous, etc.). This
  1058. * distributed adjacency list assumes a model based on non-blocking
  1059. * sends.
  1060. *
  1061. * Distribution of vertices across different processors is
  1062. * accomplished in two different ways. When initially constructing
  1063. * the graph, the user may provide a distribution object (that
  1064. * models the Distribution concept), which will determine the
  1065. * distribution of vertices to each process. Additionally, the @c
  1066. * add_vertex and @c add_edge operations add vertices or edges
  1067. * stored on the local processor. For @c add_edge, this is
  1068. * accomplished by requiring that the source vertex of the new edge
  1069. * be local to the process executing @c add_edge.
  1070. *
  1071. * Internal properties of a distributed adjacency list are
  1072. * accessible in the same manner as internal properties for a
  1073. * non-distributed adjacency list for local vertices or
  1074. * edges. Access to properties for remote vertices or edges occurs
  1075. * with the same syntax, but involve communication with the owner of
  1076. * the information: for more information, refer to class template
  1077. * @ref distributed_property_map, which manages distributed
  1078. * property maps. Note that the distributed property maps created
  1079. * for internal properties determine their reduction operation via
  1080. * the metafunction @ref property_reduce, which for the vast
  1081. * majority of uses is correct behavior.
  1082. *
  1083. * Communication among the processes coordinating on a particular
  1084. * distributed graph relies on non-blocking message passing along
  1085. * with synchronization. Local portions of the distributed graph may
  1086. * be modified concurrently, including the introduction of non-local
  1087. * edges, but prior to accessing the graph it is recommended that
  1088. * the @c synchronize free function be invoked on the graph to clear
  1089. * up any pending interprocess communication and modifications. All
  1090. * processes will then be released from the synchronization barrier
  1091. * concurrently.
  1092. *
  1093. * \todo Determine precisely what we should do with nonlocal edges
  1094. * in undirected graphs. Our parallelization of certain algorithms
  1095. * relies on the ability to access edge property maps immediately
  1096. * (e.g., edge_weight_t), so it may be necessary to duplicate the
  1097. * edge properties in both processes (but then we need some form of
  1098. * coherence protocol).
  1099. *
  1100. * \todo What does the user do if @c property_reduce doesn't do the
  1101. * right thing?
  1102. */
  1103. template<typename OutEdgeListS, typename ProcessGroup,
  1104. typename InVertexListS, typename InDistribution, typename DirectedS,
  1105. typename VertexProperty, typename EdgeProperty,
  1106. typename GraphProperty, typename EdgeListS>
  1107. class adjacency_list<OutEdgeListS,
  1108. distributedS<ProcessGroup,
  1109. InVertexListS,
  1110. InDistribution>,
  1111. DirectedS, VertexProperty,
  1112. EdgeProperty, GraphProperty, EdgeListS>
  1113. : // Support for named vertices
  1114. public graph::distributed::maybe_named_graph<
  1115. adjacency_list<OutEdgeListS,
  1116. distributedS<ProcessGroup,
  1117. InVertexListS,
  1118. InDistribution>,
  1119. DirectedS, VertexProperty,
  1120. EdgeProperty, GraphProperty, EdgeListS>,
  1121. typename adjacency_list_traits<OutEdgeListS,
  1122. distributedS<ProcessGroup,
  1123. InVertexListS,
  1124. InDistribution>,
  1125. DirectedS>::vertex_descriptor,
  1126. typename adjacency_list_traits<OutEdgeListS,
  1127. distributedS<ProcessGroup,
  1128. InVertexListS,
  1129. InDistribution>,
  1130. DirectedS>::edge_descriptor,
  1131. detail::parallel::adjacency_list_config<OutEdgeListS, ProcessGroup,
  1132. InVertexListS, InDistribution,
  1133. DirectedS, VertexProperty,
  1134. EdgeProperty, GraphProperty,
  1135. EdgeListS> >
  1136. {
  1137. typedef detail::parallel::adjacency_list_config<OutEdgeListS, ProcessGroup,
  1138. InVertexListS, InDistribution,
  1139. DirectedS, VertexProperty,
  1140. EdgeProperty, GraphProperty,
  1141. EdgeListS>
  1142. config_type;
  1143. typedef adjacency_list_traits<OutEdgeListS,
  1144. distributedS<ProcessGroup,
  1145. InVertexListS,
  1146. InDistribution>,
  1147. DirectedS>
  1148. traits_type;
  1149. typedef typename DirectedS::is_directed_t is_directed;
  1150. typedef EdgeListS edge_list_selector;
  1151. public:
  1152. /// The container type that will store incoming edges for a
  1153. /// bidirectional graph.
  1154. typedef typename config_type::in_edge_list_type in_edge_list_type;
  1155. // typedef typename inherited::edge_descriptor edge_descriptor;
  1156. /// The type of the underlying adjacency list implementation
  1157. typedef typename config_type::inherited inherited;
  1158. /// The type of properties stored in the local subgraph
  1159. /// Bidirectional graphs have an extra vertex property to store
  1160. /// the incoming edges.
  1161. typedef typename inherited::vertex_property_type
  1162. base_vertex_property_type;
  1163. /// The type of the distributed adjacency list (this type)
  1164. typedef typename config_type::graph_type graph_type;
  1165. /// Expose graph components and graph category
  1166. typedef typename traits_type::local_vertex_descriptor
  1167. local_vertex_descriptor;
  1168. typedef typename traits_type::local_edge_descriptor
  1169. local_edge_descriptor;
  1170. typedef typename traits_type::vertex_descriptor vertex_descriptor;
  1171. typedef typename traits_type::edge_descriptor edge_descriptor;
  1172. typedef typename traits_type::directed_category directed_category;
  1173. typedef typename inherited::edge_parallel_category
  1174. edge_parallel_category;
  1175. typedef typename inherited::graph_tag graph_tag;
  1176. // Current implementation requires the ability to have parallel
  1177. // edges in the underlying adjacency_list. Which processor each
  1178. // edge refers to is attached as an internal property. TBD:
  1179. // remove this restriction, which may require some rewriting.
  1180. BOOST_STATIC_ASSERT((is_same<edge_parallel_category,
  1181. allow_parallel_edge_tag>::value));
  1182. /** Determine the graph traversal category.
  1183. *
  1184. * A directed distributed adjacency list models the Distributed
  1185. * Graph, Incidence Graph, and Adjacency Graph
  1186. * concepts. Bidirectional and undirected graphs also model the
  1187. * Bidirectional Graph concept. Note that when modeling these
  1188. * concepts the domains of certain operations (e.g., in_edges)
  1189. * are restricted; see the distributed adjacency_list
  1190. * documentation.
  1191. */
  1192. typedef typename boost::mpl::if_<
  1193. is_same<DirectedS, directedS>,
  1194. directed_distributed_adj_list_tag,
  1195. typename boost::mpl::if_<is_same<DirectedS, bidirectionalS>,
  1196. bidirectional_distributed_adj_list_tag,
  1197. undirected_distributed_adj_list_tag>::type>
  1198. ::type traversal_category;
  1199. typedef typename inherited::degree_size_type degree_size_type;
  1200. typedef typename inherited::vertices_size_type vertices_size_type;
  1201. typedef typename inherited::edges_size_type edges_size_type;
  1202. typedef VertexProperty vertex_property_type;
  1203. typedef EdgeProperty edge_property_type;
  1204. typedef typename inherited::graph_property_type graph_property_type;
  1205. typedef typename inherited::vertex_bundled vertex_bundled;
  1206. typedef typename inherited::edge_bundled edge_bundled;
  1207. typedef typename inherited::graph_bundled graph_bundled;
  1208. typedef typename container_gen<edge_list_selector, edge_descriptor>::type
  1209. local_edge_list_type;
  1210. private:
  1211. typedef typename boost::mpl::if_<is_same<DirectedS, bidirectionalS>,
  1212. typename in_edge_list_type::const_iterator,
  1213. typename inherited::out_edge_iterator>::type
  1214. base_in_edge_iterator;
  1215. typedef typename inherited::out_edge_iterator base_out_edge_iterator;
  1216. typedef typename graph_traits<inherited>::edge_iterator
  1217. base_edge_iterator;
  1218. typedef typename inherited::edge_property_type base_edge_property_type;
  1219. typedef typename local_edge_list_type::const_iterator
  1220. undirected_edge_iterator;
  1221. typedef InDistribution in_distribution_type;
  1222. typedef parallel::trigger_receive_context trigger_receive_context;
  1223. public:
  1224. /// Iterator over the (local) vertices of the graph
  1225. typedef transform_iterator<typename vertex_descriptor::generator,
  1226. typename inherited::vertex_iterator>
  1227. vertex_iterator;
  1228. /// Helper for out_edge_iterator
  1229. typedef typename edge_descriptor::template out_generator<adjacency_list>
  1230. out_edge_generator;
  1231. /// Iterator over the outgoing edges of a vertex
  1232. typedef transform_iterator<out_edge_generator,
  1233. typename inherited::out_edge_iterator>
  1234. out_edge_iterator;
  1235. /// Helper for in_edge_iterator
  1236. typedef typename edge_descriptor::template in_generator<adjacency_list>
  1237. in_edge_generator;
  1238. /// Iterator over the incoming edges of a vertex
  1239. typedef transform_iterator<in_edge_generator, base_in_edge_iterator>
  1240. in_edge_iterator;
  1241. /// Iterator over the neighbors of a vertex
  1242. typedef boost::adjacency_iterator<
  1243. adjacency_list, vertex_descriptor, out_edge_iterator,
  1244. typename detail::iterator_traits<base_out_edge_iterator>
  1245. ::difference_type>
  1246. adjacency_iterator;
  1247. /// Iterator over the (local) edges in a graph
  1248. typedef typename boost::mpl::if_<is_same<DirectedS, undirectedS>,
  1249. undirected_edge_iterator,
  1250. transform_iterator<out_edge_generator,
  1251. base_edge_iterator>
  1252. >::type
  1253. edge_iterator;
  1254. public:
  1255. /// The type of the mixin for named vertices
  1256. typedef graph::distributed::maybe_named_graph<graph_type,
  1257. vertex_descriptor,
  1258. edge_descriptor,
  1259. config_type>
  1260. named_graph_mixin;
  1261. /// Process group used for communication
  1262. typedef ProcessGroup process_group_type;
  1263. /// How to refer to a process
  1264. typedef typename process_group_type::process_id_type process_id_type;
  1265. /// Whether this graph is directed, undirected, or bidirectional
  1266. typedef DirectedS directed_selector;
  1267. // Structure used for the lazy addition of vertices
  1268. struct lazy_add_vertex_with_property;
  1269. friend struct lazy_add_vertex_with_property;
  1270. // Structure used for the lazy addition of edges
  1271. struct lazy_add_edge;
  1272. friend struct lazy_add_edge;
  1273. // Structure used for the lazy addition of edges with properties
  1274. struct lazy_add_edge_with_property;
  1275. friend struct lazy_add_edge_with_property;
  1276. /// default_distribution_type is the type of the distribution used if the
  1277. /// user didn't specify an explicit one
  1278. typedef typename graph::distributed::select_distribution<
  1279. InDistribution, VertexProperty, vertices_size_type,
  1280. ProcessGroup>::default_type
  1281. default_distribution_type;
  1282. /// distribution_type is the type of the distribution instance stored in
  1283. /// the maybe_named_graph base class
  1284. typedef typename graph::distributed::select_distribution<
  1285. InDistribution, VertexProperty, vertices_size_type,
  1286. ProcessGroup>::type
  1287. base_distribution_type;
  1288. typedef graph::distributed::shuffled_distribution<
  1289. base_distribution_type> distribution_type;
  1290. private:
  1291. // FIXME: the original adjacency_list contained this comment:
  1292. // Default copy constructor and copy assignment operators OK??? TBD
  1293. // but the adj_list_impl contained these declarations:
  1294. adjacency_list(const adjacency_list& other);
  1295. adjacency_list& operator=(const adjacency_list& other);
  1296. public:
  1297. adjacency_list(const ProcessGroup& pg = ProcessGroup())
  1298. : named_graph_mixin(pg, default_distribution_type(pg, 0)),
  1299. m_local_graph(GraphProperty()),
  1300. process_group_(pg, graph::parallel::attach_distributed_object())
  1301. {
  1302. setup_triggers();
  1303. }
  1304. adjacency_list(const ProcessGroup& pg,
  1305. const base_distribution_type& distribution)
  1306. : named_graph_mixin(pg, distribution),
  1307. m_local_graph(GraphProperty()),
  1308. process_group_(pg, graph::parallel::attach_distributed_object())
  1309. {
  1310. setup_triggers();
  1311. }
  1312. adjacency_list(const GraphProperty& g,
  1313. const ProcessGroup& pg = ProcessGroup())
  1314. : named_graph_mixin(pg, default_distribution_type(pg, 0)),
  1315. m_local_graph(g),
  1316. process_group_(pg, graph::parallel::attach_distributed_object())
  1317. {
  1318. setup_triggers();
  1319. }
  1320. adjacency_list(vertices_size_type n,
  1321. const GraphProperty& p,
  1322. const ProcessGroup& pg,
  1323. const base_distribution_type& distribution)
  1324. : named_graph_mixin(pg, distribution),
  1325. m_local_graph(distribution.block_size(process_id(pg), n), p),
  1326. process_group_(pg, graph::parallel::attach_distributed_object())
  1327. {
  1328. setup_triggers();
  1329. detail::parallel::maybe_initialize_vertex_indices(vertices(base()),
  1330. get(vertex_index, base()));
  1331. }
  1332. adjacency_list(vertices_size_type n,
  1333. const ProcessGroup& pg,
  1334. const base_distribution_type& distribution)
  1335. : named_graph_mixin(pg, distribution),
  1336. m_local_graph(distribution.block_size(process_id(pg), n), GraphProperty()),
  1337. process_group_(pg, graph::parallel::attach_distributed_object())
  1338. {
  1339. setup_triggers();
  1340. detail::parallel::maybe_initialize_vertex_indices(vertices(base()),
  1341. get(vertex_index, base()));
  1342. }
  1343. adjacency_list(vertices_size_type n,
  1344. const GraphProperty& p,
  1345. const ProcessGroup& pg = ProcessGroup())
  1346. : named_graph_mixin(pg, default_distribution_type(pg, n)),
  1347. m_local_graph(this->distribution().block_size(process_id(pg), n), p),
  1348. process_group_(pg, graph::parallel::attach_distributed_object())
  1349. {
  1350. setup_triggers();
  1351. detail::parallel::maybe_initialize_vertex_indices(vertices(base()),
  1352. get(vertex_index, base()));
  1353. }
  1354. adjacency_list(vertices_size_type n,
  1355. const ProcessGroup& pg = ProcessGroup())
  1356. : named_graph_mixin(pg, default_distribution_type(pg, n)),
  1357. m_local_graph(this->distribution().block_size(process_id(pg), n),
  1358. GraphProperty()),
  1359. process_group_(pg, graph::parallel::attach_distributed_object())
  1360. {
  1361. setup_triggers();
  1362. detail::parallel::maybe_initialize_vertex_indices(vertices(base()),
  1363. get(vertex_index, base()));
  1364. }
  1365. /*
  1366. * We assume that every processor sees the same list of edges, so
  1367. * they skip over any that don't originate from themselves. This
  1368. * means that programs switching between a local and a distributed
  1369. * graph will keep the same semantics.
  1370. */
  1371. template <class EdgeIterator>
  1372. adjacency_list(EdgeIterator first, EdgeIterator last,
  1373. vertices_size_type n,
  1374. const ProcessGroup& pg = ProcessGroup(),
  1375. const GraphProperty& p = GraphProperty())
  1376. : named_graph_mixin(pg, default_distribution_type(pg, n)),
  1377. m_local_graph(this->distribution().block_size(process_id(pg), n), p),
  1378. process_group_(pg, graph::parallel::attach_distributed_object())
  1379. {
  1380. setup_triggers();
  1381. typedef typename config_type::VertexListS vertex_list_selector;
  1382. initialize(first, last, n, this->distribution(), vertex_list_selector());
  1383. detail::parallel::maybe_initialize_vertex_indices(vertices(base()),
  1384. get(vertex_index, base()));
  1385. }
  1386. template <class EdgeIterator, class EdgePropertyIterator>
  1387. adjacency_list(EdgeIterator first, EdgeIterator last,
  1388. EdgePropertyIterator ep_iter,
  1389. vertices_size_type n,
  1390. const ProcessGroup& pg = ProcessGroup(),
  1391. const GraphProperty& p = GraphProperty())
  1392. : named_graph_mixin(pg, default_distribution_type(pg, n)),
  1393. m_local_graph(this->distribution().block_size(process_id(pg), n), p),
  1394. process_group_(pg, graph::parallel::attach_distributed_object())
  1395. {
  1396. setup_triggers();
  1397. typedef typename config_type::VertexListS vertex_list_selector;
  1398. initialize(first, last, ep_iter, n, this->distribution(),
  1399. vertex_list_selector());
  1400. detail::parallel::maybe_initialize_vertex_indices(vertices(base()),
  1401. get(vertex_index, base()));
  1402. }
  1403. template <class EdgeIterator>
  1404. adjacency_list(EdgeIterator first, EdgeIterator last,
  1405. vertices_size_type n,
  1406. const ProcessGroup& pg,
  1407. const base_distribution_type& distribution,
  1408. const GraphProperty& p = GraphProperty())
  1409. : named_graph_mixin(pg, distribution),
  1410. m_local_graph(distribution.block_size(process_id(pg), n), p),
  1411. process_group_(pg, graph::parallel::attach_distributed_object())
  1412. {
  1413. setup_triggers();
  1414. typedef typename config_type::VertexListS vertex_list_selector;
  1415. initialize(first, last, n, this->distribution(), vertex_list_selector());
  1416. detail::parallel::maybe_initialize_vertex_indices(vertices(base()),
  1417. get(vertex_index, base()));
  1418. }
  1419. template <class EdgeIterator, class EdgePropertyIterator>
  1420. adjacency_list(EdgeIterator first, EdgeIterator last,
  1421. EdgePropertyIterator ep_iter,
  1422. vertices_size_type n,
  1423. const ProcessGroup& pg,
  1424. const base_distribution_type& distribution,
  1425. const GraphProperty& p = GraphProperty())
  1426. : named_graph_mixin(pg, distribution),
  1427. m_local_graph(this->distribution().block_size(process_id(pg), n), p),
  1428. process_group_(pg, graph::parallel::attach_distributed_object())
  1429. {
  1430. setup_triggers();
  1431. typedef typename config_type::VertexListS vertex_list_selector;
  1432. initialize(first, last, ep_iter, n, distribution,
  1433. vertex_list_selector());
  1434. detail::parallel::maybe_initialize_vertex_indices(vertices(base()),
  1435. get(vertex_index, base()));
  1436. }
  1437. ~adjacency_list()
  1438. {
  1439. synchronize(process_group_);
  1440. }
  1441. void clear()
  1442. {
  1443. base().clear();
  1444. local_edges_.clear();
  1445. named_graph_mixin::clearing_graph();
  1446. }
  1447. void swap(adjacency_list& other)
  1448. {
  1449. using std::swap;
  1450. base().swap(other);
  1451. swap(process_group_, other.process_group_);
  1452. }
  1453. static vertex_descriptor null_vertex()
  1454. {
  1455. return vertex_descriptor(processor_id_type(0),
  1456. inherited::null_vertex());
  1457. }
  1458. inherited& base() { return m_local_graph; }
  1459. const inherited& base() const { return m_local_graph; }
  1460. processor_id_type processor() const { return process_id(process_group_); }
  1461. process_group_type process_group() const { return process_group_.base(); }
  1462. local_edge_list_type& local_edges() { return local_edges_; }
  1463. const local_edge_list_type& local_edges() const { return local_edges_; }
  1464. // Redistribute the vertices of the graph by placing each vertex
  1465. // v on the processor get(vertex_to_processor, v).
  1466. template<typename VertexProcessorMap>
  1467. void redistribute(VertexProcessorMap vertex_to_processor);
  1468. // Directly access a vertex or edge bundle
  1469. vertex_bundled& operator[](vertex_descriptor v)
  1470. {
  1471. BOOST_ASSERT(v.owner == processor());
  1472. return base()[v.local];
  1473. }
  1474. const vertex_bundled& operator[](vertex_descriptor v) const
  1475. {
  1476. BOOST_ASSERT(v.owner == processor());
  1477. return base()[v.local];
  1478. }
  1479. edge_bundled& operator[](edge_descriptor e)
  1480. {
  1481. BOOST_ASSERT(e.owner() == processor());
  1482. return base()[e.local];
  1483. }
  1484. const edge_bundled& operator[](edge_descriptor e) const
  1485. {
  1486. BOOST_ASSERT(e.owner() == processor());
  1487. return base()[e.local];
  1488. }
  1489. graph_bundled& operator[](graph_bundle_t)
  1490. { return get_property(*this); }
  1491. graph_bundled const& operator[](graph_bundle_t) const
  1492. { return get_property(*this); }
  1493. template<typename OStreamConstructibleArchive>
  1494. void save(std::string const& filename) const;
  1495. template<typename IStreamConstructibleArchive>
  1496. void load(std::string const& filename);
  1497. // Callback that will be invoked whenever a new vertex is added locally
  1498. boost::function<void(vertex_descriptor, adjacency_list&)> on_add_vertex;
  1499. // Callback that will be invoked whenever a new edge is added locally
  1500. boost::function<void(edge_descriptor, adjacency_list&)> on_add_edge;
  1501. private:
  1502. // Request vertex->processor mapping for neighbors <does nothing>
  1503. template<typename VertexProcessorMap>
  1504. void
  1505. request_in_neighbors(vertex_descriptor,
  1506. VertexProcessorMap,
  1507. directedS) { }
  1508. // Request vertex->processor mapping for neighbors <does nothing>
  1509. template<typename VertexProcessorMap>
  1510. void
  1511. request_in_neighbors(vertex_descriptor,
  1512. VertexProcessorMap,
  1513. undirectedS) { }
  1514. // Request vertex->processor mapping for neighbors
  1515. template<typename VertexProcessorMap>
  1516. void
  1517. request_in_neighbors(vertex_descriptor v,
  1518. VertexProcessorMap vertex_to_processor,
  1519. bidirectionalS);
  1520. // Clear the list of in-edges, but don't tell the remote processor
  1521. void clear_in_edges_local(vertex_descriptor v, directedS) {}
  1522. void clear_in_edges_local(vertex_descriptor v, undirectedS) {}
  1523. void clear_in_edges_local(vertex_descriptor v, bidirectionalS)
  1524. { get(vertex_in_edges, base())[v.local].clear(); }
  1525. // Remove in-edges that have migrated <does nothing>
  1526. template<typename VertexProcessorMap>
  1527. void
  1528. remove_migrated_in_edges(vertex_descriptor,
  1529. VertexProcessorMap,
  1530. directedS) { }
  1531. // Remove in-edges that have migrated <does nothing>
  1532. template<typename VertexProcessorMap>
  1533. void
  1534. remove_migrated_in_edges(vertex_descriptor,
  1535. VertexProcessorMap,
  1536. undirectedS) { }
  1537. // Remove in-edges that have migrated
  1538. template<typename VertexProcessorMap>
  1539. void
  1540. remove_migrated_in_edges(vertex_descriptor v,
  1541. VertexProcessorMap vertex_to_processor,
  1542. bidirectionalS);
  1543. // Initialize the graph with the given edge list and vertex
  1544. // distribution. This variation works only when
  1545. // VertexListS=vecS, and we know how to create remote vertex
  1546. // descriptors based solely on the distribution.
  1547. template<typename EdgeIterator>
  1548. void
  1549. initialize(EdgeIterator first, EdgeIterator last,
  1550. vertices_size_type, const base_distribution_type& distribution,
  1551. vecS);
  1552. // Initialize the graph with the given edge list, edge
  1553. // properties, and vertex distribution. This variation works
  1554. // only when VertexListS=vecS, and we know how to create remote
  1555. // vertex descriptors based solely on the distribution.
  1556. template<typename EdgeIterator, typename EdgePropertyIterator>
  1557. void
  1558. initialize(EdgeIterator first, EdgeIterator last,
  1559. EdgePropertyIterator ep_iter,
  1560. vertices_size_type, const base_distribution_type& distribution,
  1561. vecS);
  1562. // Initialize the graph with the given edge list, edge
  1563. // properties, and vertex distribution.
  1564. template<typename EdgeIterator, typename EdgePropertyIterator,
  1565. typename VertexListS>
  1566. void
  1567. initialize(EdgeIterator first, EdgeIterator last,
  1568. EdgePropertyIterator ep_iter,
  1569. vertices_size_type n,
  1570. const base_distribution_type& distribution,
  1571. VertexListS);
  1572. // Initialize the graph with the given edge list and vertex
  1573. // distribution. This is nearly identical to the one below it,
  1574. // for which I should be flogged. However, this version does use
  1575. // slightly less memory than the version that accepts an edge
  1576. // property iterator.
  1577. template<typename EdgeIterator, typename VertexListS>
  1578. void
  1579. initialize(EdgeIterator first, EdgeIterator last,
  1580. vertices_size_type n,
  1581. const base_distribution_type& distribution,
  1582. VertexListS);
  1583. public:
  1584. //---------------------------------------------------------------------
  1585. // Build a vertex property instance for the underlying adjacency
  1586. // list from the given property instance of the type exposed to
  1587. // the user.
  1588. base_vertex_property_type
  1589. build_vertex_property(const vertex_property_type& p)
  1590. { return build_vertex_property(p, directed_selector()); }
  1591. base_vertex_property_type
  1592. build_vertex_property(const vertex_property_type& p, directedS)
  1593. {
  1594. return base_vertex_property_type(p);
  1595. }
  1596. base_vertex_property_type
  1597. build_vertex_property(const vertex_property_type& p, bidirectionalS)
  1598. {
  1599. return base_vertex_property_type(in_edge_list_type(), p);
  1600. }
  1601. base_vertex_property_type
  1602. build_vertex_property(const vertex_property_type& p, undirectedS)
  1603. {
  1604. return base_vertex_property_type(p);
  1605. }
  1606. //---------------------------------------------------------------------
  1607. //---------------------------------------------------------------------
  1608. // Build an edge property instance for the underlying adjacency
  1609. // list from the given property instance of the type exposed to
  1610. // the user.
  1611. base_edge_property_type build_edge_property(const edge_property_type& p)
  1612. { return build_edge_property(p, directed_selector()); }
  1613. base_edge_property_type
  1614. build_edge_property(const edge_property_type& p, directedS)
  1615. {
  1616. return base_edge_property_type(0, p);
  1617. }
  1618. base_edge_property_type
  1619. build_edge_property(const edge_property_type& p, bidirectionalS)
  1620. {
  1621. return base_edge_property_type(0, p);
  1622. }
  1623. base_edge_property_type
  1624. build_edge_property(const edge_property_type& p, undirectedS)
  1625. {
  1626. typedef typename base_edge_property_type::next_type
  1627. edge_property_with_id;
  1628. return base_edge_property_type(true, edge_property_with_id(0, p));
  1629. }
  1630. //---------------------------------------------------------------------
  1631. /** The set of messages that can be transmitted and received by
  1632. * a distributed adjacency list. This list will eventually be
  1633. * exhaustive, but is currently quite limited.
  1634. */
  1635. enum {
  1636. /**
  1637. * Request to add or find a vertex with the given vertex
  1638. * property. The data will be a vertex_property_type
  1639. * structure.
  1640. */
  1641. msg_add_vertex_with_property = 0,
  1642. /**
  1643. * Request to add or find a vertex with the given vertex
  1644. * property, and request that the remote processor return the
  1645. * descriptor for the added/found edge. The data will be a
  1646. * vertex_property_type structure.
  1647. */
  1648. msg_add_vertex_with_property_and_reply,
  1649. /**
  1650. * Reply to a msg_add_vertex_* message, containing the local
  1651. * vertex descriptor that was added or found.
  1652. */
  1653. msg_add_vertex_reply,
  1654. /**
  1655. * Request to add an edge remotely. The data will be a
  1656. * msg_add_edge_data structure.
  1657. */
  1658. msg_add_edge,
  1659. /**
  1660. * Request to add an edge remotely. The data will be a
  1661. * msg_add_edge_with_property_data structure.
  1662. */
  1663. msg_add_edge_with_property,
  1664. /**
  1665. * Request to add an edge remotely and reply back with the
  1666. * edge descriptor. The data will be a
  1667. * msg_add_edge_data structure.
  1668. */
  1669. msg_add_edge_with_reply,
  1670. /**
  1671. * Request to add an edge remotely and reply back with the
  1672. * edge descriptor. The data will be a
  1673. * msg_add_edge_with_property_data structure.
  1674. */
  1675. msg_add_edge_with_property_and_reply,
  1676. /**
  1677. * Reply message responding to an @c msg_add_edge_with_reply
  1678. * or @c msg_add_edge_with_property_and_reply messages. The
  1679. * data will be a std::pair<edge_descriptor, bool>.
  1680. */
  1681. msg_add_edge_reply,
  1682. /**
  1683. * Indicates that a nonlocal edge has been created that should
  1684. * be added locally. Only valid for bidirectional and
  1685. * undirected graphs. The message carries a
  1686. * msg_nonlocal_edge_data structure.
  1687. */
  1688. msg_nonlocal_edge,
  1689. /**
  1690. * Indicates that a remote edge should be removed. This
  1691. * message does not exist for directedS graphs but may refer
  1692. * to either in-edges or out-edges for undirectedS graphs.
  1693. */
  1694. msg_remove_edge,
  1695. /**
  1696. * Indicates the number of vertices and edges that will be
  1697. * relocated from the source processor to the target
  1698. * processor. The data will be a pair<vertices_size_type,
  1699. * edges_size_type>.
  1700. */
  1701. msg_num_relocated
  1702. };
  1703. typedef detail::parallel::msg_add_edge_data<vertex_descriptor,
  1704. local_vertex_descriptor>
  1705. msg_add_edge_data;
  1706. typedef detail::parallel::msg_add_edge_with_property_data
  1707. <vertex_descriptor, local_vertex_descriptor,
  1708. edge_property_type> msg_add_edge_with_property_data;
  1709. typedef boost::detail::parallel::msg_nonlocal_edge_data<
  1710. edge_property_type,local_edge_descriptor> msg_nonlocal_edge_data;
  1711. typedef boost::detail::parallel::msg_remove_edge_data<edge_descriptor>
  1712. msg_remove_edge_data;
  1713. void send_remove_edge_request(edge_descriptor e)
  1714. {
  1715. process_id_type dest = e.target_processor;
  1716. if (e.target_processor == process_id(process_group_))
  1717. dest = e.source_processor;
  1718. send(process_group_, dest, msg_remove_edge, msg_remove_edge_data(e));
  1719. }
  1720. /// Process incoming messages.
  1721. void setup_triggers();
  1722. void
  1723. handle_add_vertex_with_property(int source, int tag,
  1724. const vertex_property_type&,
  1725. trigger_receive_context);
  1726. local_vertex_descriptor
  1727. handle_add_vertex_with_property_and_reply(int source, int tag,
  1728. const vertex_property_type&,
  1729. trigger_receive_context);
  1730. void
  1731. handle_add_edge(int source, int tag, const msg_add_edge_data& data,
  1732. trigger_receive_context);
  1733. boost::parallel::detail::untracked_pair<edge_descriptor, bool>
  1734. handle_add_edge_with_reply(int source, int tag,
  1735. const msg_add_edge_data& data,
  1736. trigger_receive_context);
  1737. void
  1738. handle_add_edge_with_property(int source, int tag,
  1739. const msg_add_edge_with_property_data&,
  1740. trigger_receive_context);
  1741. boost::parallel::detail::untracked_pair<edge_descriptor, bool>
  1742. handle_add_edge_with_property_and_reply
  1743. (int source, int tag, const msg_add_edge_with_property_data&,
  1744. trigger_receive_context);
  1745. void
  1746. handle_nonlocal_edge(int source, int tag,
  1747. const msg_nonlocal_edge_data& data,
  1748. trigger_receive_context);
  1749. void
  1750. handle_remove_edge(int source, int tag,
  1751. const msg_remove_edge_data& data,
  1752. trigger_receive_context);
  1753. protected:
  1754. /** Add an edge (locally) that was received from another
  1755. * processor. This operation is a no-op for directed graphs,
  1756. * because all edges reside on the local processor. For
  1757. * bidirectional graphs, this routine places the edge onto the
  1758. * list of incoming edges for the target vertex. For undirected
  1759. * graphs, the edge is placed along with all of the other edges
  1760. * for the target vertex, but it is marked as a non-local edge
  1761. * descriptor.
  1762. *
  1763. * \todo There is a potential problem here, where we could
  1764. * unintentionally allow duplicate edges in undirected graphs
  1765. * because the same edge is added on two different processors
  1766. * simultaneously. It's not an issue now, because we require
  1767. * that the graph allow parallel edges. Once we do support
  1768. * containers such as setS or hash_setS that disallow parallel
  1769. * edges we will need to deal with this.
  1770. */
  1771. void
  1772. add_remote_edge(const msg_nonlocal_edge_data&,
  1773. processor_id_type, directedS)
  1774. { }
  1775. /**
  1776. * \overload
  1777. */
  1778. void
  1779. add_remote_edge(const msg_nonlocal_edge_data& data,
  1780. processor_id_type other_proc, bidirectionalS)
  1781. {
  1782. typedef detail::parallel::stored_in_edge<local_edge_descriptor> stored_edge;
  1783. stored_edge edge(other_proc, data.e);
  1784. local_vertex_descriptor v = target(data.e, base());
  1785. boost::graph_detail::push(get(vertex_in_edges, base())[v], edge);
  1786. }
  1787. /**
  1788. * \overload
  1789. */
  1790. void
  1791. add_remote_edge(const msg_nonlocal_edge_data& data,
  1792. processor_id_type other_proc, undirectedS)
  1793. {
  1794. std::pair<local_edge_descriptor, bool> edge =
  1795. detail::parallel::add_local_edge(target(data.e, base()),
  1796. source(data.e, base()),
  1797. build_edge_property(data.get_property()), base());
  1798. BOOST_ASSERT(edge.second);
  1799. put(edge_target_processor_id, base(), edge.first, other_proc);
  1800. if (edge.second && on_add_edge)
  1801. on_add_edge(edge_descriptor(processor(), other_proc, false,
  1802. edge.first),
  1803. *this);
  1804. }
  1805. void
  1806. remove_local_edge(const msg_remove_edge_data&, processor_id_type,
  1807. directedS)
  1808. { }
  1809. void
  1810. remove_local_edge(const msg_remove_edge_data& data,
  1811. processor_id_type other_proc, bidirectionalS)
  1812. {
  1813. /* When the source is local, we first check if the edge still
  1814. * exists (it may have been deleted locally) and, if so,
  1815. * remove it locally.
  1816. */
  1817. vertex_descriptor src = source(data.e, *this);
  1818. vertex_descriptor tgt = target(data.e, *this);
  1819. if (src.owner == process_id(process_group_)) {
  1820. base_out_edge_iterator ei, ei_end;
  1821. for (boost::tie(ei, ei_end) = out_edges(src.local, base());
  1822. ei != ei_end; ++ei) {
  1823. // TBD: can't check the descriptor here, because it could
  1824. // have changed if we're allowing the removal of
  1825. // edges. Egads!
  1826. if (tgt.local == target(*ei, base())
  1827. && get(edge_target_processor_id, base(), *ei) == other_proc)
  1828. break;
  1829. }
  1830. if (ei != ei_end) boost::remove_edge(ei, base());
  1831. remove_local_edge_from_list(src, tgt, undirectedS());
  1832. } else {
  1833. BOOST_ASSERT(tgt.owner == process_id(process_group_));
  1834. in_edge_list_type& in_edges =
  1835. get(vertex_in_edges, base())[tgt.local];
  1836. typename in_edge_list_type::iterator ei;
  1837. for (ei = in_edges.begin(); ei != in_edges.end(); ++ei) {
  1838. if (src.local == source(ei->e, base())
  1839. && src.owner == ei->source_processor)
  1840. break;
  1841. }
  1842. if (ei != in_edges.end()) in_edges.erase(ei);
  1843. }
  1844. }
  1845. void
  1846. remove_local_edge(const msg_remove_edge_data& data,
  1847. processor_id_type other_proc, undirectedS)
  1848. {
  1849. vertex_descriptor local_vertex = source(data.e, *this);
  1850. vertex_descriptor remote_vertex = target(data.e, *this);
  1851. if (remote_vertex.owner == process_id(process_group_)) {
  1852. using std::swap;
  1853. swap(local_vertex, remote_vertex);
  1854. }
  1855. // Remove the edge from the out-edge list, if it is there
  1856. {
  1857. base_out_edge_iterator ei, ei_end;
  1858. for (boost::tie(ei, ei_end) = out_edges(local_vertex.local, base());
  1859. ei != ei_end; ++ei) {
  1860. // TBD: can't check the descriptor here, because it could
  1861. // have changed if we're allowing the removal of
  1862. // edges. Egads!
  1863. if (remote_vertex.local == target(*ei, base())
  1864. && get(edge_target_processor_id, base(), *ei) == other_proc)
  1865. break;
  1866. }
  1867. if (ei != ei_end) boost::remove_edge(ei, base());
  1868. }
  1869. remove_local_edge_from_list(local_vertex, remote_vertex, undirectedS());
  1870. }
  1871. public:
  1872. void
  1873. remove_local_edge_from_list(vertex_descriptor, vertex_descriptor,
  1874. directedS)
  1875. {
  1876. }
  1877. void
  1878. remove_local_edge_from_list(vertex_descriptor, vertex_descriptor,
  1879. bidirectionalS)
  1880. {
  1881. }
  1882. void
  1883. remove_local_edge_from_list(vertex_descriptor src, vertex_descriptor tgt,
  1884. undirectedS)
  1885. {
  1886. // TBD: At some point we'll be able to improve the speed here
  1887. // because we'll know when the edge can't be in the local
  1888. // list.
  1889. {
  1890. typename local_edge_list_type::iterator ei;
  1891. for (ei = local_edges_.begin(); ei != local_edges_.end(); ++ei) {
  1892. if ((source(*ei, *this) == src
  1893. && target(*ei, *this) == tgt)
  1894. || (source(*ei, *this) == tgt
  1895. && target(*ei, *this) == src))
  1896. break;
  1897. }
  1898. if (ei != local_edges_.end()) local_edges_.erase(ei);
  1899. }
  1900. }
  1901. private:
  1902. /// The local subgraph
  1903. inherited m_local_graph;
  1904. /// The process group through which this distributed graph
  1905. /// communicates.
  1906. process_group_type process_group_;
  1907. // TBD: should only be available for undirected graphs, but for
  1908. // now it'll just be empty for directed and bidirectional
  1909. // graphs.
  1910. local_edge_list_type local_edges_;
  1911. };
  1912. //------------------------------------------------------------------------
  1913. // Lazy addition of vertices
  1914. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  1915. struct PBGL_DISTRIB_ADJLIST_TYPE::lazy_add_vertex_with_property
  1916. {
  1917. /// Construct a lazy request to add a vertex
  1918. lazy_add_vertex_with_property(adjacency_list& self,
  1919. const vertex_property_type& property)
  1920. : self(self), property(property), committed(false) { }
  1921. /// Copying a lazy_add_vertex_with_property transfers the
  1922. /// responsibility for adding the vertex to the newly-constructed
  1923. /// object.
  1924. lazy_add_vertex_with_property(const lazy_add_vertex_with_property& other)
  1925. : self(other.self), property(other.property),
  1926. committed(other.committed)
  1927. {
  1928. other.committed = true;
  1929. }
  1930. /// If the vertex has not yet been added, add the vertex but don't
  1931. /// wait for a reply.
  1932. ~lazy_add_vertex_with_property();
  1933. /// Returns commit().
  1934. operator vertex_descriptor() const { return commit(); }
  1935. // Add the vertex. This operation will block if the vertex is
  1936. // being added remotely.
  1937. vertex_descriptor commit() const;
  1938. protected:
  1939. adjacency_list& self;
  1940. vertex_property_type property;
  1941. mutable bool committed;
  1942. private:
  1943. // No copy-assignment semantics
  1944. void operator=(lazy_add_vertex_with_property&);
  1945. };
  1946. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  1947. PBGL_DISTRIB_ADJLIST_TYPE::lazy_add_vertex_with_property::
  1948. ~lazy_add_vertex_with_property()
  1949. {
  1950. /// If this vertex has already been created or will be created by
  1951. /// someone else, or if someone threw an exception, we will not
  1952. /// create the vertex now.
  1953. if (committed || std::uncaught_exception())
  1954. return;
  1955. committed = true;
  1956. process_id_type owner
  1957. = static_cast<graph_type&>(self).owner_by_property(property);
  1958. if (owner == self.processor()) {
  1959. /// Add the vertex locally.
  1960. vertex_descriptor v(owner,
  1961. add_vertex(self.build_vertex_property(property),
  1962. self.base()));
  1963. if (self.on_add_vertex)
  1964. self.on_add_vertex(v, self);
  1965. }
  1966. else
  1967. /// Ask the owner of this new vertex to add the vertex. We
  1968. /// don't need a reply.
  1969. send(self.process_group_, owner, msg_add_vertex_with_property,
  1970. property);
  1971. }
  1972. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  1973. typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor
  1974. PBGL_DISTRIB_ADJLIST_TYPE::lazy_add_vertex_with_property::
  1975. commit() const
  1976. {
  1977. BOOST_ASSERT(!this->committed);
  1978. this->committed = true;
  1979. process_id_type owner
  1980. = static_cast<graph_type&>(self).owner_by_property(property);
  1981. local_vertex_descriptor local_v;
  1982. if (owner == self.processor())
  1983. /// Add the vertex locally.
  1984. local_v = add_vertex(self.build_vertex_property(property),
  1985. self.base());
  1986. else {
  1987. // Request that the remote process add the vertex immediately
  1988. send_oob_with_reply(self.process_group_, owner,
  1989. msg_add_vertex_with_property_and_reply, property,
  1990. local_v);
  1991. }
  1992. vertex_descriptor v(owner, local_v);
  1993. if (self.on_add_vertex)
  1994. self.on_add_vertex(v, self);
  1995. // Build the full vertex descriptor to return
  1996. return v;
  1997. }
  1998. /**
  1999. * Data structure returned from add_edge that will "lazily" add
  2000. * the edge, either when it is converted to a
  2001. * @c pair<edge_descriptor, bool> or when the most recent copy has
  2002. * been destroyed.
  2003. */
  2004. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2005. struct PBGL_DISTRIB_ADJLIST_TYPE::lazy_add_edge
  2006. {
  2007. /// Construct a lazy request to add an edge
  2008. lazy_add_edge(adjacency_list& self,
  2009. vertex_descriptor source, vertex_descriptor target)
  2010. : self(self), source(source), target(target), committed(false) { }
  2011. /// Copying a lazy_add_edge transfers the responsibility for
  2012. /// adding the edge to the newly-constructed object.
  2013. lazy_add_edge(const lazy_add_edge& other)
  2014. : self(other.self), source(other.source), target(other.target),
  2015. committed(other.committed)
  2016. {
  2017. other.committed = true;
  2018. }
  2019. /// If the edge has not yet been added, add the edge but don't
  2020. /// wait for a reply.
  2021. ~lazy_add_edge();
  2022. /// Returns commit().
  2023. operator std::pair<edge_descriptor, bool>() const { return commit(); }
  2024. // Add the edge. This operation will block if a remote edge is
  2025. // being added.
  2026. std::pair<edge_descriptor, bool> commit() const;
  2027. protected:
  2028. std::pair<edge_descriptor, bool>
  2029. add_local_edge(const edge_property_type& property, directedS) const;
  2030. std::pair<edge_descriptor, bool>
  2031. add_local_edge(const edge_property_type& property, bidirectionalS) const;
  2032. std::pair<edge_descriptor, bool>
  2033. add_local_edge(const edge_property_type& property, undirectedS) const;
  2034. adjacency_list& self;
  2035. vertex_descriptor source;
  2036. vertex_descriptor target;
  2037. mutable bool committed;
  2038. private:
  2039. // No copy-assignment semantics
  2040. void operator=(lazy_add_edge&);
  2041. };
  2042. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2043. PBGL_DISTRIB_ADJLIST_TYPE::lazy_add_edge::~lazy_add_edge()
  2044. {
  2045. /// If this edge has already been created or will be created by
  2046. /// someone else, or if someone threw an exception, we will not
  2047. /// create the edge now.
  2048. if (committed || std::uncaught_exception())
  2049. return;
  2050. committed = true;
  2051. if (source.owner == self.processor())
  2052. this->add_local_edge(edge_property_type(), DirectedS());
  2053. else
  2054. // Request that the remote processor add an edge and, but
  2055. // don't wait for a reply.
  2056. send(self.process_group_, source.owner, msg_add_edge,
  2057. msg_add_edge_data(source, target));
  2058. }
  2059. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2060. std::pair<typename PBGL_DISTRIB_ADJLIST_TYPE::edge_descriptor, bool>
  2061. PBGL_DISTRIB_ADJLIST_TYPE::lazy_add_edge::commit() const
  2062. {
  2063. BOOST_ASSERT(!committed);
  2064. committed = true;
  2065. if (source.owner == self.processor())
  2066. return this->add_local_edge(edge_property_type(), DirectedS());
  2067. else {
  2068. // Request that the remote processor add an edge
  2069. boost::parallel::detail::untracked_pair<edge_descriptor, bool> result;
  2070. send_oob_with_reply(self.process_group_, source.owner,
  2071. msg_add_edge_with_reply,
  2072. msg_add_edge_data(source, target), result);
  2073. return result;
  2074. }
  2075. }
  2076. // Add a local edge into a directed graph
  2077. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2078. std::pair<typename PBGL_DISTRIB_ADJLIST_TYPE::edge_descriptor, bool>
  2079. PBGL_DISTRIB_ADJLIST_TYPE::lazy_add_edge::
  2080. add_local_edge(const edge_property_type& property, directedS) const
  2081. {
  2082. // Add the edge to the local part of the graph
  2083. std::pair<local_edge_descriptor, bool> inserted =
  2084. detail::parallel::add_local_edge(source.local, target.local,
  2085. self.build_edge_property(property),
  2086. self.base());
  2087. if (inserted.second)
  2088. // Keep track of the owner of the target
  2089. put(edge_target_processor_id, self.base(), inserted.first,
  2090. target.owner);
  2091. // Compose the edge descriptor and return the result
  2092. edge_descriptor e(source.owner, target.owner, true, inserted.first);
  2093. // Trigger the on_add_edge event
  2094. if (inserted.second && self.on_add_edge)
  2095. self.on_add_edge(e, self);
  2096. return std::pair<edge_descriptor, bool>(e, inserted.second);
  2097. }
  2098. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2099. std::pair<typename PBGL_DISTRIB_ADJLIST_TYPE::edge_descriptor, bool>
  2100. PBGL_DISTRIB_ADJLIST_TYPE::lazy_add_edge::
  2101. add_local_edge(const edge_property_type& property, bidirectionalS) const
  2102. {
  2103. // Add the directed edge.
  2104. std::pair<edge_descriptor, bool> result
  2105. = this->add_local_edge(property, directedS());
  2106. if (result.second) {
  2107. if (target.owner == self.processor()) {
  2108. // Edge is local, so add the stored edge to the in_edges list
  2109. typedef detail::parallel::stored_in_edge<local_edge_descriptor>
  2110. stored_edge;
  2111. stored_edge e(self.processor(), result.first.local);
  2112. boost::graph_detail::push(get(vertex_in_edges,
  2113. self.base())[target.local], e);
  2114. }
  2115. else {
  2116. // Edge is remote, so notify the target's owner that an edge
  2117. // has been added.
  2118. if (self.process_group_.trigger_context() == graph::parallel::trc_out_of_band)
  2119. send_oob(self.process_group_, target.owner, msg_nonlocal_edge,
  2120. msg_nonlocal_edge_data(result.first.local, property));
  2121. else
  2122. send(self.process_group_, target.owner, msg_nonlocal_edge,
  2123. msg_nonlocal_edge_data(result.first.local, property));
  2124. }
  2125. }
  2126. return result;
  2127. }
  2128. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2129. std::pair<typename PBGL_DISTRIB_ADJLIST_TYPE::edge_descriptor, bool>
  2130. PBGL_DISTRIB_ADJLIST_TYPE::lazy_add_edge::
  2131. add_local_edge(const edge_property_type& property, undirectedS) const
  2132. {
  2133. // Add the directed edge
  2134. std::pair<edge_descriptor, bool> result
  2135. = this->add_local_edge(property, directedS());
  2136. typedef detail::parallel::stored_in_edge<local_edge_descriptor>
  2137. stored_edge;
  2138. if (result.second) {
  2139. if (target.owner == self.processor()) {
  2140. // Edge is local, so add the new edge to the list
  2141. // TODO: This is not what we want to do for an undirected
  2142. // edge, because we haven't linked the source and target's
  2143. // representations of those edges.
  2144. local_edge_descriptor return_edge =
  2145. detail::parallel::add_local_edge(target.local, source.local,
  2146. self.build_edge_property(property),
  2147. self.base()).first;
  2148. put(edge_target_processor_id, self.base(), return_edge,
  2149. source.owner);
  2150. }
  2151. else {
  2152. // Edge is remote, so notify the target's owner that an edge
  2153. // has been added.
  2154. if (self.process_group_.trigger_context() == graph::parallel::trc_out_of_band)
  2155. send_oob(self.process_group_, target.owner, msg_nonlocal_edge,
  2156. msg_nonlocal_edge_data(result.first.local, property));
  2157. else
  2158. send(self.process_group_, target.owner, msg_nonlocal_edge,
  2159. msg_nonlocal_edge_data(result.first.local, property));
  2160. }
  2161. // Add this edge to the list of local edges
  2162. graph_detail::push(self.local_edges(), result.first);
  2163. }
  2164. return result;
  2165. }
  2166. /**
  2167. * Data structure returned from add_edge that will "lazily" add
  2168. * the edge with its property, either when it is converted to a
  2169. * pair<edge_descriptor, bool> or when the most recent copy has
  2170. * been destroyed.
  2171. */
  2172. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2173. struct PBGL_DISTRIB_ADJLIST_TYPE::lazy_add_edge_with_property
  2174. : lazy_add_edge
  2175. {
  2176. /// Construct a lazy request to add an edge
  2177. lazy_add_edge_with_property(adjacency_list& self,
  2178. vertex_descriptor source,
  2179. vertex_descriptor target,
  2180. const edge_property_type& property)
  2181. : lazy_add_edge(self, source, target), property(property) { }
  2182. /// Copying a lazy_add_edge transfers the responsibility for
  2183. /// adding the edge to the newly-constructed object.
  2184. lazy_add_edge_with_property(const lazy_add_edge& other)
  2185. : lazy_add_edge(other), property(other.property) { }
  2186. /// If the edge has not yet been added, add the edge but don't
  2187. /// wait for a reply.
  2188. ~lazy_add_edge_with_property();
  2189. /// Returns commit().
  2190. operator std::pair<edge_descriptor, bool>() const { return commit(); }
  2191. // Add the edge. This operation will block if a remote edge is
  2192. // being added.
  2193. std::pair<edge_descriptor, bool> commit() const;
  2194. private:
  2195. // No copy-assignment semantics
  2196. void operator=(lazy_add_edge_with_property&);
  2197. edge_property_type property;
  2198. };
  2199. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2200. PBGL_DISTRIB_ADJLIST_TYPE::lazy_add_edge_with_property::
  2201. ~lazy_add_edge_with_property()
  2202. {
  2203. /// If this edge has already been created or will be created by
  2204. /// someone else, or if someone threw an exception, we will not
  2205. /// create the edge now.
  2206. if (this->committed || std::uncaught_exception())
  2207. return;
  2208. this->committed = true;
  2209. if (this->source.owner == this->self.processor())
  2210. // Add a local edge
  2211. this->add_local_edge(property, DirectedS());
  2212. else
  2213. // Request that the remote processor add an edge and, but
  2214. // don't wait for a reply.
  2215. send(this->self.process_group_, this->source.owner,
  2216. msg_add_edge_with_property,
  2217. msg_add_edge_with_property_data(this->source, this->target,
  2218. property));
  2219. }
  2220. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2221. std::pair<typename PBGL_DISTRIB_ADJLIST_TYPE::edge_descriptor, bool>
  2222. PBGL_DISTRIB_ADJLIST_TYPE::lazy_add_edge_with_property::
  2223. commit() const
  2224. {
  2225. BOOST_ASSERT(!this->committed);
  2226. this->committed = true;
  2227. if (this->source.owner == this->self.processor())
  2228. // Add a local edge
  2229. return this->add_local_edge(property, DirectedS());
  2230. else {
  2231. // Request that the remote processor add an edge
  2232. boost::parallel::detail::untracked_pair<edge_descriptor, bool> result;
  2233. send_oob_with_reply(this->self.process_group_, this->source.owner,
  2234. msg_add_edge_with_property_and_reply,
  2235. msg_add_edge_with_property_data(this->source,
  2236. this->target,
  2237. property),
  2238. result);
  2239. return result;
  2240. }
  2241. }
  2242. /**
  2243. * Returns the set of vertices local to this processor. Note that
  2244. * although this routine matches a valid expression of a
  2245. * VertexListGraph, it does not meet the semantic requirements of
  2246. * VertexListGraph because it returns only local vertices (not all
  2247. * vertices).
  2248. */
  2249. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2250. std::pair<typename PBGL_DISTRIB_ADJLIST_TYPE
  2251. ::vertex_iterator,
  2252. typename PBGL_DISTRIB_ADJLIST_TYPE
  2253. ::vertex_iterator>
  2254. vertices(const PBGL_DISTRIB_ADJLIST_TYPE& g)
  2255. {
  2256. typedef typename PBGL_DISTRIB_ADJLIST_TYPE
  2257. ::vertex_descriptor Vertex;
  2258. typedef typename Vertex::generator generator;
  2259. return std::make_pair(make_transform_iterator(vertices(g.base()).first,
  2260. generator(g.processor())),
  2261. make_transform_iterator(vertices(g.base()).second,
  2262. generator(g.processor())));
  2263. }
  2264. /**
  2265. * Returns the number of vertices local to this processor. Note that
  2266. * although this routine matches a valid expression of a
  2267. * VertexListGraph, it does not meet the semantic requirements of
  2268. * VertexListGraph because it returns only a count of local vertices
  2269. * (not all vertices).
  2270. */
  2271. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2272. typename PBGL_DISTRIB_ADJLIST_TYPE
  2273. ::vertices_size_type
  2274. num_vertices(const PBGL_DISTRIB_ADJLIST_TYPE& g)
  2275. {
  2276. return num_vertices(g.base());
  2277. }
  2278. /***************************************************************************
  2279. * Implementation of Incidence Graph concept
  2280. ***************************************************************************/
  2281. /**
  2282. * Returns the source of edge @param e in @param g.
  2283. */
  2284. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS, typename Edge>
  2285. typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor
  2286. source(const detail::parallel::edge_descriptor<Edge>& e,
  2287. const PBGL_DISTRIB_ADJLIST_TYPE& g)
  2288. {
  2289. typedef typename PBGL_DISTRIB_ADJLIST_TYPE
  2290. ::vertex_descriptor Vertex;
  2291. return Vertex(e.source_processor, source(e.local, g.base()));
  2292. }
  2293. /**
  2294. * Returns the target of edge @param e in @param g.
  2295. */
  2296. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS, typename Edge>
  2297. typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor
  2298. target(const detail::parallel::edge_descriptor<Edge>& e,
  2299. const PBGL_DISTRIB_ADJLIST_TYPE& g)
  2300. {
  2301. typedef typename PBGL_DISTRIB_ADJLIST_TYPE
  2302. ::vertex_descriptor Vertex;
  2303. return Vertex(e.target_processor, target(e.local, g.base()));
  2304. }
  2305. /**
  2306. * Return the set of edges outgoing from a particular vertex. The
  2307. * vertex @param v must be local to the processor executing this
  2308. * routine.
  2309. */
  2310. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2311. std::pair<typename PBGL_DISTRIB_ADJLIST_TYPE::out_edge_iterator,
  2312. typename PBGL_DISTRIB_ADJLIST_TYPE::out_edge_iterator>
  2313. out_edges(typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor v,
  2314. const PBGL_DISTRIB_ADJLIST_TYPE& g)
  2315. {
  2316. BOOST_ASSERT(v.owner == g.processor());
  2317. typedef PBGL_DISTRIB_ADJLIST_TYPE impl;
  2318. typedef typename impl::out_edge_generator generator;
  2319. return std::make_pair(
  2320. make_transform_iterator(out_edges(v.local, g.base()).first,
  2321. generator(g)),
  2322. make_transform_iterator(out_edges(v.local, g.base()).second,
  2323. generator(g)));
  2324. }
  2325. /**
  2326. * Return the number of edges outgoing from a particular vertex. The
  2327. * vertex @param v must be local to the processor executing this
  2328. * routine.
  2329. */
  2330. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2331. typename PBGL_DISTRIB_ADJLIST_TYPE::degree_size_type
  2332. out_degree(typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor v,
  2333. const PBGL_DISTRIB_ADJLIST_TYPE& g)
  2334. {
  2335. BOOST_ASSERT(v.owner == g.processor());
  2336. return out_degree(v.local, g.base());
  2337. }
  2338. /***************************************************************************
  2339. * Implementation of Bidirectional Graph concept
  2340. ***************************************************************************/
  2341. /**
  2342. * Returns the set of edges incoming to a particular vertex. The
  2343. * vertex @param v must be local to the processor executing this
  2344. * routine.
  2345. */
  2346. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG>
  2347. std::pair<typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)
  2348. ::in_edge_iterator,
  2349. typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)
  2350. ::in_edge_iterator>
  2351. in_edges(typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)
  2352. ::vertex_descriptor v,
  2353. const PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)& g)
  2354. {
  2355. BOOST_ASSERT(v.owner == g.processor());
  2356. typedef PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS) impl;
  2357. typedef typename impl::inherited base_graph_type;
  2358. typedef typename impl::in_edge_generator generator;
  2359. typename property_map<base_graph_type, vertex_in_edges_t>::const_type
  2360. in_edges = get(vertex_in_edges, g.base());
  2361. return std::make_pair(make_transform_iterator(in_edges[v.local].begin(),
  2362. generator(g)),
  2363. make_transform_iterator(in_edges[v.local].end(),
  2364. generator(g)));
  2365. }
  2366. /**
  2367. * \overload
  2368. */
  2369. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG>
  2370. std::pair<typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)
  2371. ::in_edge_iterator,
  2372. typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)
  2373. ::in_edge_iterator>
  2374. in_edges(typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)
  2375. ::vertex_descriptor v,
  2376. const PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)& g)
  2377. {
  2378. BOOST_ASSERT(v.owner == g.processor());
  2379. typedef PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS) impl;
  2380. typedef typename impl::in_edge_generator generator;
  2381. return std::make_pair(
  2382. make_transform_iterator(out_edges(v.local, g.base()).first,
  2383. generator(g)),
  2384. make_transform_iterator(out_edges(v.local, g.base()).second,
  2385. generator(g)));
  2386. }
  2387. /**
  2388. * Returns the number of edges incoming to a particular vertex. The
  2389. * vertex @param v must be local to the processor executing this
  2390. * routine.
  2391. */
  2392. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG>
  2393. typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)::degree_size_type
  2394. in_degree(typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)
  2395. ::vertex_descriptor v,
  2396. const PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)& g)
  2397. {
  2398. BOOST_ASSERT(v.owner == g.processor());
  2399. return get(vertex_in_edges, g.base())[v.local].size();
  2400. }
  2401. /**
  2402. * \overload
  2403. */
  2404. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG>
  2405. typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)::degree_size_type
  2406. in_degree(typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)
  2407. ::vertex_descriptor v,
  2408. const PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)& g)
  2409. {
  2410. BOOST_ASSERT(v.owner == g.processor());
  2411. return out_degree(v.local, g.base());
  2412. }
  2413. /**
  2414. * Returns the number of edges incident on the given vertex. The
  2415. * vertex @param v must be local to the processor executing this
  2416. * routine.
  2417. */
  2418. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG>
  2419. typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)
  2420. ::degree_size_type
  2421. degree(typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)
  2422. ::vertex_descriptor v,
  2423. const PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)& g)
  2424. {
  2425. BOOST_ASSERT(v.owner == g.processor());
  2426. return out_degree(v.local, g.base());
  2427. }
  2428. /**
  2429. * \overload
  2430. */
  2431. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG>
  2432. typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)
  2433. ::degree_size_type
  2434. degree(typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)
  2435. ::vertex_descriptor v,
  2436. const PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)& g)
  2437. {
  2438. BOOST_ASSERT(v.owner == g.processor());
  2439. return out_degree(v, g) + in_degree(v, g);
  2440. }
  2441. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2442. typename PBGL_DISTRIB_ADJLIST_TYPE::edges_size_type
  2443. num_edges(const PBGL_DISTRIB_ADJLIST_TYPE& g)
  2444. {
  2445. return num_edges(g.base());
  2446. }
  2447. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG>
  2448. typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)::edges_size_type
  2449. num_edges(const PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)& g)
  2450. {
  2451. return g.local_edges().size();
  2452. }
  2453. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2454. std::pair<
  2455. typename PBGL_DISTRIB_ADJLIST_TYPE::edge_iterator,
  2456. typename PBGL_DISTRIB_ADJLIST_TYPE::edge_iterator>
  2457. edges(const PBGL_DISTRIB_ADJLIST_TYPE& g)
  2458. {
  2459. typedef PBGL_DISTRIB_ADJLIST_TYPE impl;
  2460. typedef typename impl::out_edge_generator generator;
  2461. return std::make_pair(make_transform_iterator(edges(g.base()).first,
  2462. generator(g)),
  2463. make_transform_iterator(edges(g.base()).second,
  2464. generator(g)));
  2465. }
  2466. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG>
  2467. std::pair<
  2468. typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)::edge_iterator,
  2469. typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)::edge_iterator>
  2470. edges(const PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)& g)
  2471. {
  2472. return std::make_pair(g.local_edges().begin(), g.local_edges().end());
  2473. }
  2474. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2475. inline
  2476. typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor
  2477. vertex(typename PBGL_DISTRIB_ADJLIST_TYPE::vertices_size_type n,
  2478. const PBGL_DISTRIB_ADJLIST_TYPE& g)
  2479. {
  2480. typedef typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor
  2481. vertex_descriptor;
  2482. return vertex_descriptor(g.distribution()(n), g.distribution().local(n));
  2483. }
  2484. /***************************************************************************
  2485. * Access to particular edges
  2486. ***************************************************************************/
  2487. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG>
  2488. std::pair<
  2489. typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(directedS)::edge_descriptor,
  2490. bool
  2491. >
  2492. edge(typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(directedS)::vertex_descriptor u,
  2493. typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(directedS)::vertex_descriptor v,
  2494. const PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(directedS)& g)
  2495. {
  2496. typedef typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(directedS)
  2497. ::edge_descriptor edge_descriptor;
  2498. // For directed graphs, u must be local
  2499. BOOST_ASSERT(u.owner == process_id(g.process_group()));
  2500. typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(directedS)
  2501. ::out_edge_iterator ei, ei_end;
  2502. for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei) {
  2503. if (target(*ei, g) == v) return std::make_pair(*ei, true);
  2504. }
  2505. return std::make_pair(edge_descriptor(), false);
  2506. }
  2507. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2508. std::pair<
  2509. typename PBGL_DISTRIB_ADJLIST_TYPE::edge_descriptor,
  2510. bool
  2511. >
  2512. edge(typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor u,
  2513. typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor v,
  2514. const PBGL_DISTRIB_ADJLIST_TYPE& g)
  2515. {
  2516. typedef typename PBGL_DISTRIB_ADJLIST_TYPE
  2517. ::edge_descriptor edge_descriptor;
  2518. // For bidirectional and undirected graphs, u must be local or v
  2519. // must be local
  2520. if (u.owner == process_id(g.process_group())) {
  2521. typename PBGL_DISTRIB_ADJLIST_TYPE::out_edge_iterator ei, ei_end;
  2522. for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei) {
  2523. if (target(*ei, g) == v) return std::make_pair(*ei, true);
  2524. }
  2525. return std::make_pair(edge_descriptor(), false);
  2526. } else if (v.owner == process_id(g.process_group())) {
  2527. typename PBGL_DISTRIB_ADJLIST_TYPE::in_edge_iterator ei, ei_end;
  2528. for (boost::tie(ei, ei_end) = in_edges(v, g); ei != ei_end; ++ei) {
  2529. if (source(*ei, g) == u) return std::make_pair(*ei, true);
  2530. }
  2531. return std::make_pair(edge_descriptor(), false);
  2532. } else {
  2533. BOOST_ASSERT(false);
  2534. exit(1);
  2535. }
  2536. }
  2537. #if 0
  2538. // TBD: not yet supported
  2539. std::pair<out_edge_iterator, out_edge_iterator>
  2540. edge_range(vertex_descriptor u, vertex_descriptor v,
  2541. const adjacency_list& g);
  2542. #endif
  2543. /***************************************************************************
  2544. * Implementation of Adjacency Graph concept
  2545. ***************************************************************************/
  2546. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2547. std::pair<typename PBGL_DISTRIB_ADJLIST_TYPE::adjacency_iterator,
  2548. typename PBGL_DISTRIB_ADJLIST_TYPE::adjacency_iterator>
  2549. adjacent_vertices(typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor v,
  2550. const PBGL_DISTRIB_ADJLIST_TYPE& g)
  2551. {
  2552. typedef typename PBGL_DISTRIB_ADJLIST_TYPE::adjacency_iterator iter;
  2553. return std::make_pair(iter(out_edges(v, g).first, &g),
  2554. iter(out_edges(v, g).second, &g));
  2555. }
  2556. /***************************************************************************
  2557. * Implementation of Mutable Graph concept
  2558. ***************************************************************************/
  2559. /************************************************************************
  2560. * add_edge
  2561. ************************************************************************/
  2562. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2563. typename PBGL_DISTRIB_ADJLIST_TYPE::lazy_add_edge
  2564. add_edge(typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor u,
  2565. typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor v,
  2566. PBGL_DISTRIB_ADJLIST_TYPE& g)
  2567. {
  2568. typedef typename PBGL_DISTRIB_ADJLIST_TYPE::lazy_add_edge lazy_add_edge;
  2569. return lazy_add_edge(g, u, v);
  2570. }
  2571. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2572. typename PBGL_DISTRIB_ADJLIST_TYPE
  2573. ::lazy_add_edge_with_property
  2574. add_edge(typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor u,
  2575. typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor v,
  2576. typename PBGL_DISTRIB_ADJLIST_TYPE::edge_property_type const& p,
  2577. PBGL_DISTRIB_ADJLIST_TYPE& g)
  2578. {
  2579. typedef typename PBGL_DISTRIB_ADJLIST_TYPE
  2580. ::lazy_add_edge_with_property lazy_add_edge_with_property;
  2581. return lazy_add_edge_with_property(g, u, v, p);
  2582. }
  2583. /************************************************************************
  2584. *
  2585. * remove_edge
  2586. *
  2587. ************************************************************************/
  2588. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2589. void
  2590. remove_edge(typename PBGL_DISTRIB_ADJLIST_TYPE::edge_descriptor e,
  2591. PBGL_DISTRIB_ADJLIST_TYPE& g)
  2592. {
  2593. BOOST_ASSERT(source(e, g).owner == g.processor()
  2594. || target(e, g).owner == g.processor());
  2595. if (target(e, g).owner == g.processor())
  2596. detail::parallel::remove_in_edge(e, g, DirectedS());
  2597. if (source(e, g).owner == g.processor())
  2598. remove_edge(e.local, g.base());
  2599. g.remove_local_edge_from_list(source(e, g), target(e, g), DirectedS());
  2600. if (source(e, g).owner != g.processor()
  2601. || (target(e, g).owner != g.processor()
  2602. && !(is_same<DirectedS, directedS>::value))) {
  2603. g.send_remove_edge_request(e);
  2604. }
  2605. }
  2606. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2607. void
  2608. remove_edge(typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor u,
  2609. typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor v,
  2610. PBGL_DISTRIB_ADJLIST_TYPE& g)
  2611. {
  2612. typedef typename PBGL_DISTRIB_ADJLIST_TYPE
  2613. ::vertex_descriptor vertex_descriptor;
  2614. typedef typename PBGL_DISTRIB_ADJLIST_TYPE
  2615. ::edge_descriptor edge_descriptor;
  2616. std::pair<edge_descriptor, bool> the_edge = edge(u, v, g);
  2617. if (the_edge.second) remove_edge(the_edge.first, g);
  2618. }
  2619. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2620. inline void
  2621. remove_edge(typename PBGL_DISTRIB_ADJLIST_TYPE::out_edge_iterator ei,
  2622. PBGL_DISTRIB_ADJLIST_TYPE& g)
  2623. {
  2624. remove_edge(*ei, g);
  2625. }
  2626. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG>
  2627. inline void
  2628. remove_edge(typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(directedS)
  2629. ::out_edge_iterator ei,
  2630. PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(directedS)& g)
  2631. {
  2632. BOOST_ASSERT(source(*ei, g).owner == g.processor());
  2633. remove_edge(ei->local, g.base());
  2634. }
  2635. /************************************************************************
  2636. *
  2637. * remove_out_edge_if
  2638. *
  2639. ************************************************************************/
  2640. namespace parallel { namespace detail {
  2641. /**
  2642. * Function object that applies the underlying predicate to
  2643. * determine if an out-edge should be removed. If so, either
  2644. * removes the incoming edge (if it is stored locally) or sends a
  2645. * message to the owner of the target requesting that it remove
  2646. * the edge.
  2647. */
  2648. template<typename Graph, typename Predicate>
  2649. struct remove_out_edge_predicate
  2650. {
  2651. typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
  2652. typedef typename Graph::local_edge_descriptor argument_type;
  2653. typedef typename Graph::directed_selector directed_selector;
  2654. typedef bool result_type;
  2655. remove_out_edge_predicate(Graph& g, Predicate& predicate)
  2656. : g(g), predicate(predicate) { }
  2657. bool operator()(const argument_type& le)
  2658. {
  2659. typedef typename edge_descriptor::template out_generator<Graph>
  2660. generator;
  2661. edge_descriptor e = generator(g)(le);
  2662. if (predicate(e)) {
  2663. if (source(e, g).owner != target(e, g).owner
  2664. && !(is_same<directed_selector, directedS>::value))
  2665. g.send_remove_edge_request(e);
  2666. else
  2667. ::boost::detail::parallel::remove_in_edge(e, g,
  2668. directed_selector());
  2669. g.remove_local_edge_from_list(source(e, g), target(e, g),
  2670. directed_selector());
  2671. return true;
  2672. } else return false;
  2673. }
  2674. private:
  2675. Graph& g;
  2676. Predicate predicate;
  2677. };
  2678. } } // end namespace parallel::detail
  2679. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS, typename Predicate>
  2680. inline void
  2681. remove_out_edge_if
  2682. (typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor u,
  2683. Predicate predicate,
  2684. PBGL_DISTRIB_ADJLIST_TYPE& g)
  2685. {
  2686. typedef PBGL_DISTRIB_ADJLIST_TYPE Graph;
  2687. typedef parallel::detail::remove_out_edge_predicate<Graph, Predicate>
  2688. Pred;
  2689. BOOST_ASSERT(u.owner == g.processor());
  2690. remove_out_edge_if(u.local, Pred(g, predicate), g.base());
  2691. }
  2692. /************************************************************************
  2693. *
  2694. * remove_in_edge_if
  2695. *
  2696. ************************************************************************/
  2697. namespace parallel { namespace detail {
  2698. /**
  2699. * Function object that applies the underlying predicate to
  2700. * determine if an in-edge should be removed. If so, either
  2701. * removes the outgoing edge (if it is stored locally) or sends a
  2702. * message to the owner of the target requesting that it remove
  2703. * the edge. Only required for bidirectional graphs.
  2704. */
  2705. template<typename Graph, typename Predicate>
  2706. struct remove_in_edge_predicate
  2707. {
  2708. typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
  2709. typedef bool result_type;
  2710. remove_in_edge_predicate(Graph& g, const Predicate& predicate)
  2711. : g(g), predicate(predicate) { }
  2712. template<typename StoredEdge>
  2713. bool operator()(const StoredEdge& le)
  2714. {
  2715. typedef typename edge_descriptor::template in_generator<Graph>
  2716. generator;
  2717. edge_descriptor e = generator(g)(le);
  2718. if (predicate(e)) {
  2719. if (source(e, g).owner != target(e, g).owner)
  2720. g.send_remove_edge_request(e);
  2721. else
  2722. remove_edge(source(e, g).local, target(e, g).local, g.base());
  2723. return true;
  2724. } else return false;
  2725. }
  2726. private:
  2727. Graph& g;
  2728. Predicate predicate;
  2729. };
  2730. } } // end namespace parallel::detail
  2731. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG, typename Predicate>
  2732. inline void
  2733. remove_in_edge_if
  2734. (typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)
  2735. ::vertex_descriptor u,
  2736. Predicate predicate,
  2737. PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)& g)
  2738. {
  2739. typedef PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS) Graph;
  2740. typedef parallel::detail::remove_in_edge_predicate<Graph, Predicate>
  2741. Pred;
  2742. BOOST_ASSERT(u.owner == g.processor());
  2743. graph_detail::erase_if(get(vertex_in_edges, g.base())[u.local],
  2744. Pred(g, predicate));
  2745. }
  2746. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG, typename Predicate>
  2747. inline void
  2748. remove_in_edge_if
  2749. (typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)
  2750. ::vertex_descriptor u,
  2751. Predicate predicate,
  2752. PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)& g)
  2753. {
  2754. remove_out_edge_if(u, predicate, g);
  2755. }
  2756. /************************************************************************
  2757. *
  2758. * remove_edge_if
  2759. *
  2760. ************************************************************************/
  2761. namespace parallel { namespace detail {
  2762. /**
  2763. * Function object that applies the underlying predicate to
  2764. * determine if a directed edge can be removed. This only applies
  2765. * to directed graphs.
  2766. */
  2767. template<typename Graph, typename Predicate>
  2768. struct remove_directed_edge_predicate
  2769. {
  2770. typedef typename Graph::local_edge_descriptor argument_type;
  2771. typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
  2772. typedef bool result_type;
  2773. remove_directed_edge_predicate(Graph& g, const Predicate& predicate)
  2774. : g(g), predicate(predicate) { }
  2775. bool operator()(const argument_type& le)
  2776. {
  2777. typedef typename edge_descriptor::template out_generator<Graph>
  2778. generator;
  2779. edge_descriptor e = generator(g)(le);
  2780. return predicate(e);
  2781. }
  2782. private:
  2783. Graph& g;
  2784. Predicate predicate;
  2785. };
  2786. } } // end namespace parallel::detail
  2787. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG, typename Predicate>
  2788. inline void
  2789. remove_edge_if(Predicate predicate,
  2790. PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(directedS)& g)
  2791. {
  2792. typedef PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(directedS) Graph;
  2793. typedef parallel::detail::remove_directed_edge_predicate<Graph,
  2794. Predicate> Pred;
  2795. remove_edge_if(Pred(g, predicate), g.base());
  2796. }
  2797. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG, typename Predicate>
  2798. inline void
  2799. remove_edge_if(Predicate predicate,
  2800. PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)& g)
  2801. {
  2802. typedef PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS) Graph;
  2803. typedef parallel::detail::remove_out_edge_predicate<Graph,
  2804. Predicate> Pred;
  2805. remove_edge_if(Pred(g, predicate), g.base());
  2806. }
  2807. namespace parallel { namespace detail {
  2808. /**
  2809. * Function object that applies the underlying predicate to
  2810. * determine if an undirected edge should be removed. If so,
  2811. * removes the local edges associated with the edge and
  2812. * (potentially) sends a message to the remote processor that also
  2813. * is removing this edge.
  2814. */
  2815. template<typename Graph, typename Predicate>
  2816. struct remove_undirected_edge_predicate
  2817. {
  2818. typedef typename graph_traits<Graph>::edge_descriptor argument_type;
  2819. typedef bool result_type;
  2820. remove_undirected_edge_predicate(Graph& g, Predicate& predicate)
  2821. : g(g), predicate(predicate) { }
  2822. bool operator()(const argument_type& e)
  2823. {
  2824. if (predicate(e)) {
  2825. if (source(e, g).owner != target(e, g).owner)
  2826. g.send_remove_edge_request(e);
  2827. if (target(e, g).owner == g.processor())
  2828. ::boost::detail::parallel::remove_in_edge(e, g, undirectedS());
  2829. if (source(e, g).owner == g.processor())
  2830. remove_edge(e.local, g.base());
  2831. return true;
  2832. } else return false;
  2833. }
  2834. private:
  2835. Graph& g;
  2836. Predicate predicate;
  2837. };
  2838. } } // end namespace parallel::detail
  2839. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG, typename Predicate>
  2840. inline void
  2841. remove_edge_if(Predicate predicate,
  2842. PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)& g)
  2843. {
  2844. typedef PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS) Graph;
  2845. typedef parallel::detail::remove_undirected_edge_predicate<Graph,
  2846. Predicate> Pred;
  2847. graph_detail::erase_if(g.local_edges(), Pred(g, predicate));
  2848. }
  2849. /************************************************************************
  2850. *
  2851. * clear_vertex
  2852. *
  2853. ************************************************************************/
  2854. namespace parallel { namespace detail {
  2855. struct always_true
  2856. {
  2857. typedef bool result_type;
  2858. template<typename T> bool operator()(const T&) const { return true; }
  2859. };
  2860. } } // end namespace parallel::detail
  2861. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG>
  2862. void
  2863. clear_vertex
  2864. (typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)
  2865. ::vertex_descriptor u,
  2866. PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)& g)
  2867. {
  2868. clear_out_edges(u, g);
  2869. clear_in_edges(u, g);
  2870. }
  2871. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG>
  2872. void
  2873. clear_vertex
  2874. (typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)
  2875. ::vertex_descriptor u,
  2876. PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(undirectedS)& g)
  2877. {
  2878. remove_out_edge_if(u, parallel::detail::always_true(), g);
  2879. }
  2880. /************************************************************************
  2881. *
  2882. * clear_out_edges
  2883. *
  2884. ************************************************************************/
  2885. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG>
  2886. void
  2887. clear_out_edges
  2888. (typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(directedS)::vertex_descriptor u,
  2889. PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(directedS)& g)
  2890. {
  2891. BOOST_ASSERT(u.owner == g.processor());
  2892. clear_out_edges(u.local, g.base());
  2893. }
  2894. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG>
  2895. void
  2896. clear_out_edges
  2897. (typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)
  2898. ::vertex_descriptor u,
  2899. PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)& g)
  2900. {
  2901. remove_out_edge_if(u, parallel::detail::always_true(), g);
  2902. }
  2903. /************************************************************************
  2904. *
  2905. * clear_in_edges
  2906. *
  2907. ************************************************************************/
  2908. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS_CONFIG>
  2909. void
  2910. clear_in_edges
  2911. (typename PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)
  2912. ::vertex_descriptor u,
  2913. PBGL_DISTRIB_ADJLIST_TYPE_CONFIG(bidirectionalS)& g)
  2914. {
  2915. remove_in_edge_if(u, parallel::detail::always_true(), g);
  2916. }
  2917. /************************************************************************
  2918. *
  2919. * add_vertex
  2920. *
  2921. ************************************************************************/
  2922. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2923. typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor
  2924. add_vertex(PBGL_DISTRIB_ADJLIST_TYPE& g)
  2925. {
  2926. typedef PBGL_DISTRIB_ADJLIST_TYPE graph_type;
  2927. typename graph_type::vertex_property_type p;
  2928. return add_vertex(p, g);
  2929. }
  2930. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2931. typename PBGL_DISTRIB_ADJLIST_TYPE::lazy_add_vertex_with_property
  2932. add_vertex(typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_property_type const& p,
  2933. PBGL_DISTRIB_ADJLIST_TYPE& g)
  2934. {
  2935. typedef typename PBGL_DISTRIB_ADJLIST_TYPE
  2936. ::lazy_add_vertex_with_property lazy_add_vertex;
  2937. return lazy_add_vertex(g, p);
  2938. }
  2939. /************************************************************************
  2940. *
  2941. * remove_vertex
  2942. *
  2943. ************************************************************************/
  2944. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2945. void
  2946. remove_vertex(typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor u,
  2947. PBGL_DISTRIB_ADJLIST_TYPE& g)
  2948. {
  2949. typedef typename PBGL_DISTRIB_ADJLIST_TYPE::graph_type graph_type;
  2950. typedef typename graph_type::named_graph_mixin named_graph_mixin;
  2951. BOOST_ASSERT(u.owner == g.processor());
  2952. static_cast<named_graph_mixin&>(static_cast<graph_type&>(g))
  2953. .removing_vertex(u);
  2954. g.distribution().clear();
  2955. remove_vertex(u.local, g.base());
  2956. }
  2957. /***************************************************************************
  2958. * Implementation of Property Graph concept
  2959. ***************************************************************************/
  2960. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS, typename Property>
  2961. struct property_map<PBGL_DISTRIB_ADJLIST_TYPE, Property>
  2962. : detail::parallel::get_adj_list_pmap<Property>
  2963. ::template apply<PBGL_DISTRIB_ADJLIST_TYPE>
  2964. { };
  2965. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS, typename Property>
  2966. struct property_map<PBGL_DISTRIB_ADJLIST_TYPE const, Property>
  2967. : boost::detail::parallel::get_adj_list_pmap<Property>
  2968. // FIXME: in the original code the following was not const
  2969. ::template apply<PBGL_DISTRIB_ADJLIST_TYPE const>
  2970. { };
  2971. template<typename Property, PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2972. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, Property>::type
  2973. get(Property p, PBGL_DISTRIB_ADJLIST_TYPE& g)
  2974. {
  2975. typedef PBGL_DISTRIB_ADJLIST_TYPE Graph;
  2976. typedef typename property_map<Graph, Property>::type result_type;
  2977. typedef typename property_traits<result_type>::value_type value_type;
  2978. typedef typename property_reduce<Property>::template apply<value_type>
  2979. reduce;
  2980. typedef typename property_traits<result_type>::key_type descriptor;
  2981. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  2982. typedef typename mpl::if_<is_same<descriptor, vertex_descriptor>,
  2983. vertex_global_t, edge_global_t>::type
  2984. global_map_t;
  2985. return result_type(g.process_group(), get(global_map_t(), g),
  2986. get(p, g.base()), reduce());
  2987. }
  2988. template<typename Property, PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  2989. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, Property>::const_type
  2990. get(Property p, const PBGL_DISTRIB_ADJLIST_TYPE& g)
  2991. {
  2992. typedef PBGL_DISTRIB_ADJLIST_TYPE Graph;
  2993. typedef typename property_map<Graph, Property>::const_type result_type;
  2994. typedef typename property_traits<result_type>::value_type value_type;
  2995. typedef typename property_reduce<Property>::template apply<value_type>
  2996. reduce;
  2997. typedef typename property_traits<result_type>::key_type descriptor;
  2998. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  2999. typedef typename mpl::if_<is_same<descriptor, vertex_descriptor>,
  3000. vertex_global_t, edge_global_t>::type
  3001. global_map_t;
  3002. return result_type(g.process_group(), get(global_map_t(), g),
  3003. get(p, g.base()), reduce());
  3004. }
  3005. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3006. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, vertex_local_index_t>::type
  3007. get(vertex_local_index_t, PBGL_DISTRIB_ADJLIST_TYPE& g)
  3008. {
  3009. return get(vertex_local_index, g.base());
  3010. }
  3011. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3012. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE,
  3013. vertex_local_index_t>::const_type
  3014. get(vertex_local_index_t, const PBGL_DISTRIB_ADJLIST_TYPE& g)
  3015. {
  3016. return get(vertex_local_index, g.base());
  3017. }
  3018. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3019. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, vertex_global_t>::const_type
  3020. get(vertex_global_t, const PBGL_DISTRIB_ADJLIST_TYPE& g)
  3021. {
  3022. typedef typename property_map<
  3023. PBGL_DISTRIB_ADJLIST_TYPE,
  3024. vertex_global_t>::const_type result_type;
  3025. return result_type();
  3026. }
  3027. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3028. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, vertex_global_t>::const_type
  3029. get(vertex_global_t, PBGL_DISTRIB_ADJLIST_TYPE& g)
  3030. {
  3031. typedef typename property_map<
  3032. PBGL_DISTRIB_ADJLIST_TYPE,
  3033. vertex_global_t>::const_type result_type;
  3034. return result_type();
  3035. }
  3036. /// Retrieve a property map mapping from a vertex descriptor to its
  3037. /// owner.
  3038. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3039. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, vertex_owner_t>::type
  3040. get(vertex_owner_t, PBGL_DISTRIB_ADJLIST_TYPE& g)
  3041. {
  3042. typedef typename property_map<
  3043. PBGL_DISTRIB_ADJLIST_TYPE,
  3044. vertex_owner_t>::type result_type;
  3045. return result_type();
  3046. }
  3047. /// Retrieve a property map mapping from a vertex descriptor to its
  3048. /// owner.
  3049. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3050. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, vertex_owner_t>::const_type
  3051. get(vertex_owner_t, const PBGL_DISTRIB_ADJLIST_TYPE& g)
  3052. {
  3053. typedef typename property_map<
  3054. PBGL_DISTRIB_ADJLIST_TYPE,
  3055. vertex_owner_t>::const_type result_type;
  3056. return result_type();
  3057. }
  3058. /// Retrieve the owner of a vertex
  3059. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3060. inline processor_id_type
  3061. get(vertex_owner_t, PBGL_DISTRIB_ADJLIST_TYPE&,
  3062. typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor v)
  3063. {
  3064. return v.owner;
  3065. }
  3066. /// Retrieve the owner of a vertex
  3067. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3068. inline processor_id_type
  3069. get(vertex_owner_t, const PBGL_DISTRIB_ADJLIST_TYPE&,
  3070. typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor v)
  3071. {
  3072. return v.owner;
  3073. }
  3074. /// Retrieve a property map that maps from a vertex descriptor to
  3075. /// its local descriptor.
  3076. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3077. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, vertex_local_t>::type
  3078. get(vertex_local_t, PBGL_DISTRIB_ADJLIST_TYPE& g)
  3079. {
  3080. typedef typename property_map<
  3081. PBGL_DISTRIB_ADJLIST_TYPE,
  3082. vertex_local_t>::type result_type;
  3083. return result_type();
  3084. }
  3085. /// Retrieve a property map that maps from a vertex descriptor to
  3086. /// its local descriptor.
  3087. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3088. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, vertex_local_t>::const_type
  3089. get(vertex_local_t, const PBGL_DISTRIB_ADJLIST_TYPE& g)
  3090. {
  3091. typedef typename property_map<
  3092. PBGL_DISTRIB_ADJLIST_TYPE,
  3093. vertex_local_t>::const_type result_type;
  3094. return result_type();
  3095. }
  3096. /// Retrieve the local descriptor of a vertex
  3097. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3098. inline typename PBGL_DISTRIB_ADJLIST_TYPE::local_vertex_descriptor
  3099. get(vertex_local_t, PBGL_DISTRIB_ADJLIST_TYPE&,
  3100. typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor v)
  3101. {
  3102. return v.local;
  3103. }
  3104. /// Retrieve the local descriptor of a vertex
  3105. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3106. inline typename PBGL_DISTRIB_ADJLIST_TYPE::local_vertex_descriptor
  3107. get(vertex_local_t, const PBGL_DISTRIB_ADJLIST_TYPE&,
  3108. typename PBGL_DISTRIB_ADJLIST_TYPE::vertex_descriptor v)
  3109. {
  3110. return v.local;
  3111. }
  3112. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3113. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, edge_global_t>::const_type
  3114. get(edge_global_t, const PBGL_DISTRIB_ADJLIST_TYPE& g)
  3115. {
  3116. typedef typename property_map<
  3117. PBGL_DISTRIB_ADJLIST_TYPE,
  3118. edge_global_t>::const_type result_type;
  3119. return result_type();
  3120. }
  3121. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3122. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, edge_global_t>::const_type
  3123. get(edge_global_t, PBGL_DISTRIB_ADJLIST_TYPE& g)
  3124. {
  3125. typedef typename property_map<
  3126. PBGL_DISTRIB_ADJLIST_TYPE,
  3127. edge_global_t>::const_type result_type;
  3128. return result_type();
  3129. }
  3130. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3131. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, edge_owner_t>::type
  3132. get(edge_owner_t, PBGL_DISTRIB_ADJLIST_TYPE& g)
  3133. {
  3134. typedef typename property_map<
  3135. PBGL_DISTRIB_ADJLIST_TYPE,
  3136. edge_owner_t>::type result_type;
  3137. return result_type();
  3138. }
  3139. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3140. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, edge_owner_t>::const_type
  3141. get(edge_owner_t, const PBGL_DISTRIB_ADJLIST_TYPE& g)
  3142. {
  3143. typedef typename property_map<
  3144. PBGL_DISTRIB_ADJLIST_TYPE,
  3145. edge_owner_t>::const_type result_type;
  3146. return result_type();
  3147. }
  3148. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3149. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, edge_local_t>::type
  3150. get(edge_local_t, PBGL_DISTRIB_ADJLIST_TYPE& g)
  3151. {
  3152. typedef typename property_map<
  3153. PBGL_DISTRIB_ADJLIST_TYPE,
  3154. edge_local_t>::type result_type;
  3155. return result_type();
  3156. }
  3157. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3158. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, edge_local_t>::const_type
  3159. get(edge_local_t, const PBGL_DISTRIB_ADJLIST_TYPE& g)
  3160. {
  3161. typedef typename property_map<
  3162. PBGL_DISTRIB_ADJLIST_TYPE,
  3163. edge_local_t>::const_type result_type;
  3164. return result_type();
  3165. }
  3166. template<typename Property, PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS,
  3167. typename Key>
  3168. inline
  3169. typename property_traits<typename property_map<
  3170. PBGL_DISTRIB_ADJLIST_TYPE, Property>::const_type
  3171. >::value_type
  3172. get(Property p, const PBGL_DISTRIB_ADJLIST_TYPE& g, const Key& key)
  3173. {
  3174. if (owner(key) == process_id(g.process_group()))
  3175. return get(p, g.base(), local(key));
  3176. else
  3177. BOOST_ASSERT(false);
  3178. }
  3179. template<typename Property, PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS,
  3180. typename Key, typename Value>
  3181. void
  3182. put(Property p, PBGL_DISTRIB_ADJLIST_TYPE& g, const Key& key, const Value& v)
  3183. {
  3184. if (owner(key) == process_id(g.process_group()))
  3185. put(p, g.base(), local(key), v);
  3186. else
  3187. BOOST_ASSERT(false);
  3188. }
  3189. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3190. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, vertex_index_t>::type
  3191. get(vertex_index_t vi, PBGL_DISTRIB_ADJLIST_TYPE& g)
  3192. {
  3193. typedef PBGL_DISTRIB_ADJLIST_TYPE graph_type;
  3194. typedef typename property_map<graph_type, vertex_index_t>::type
  3195. result_type;
  3196. return result_type(g.process_group(), get(vertex_global, g),
  3197. get(vi, g.base()));
  3198. }
  3199. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3200. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, vertex_index_t>::const_type
  3201. get(vertex_index_t vi, const PBGL_DISTRIB_ADJLIST_TYPE& g)
  3202. {
  3203. typedef PBGL_DISTRIB_ADJLIST_TYPE graph_type;
  3204. typedef typename property_map<graph_type, vertex_index_t>::const_type
  3205. result_type;
  3206. return result_type(g.process_group(), get(vertex_global, g),
  3207. get(vi, g.base()));
  3208. }
  3209. /***************************************************************************
  3210. * Implementation of bundled properties
  3211. ***************************************************************************/
  3212. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS, typename T, typename Bundle>
  3213. struct property_map<PBGL_DISTRIB_ADJLIST_TYPE, T Bundle::*>
  3214. : detail::parallel::get_adj_list_pmap<T Bundle::*>
  3215. ::template apply<PBGL_DISTRIB_ADJLIST_TYPE>
  3216. { };
  3217. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS, typename T, typename Bundle>
  3218. struct property_map<PBGL_DISTRIB_ADJLIST_TYPE const, T Bundle::*>
  3219. : detail::parallel::get_adj_list_pmap<T Bundle::*>
  3220. ::template apply<PBGL_DISTRIB_ADJLIST_TYPE const>
  3221. { };
  3222. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS, typename T, typename Bundle>
  3223. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, T Bundle::*>::type
  3224. get(T Bundle::* p, PBGL_DISTRIB_ADJLIST_TYPE& g)
  3225. {
  3226. typedef PBGL_DISTRIB_ADJLIST_TYPE Graph;
  3227. typedef typename property_map<Graph, T Bundle::*>::type result_type;
  3228. typedef typename property_traits<result_type>::value_type value_type;
  3229. typedef typename property_reduce<T Bundle::*>::template apply<value_type>
  3230. reduce;
  3231. typedef typename property_traits<result_type>::key_type descriptor;
  3232. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  3233. typedef typename mpl::if_<is_same<descriptor, vertex_descriptor>,
  3234. vertex_global_t, edge_global_t>::type
  3235. global_map_t;
  3236. return result_type(g.process_group(), get(global_map_t(), g),
  3237. get(p, g.base()), reduce());
  3238. }
  3239. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS, typename T, typename Bundle>
  3240. typename property_map<PBGL_DISTRIB_ADJLIST_TYPE, T Bundle::*>::const_type
  3241. get(T Bundle::* p, const PBGL_DISTRIB_ADJLIST_TYPE& g)
  3242. {
  3243. typedef PBGL_DISTRIB_ADJLIST_TYPE Graph;
  3244. typedef typename property_map<Graph, T Bundle::*>::const_type result_type;
  3245. typedef typename property_traits<result_type>::value_type value_type;
  3246. typedef typename property_reduce<T Bundle::*>::template apply<value_type>
  3247. reduce;
  3248. typedef typename property_traits<result_type>::key_type descriptor;
  3249. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  3250. typedef typename mpl::if_<is_same<descriptor, vertex_descriptor>,
  3251. vertex_global_t, edge_global_t>::type
  3252. global_map_t;
  3253. return result_type(g.process_group(), get(global_map_t(), g),
  3254. get(p, g.base()), reduce());
  3255. }
  3256. /***************************************************************************
  3257. * Implementation of DistributedGraph concept
  3258. ***************************************************************************/
  3259. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3260. void synchronize(const PBGL_DISTRIB_ADJLIST_TYPE& g)
  3261. {
  3262. typedef PBGL_DISTRIB_ADJLIST_TYPE graph_type;
  3263. synchronize(g.process_group());
  3264. }
  3265. template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
  3266. ProcessGroup
  3267. process_group(const PBGL_DISTRIB_ADJLIST_TYPE& g)
  3268. { return g.process_group(); }
  3269. /***************************************************************************
  3270. * Specializations of is_mpi_datatype for Serializable entities
  3271. ***************************************************************************/
  3272. namespace mpi {
  3273. template<typename Directed, typename Vertex>
  3274. struct is_mpi_datatype<boost::detail::edge_base<Directed, Vertex> >
  3275. : is_mpi_datatype<Vertex> { };
  3276. template<typename Directed, typename Vertex>
  3277. struct is_mpi_datatype<boost::detail::edge_desc_impl<Directed, Vertex> >
  3278. : is_mpi_datatype<boost::detail::edge_base<Directed, Vertex> > { };
  3279. template<typename LocalDescriptor>
  3280. struct is_mpi_datatype<boost::detail::parallel::global_descriptor<LocalDescriptor> >
  3281. : is_mpi_datatype<LocalDescriptor> { };
  3282. template<typename Edge>
  3283. struct is_mpi_datatype<boost::detail::parallel::edge_descriptor<Edge> >
  3284. : is_mpi_datatype<Edge> { };
  3285. template<typename Vertex, typename LocalVertex>
  3286. struct is_mpi_datatype<boost::detail::parallel::
  3287. msg_add_edge_data<Vertex, LocalVertex> >
  3288. : is_mpi_datatype<Vertex> { };
  3289. template<typename Vertex, typename LocalVertex, typename EdgeProperty>
  3290. struct is_mpi_datatype<boost::detail::parallel::
  3291. msg_add_edge_with_property_data<Vertex,
  3292. LocalVertex,
  3293. EdgeProperty> >
  3294. : mpl::and_<is_mpi_datatype<Vertex>, is_mpi_datatype<EdgeProperty> > { };
  3295. template<typename EdgeProperty, typename EdgeDescriptor>
  3296. struct is_mpi_datatype<boost::detail::parallel::msg_nonlocal_edge_data<
  3297. EdgeProperty,EdgeDescriptor> >
  3298. : mpl::and_<
  3299. is_mpi_datatype<boost::detail::parallel::maybe_store_property<
  3300. EdgeProperty> >,
  3301. is_mpi_datatype<EdgeDescriptor> >
  3302. {};
  3303. template<typename EdgeDescriptor>
  3304. struct is_mpi_datatype<
  3305. boost::detail::parallel::msg_remove_edge_data<EdgeDescriptor> >
  3306. : is_mpi_datatype<EdgeDescriptor> {};
  3307. }
  3308. /***************************************************************************
  3309. * Specializations of is_bitwise_serializable for Serializable entities
  3310. ***************************************************************************/
  3311. namespace serialization {
  3312. template<typename Directed, typename Vertex>
  3313. struct is_bitwise_serializable<boost::detail::edge_base<Directed, Vertex> >
  3314. : is_bitwise_serializable<Vertex> { };
  3315. template<typename Directed, typename Vertex>
  3316. struct is_bitwise_serializable<boost::detail::edge_desc_impl<Directed, Vertex> >
  3317. : is_bitwise_serializable<boost::detail::edge_base<Directed, Vertex> > { };
  3318. template<typename LocalDescriptor>
  3319. struct is_bitwise_serializable<boost::detail::parallel::global_descriptor<LocalDescriptor> >
  3320. : is_bitwise_serializable<LocalDescriptor> { };
  3321. template<typename Edge>
  3322. struct is_bitwise_serializable<boost::detail::parallel::edge_descriptor<Edge> >
  3323. : is_bitwise_serializable<Edge> { };
  3324. template<typename Vertex, typename LocalVertex>
  3325. struct is_bitwise_serializable<boost::detail::parallel::
  3326. msg_add_edge_data<Vertex, LocalVertex> >
  3327. : is_bitwise_serializable<Vertex> { };
  3328. template<typename Vertex, typename LocalVertex, typename EdgeProperty>
  3329. struct is_bitwise_serializable<boost::detail::parallel::
  3330. msg_add_edge_with_property_data<Vertex,
  3331. LocalVertex,
  3332. EdgeProperty> >
  3333. : mpl::and_<is_bitwise_serializable<Vertex>,
  3334. is_bitwise_serializable<EdgeProperty> > { };
  3335. template<typename EdgeProperty, typename EdgeDescriptor>
  3336. struct is_bitwise_serializable<boost::detail::parallel::msg_nonlocal_edge_data<
  3337. EdgeProperty,EdgeDescriptor> >
  3338. : mpl::and_<
  3339. is_bitwise_serializable<
  3340. boost::detail::parallel::maybe_store_property<EdgeProperty> >,
  3341. is_bitwise_serializable<EdgeDescriptor> >
  3342. {};
  3343. template<typename EdgeDescriptor>
  3344. struct is_bitwise_serializable<
  3345. boost::detail::parallel::msg_remove_edge_data<EdgeDescriptor> >
  3346. : is_bitwise_serializable<EdgeDescriptor> {};
  3347. template<typename Directed, typename Vertex>
  3348. struct implementation_level<boost::detail::edge_base<Directed, Vertex> >
  3349. : mpl::int_<object_serializable> {};
  3350. template<typename Directed, typename Vertex>
  3351. struct implementation_level<boost::detail::edge_desc_impl<Directed, Vertex> >
  3352. : mpl::int_<object_serializable> {};
  3353. template<typename LocalDescriptor>
  3354. struct implementation_level<boost::detail::parallel::global_descriptor<LocalDescriptor> >
  3355. : mpl::int_<object_serializable> {};
  3356. template<typename Edge>
  3357. struct implementation_level<boost::detail::parallel::edge_descriptor<Edge> >
  3358. : mpl::int_<object_serializable> {};
  3359. template<typename Vertex, typename LocalVertex>
  3360. struct implementation_level<boost::detail::parallel::
  3361. msg_add_edge_data<Vertex, LocalVertex> >
  3362. : mpl::int_<object_serializable> {};
  3363. template<typename Vertex, typename LocalVertex, typename EdgeProperty>
  3364. struct implementation_level<boost::detail::parallel::
  3365. msg_add_edge_with_property_data<Vertex,
  3366. LocalVertex,
  3367. EdgeProperty> >
  3368. : mpl::int_<object_serializable> {};
  3369. template<typename EdgeProperty, typename EdgeDescriptor>
  3370. struct implementation_level<boost::detail::parallel::msg_nonlocal_edge_data<
  3371. EdgeProperty,EdgeDescriptor> >
  3372. : mpl::int_<object_serializable> {};
  3373. template<typename EdgeDescriptor>
  3374. struct implementation_level<
  3375. boost::detail::parallel::msg_remove_edge_data<EdgeDescriptor> >
  3376. : mpl::int_<object_serializable> {};
  3377. template<typename Directed, typename Vertex>
  3378. struct tracking_level<boost::detail::edge_base<Directed, Vertex> >
  3379. : mpl::int_<track_never> {};
  3380. template<typename Directed, typename Vertex>
  3381. struct tracking_level<boost::detail::edge_desc_impl<Directed, Vertex> >
  3382. : mpl::int_<track_never> {};
  3383. template<typename LocalDescriptor>
  3384. struct tracking_level<boost::detail::parallel::global_descriptor<LocalDescriptor> >
  3385. : mpl::int_<track_never> {};
  3386. template<typename Edge>
  3387. struct tracking_level<boost::detail::parallel::edge_descriptor<Edge> >
  3388. : mpl::int_<track_never> {};
  3389. template<typename Vertex, typename LocalVertex>
  3390. struct tracking_level<boost::detail::parallel::
  3391. msg_add_edge_data<Vertex, LocalVertex> >
  3392. : mpl::int_<track_never> {};
  3393. template<typename Vertex, typename LocalVertex, typename EdgeProperty>
  3394. struct tracking_level<boost::detail::parallel::
  3395. msg_add_edge_with_property_data<Vertex,
  3396. LocalVertex,
  3397. EdgeProperty> >
  3398. : mpl::int_<track_never> {};
  3399. template<typename EdgeProperty, typename EdgeDescriptor>
  3400. struct tracking_level<boost::detail::parallel::msg_nonlocal_edge_data<
  3401. EdgeProperty,EdgeDescriptor> >
  3402. : mpl::int_<track_never> {};
  3403. template<typename EdgeDescriptor>
  3404. struct tracking_level<
  3405. boost::detail::parallel::msg_remove_edge_data<EdgeDescriptor> >
  3406. : mpl::int_<track_never> {};
  3407. }
  3408. // Hash function for global descriptors
  3409. template<typename LocalDescriptor>
  3410. struct hash<detail::parallel::global_descriptor<LocalDescriptor> >
  3411. {
  3412. typedef detail::parallel::global_descriptor<LocalDescriptor> argument_type;
  3413. std::size_t operator()(argument_type const& x) const
  3414. {
  3415. std::size_t hash = hash_value(x.owner);
  3416. hash_combine(hash, x.local);
  3417. return hash;
  3418. }
  3419. };
  3420. // Hash function for parallel edge descriptors
  3421. template<typename Edge>
  3422. struct hash<detail::parallel::edge_descriptor<Edge> >
  3423. {
  3424. typedef detail::parallel::edge_descriptor<Edge> argument_type;
  3425. std::size_t operator()(argument_type const& x) const
  3426. {
  3427. std::size_t hash = hash_value(x.owner());
  3428. hash_combine(hash, x.local);
  3429. return hash;
  3430. }
  3431. };
  3432. } // end namespace boost
  3433. #include <boost/graph/distributed/adjlist/handlers.hpp>
  3434. #include <boost/graph/distributed/adjlist/initialize.hpp>
  3435. #include <boost/graph/distributed/adjlist/redistribute.hpp>
  3436. #include <boost/graph/distributed/adjlist/serialization.hpp>
  3437. #endif // BOOST_GRAPH_DISTRIBUTED_ADJACENCY_LIST_HPP