1 // Copyright (C) 2007 Trustees of Indiana University
2 
3 // Authors: Douglas Gregor
4 //          Andrew Lumsdaine
5 
6 // Use, modification and distribution is subject to the Boost Software
7 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 #include <boost/mpi/graph_communicator.hpp>
10 
11 namespace boost { namespace mpi {
12 
13 // Incidence Graph requirements
14 std::pair<detail::comm_out_edge_iterator, detail::comm_out_edge_iterator>
out_edges(int vertex,const graph_communicator & comm)15 out_edges(int vertex, const graph_communicator& comm)
16 {
17   int nneighbors = out_degree(vertex, comm);
18   shared_array<int> neighbors(new int[nneighbors]);
19   BOOST_MPI_CHECK_RESULT(MPI_Graph_neighbors,
20                          ((MPI_Comm)comm, vertex, nneighbors, neighbors.get()));
21   return std::make_pair(detail::comm_out_edge_iterator(vertex, neighbors, 0),
22                         detail::comm_out_edge_iterator(vertex, neighbors,
23                                                        nneighbors));
24 }
25 
out_degree(int vertex,const graph_communicator & comm)26 int out_degree(int vertex, const graph_communicator& comm)
27 {
28   int nneighbors;
29   BOOST_MPI_CHECK_RESULT(MPI_Graph_neighbors_count,
30                          ((MPI_Comm)comm, vertex, &nneighbors));
31   return nneighbors;
32 }
33 
34 // Adjacency Graph requirements
35 std::pair<detail::comm_adj_iterator, detail::comm_adj_iterator>
adjacent_vertices(int vertex,const graph_communicator & comm)36 adjacent_vertices(int vertex, const graph_communicator& comm)
37 {
38   int nneighbors = out_degree(vertex, comm);
39   shared_array<int> neighbors(new int[nneighbors]);
40   BOOST_MPI_CHECK_RESULT(MPI_Graph_neighbors,
41                          ((MPI_Comm)comm, vertex, nneighbors, neighbors.get()));
42   return std::make_pair(detail::comm_adj_iterator(neighbors, 0),
43                         detail::comm_adj_iterator(neighbors, nneighbors));
44 }
45 
46 // Edge List Graph requirements
47 std::pair<detail::comm_edge_iterator, detail::comm_edge_iterator>
48 edges(const graph_communicator& comm);
49 
50 std::pair<detail::comm_edge_iterator, detail::comm_edge_iterator>
edges(const graph_communicator & comm)51 edges(const graph_communicator& comm)
52 {
53   int nnodes, nedges;
54   BOOST_MPI_CHECK_RESULT(MPI_Graphdims_get, ((MPI_Comm)comm, &nnodes, &nedges));
55 
56   shared_array<int> indices(new int[nnodes]);
57   shared_array<int> edges(new int[nedges]);
58   BOOST_MPI_CHECK_RESULT(MPI_Graph_get,
59                          ((MPI_Comm)comm, nnodes, nedges,
60                           indices.get(), edges.get()));
61   return std::make_pair(detail::comm_edge_iterator(indices, edges),
62                         detail::comm_edge_iterator(nedges));
63 }
64 
65 
num_edges(const graph_communicator & comm)66 int num_edges(const graph_communicator& comm)
67 {
68   int nnodes, nedges;
69   BOOST_MPI_CHECK_RESULT(MPI_Graphdims_get, ((MPI_Comm)comm, &nnodes, &nedges));
70   return nedges;
71 }
72 
73 } } // end namespace boost::mpi
74