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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
|
// Copyright 2004 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
#include <boost/graph/use_mpi.hpp>
#define CSR
#ifdef CSR
# include <boost/graph/distributed/compressed_sparse_row_graph.hpp>
#else
# include <boost/graph/distributed/adjacency_list.hpp>
#endif
#include <boost/test/minimal.hpp>
#include <boost/graph/distributed/mpi_process_group.hpp>
#include <boost/graph/distributed/queue.hpp>
#include <boost/graph/parallel/distribution.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>
#include <sys/time.h>
#include <time.h>
#include <boost/random.hpp>
#include <boost/property_map/parallel/distributed_property_map.hpp>
#include <boost/random/linear_congruential.hpp>
#include <boost/graph/distributed/graphviz.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/iteration_macros.hpp>
#include <boost/graph/parallel/algorithm.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/pending/queue.hpp>
#include <boost/graph/rmat_graph_generator.hpp>
#include <boost/graph/distributed/betweenness_centrality.hpp>
#include <boost/graph/distributed/filtered_graph.hpp>
#include <boost/graph/parallel/container_traits.hpp>
#include <boost/graph/properties.hpp>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <stdint.h>
using namespace boost;
// #define DEBUG
typedef rand48 RandomGenerator;
/****************************************************************************
* Timing
****************************************************************************/
#ifndef PBGL_ACCOUNTING
typedef double time_type;
inline time_type get_time()
{
timeval tp;
gettimeofday(&tp, 0);
return tp.tv_sec + tp.tv_usec / 1000000.0;
}
std::string print_time(time_type t)
{
std::ostringstream out;
out << std::setiosflags(std::ios::fixed) << std::setprecision(2) << t;
return out.str();
}
#endif // PBGL_ACCOUNTING
/****************************************************************************
* 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); }
template<typename Graph, typename DistanceMap, typename WeightMap, typename ColorMap>
struct ssca_visitor : bfs_visitor<>
{
typedef typename property_traits<WeightMap>::value_type Weight;
typedef typename property_traits<ColorMap>::value_type ColorValue;
typedef color_traits<ColorValue> Color;
ssca_visitor(DistanceMap& distance, const WeightMap& weight, ColorMap& color,
Weight max_)
: distance(distance), weight(weight), color(color), max_(max_) {}
template<typename Edge>
void tree_edge(Edge e, const Graph& g)
{
int new_distance = get(weight, e) == (std::numeric_limits<Weight>::max)() ?
(std::numeric_limits<Weight>::max)() : get(distance, source(e, g)) + get(weight, e);
put(distance, target(e, g), new_distance);
if (new_distance > max_)
put(color, target(e, g), Color::black());
}
private:
DistanceMap& distance;
const WeightMap& weight;
ColorMap& color;
Weight max_;
};
// Generate source vertices for approximate BC
template <typename Graph, typename Buffer>
void
generate_sources(const Graph& g, Buffer sources,
typename graph_traits<Graph>::vertices_size_type num_sources)
{
typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
typedef typename boost::graph::parallel::process_group_type<Graph>::type
process_group_type;
process_group_type pg = g.process_group();
typename process_group_type::process_id_type id = process_id(pg);
typename process_group_type::process_size_type p = num_processes(pg);
// Don't feel like adding a special case for num_sources < p
assert(num_sources >= p);
minstd_rand gen;
uniform_int<vertices_size_type> rand_vertex(0, num_vertices(g) - 1);
std::vector<vertex_descriptor> all_sources, local_sources;
vertices_size_type local_vertices = vertices_size_type(floor((double)num_sources / p));
local_vertices += (id < (num_sources - (p * local_vertices)) ? 1 : 0);
while (local_vertices > 0) {
vertex_iterator iter = vertices(g).first;
std::advance(iter, rand_vertex(gen));
if (out_degree(*iter, g) != 0
&& std::find(local_sources.begin(), local_sources.end(), *iter) == local_sources.end()) {
local_sources.push_back(*iter);
--local_vertices;
}
}
all_gather(pg, local_sources.begin(), local_sources.end(), all_sources);
std::sort(all_sources.begin(), all_sources.end());
for (typename std::vector<vertex_descriptor>::iterator iter = all_sources.begin();
iter != all_sources.end(); ++iter)
sources.push(*iter);
}
// Kernel 2 - Classify large sets
template <typename Graph, typename WeightMap>
void classify_sets(const Graph& g, const WeightMap& weight_map,
std::vector<std::pair<typename graph_traits<Graph>::vertex_descriptor,
typename graph_traits<Graph>::vertex_descriptor> > & global_S)
{
typedef typename boost::graph::parallel::process_group_type<Graph>::type
process_group_type;
process_group_type pg = g.process_group();
typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
std::vector<std::pair<vertex_descriptor, vertex_descriptor> > S;
time_type start = get_time();
#ifdef CSR
typedef typename property_map<Graph, vertex_owner_t>::const_type OwnerMap;
typedef typename property_map<Graph, vertex_local_t>::const_type LocalMap;
OwnerMap owner = get(vertex_owner, g);
LocalMap local = get(vertex_local, g);
#endif
int max_ = 0;
BGL_FORALL_EDGES_T(e, g, Graph) {
#ifdef CSR
if (get(owner, source(e, g)) == process_id(pg)) {
#endif
int w = get(weight_map, e);
if (w > max_) {
max_ = w;
S.clear();
}
if (w >= max_)
S.push_back(std::make_pair(source(e, g), target(e, g)));
#ifdef CSR
}
#endif
}
int global_max = all_reduce(pg, max_, boost::parallel::maximum<int>());
if (max_ < global_max)
S.clear();
global_S.clear();
all_gather(pg, S.begin(), S.end(), global_S);
// This is probably unnecessary as long as the sets of edges owned by procs is disjoint
std::sort(global_S.begin(), global_S.end());
std::unique(global_S.begin(), global_S.end());
synchronize(pg);
time_type end = get_time();
if (process_id(pg) == 0) {
std::cerr << " Distributed Graph: " << print_time(end - start) << std::endl
<< " Max int weight = " << global_max << std::endl;
}
}
template <typename ProcessGroup, typename Graph, typename WeightMap,
typename EdgeVector>
void seq_classify_sets(const ProcessGroup& pg, const Graph& g,
const WeightMap& weight_map, EdgeVector& S)
{
typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
typedef typename property_traits<WeightMap>::value_type edge_weight_type;
time_type start = get_time();
edge_weight_type max_ = 0;
BGL_FORALL_EDGES_T(e, g, Graph) {
edge_weight_type w = get(weight_map, e);
if (w > max_) {
max_ = w;
S.clear();
}
if (w >= max_)
S.push_back(e);
}
synchronize(pg);
time_type end = get_time();
if (process_id(pg) == 0)
std::cerr << " Non-Distributed Graph: " << print_time(end - start) << std::endl
<< " Max int weight = " << max_ << std::endl;
}
// Kernel 3 - Graph Extraction
template <typename Graph, typename OwnerMap, typename LocalMap,
typename WeightMap, typename DistanceMap, typename ColorMap,
typename EdgeVector>
void subgraph_extraction(Graph& g, const OwnerMap& owner, const LocalMap& local,
const WeightMap& weight_map, DistanceMap distances,
ColorMap color_map, const EdgeVector& S,
int subGraphEdgeLength)
{
// Nick: I think turning the vertex black when the maximum distance is
// exceeded will prevent BFS from exploring beyond the subgraph.
// Unfortunately we can't run subgraph extraction in parallel
// because the subgraphs may overlap
typedef typename property_traits<ColorMap>::value_type ColorValue;
typedef color_traits<ColorValue> Color;
typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
typedef typename boost::graph::parallel::process_group_type<Graph>::type
process_group_type;
typedef boost::graph::distributed::distributed_queue<process_group_type,
OwnerMap, queue<vertex_descriptor> > queue_t;
process_group_type pg = g.process_group();
typename process_group_type::process_id_type id = process_id(pg);
queue_t Q(pg, owner);
EdgeVector sources(S.begin(), S.end());
#ifdef DEBUG
std::vector<std::vector<vertex_descriptor> > subgraphs;
#endif
synchronize(pg);
typedef typename std::vector<std::pair<vertex_descriptor, vertex_descriptor> >::iterator
source_iterator;
time_type start = get_time();
for (source_iterator iter = sources.begin(); iter != sources.end(); ++iter) {
// Reinitialize distance and color maps every BFS
BGL_FORALL_VERTICES_T(v, g, Graph) {
if (get(owner, v) == id) {
local_put(color_map, v, Color::white());
local_put(distances, v, (std::numeric_limits<int>::max)());
}
}
vertex_descriptor u = iter->first, v = iter->second;
local_put(distances, u, 0);
local_put(distances, v, 0);
while (!Q.empty()) Q.pop();
if (get(owner, u) == id)
Q.push(u);
local_put(color_map, u, Color::gray());
breadth_first_search(g, v, Q,
ssca_visitor<Graph, DistanceMap, WeightMap, ColorMap>
(distances, weight_map, color_map, subGraphEdgeLength),
color_map);
// At this point all vertices with distance > 0 in addition to the
// starting vertices compose the subgraph.
#ifdef DEBUG
subgraphs.push_back(std::vector<vertex_descriptor>());
std::vector<vertex_descriptor>& subgraph = subgraphs.back();
BGL_FORALL_VERTICES_T(v, g, Graph) {
if (get(distances, v) < (std::numeric_limits<int>::max)())
subgraph.push_back(v);
}
#endif
}
synchronize(pg);
time_type end = get_time();
#ifdef DEBUG
for (unsigned int i = 0; i < subgraphs.size(); i++) {
all_gather(pg, subgraphs[i].begin(), subgraphs[i].end(), subgraphs[i]);
std::sort(subgraphs[i].begin(), subgraphs[i].end());
subgraphs[i].erase(std::unique(subgraphs[i].begin(), subgraphs[i].end()),
subgraphs[i].end());
}
if (process_id(pg) == 0)
for (int i = 0; abs(i) < subgraphs.size(); i++) {
std::cerr << "Subgraph " << i << " :\n";
for (int j = 0; abs(j) < subgraphs[i].size(); j++)
std::cerr << " " << get(local, subgraphs[i][j]) << "@"
<< get(owner, subgraphs[i][j]) << std::endl;
}
#endif
if (process_id(pg) == 0)
std::cerr << " Distributed Graph: " << print_time(end - start) << std::endl;
}
template <typename ProcessGroup, typename Graph, typename WeightMap,
typename DistanceMap, typename ColorMap, typename EdgeVector>
void seq_subgraph_extraction(const ProcessGroup& pg, const Graph& g,
const WeightMap& weight_map, DistanceMap distances,
ColorMap color_map, const EdgeVector& S,
int subGraphEdgeLength)
{
// Nick: I think turning the vertex black when the maximum distance is
// exceeded will prevent BFS from exploring beyond the subgraph.
using boost::graph::distributed::mpi_process_group;
typedef typename property_traits<ColorMap>::value_type ColorValue;
typedef color_traits<ColorValue> Color;
typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
boost::queue<vertex_descriptor> Q;
std::vector<edge_descriptor> sources(S.begin(), S.end());
#ifdef DEBUG
std::vector<std::vector<vertex_descriptor> > subgraphs;
#endif
synchronize(pg);
typedef ProcessGroup process_group_type;
typename process_group_type::process_id_type id = process_id(pg);
typename process_group_type::process_size_type p = num_processes(pg);
time_type start = get_time();
for (int i = id; i < sources.size(); i += p) {
// Reinitialize distance and color maps every BFS
BGL_FORALL_VERTICES_T(v, g, Graph) {
put(color_map, v, Color::white());
put(distances, v, (std::numeric_limits<int>::max)());
}
vertex_descriptor u = source(sources[i], g),
v = target(sources[i], g);
put(distances, u, 0);
put(distances, v, 0);
while (!Q.empty()) Q.pop();
Q.push(u);
put(color_map, u, Color::gray());
breadth_first_search(g, v, Q,
ssca_visitor<Graph, DistanceMap, WeightMap, ColorMap>
(distances, weight_map, color_map, subGraphEdgeLength),
color_map);
#ifdef DEBUG
subgraphs.push_back(std::vector<vertex_descriptor>());
std::vector<vertex_descriptor>& subgraph = subgraphs.back();
BGL_FORALL_VERTICES_T(v, g, Graph) {
if (get(distances, v) < (std::numeric_limits<int>::max)())
subgraph.push_back(v);
}
#endif
}
synchronize(pg);
time_type end = get_time();
#ifdef DEBUG
std::vector<vertex_descriptor> ser_subgraphs;
for (int i = 0; i < subgraphs.size(); ++i) {
for (int j = 0; j < subgraphs[i].size(); ++j)
ser_subgraphs.push_back(subgraphs[i][j]);
ser_subgraphs.push_back(graph_traits<Graph>::null_vertex());
}
all_gather(pg, ser_subgraphs.begin(), ser_subgraphs.end(), ser_subgraphs);
int i = 0;
typename std::vector<vertex_descriptor>::iterator iter(ser_subgraphs.begin());
while (iter != ser_subgraphs.end()) {
std::cerr << "Subgraph " << i << " :\n";
while (*iter != graph_traits<Graph>::null_vertex()) {
std::cerr << " " << *iter << std::endl;
++iter;
}
++i;
++iter;
}
#endif
if (process_id(pg) == 0)
std::cerr << " Non-Distributed Graph: " << print_time(end - start) << std::endl;
}
template <typename ProcessGroup, typename Graph, typename CentralityMap>
void
extract_max_bc_vertices(const ProcessGroup& pg, const Graph& g, const CentralityMap& centrality,
std::vector<typename graph_traits<Graph>::vertex_descriptor>& max_bc_vec)
{
using boost::graph::parallel::process_group;
using boost::parallel::all_gather;
using boost::parallel::all_reduce;
// Find set of vertices with highest BC score
typedef typename property_traits<CentralityMap>::value_type centrality_type;
std::vector<centrality_type> max_bc_vertices;
centrality_type max_ = 0;
max_bc_vec.clear();
BGL_FORALL_VERTICES_T(v, g, Graph) {
if (get(centrality, v) == max_)
max_bc_vec.push_back(v);
else if (get(centrality, v) > max_) {
max_ = get(centrality, v);
max_bc_vec.clear();
max_bc_vec.push_back(v);
}
}
centrality_type global_max = all_reduce(pg, max_, boost::parallel::minimum<int>());
if (global_max > max_)
max_bc_vec.clear();
all_gather(pg, max_bc_vec.begin(), max_bc_vec.end(), max_bc_vec);
}
// Function object to filter edges divisible by 8
// EdgeWeightMap::value_type must be integral!
template <typename EdgeWeightMap>
struct edge_weight_not_divisible_by_eight {
typedef typename property_traits<EdgeWeightMap>::value_type weight_type;
edge_weight_not_divisible_by_eight() { }
edge_weight_not_divisible_by_eight(EdgeWeightMap weight) : m_weight(weight) { }
template <typename Edge>
bool operator()(const Edge& e) const {
return (get(m_weight, e) & ((std::numeric_limits<weight_type>::max)() - 7)) != get(m_weight, e);
}
EdgeWeightMap m_weight;
};
//
// Vertex and Edge properties
//
#ifdef CSR
typedef int weight_type;
struct WeightedEdge {
WeightedEdge(weight_type weight = 0) : weight(weight) { }
weight_type weight;
};
struct VertexProperties {
VertexProperties(int distance = 0, default_color_type color = white_color)
: distance(distance), color(color) { }
int distance;
default_color_type color;
};
#endif
template <typename RandomGenerator, typename ProcessGroup, typename vertices_size_type,
typename edges_size_type>
void
run_non_distributed_graph_tests(RandomGenerator& gen, const ProcessGroup& pg,
vertices_size_type n, edges_size_type m,
std::size_t maxEdgeWeight, uint64_t seed,
int K4Alpha, double a, double b, double c, double d,
int subGraphEdgeLength, bool show_degree_dist,
bool full_bc, bool verify)
{
#ifdef CSR
typedef compressed_sparse_row_graph<directedS, VertexProperties, WeightedEdge>
seqGraph;
#else
typedef adjacency_list<vecS, vecS, directedS,
// Vertex properties
property<vertex_distance_t, int,
property<vertex_color_t, default_color_type> >,
// Edge properties
property<edge_weight_t, int> > seqGraph;
#endif
// Generate sequential graph for non_distributed betweenness centrality
// Reseed the PRNG to get the same graph
gen.seed(seed);
synchronize(pg);
time_type start = get_time();
#ifdef CSR
seqGraph sg(edges_are_sorted,
sorted_unique_rmat_iterator<RandomGenerator, seqGraph>(gen, n, m, a, b, c, d),
sorted_unique_rmat_iterator<RandomGenerator, seqGraph>(),
make_generator_iterator(gen, uniform_int<int>(0, maxEdgeWeight)),
n);
#else
seqGraph sg(unique_rmat_iterator<RandomGenerator, seqGraph>(gen, n, m, a, b, c, d),
unique_rmat_iterator<RandomGenerator, seqGraph>(),
make_generator_iterator(gen, uniform_int<int>(0, maxEdgeWeight)),
n);
#endif
// Not strictly necessary to synchronize here, but it make sure that the
// time we measure is the time needed for all copies of the graph to be
// constructed
synchronize(pg);
time_type end = get_time();
if (process_id(pg) == 0)
std::cerr<< "Kernel 1:\n"
<< " Non-Distributed Graph: " << print_time(end - start) << std::endl;
std::map<int, int> degree_dist;
if ( show_degree_dist ) {
BGL_FORALL_VERTICES_T(v, sg, seqGraph) {
degree_dist[out_degree(v, sg)]++;
}
std::cerr << "Degree - Fraction of vertices of that degree\n";
for (std::map<int, int>::iterator iter = degree_dist.begin();
iter != degree_dist.end(); ++iter)
std::cerr << " " << iter->first << " - " << double(iter->second) / num_vertices(sg) << std::endl << std::endl;
}
//
// Kernel 2 - Classify large sets
//
std::vector<graph_traits<seqGraph>::edge_descriptor> seqS;
if (process_id(pg) == 0)
std::cerr << "Kernel 2:\n";
seq_classify_sets(pg, sg,
#ifdef CSR
get(&WeightedEdge::weight, sg),
#else
get(edge_weight, sg),
#endif
seqS);
//
// Kernel 3 - Graph Extraction
//
#ifdef CSR
typedef weight_type weight_t;
weight_t unit_weight(1);
#else
int unit_weight(1);;
#endif
if (process_id(pg) == 0)
std::cerr << "Kernel 3:\n";
seq_subgraph_extraction(pg, sg,
#ifdef CSR
// get(&WeightedEdge::weight, sg),
ref_property_map<graph_traits<seqGraph>::edge_descriptor, weight_t>(unit_weight),
get(&VertexProperties::distance, sg),
get(&VertexProperties::color, sg),
#else
// get(edge_weight, sg),
ref_property_map<graph_traits<seqGraph>::edge_descriptor, int>(unit_weight),
get(vertex_distance, sg),
get(vertex_color, sg),
#endif
seqS, subGraphEdgeLength);
#ifdef CSR
typedef property_map<seqGraph, weight_type WeightedEdge::*>::type seqEdgeWeightMap;
edge_weight_not_divisible_by_eight<seqEdgeWeightMap> sg_filter(get(&WeightedEdge::weight, sg));
#else
typedef property_map<seqGraph, edge_weight_t>::type seqEdgeWeightMap;
edge_weight_not_divisible_by_eight<seqEdgeWeightMap> sg_filter(get(edge_weight, sg));
#endif
typedef filtered_graph<const seqGraph, edge_weight_not_divisible_by_eight<seqEdgeWeightMap> >
filteredSeqGraph;
filteredSeqGraph fsg(sg, sg_filter);
std::vector<graph_traits<seqGraph>::vertex_descriptor> max_seq_bc_vec;
// Non-Distributed Centrality Map
typedef property_map<seqGraph, vertex_index_t>::const_type seqIndexMap;
typedef iterator_property_map<std::vector<int>::iterator, seqIndexMap> seqCentralityMap;
std::vector<int> non_distributed_centralityS(num_vertices(sg), 0);
seqCentralityMap non_distributed_centrality(non_distributed_centralityS.begin(),
get(vertex_index, sg));
vertices_size_type n0 = 0;
BGL_FORALL_VERTICES_T(v, fsg, filteredSeqGraph) {
if (out_degree(v, fsg) == 0) ++n0;
}
if (process_id(pg) == 0)
std::cerr << "Kernel 4:\n";
// Run Betweenness Centrality
if (full_bc) {
// Non-Distributed Graph BC
start = get_time();
non_distributed_brandes_betweenness_centrality(pg, fsg, non_distributed_centrality);
extract_max_bc_vertices(pg, fsg, non_distributed_centrality, max_seq_bc_vec);
end = get_time();
edges_size_type nonDistributedExactTEPs = edges_size_type(floor(7 * n* (n - n0) / (end - start)));
if (process_id(pg) == 0)
std::cerr << " non-Distributed Graph Exact = " << print_time(end - start) << " ("
<< nonDistributedExactTEPs << " TEPs)\n";
}
// Non-Distributed Graph Approximate BC
std::vector<int> nonDistributedApproxCentralityS(num_vertices(sg), 0);
seqCentralityMap nonDistributedApproxCentrality(nonDistributedApproxCentralityS.begin(),
get(vertex_index, sg));
queue<typename graph_traits<filteredSeqGraph>::vertex_descriptor> sources;
{
minstd_rand gen;
uniform_int<vertices_size_type> rand_vertex(0, num_vertices(fsg) - 1);
int remaining_sources = floor(pow(2, K4Alpha));
std::vector<typename graph_traits<filteredSeqGraph>::vertex_descriptor> temp_sources;
while (remaining_sources > 0) {
typename graph_traits<filteredSeqGraph>::vertex_descriptor v =
vertex(rand_vertex(gen), fsg);
if (out_degree(v, fsg) != 0
&& std::find(temp_sources.begin(), temp_sources.end(), v) == temp_sources.end()) {
temp_sources.push_back(v);
--remaining_sources;
}
}
for (int i = 0; i < temp_sources.size(); ++i)
sources.push(temp_sources[i]);
}
start = get_time();
non_distributed_brandes_betweenness_centrality(pg, fsg, buffer(sources).
centrality_map(nonDistributedApproxCentrality));
extract_max_bc_vertices(pg, fsg, nonDistributedApproxCentrality, max_seq_bc_vec);
end = get_time();
edges_size_type nonDistributedApproxTEPs = edges_size_type(floor(7 * n * pow(2, K4Alpha) / (end - start)));
if (process_id(pg) == 0)
std::cerr << " Non-Distributed Graph Approximate (" << floor(pow(2, K4Alpha)) << " sources) = "
<< print_time(end - start) << " (" << nonDistributedApproxTEPs << " TEPs)\n";
// Verify Correctness of Kernel 4
if (full_bc && verify && process_id(pg) == 0) {
std::vector<int> seq_centralityS(num_vertices(fsg), 0);
seqCentralityMap seq_centrality(seq_centralityS.begin(), get(vertex_index, fsg));
max_seq_bc_vec.clear();
property_traits<seqCentralityMap>::value_type max_ = 0;
start = get_time();
brandes_betweenness_centrality(fsg, seq_centrality);
typedef filtered_graph<const seqGraph, edge_weight_not_divisible_by_eight<seqEdgeWeightMap> >
filteredSeqGraph;
BGL_FORALL_VERTICES_T(v, fsg, filteredSeqGraph ) {
if (get(seq_centrality, v) == max_)
max_seq_bc_vec.push_back(v);
else if (get(seq_centrality, v) > max_) {
max_ = get(seq_centrality, v);
max_seq_bc_vec.clear();
max_seq_bc_vec.push_back(v);
}
}
end = get_time();
edges_size_type sequentialTEPs = edges_size_type(floor(7 * n* (n - n0) / (end - start)));
std::cerr << " Sequential = " << print_time(end - start) << " (" << sequentialTEPs << " TEPs)\n";
typename ProcessGroup::process_id_type id = process_id(pg);
typename ProcessGroup::process_size_type p = num_processes(pg);
assert((double)n/p == floor((double)n/p));
std::cerr << "\nVerifying non-scalable betweenness centrality...\n";
{
bool passed = true;
// Verify non-scalable betweenness centrality
BGL_FORALL_VERTICES_T(v, sg, seqGraph) {
if (get(non_distributed_centrality, v) != get(seq_centrality, v)) {
std::cerr << " " << id << ": Error - centrality of " << v
<< " does not match the sequential result ("
<< get(non_distributed_centrality, v) << " vs. "
<< get(seq_centrality, v) << ")\n";
passed = false;
}
}
if (passed)
std::cerr << " PASSED\n";
}
}
}
template <typename RandomGenerator, typename ProcessGroup, typename vertices_size_type,
typename edges_size_type>
void
run_distributed_graph_tests(RandomGenerator& gen, const ProcessGroup& pg,
vertices_size_type n, edges_size_type m,
std::size_t maxEdgeWeight, uint64_t seed,
int K4Alpha, double a, double b, double c, double d,
int subGraphEdgeLength, bool show_degree_dist,
bool emit_dot_file, bool full_bc, bool verify)
{
#ifdef CSR
typedef compressed_sparse_row_graph<directedS, VertexProperties, WeightedEdge, no_property,
distributedS<ProcessGroup> > Graph;
#else
typedef adjacency_list<vecS,
distributedS<ProcessGroup, vecS>,
directedS,
// Vertex properties
property<vertex_distance_t, int,
property<vertex_color_t, default_color_type> >,
// Edge properties
property<edge_weight_t, int> > Graph;
#endif
gen.seed(seed);
parallel::variant_distribution<ProcessGroup> distrib
= parallel::block(pg, n);
typedef typename ProcessGroup::process_id_type process_id_type;
process_id_type id = process_id(pg);
typedef typename property_map<Graph, vertex_owner_t>::const_type OwnerMap;
typedef typename property_map<Graph, vertex_local_t>::const_type LocalMap;
typedef keep_local_edges<parallel::variant_distribution<ProcessGroup>,
process_id_type>
EdgeFilter;
//
// Kernel 1 - Graph construction
// Nick: The benchmark specifies that we only have to time graph generation from
// edge tuples, the generator generates the edge tuples at graph construction
// time so we're timing some overhead in the random number generator, etc.
synchronize(pg);
time_type start = get_time();
#ifdef CSR
// typedef sorted_unique_rmat_iterator<RandomGenerator, Graph, EdgeFilter> RMATIter;
typedef sorted_rmat_iterator<RandomGenerator, Graph, keep_all_edges> RMATIter;
Graph g(//RMATIter(gen, n, m, a, b, c, d, false, true, EdgeFilter(distrib, id)),
RMATIter(gen, n, m, a, b, c, d, true, keep_all_edges()),
RMATIter(),
make_generator_iterator(gen, uniform_int<int>(0, maxEdgeWeight)),
n, pg, distrib);
#else
typedef unique_rmat_iterator<RandomGenerator, Graph, EdgeFilter> RMATIter;
Graph g(RMATIter(gen, n, m, a, b, c, d, true EdgeFilter(distrib, id)),
RMATIter(),
make_generator_iterator(gen, uniform_int<int>(0, maxEdgeWeight)),
n, pg, distrib);
#endif
synchronize(pg);
time_type end = get_time();
if (id == 0)
std::cerr<< "Kernel 1:\n"
<< " Distributed Graph: " << print_time(end - start) << std::endl;
if ( emit_dot_file )
write_graphviz("ssca.dot", g);
//
// Kernel 2 - Classify large sets
//
typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
std::vector<std::pair<vertex_descriptor, vertex_descriptor> > S;
if (id == 0)
std::cerr << "Kernel 2:\n";
classify_sets(g,
#ifdef CSR
get(&WeightedEdge::weight, g),
#else
get(edge_weight, g),
#endif
S);
//
// Kernel 3 - Graph Extraction
//
OwnerMap owner = get(vertex_owner, g);
LocalMap local = get(vertex_local, g);
if (id == 0)
std::cerr << "Kernel 3:\n";
#ifdef CSR
typedef weight_type weight_t;
weight_t unit_weight(1);
#else
int unit_weight(1);;
#endif
subgraph_extraction(g, owner, local,
#ifdef CSR
// get(&WeightedEdge::weight, g),
ref_property_map<typename graph_traits<Graph>::edge_descriptor, weight_t>(unit_weight),
get(&VertexProperties::distance, g),
get(&VertexProperties::color, g),
#else
// get(edge_weight, g),
ref_property_map<graph_traits<Graph>::edge_descriptor, int>(unit_weight),
get(vertex_distance, g),
get(vertex_color, g),
#endif
S, subGraphEdgeLength);
//
// Kernel 4 - Betweenness Centrality
//
// Filter edges with weights divisible by 8
#ifdef CSR
typedef typename property_map<Graph, weight_type WeightedEdge::*>::type EdgeWeightMap;
edge_weight_not_divisible_by_eight<EdgeWeightMap> filter(get(&WeightedEdge::weight, g));
#else
typedef typename property_map<Graph, edge_weight_t>::type EdgeWeightMap;
edge_weight_not_divisible_by_eight<EdgeWeightMap> filter(get(edge_weight, g));
#endif
typedef filtered_graph<const Graph, edge_weight_not_divisible_by_eight<EdgeWeightMap> >
filteredGraph;
filteredGraph fg(g, filter);
// Vectors of max BC scores for all tests
std::vector<typename graph_traits<Graph>::vertex_descriptor> max_bc_vec;
// Distributed Centrality Map
typedef typename property_map<Graph, vertex_index_t>::const_type IndexMap;
typedef iterator_property_map<std::vector<int>::iterator, IndexMap> CentralityMap;
std::vector<int> centralityS(num_vertices(g), 0);
CentralityMap centrality(centralityS.begin(), get(vertex_index, g));
// Calculate number of vertices of degree 0
vertices_size_type local_n0 = 0, n0;
BGL_FORALL_VERTICES_T(v, fg, filteredGraph) {
if (out_degree(v, g) == 0) local_n0++;
}
n0 = boost::parallel::all_reduce(pg, local_n0, std::plus<vertices_size_type>());
if (id == 0)
std::cerr << "Kernel 4:\n";
// Run Betweenness Centrality
if (full_bc) {
// Distributed Graph Full BC
start = get_time();
brandes_betweenness_centrality(fg, centrality);
extract_max_bc_vertices(pg, g, centrality, max_bc_vec);
end = get_time();
edges_size_type exactTEPs = edges_size_type(floor(7 * n* (n - n0) / (end - start)));
if (id == 0)
std::cerr << " Exact = " << print_time(end - start) << " ("
<< exactTEPs << " TEPs)\n";
}
// Distributed Graph Approximate BC
std::vector<int> approxCentralityS(num_vertices(g), 0);
CentralityMap approxCentrality(approxCentralityS.begin(), get(vertex_index, g));
queue<vertex_descriptor> sources;
generate_sources(g, sources, vertices_size_type(floor(pow(2, K4Alpha))));
start = get_time();
brandes_betweenness_centrality(fg, buffer(sources).centrality_map(approxCentrality));
extract_max_bc_vertices(pg, fg, approxCentrality, max_bc_vec);
end = get_time();
edges_size_type approxTEPs = edges_size_type(floor(7 * n * pow(2, K4Alpha) / (end - start)));
if (id == 0)
std::cerr << " Approximate (" << floor(pow(2, K4Alpha)) << " sources) = "
<< print_time(end - start) << " (" << approxTEPs << " TEPs)\n";
// Verify Correctness of Kernel 4
if (full_bc && verify && id == 0) {
// Build non-distributed graph to verify against
typedef adjacency_list<vecS, vecS, directedS,
// Vertex properties
property<vertex_distance_t, int,
property<vertex_color_t, default_color_type> >,
// Edge properties
property<edge_weight_t, int> > seqGraph;
gen.seed(seed);
#ifdef CSR
seqGraph sg(sorted_unique_rmat_iterator<RandomGenerator, seqGraph>(gen, n, m, a, b, c, d),
sorted_unique_rmat_iterator<RandomGenerator, seqGraph>(),
make_generator_iterator(gen, uniform_int<int>(0, maxEdgeWeight)),
n);
#else
seqGraph sg(unique_rmat_iterator<RandomGenerator, seqGraph>(gen, n, m, a, b, c, d),
unique_rmat_iterator<RandomGenerator, seqGraph>(),
make_generator_iterator(gen, uniform_int<int>(0, maxEdgeWeight)),
n);
#endif
typedef property_map<seqGraph, edge_weight_t>::type seqEdgeWeightMap;
edge_weight_not_divisible_by_eight<seqEdgeWeightMap> sg_filter(get(edge_weight, sg));
filtered_graph<const seqGraph, edge_weight_not_divisible_by_eight<seqEdgeWeightMap> >
fsg(sg, sg_filter);
// Build sequential centrality map
typedef property_map<seqGraph, vertex_index_t>::const_type seqIndexMap;
typedef iterator_property_map<std::vector<int>::iterator, seqIndexMap> seqCentralityMap;
std::vector<int> seq_centralityS(num_vertices(sg), 0);
seqCentralityMap seq_centrality(seq_centralityS.begin(), get(vertex_index, sg));
std::vector<graph_traits<seqGraph>::vertex_descriptor> max_seq_bc_vec;
max_seq_bc_vec.clear();
property_traits<seqCentralityMap>::value_type max_ = 0;
start = get_time();
brandes_betweenness_centrality(fsg, seq_centrality);
typedef filtered_graph<const seqGraph, edge_weight_not_divisible_by_eight<seqEdgeWeightMap> >
filteredSeqGraph;
BGL_FORALL_VERTICES_T(v, fsg, filteredSeqGraph ) {
if (get(seq_centrality, v) == max_)
max_seq_bc_vec.push_back(v);
else if (get(seq_centrality, v) > max_) {
max_ = get(seq_centrality, v);
max_seq_bc_vec.clear();
max_seq_bc_vec.push_back(v);
}
}
end = get_time();
edges_size_type sequentialTEPs = edges_size_type(floor(7 * n* (n - n0) / (end - start)));
std::cerr << " Sequential = " << print_time(end - start) << " (" << sequentialTEPs << " TEPs)\n";
typename ProcessGroup::process_size_type p = num_processes(pg);
assert((double)n/p == floor((double)n/p));
std::cerr << "\nVerifying betweenness centrality...\n";
{
bool passed = true;
// Verify exact betweenness centrality
BGL_FORALL_VERTICES_T(v, g, Graph) {
if (get(centrality, v) != seq_centralityS[(n/p) * get(owner, v) + get(local, v)]) {
std::cerr << " " << id << ": Error - centrality of " << get(local, v) << "@" << get(owner, v)
<< " does not match the sequential result (" << get(centrality, v) << " vs. "
<< seq_centralityS[(n/p) * get(owner, v) + get(local, v)] << ")\n";
passed = false;
}
}
if (passed)
std::cerr << " PASSED\n";
}
}
}
void usage()
{
std::cerr << "SSCA benchmark.\n\n"
<< "Usage : ssca [options]\n\n"
<< "Options are:\n"
<< "\t--vertices v\t\t\tNumber of vertices in the graph\n"
<< "\t--edges v\t\t\tNumber of edges in the graph\n"
<< "\t--seed s\t\t\tSeed for synchronized random number generator\n"
<< "\t--full-bc\t\t\tRun full (exact) Betweenness Centrality\n"
<< "\t--max-weight miw\t\tMaximum integer edge weight\n"
<< "\t--subgraph-edge-length sel\tEdge length of subgraphs to extract in Kernel 3\n"
<< "\t--k4alpha k\t\t\tValue of K4Alpha in Kernel 4\n"
<< "\t--scale s\t\t\tSCALE parameter for the SSCA benchmark (sets n, m, and C)\n"
<< "\t--dot\t\t\t\tEmit a dot file containing the graph\n"
<< "\t--verify\t\t\tVerify result\n"
<< "\t--degree-dist\t\t\t Output degree distribution of graph\n"
<< "\t--no-distributed-graph\t\tOmit distributed graph tests\n";
}
int test_main(int argc, char* argv[])
{
mpi::environment env(argc, argv);
using boost::graph::distributed::mpi_process_group;
#ifdef CSR
typedef compressed_sparse_row_graph<directedS, VertexProperties, WeightedEdge, no_property,
distributedS<mpi_process_group> > Graph;
#else
typedef adjacency_list<vecS,
distributedS<mpi_process_group, vecS>,
directedS,
// Vertex properties
property<vertex_distance_t, int,
property<vertex_color_t, default_color_type> >,
// Edge properties
property<edge_weight_t, int> > Graph;
#endif
typedef graph_traits<Graph>::vertices_size_type vertices_size_type;
typedef graph_traits<Graph>::edges_size_type edges_size_type;
RandomGenerator gen;
// Default args
vertices_size_type n = 100;
edges_size_type m = 8*n;
uint64_t seed = 1;
int maxEdgeWeight = 100,
subGraphEdgeLength = 8,
K4Alpha = 0.5;
double a = 0.57, b = 0.19, c = 0.19, d = 0.05;
bool emit_dot_file = false, verify = false, full_bc = true,
distributed_graph = true, show_degree_dist = false,
non_distributed_graph = true;
mpi_process_group pg;
if (argc == 1) {
if (process_id(pg) == 0)
usage();
exit(-1);
}
// Parse args
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--vertices")
n = boost::lexical_cast<vertices_size_type>( argv[i+1] );
if (arg == "--seed")
seed = boost::lexical_cast<uint64_t>( argv[i+1] );
if (arg == "--full-bc")
full_bc = (argv[i+1]== "true");
if (arg == "--max-weight")
maxEdgeWeight = boost::lexical_cast<int>( argv[i+1] );
if (arg == "--subgraph-edge-length")
subGraphEdgeLength = boost::lexical_cast<int>( argv[i+1] );
if (arg == "--edges")
m = boost::lexical_cast<edges_size_type>( argv[i+1] );
if (arg == "--k4alpha")
K4Alpha = boost::lexical_cast<int>( argv[i+1] );
if (arg == "--dot")
emit_dot_file = true;
if (arg == "--verify")
verify = true;
if (arg == "--degree-dist")
show_degree_dist = true;
if (arg == "--no-distributed-graph")
distributed_graph = false;
if (arg == "--no-non-distributed-graph")
non_distributed_graph = false;
if (arg == "--scale") {
vertices_size_type scale = boost::lexical_cast<vertices_size_type>( argv[i+1] );
maxEdgeWeight = n = vertices_size_type(floor(pow(2, scale)));
m = 8 * n;
}
if (arg == "--help") {
if (process_id(pg) == 0)
usage();
exit(-1);
}
}
if (non_distributed_graph) {
if (process_id(pg) == 0)
std::cerr << "Non-Distributed Graph Tests\n";
run_non_distributed_graph_tests(gen, pg, n, m, maxEdgeWeight, seed, K4Alpha, a, b, c, d,
subGraphEdgeLength, show_degree_dist, full_bc, verify);
}
if (distributed_graph) {
if (process_id(pg) == 0)
std::cerr << "Distributed Graph Tests\n";
run_distributed_graph_tests(gen, pg, n, m, maxEdgeWeight, seed, K4Alpha, a, b, c, d,
subGraphEdgeLength, show_degree_dist, emit_dot_file,
full_bc, verify);
}
return 0;
}
|