-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_label_propagation.cc
194 lines (160 loc) · 5.9 KB
/
base_label_propagation.cc
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
#include "base_label_propagation.h"
#include <kaminpar-common/random.h>
#include <kaminpar-dist/timer.h>
#include <mpi.h>
namespace kaminpar::dist {
BaseLP::BaseLP(Context const& ctx)
: _ctx(ctx), _lp_ctx(ctx.coarsening.global_lp) {
_global_cluster_weights.set_empty_key(kInvalidGlobalNodeID);
_cluster_weight_deltas.set_empty_key(kInvalidGlobalNodeID);
}
void BaseLP::initialize(DistributedGraph const& graph) {
_graph = &graph;
MPI_Barrier(graph.communicator());
SCOPED_TIMER("Label propagation clustering");
_global_cluster_weights.clear();
_cluster_weight_deltas.clear();
_prev_clustering.resize(_graph->total_n());
_clustering.resize(_graph->total_n());
_local_cluster_weights.resize(_graph->n());
for (NodeID lu : _graph->all_nodes()) {
_prev_clustering[lu] = _graph->local_to_global_node(lu);
_clustering[lu] = _graph->local_to_global_node(lu);
}
for (NodeID lu : _graph->nodes()) {
_local_cluster_weights[lu] = _graph->node_weight(lu);
}
for (NodeID lu : _graph->ghost_nodes()) {
_global_cluster_weights[_graph->local_to_global_node(lu)] =
_graph->node_weight(lu);
}
_ratings.change_max_size(_graph->total_n());
}
GlobalClusterer::ClusterArray& BaseLP::cluster(
DistributedGraph const& graph, GlobalNodeWeight max_cluster_weight) {
MPI_Barrier(graph.communicator());
SCOPED_TIMER("Label propagation clustering");
_max_cluster_weight = max_cluster_weight;
int const num_chunks = _lp_ctx.chunks.compute(_ctx.parallel);
GlobalNodeID num_moved_nodes_overall = 0;
for (int it = 0; it < _lp_ctx.num_iterations; ++it) {
GlobalNodeID num_moved_nodes = 0;
for (int chunk = 0; chunk < num_chunks; ++chunk) {
auto const [from, to] =
math::compute_local_range<NodeID>(_graph->n(), num_chunks, chunk);
num_moved_nodes += process_chunk(from, to);
}
num_moved_nodes_overall += num_moved_nodes;
if (num_moved_nodes == 0) {
break;
}
}
// If we couldn't reduce the number of nodes by at least x2, try to shrink the
// graph further by clustering isolated nodes
if (2 * num_moved_nodes_overall < _graph->global_n()) {
cluster_isolated_nodes();
}
return _clustering;
}
GlobalNodeID BaseLP::process_chunk(NodeID const from, NodeID const to) {
GlobalNodeID num_moved_nodes = 0;
for (NodeID u = from; u < to; ++u) {
if (handle_node(u)) {
++num_moved_nodes;
}
}
MPI_Allreduce(MPI_IN_PLACE, &num_moved_nodes, 1,
mpi::type::get<GlobalNodeID>(), MPI_SUM,
_graph->communicator());
if (num_moved_nodes > 0) {
enforce_max_cluster_weights(from, to);
synchronize_ghost_node_clusters(from, to);
}
_graph->pfor_nodes(from, to, [&](NodeID const lnode) {
_prev_clustering[lnode] = _clustering[lnode];
});
return num_moved_nodes;
}
bool BaseLP::handle_node(NodeID const u) {
NodeWeight const w_u = _graph->node_weight(u);
GlobalNodeID const old_gcluster = _prev_clustering[u];
GlobalNodeID const new_gcluster = find_best_cluster(u, w_u, old_gcluster);
if (old_gcluster != new_gcluster) {
move_node(u, w_u, old_gcluster, new_gcluster);
}
return old_gcluster != new_gcluster;
}
GlobalNodeID BaseLP::find_best_cluster(NodeID const u, NodeWeight const w_u,
GlobalNodeID const old_gcluster) {
return _ratings.execute(_graph->degree(u), [&](auto& map) {
for (auto const [e, v] : _graph->neighbors(u)) {
map[_clustering[v]] += _graph->edge_weight(e);
}
Random& rand = Random::instance();
EdgeWeight max_conn = 0;
GlobalNodeID new_gcluster = old_gcluster;
for (auto const [gcluster, conn] : map.entries()) {
if ((conn > max_conn || (conn == max_conn && rand.random_bool())) &&
cluster_weight(gcluster) + w_u <= _max_cluster_weight) {
new_gcluster = gcluster;
max_conn = conn;
}
}
map.clear();
return new_gcluster;
});
}
GlobalNodeWeight BaseLP::cluster_weight(GlobalNodeID const gcluster) {
if (_graph->is_owned_global_node(gcluster)) {
return _local_cluster_weights[_graph->global_to_local_node(gcluster)];
} else {
return _global_cluster_weights[gcluster];
}
}
void BaseLP::init_cluster_weight(GlobalNodeID const lcluster,
GlobalNodeWeight const weight) {
if (_graph->is_owned_node(lcluster)) {
_local_cluster_weights[lcluster] = weight;
} else {
auto const gcluster =
_graph->local_to_global_node(static_cast<NodeID>(lcluster));
_global_cluster_weights[gcluster] = weight;
}
}
void BaseLP::move_node(NodeID const lnode, NodeWeight const w_lnode,
GlobalNodeID const old_gcluster,
GlobalNodeID const new_gcluster) {
_clustering[lnode] = new_gcluster;
change_cluster_weight(old_gcluster, -w_lnode, true);
change_cluster_weight(new_gcluster, w_lnode, true);
}
void BaseLP::change_cluster_weight(GlobalNodeID const gcluster,
GlobalNodeWeight const delta,
[[maybe_unused]] bool const must_exist) {
if (_graph->is_owned_global_node(gcluster)) {
_local_cluster_weights[_graph->global_to_local_node(gcluster)] += delta;
} else {
_global_cluster_weights[gcluster] += delta;
}
}
void BaseLP::cluster_isolated_nodes(NodeID const from, NodeID const to) {
GlobalNodeID cur_C = kInvalidGlobalNodeID;
GlobalNodeWeight cur_w_C = kInvalidGlobalNodeWeight;
for (NodeID const u :
_graph->nodes(from, std::min<NodeID>(to, _graph->n()))) {
if (_graph->degree(u) == 0) {
auto const C_u = _prev_clustering[u];
auto const w_C_u = cluster_weight(C_u);
if (cur_C != kInvalidGlobalNodeID &&
cur_w_C + w_C_u <= _max_cluster_weight) {
change_cluster_weight(cur_C, w_C_u, true);
_clustering[u] = cur_C;
cur_w_C += w_C_u;
} else {
cur_C = C_u;
cur_w_C = w_C_u;
}
}
}
}
} // namespace kaminpar::dist