distributed_betweenness_centrality_test.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // Copyright (C) 2006 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Nick Edmonds
  6. // Andrew Lumsdaine
  7. #include <boost/graph/use_mpi.hpp>
  8. #define CSR
  9. #ifdef CSR
  10. # include <boost/graph/distributed/compressed_sparse_row_graph.hpp>
  11. #else
  12. # include <boost/graph/distributed/adjacency_list.hpp>
  13. #endif
  14. #include <boost/config.hpp>
  15. #include <boost/throw_exception.hpp>
  16. #include <boost/graph/distributed/mpi_process_group.hpp>
  17. #include <boost/graph/distributed/concepts.hpp>
  18. #include <boost/graph/erdos_renyi_generator.hpp>
  19. #include <boost/graph/distributed/betweenness_centrality.hpp>
  20. #include <boost/random/linear_congruential.hpp>
  21. #include <boost/graph/graphviz.hpp>
  22. #include <boost/property_map/vector_property_map.hpp>
  23. #include <boost/test/minimal.hpp>
  24. #ifdef BOOST_NO_EXCEPTIONS
  25. void
  26. boost::throw_exception(std::exception const& ex)
  27. {
  28. std::cout << ex.what() << std::endl;
  29. abort();
  30. }
  31. #endif
  32. /****************************************************************************
  33. * Edge weight generator iterator *
  34. ****************************************************************************/
  35. template<typename F, typename RandomGenerator>
  36. class generator_iterator
  37. {
  38. public:
  39. typedef std::input_iterator_tag iterator_category;
  40. typedef typename F::result_type value_type;
  41. typedef const value_type& reference;
  42. typedef const value_type* pointer;
  43. typedef void difference_type;
  44. explicit
  45. generator_iterator(RandomGenerator& gen, const F& f = F())
  46. : f(f), gen(&gen)
  47. {
  48. value = this->f(gen);
  49. }
  50. reference operator*() const { return value; }
  51. pointer operator->() const { return &value; }
  52. generator_iterator& operator++()
  53. {
  54. value = f(*gen);
  55. return *this;
  56. }
  57. generator_iterator operator++(int)
  58. {
  59. generator_iterator temp(*this);
  60. ++(*this);
  61. return temp;
  62. }
  63. bool operator==(const generator_iterator& other) const
  64. { return f == other.f; }
  65. bool operator!=(const generator_iterator& other) const
  66. { return !(*this == other); }
  67. private:
  68. F f;
  69. RandomGenerator* gen;
  70. value_type value;
  71. };
  72. template<typename F, typename RandomGenerator>
  73. inline generator_iterator<F, RandomGenerator>
  74. make_generator_iterator( RandomGenerator& gen, const F& f)
  75. { return generator_iterator<F, RandomGenerator>(gen, f); }
  76. using namespace boost;
  77. using boost::graph::distributed::mpi_process_group;
  78. typedef int weight_type;
  79. struct WeightedEdge {
  80. WeightedEdge(weight_type weight = 0) : weight(weight) { }
  81. weight_type weight;
  82. template<typename Archiver>
  83. void serialize(Archiver& ar, const unsigned int /*version*/)
  84. {
  85. ar & weight;
  86. }
  87. };
  88. int test_main(int argc, char* argv[])
  89. {
  90. mpi::environment env(argc, argv);
  91. #ifdef CSR
  92. typedef compressed_sparse_row_graph<directedS, no_property, WeightedEdge,
  93. no_property, distributedS<mpi_process_group> >
  94. Graph;
  95. typedef compressed_sparse_row_graph<directedS, no_property, WeightedEdge>
  96. seqGraph;
  97. #else
  98. typedef adjacency_list<vecS,
  99. distributedS<mpi_process_group, vecS>,
  100. directedS,
  101. no_property,
  102. property<edge_weight_t, int> > Graph;
  103. typedef adjacency_list<vecS, vecS, directedS, no_property,
  104. property<edge_weight_t, int> > seqGraph;
  105. #endif
  106. typedef sorted_erdos_renyi_iterator<minstd_rand, Graph> ERIter;
  107. int n = 100;
  108. double prob = 0.1;
  109. int C = 3;
  110. minstd_rand gen;
  111. gen.seed(1);
  112. // NOTE: Weighted betweenness centrality only works with non-zero edge weights
  113. // Build graphs
  114. Graph g(edges_are_sorted, ERIter(gen, n, prob), ERIter(),
  115. make_generator_iterator(gen, uniform_int<int>(1, C)),
  116. n);
  117. gen.seed(1); // Re-seed PRNG so we get the same graph
  118. seqGraph sg(
  119. #ifdef CSR
  120. edges_are_sorted,
  121. #endif
  122. ERIter(gen, n, prob), ERIter(),
  123. make_generator_iterator(gen, uniform_int<int>(1, C)),
  124. n);
  125. // Test Betweenness Centrality
  126. typedef property_map<Graph, vertex_index_t>::const_type IndexMap;
  127. typedef iterator_property_map<std::vector<int>::iterator, IndexMap>
  128. CentralityMap;
  129. std::vector<int> centralityS(num_vertices(g), 0);
  130. CentralityMap centrality(centralityS.begin(), get(vertex_index, g));
  131. brandes_betweenness_centrality(g, centrality);
  132. std::vector<int> weightedCentralityS(num_vertices(g), 0);
  133. CentralityMap weightedCentrality(weightedCentralityS.begin(), get(vertex_index, g));
  134. brandes_betweenness_centrality(g, centrality_map(weightedCentrality).
  135. #ifdef CSR
  136. weight_map(get(&WeightedEdge::weight, g)));
  137. #else
  138. weight_map(get(edge_weight, g)));
  139. #endif
  140. int g_cpd = central_point_dominance(g, centrality);
  141. //
  142. // Non-distributed (embarassingly parallel) Betweenness Centrality
  143. //
  144. typedef boost::graph::parallel::process_group_type<Graph>::type
  145. process_group_type;
  146. process_group_type pg = process_group(g);
  147. process_group_type::process_id_type id = process_id(pg);
  148. process_group_type::process_size_type p = num_processes(pg);
  149. typedef property_map<seqGraph, vertex_index_t>::const_type seqIndexMap;
  150. typedef iterator_property_map<std::vector<int>::iterator, seqIndexMap> seqCentralityMap;
  151. std::vector<int> nonDistributedCentralityS(num_vertices(sg), 0);
  152. seqCentralityMap nonDistributedCentrality(nonDistributedCentralityS.begin(), get(vertex_index, sg));
  153. std::vector<int> nonDistributedWeightedCentralityS(num_vertices(sg), 0);
  154. seqCentralityMap nonDistributedWeightedCentrality(nonDistributedWeightedCentralityS.begin(),
  155. get(vertex_index, sg));
  156. non_distributed_brandes_betweenness_centrality(pg, sg, nonDistributedCentrality);
  157. non_distributed_brandes_betweenness_centrality(pg, sg,
  158. centrality_map(nonDistributedWeightedCentrality).
  159. #ifdef CSR
  160. weight_map(get(&WeightedEdge::weight, sg)));
  161. #else
  162. weight_map(get(edge_weight, sg)));
  163. #endif
  164. //
  165. // Verify
  166. //
  167. std::vector<int> seqCentralityS(num_vertices(sg), 0);
  168. seqCentralityMap seqCentrality(seqCentralityS.begin(), get(vertex_index, sg));
  169. std::vector<int> seqWeightedCentralityS(num_vertices(sg), 0);
  170. seqCentralityMap seqWeightedCentrality(seqWeightedCentralityS.begin(), get(vertex_index, sg));
  171. brandes_betweenness_centrality(sg, seqCentrality);
  172. brandes_betweenness_centrality(sg, centrality_map(seqWeightedCentrality).
  173. #ifdef CSR
  174. weight_map(get(&WeightedEdge::weight, sg)));
  175. #else
  176. weight_map(get(edge_weight, sg)));
  177. #endif
  178. int sg_cpd = central_point_dominance(sg, seqCentrality);
  179. // Verify exact betweenness centrality
  180. //
  181. // We're cheating when we map vertices in g to vertices in sg
  182. // since we know that g is using the block distribution here
  183. typedef property_map<Graph, vertex_owner_t>::const_type OwnerMap;
  184. typedef property_map<Graph, vertex_local_t>::const_type LocalMap;
  185. OwnerMap owner = get(vertex_owner, g);
  186. LocalMap local = get(vertex_local, g);
  187. bool passed = true;
  188. {
  189. typedef graph_traits<Graph>::vertex_iterator vertex_iterator;
  190. vertex_iterator v, v_end;
  191. for (boost::tie(v, v_end) = vertices(g); v != v_end; ++v) {
  192. if (get(centrality, *v) != seqCentralityS[(n/p) * get(owner, *v) + get(local, *v)]) {
  193. std::cerr << " " << id << ": Error - centrality of " << get(local, *v) << "@" << get(owner, *v)
  194. << " does not match the sequential result (" << get(centrality, *v) << " vs. "
  195. << seqCentralityS[(n/p) * get(owner, *v) + get(local, *v)] << ")\n";
  196. passed = false;
  197. }
  198. if (get(weightedCentrality, *v) != seqWeightedCentralityS[(n/p) * get(owner, *v) + get(local, *v)]) {
  199. std::cerr << " " << id << ": Error - weighted centrality of " << get(local, *v) << "@" << get(owner, *v)
  200. << " does not match the sequential result (" << get(weightedCentrality, *v) << " vs. "
  201. << seqWeightedCentralityS[(n/p) * get(owner, *v) + get(local, *v)] << ")\n";
  202. passed = false;
  203. }
  204. }
  205. }
  206. if (id == 0) {
  207. typedef graph_traits<seqGraph>::vertex_iterator vertex_iterator;
  208. vertex_iterator v, v_end;
  209. for (boost::tie(v, v_end) = vertices(sg); v != v_end; ++v) {
  210. if (get(seqCentrality, *v) != get(nonDistributedCentrality, *v)) {
  211. std::cerr << " " << id << ": Error - non-distributed centrality of " << *v
  212. << " does not match the sequential result (" << get(nonDistributedCentrality, *v)
  213. << " vs. " << get(seqCentrality, *v) << ")\n";
  214. passed = false;
  215. }
  216. if (get(seqWeightedCentrality, *v) != get(nonDistributedWeightedCentrality, *v)) {
  217. std::cerr << " " << id << ": Error - non-distributed weighted centrality of " << *v
  218. << " does not match the sequential result (" << get(nonDistributedWeightedCentrality, *v)
  219. << " vs. " << get(seqCentrality, *v) << ")\n";
  220. passed = false;
  221. }
  222. }
  223. }
  224. if (g_cpd != sg_cpd) {
  225. passed = false;
  226. std::cerr << "Central point dominance did not match the sequential result\n";
  227. }
  228. if (!passed)
  229. MPI_Abort(MPI_COMM_WORLD, -1);
  230. return 0;
  231. }