123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574 |
- #ifndef BOOST_MPI_GRAPH_COMMUNICATOR_HPP
- #define BOOST_MPI_GRAPH_COMMUNICATOR_HPP
- #include <boost/mpi/communicator.hpp>
- #include <vector>
- #include <utility>
- #include <boost/graph/graph_traits.hpp>
- #include <boost/graph/properties.hpp>
- #include <boost/iterator/counting_iterator.hpp>
- #include <boost/graph/iteration_macros.hpp>
- #include <boost/shared_array.hpp>
- #include <boost/assert.hpp>
- namespace boost { namespace mpi {
- class BOOST_MPI_DECL graph_communicator : public communicator
- {
- friend class communicator;
- /**
- * INTERNAL ONLY
- *
- * Construct a graph communicator given a shared pointer to the
- * underlying MPI_Comm. This operation is used for "casting" from a
- * communicator to a graph communicator.
- */
- explicit graph_communicator(const shared_ptr<MPI_Comm>& comm_ptr)
- {
- #ifndef BOOST_DISABLE_ASSERTS
- int status;
- BOOST_MPI_CHECK_RESULT(MPI_Topo_test, ((MPI_Comm)*this, &status));
- BOOST_ASSERT(status == MPI_GRAPH);
- #endif
- this->comm_ptr = comm_ptr;
- }
- public:
-
- graph_communicator(const MPI_Comm& comm, comm_create_kind kind)
- : communicator(comm, kind)
- {
- #ifndef BOOST_DISABLE_ASSERTS
- int status;
- BOOST_MPI_CHECK_RESULT(MPI_Topo_test, ((MPI_Comm)*this, &status));
- BOOST_ASSERT(status == MPI_GRAPH);
- #endif
- }
-
- template<typename Graph>
- explicit
- graph_communicator(const communicator& comm, const Graph& graph,
- bool reorder = false);
-
- template<typename Graph, typename RankMap>
- explicit
- graph_communicator(const communicator& comm, const Graph& graph,
- RankMap rank, bool reorder = false);
- protected:
-
- template<typename Graph, typename RankMap>
- void
- setup_graph(const communicator& comm, const Graph& graph, RankMap rank,
- bool reorder);
- };
- template<typename Graph>
- graph_communicator::graph_communicator(const communicator& comm,
- const Graph& graph,
- bool reorder)
- {
- this->setup_graph(comm, graph, get(vertex_index, graph), reorder);
- }
- template<typename Graph, typename RankMap>
- graph_communicator::graph_communicator(const communicator& comm,
- const Graph& graph,
- RankMap rank, bool reorder)
- {
- this->setup_graph(comm, graph, rank, reorder);
- }
- template<typename Graph, typename RankMap>
- void
- graph_communicator::setup_graph(const communicator& comm, const Graph& graph,
- RankMap rank, bool reorder)
- {
- typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
-
- std::vector<vertex_descriptor> vertex_with_rank(num_vertices(graph));
- if (vertex_with_rank.empty())
- return;
- BGL_FORALL_VERTICES_T(v, graph, Graph)
- vertex_with_rank[get(rank, v)] = v;
-
-
- std::vector<int> indices(num_vertices(graph));
- std::vector<int> edges;
- int nvertices = indices.size();
- for (int vertex_index = 0; vertex_index < nvertices; ++vertex_index) {
- vertex_descriptor v = vertex_with_rank[vertex_index];
- BGL_FORALL_OUTEDGES_T(v, e, graph, Graph)
- edges.push_back(get(rank, target(e, graph)));
- indices[vertex_index] = edges.size();
- }
-
- MPI_Comm newcomm;
- BOOST_MPI_CHECK_RESULT(MPI_Graph_create,
- ((MPI_Comm)comm,
- nvertices,
- detail::c_data(indices),
- detail::c_data(edges),
- reorder,
- &newcomm));
- this->comm_ptr.reset(new MPI_Comm(newcomm), comm_free());
- }
- namespace detail {
-
- class comm_out_edge_iterator
- : public iterator_facade<comm_out_edge_iterator,
- std::pair<int, int>,
- random_access_traversal_tag,
- const std::pair<int, int>&,
- int>
- {
- public:
- comm_out_edge_iterator() { }
- comm_out_edge_iterator(int source, shared_array<int> neighbors, int index)
- : edge(source, -1), neighbors(neighbors), index(index) { }
- protected:
- friend class boost::iterator_core_access;
- const std::pair<int, int>& dereference() const
- {
- edge.second = neighbors[index];
- return edge;
- }
- bool equal(const comm_out_edge_iterator& other) const
- {
- return (edge.first == other.edge.first
- && index == other.index);
- }
- void increment() { ++index; }
- void decrement() { --index; }
- void advance(int n) { index += n; }
- int distance_to(const comm_out_edge_iterator& other) const
- {
- return other.index - index;
- }
- mutable std::pair<int, int> edge;
- shared_array<int> neighbors;
- int index;
- };
-
- class comm_adj_iterator
- : public iterator_facade<comm_adj_iterator,
- int,
- random_access_traversal_tag,
- int,
- int>
- {
- public:
- comm_adj_iterator() { }
- comm_adj_iterator(shared_array<int> neighbors, int index)
- : neighbors(neighbors), index(index) { }
- protected:
- friend class boost::iterator_core_access;
- int dereference() const { return neighbors[index]; }
- bool equal(const comm_adj_iterator& other) const
- {
- return (neighbors == other.neighbors
- && index == other.index);
- }
- void increment() { ++index; }
- void decrement() { --index; }
- void advance(int n) { index += n; }
- int distance_to(const comm_adj_iterator& other) const
- {
- return other.index - index;
- }
- shared_array<int> neighbors;
- int index;
- };
-
- class comm_edge_iterator
- : public iterator_facade<comm_edge_iterator,
- std::pair<int, int>,
- forward_traversal_tag,
- const std::pair<int, int>&,
- int>
- {
- public:
- comm_edge_iterator() { }
-
- comm_edge_iterator(int nedges) : edge_index(nedges) { }
- comm_edge_iterator(shared_array<int> indices, shared_array<int> edges)
- : indices(indices), edges(edges), edge_index(0), edge(0, 0)
- { }
- protected:
- friend class boost::iterator_core_access;
- const std::pair<int, int>& dereference() const
- {
- while (edge_index == indices[edge.first])
- ++edge.first;
- edge.second = edges[edge_index];
- return edge;
- }
- bool equal(const comm_edge_iterator& other) const
- {
- return edge_index == other.edge_index;
- }
- void increment()
- {
- ++edge_index;
- }
- shared_array<int> indices;
- shared_array<int> edges;
- int edge_index;
- mutable std::pair<int, int> edge;
- };
- }
- inline int source(const std::pair<int, int>& edge, const graph_communicator&)
- {
- return edge.first;
- }
- inline int target(const std::pair<int, int>& edge, const graph_communicator&)
- {
- return edge.second;
- }
- std::pair<detail::comm_out_edge_iterator, detail::comm_out_edge_iterator>
- out_edges(int vertex, const graph_communicator& comm);
- int out_degree(int vertex, const graph_communicator& comm);
- std::pair<detail::comm_adj_iterator, detail::comm_adj_iterator>
- adjacent_vertices(int vertex, const graph_communicator& comm);
- inline std::pair<counting_iterator<int>, counting_iterator<int> >
- vertices(const graph_communicator& comm)
- {
- return std::make_pair(counting_iterator<int>(0),
- counting_iterator<int>(comm.size()));
- }
- inline int num_vertices(const graph_communicator& comm) { return comm.size(); }
- std::pair<detail::comm_edge_iterator, detail::comm_edge_iterator>
- edges(const graph_communicator& comm);
- int num_edges(const graph_communicator& comm);
- inline identity_property_map get(vertex_index_t, const graph_communicator&)
- {
- return identity_property_map();
- }
- inline int get(vertex_index_t, const graph_communicator&, int vertex)
- {
- return vertex;
- }
- } }
- namespace boost {
- template<>
- struct graph_traits<mpi::graph_communicator> {
-
- typedef int vertex_descriptor;
- typedef std::pair<int, int> edge_descriptor;
- typedef directed_tag directed_category;
- typedef disallow_parallel_edge_tag edge_parallel_category;
-
-
- struct traversal_category
- : incidence_graph_tag,
- adjacency_graph_tag,
- vertex_list_graph_tag,
- edge_list_graph_tag
- {
- };
-
- static vertex_descriptor null_vertex() { return -1; }
-
- typedef mpi::detail::comm_out_edge_iterator out_edge_iterator;
- typedef int degree_size_type;
-
- typedef mpi::detail::comm_adj_iterator adjacency_iterator;
-
- typedef counting_iterator<int> vertex_iterator;
- typedef int vertices_size_type;
-
- typedef mpi::detail::comm_edge_iterator edge_iterator;
- typedef int edges_size_type;
- };
- template<>
- struct property_map<mpi::graph_communicator, vertex_index_t>
- {
- typedef identity_property_map type;
- typedef identity_property_map const_type;
- };
- }
- #endif
|