1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
// Copyright (C) 2006 The Trustees of Indiana University.
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Authors: Nick Edmonds
// Andrew Lumsdaine
// A test of the distributed compressed sparse row graph type
#include <boost/graph/use_mpi.hpp>
#include <boost/config.hpp>
#include <boost/throw_exception.hpp>
#include <boost/graph/distributed/compressed_sparse_row_graph.hpp>
#include <boost/graph/distributed/mpi_process_group.hpp>
#include <boost/graph/distributed/concepts.hpp>
#include <boost/graph/erdos_renyi_generator.hpp>
#include <boost/graph/small_world_generator.hpp>
#include <boost/graph/rmat_graph_generator.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/distributed/delta_stepping_shortest_paths.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/distributed/page_rank.hpp>
#include <boost/graph/distributed/boman_et_al_graph_coloring.hpp>
#include <boost/graph/connected_components.hpp>
#include <boost/graph/strong_components.hpp>
#include <boost/graph/distributed/betweenness_centrality.hpp>
#include <boost/graph/distributed/dehne_gotz_min_spanning_tree.hpp>
#include <boost/graph/distributed/st_connected.hpp>
#if 0 // Contains internal AdjList types not present in CSR graph
# include <boost/graph/distributed/connected_components_parallel_search.hpp>
#endif
#include <boost/graph/distributed/vertex_list_adaptor.hpp> // Needed for MST
#include <boost/random/linear_congruential.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/property_map/vector_property_map.hpp>
#include <boost/core/lightweight_test.hpp>
#ifdef BOOST_NO_EXCEPTIONS
void
boost::throw_exception(std::exception const& ex)
{
std::cout << ex.what() << std::endl;
abort();
}
#endif
/****************************************************************************
* Edge weight generator iterator *
****************************************************************************/
template<typename F, typename RandomGenerator>
class generator_iterator
{
public:
typedef std::input_iterator_tag iterator_category;
typedef typename F::result_type value_type;
typedef const value_type& reference;
typedef const value_type* pointer;
typedef void difference_type;
explicit
generator_iterator(RandomGenerator& gen, const F& f = F())
: f(f), gen(&gen)
{
value = this->f(gen);
}
reference operator*() const { return value; }
pointer operator->() const { return &value; }
generator_iterator& operator++()
{
value = f(*gen);
return *this;
}
generator_iterator operator++(int)
{
generator_iterator temp(*this);
++(*this);
return temp;
}
bool operator==(const generator_iterator& other) const
{ return f == other.f; }
bool operator!=(const generator_iterator& other) const
{ return !(*this == other); }
private:
F f;
RandomGenerator* gen;
value_type value;
};
template<typename F, typename RandomGenerator>
inline generator_iterator<F, RandomGenerator>
make_generator_iterator( RandomGenerator& gen, const F& f)
{ return generator_iterator<F, RandomGenerator>(gen, f); }
/****************************************************************************
* Printing DFS Visitor *
****************************************************************************/
struct printing_dfs_visitor
{
template<typename Vertex, typename Graph>
void initialize_vertex(Vertex v, const Graph& g)
{
vertex_event("initialize_vertex", v, g);
}
template<typename Vertex, typename Graph>
void start_vertex(Vertex v, const Graph& g)
{
vertex_event("start_vertex", v, g);
}
template<typename Vertex, typename Graph>
void discover_vertex(Vertex v, const Graph& g)
{
vertex_event("discover_vertex", v, g);
}
template<typename Edge, typename Graph>
void examine_edge(Edge e, const Graph& g)
{
edge_event("examine_edge", e, g);
}
template<typename Edge, typename Graph>
void tree_edge(Edge e, const Graph& g)
{
edge_event("tree_edge", e, g);
}
template<typename Edge, typename Graph>
void back_edge(Edge e, const Graph& g)
{
edge_event("back_edge", e, g);
}
template<typename Edge, typename Graph>
void forward_or_cross_edge(Edge e, const Graph& g)
{
edge_event("forward_or_cross_edge", e, g);
}
template<typename Vertex, typename Graph>
void finish_vertex(Vertex v, const Graph& g)
{
vertex_event("finish_vertex", v, g);
}
private:
template<typename Vertex, typename Graph>
void vertex_event(const char* name, Vertex v, const Graph& g)
{
std::cerr << "#" << process_id(g.process_group()) << ": " << name << "("
<< get_vertex_name(v, g) << ": " << local(v) << "@" << owner(v)
<< ")\n";
}
template<typename Edge, typename Graph>
void edge_event(const char* name, Edge e, const Graph& g)
{
std::cerr << "#" << process_id(g.process_group()) << ": " << name << "("
<< get_vertex_name(source(e, g), g) << ": "
<< local(source(e, g)) << "@" << owner(source(e, g)) << ", "
<< get_vertex_name(target(e, g), g) << ": "
<< local(target(e, g)) << "@" << owner(target(e, g)) << ")\n";
}
};
using namespace boost;
using boost::graph::distributed::mpi_process_group;
typedef int weight_type;
struct WeightedEdge {
WeightedEdge(weight_type weight = 0) : weight(weight) { }
weight_type weight;
template<typename Archiver>
void serialize(Archiver& ar, const unsigned int /*version*/)
{
ar & weight;
}
};
struct VertexProperties {
VertexProperties(int d = 0)
: distance(d) { }
int distance;
template<typename Archiver>
void serialize(Archiver& ar, const unsigned int /*version*/)
{
ar & distance;
}
};
int main(int argc, char* argv[])
{
mpi::environment env(argc, argv);
typedef compressed_sparse_row_graph<directedS, VertexProperties, WeightedEdge,
no_property, distributedS<mpi_process_group> >
Digraph;
// Make sure we can build graphs using common graph generators
typedef sorted_erdos_renyi_iterator<minstd_rand, Digraph> ERIter;
typedef small_world_iterator<minstd_rand, Digraph> SWIter;
typedef sorted_rmat_iterator<minstd_rand, Digraph> RMATIter;
typedef graph_traits<Digraph>::edge_descriptor edge_descriptor;
int n = 40;
int k = 3;
double prob = 0.1;
int C = 10;
double a = 0.5, b = 0.1, c = 0.25, d = 0.15;
int iterations = 50;
int num_colors = n / 10;
int lookahead = C / 10;
minstd_rand gen;
// Directed Graphs
Digraph g(ERIter(gen, n, prob), ERIter(),
make_generator_iterator(gen, uniform_int<int>(0, C)),
n);
Digraph g2(SWIter(gen, n, k, prob), SWIter(), n);
Digraph g3(RMATIter(gen, n, size_t(n*n*prob), a, b, c, d), RMATIter(), n);
// Test BFS
breadth_first_search(g, vertex(0, g), visitor(bfs_visitor<>()));
// Test SSSP Algorithms
graph::distributed::delta_stepping_shortest_paths(g,
vertex(0, g),
dummy_property_map(),
get(&VertexProperties::distance, g),
get(&WeightedEdge::weight, g));
dijkstra_shortest_paths(g, vertex(0, g),
distance_map(get(&VertexProperties::distance, g)).
weight_map(get(&WeightedEdge::weight, g)));
dijkstra_shortest_paths(g, vertex(0, g),
distance_map(get(&VertexProperties::distance, g)).
weight_map(get(&WeightedEdge::weight, g)).
lookahead(lookahead));
// Test PageRank
using boost::graph::n_iterations;
std::vector<double> ranks(num_vertices(g));
page_rank(g,
make_iterator_property_map(ranks.begin(),
get(boost::vertex_index, g)),
n_iterations(iterations), 0.85, vertex(0, g));
// Test Graph Coloring
typedef property_map<Digraph, vertex_index_t>::type vertex_index_map;
std::vector<int> colors_vec(num_vertices(g));
iterator_property_map<int*, vertex_index_map> color(&colors_vec[0],
get(vertex_index, g));
graph::boman_et_al_graph_coloring(g, color, num_colors);
// Test DFS
//
// DFS requires an undirected graph, currently CSR graphs must be directed
#if 0
std::vector<vertex_descriptor> parent(num_vertices(g));
std::vector<vertex_descriptor> explore(num_vertices(g));
boost::graph::tsin_depth_first_visit
(g,
vertex(0, g),
printing_dfs_visitor(),
color,
make_iterator_property_map(parent.begin(), get(vertex_index, g)),
make_iterator_property_map(explore.begin(), get(vertex_index, g)),
get(vertex_index, g));
#endif
// Test S-T Connected
st_connected(g, vertex(0, g), vertex(1, g), color, get(vertex_owner, g));
// Test Connected Components
//
// CC requires an undirected graph, currently CSR graphs must be directed
#if 0
std::vector<int> local_components_vec(num_vertices(g));
typedef iterator_property_map<std::vector<int>::iterator,
vertex_index_map> ComponentMap;
ComponentMap component(local_components_vec.begin(), get(vertex_index, g));
assert(connected_components(g, component) ==
connected_components_ps(g, component));
#endif
// Test Betweenness Centrality
//
// Betweenness Centrality is broken at the moment
typedef iterator_property_map<std::vector<int>::iterator, vertex_index_map>
CentralityMap;
std::vector<int> centralityS(num_vertices(g), 0);
CentralityMap centrality(centralityS.begin(), get(vertex_index, g));
brandes_betweenness_centrality(g, centrality);
//
// Test MST
//
std::vector<edge_descriptor> mst_edges;
dense_boruvka_minimum_spanning_tree(make_vertex_list_adaptor(g),
get(&WeightedEdge::weight, g),
std::back_inserter(mst_edges));
mst_edges.clear();
merge_local_minimum_spanning_trees(make_vertex_list_adaptor(g),
get(&WeightedEdge::weight, g),
std::back_inserter(mst_edges));
mst_edges.clear();
boruvka_then_merge(make_vertex_list_adaptor(g),
get(&WeightedEdge::weight, g),
std::back_inserter(mst_edges));
mst_edges.clear();
boruvka_mixed_merge(make_vertex_list_adaptor(g),
get(&WeightedEdge::weight, g),
std::back_inserter(mst_edges));
// Test Strong Components
//
// Can't build reverse graph of a CSR graph
#if 0
std::vector<int> local_components_vec(num_vertices(g));
typedef iterator_property_map<std::vector<int>::iterator,
vertex_index_map> ComponentMap;
ComponentMap component(local_components_vec.begin(), get(vertex_index, g));
int num_components = strong_components(g, component);
#endif
std::ofstream out("dcsr.dot");
write_graphviz(out, g);
return boost::report_errors();
}
|