-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsssp.h
More file actions
328 lines (305 loc) · 9.91 KB
/
Copy pathsssp.h
File metadata and controls
328 lines (305 loc) · 9.91 KB
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
#pragma once
#include "graph.h"
#include "hashbag.h"
#include "parlay/internal/get_time.h"
using namespace std;
using namespace parlay;
constexpr int NUM_SRC = 20;
constexpr int NUM_ROUND = 5;
constexpr size_t LOCAL_QUEUE_SIZE = 4096;
constexpr size_t DEG_THLD = 20;
constexpr size_t SSSP_SAMPLES = 1000;
enum Algorithm { rho_stepping = 0, delta_stepping, bellman_ford };
class SSSP {
protected:
const Graph &G;
bool sparse;
const int sd_scale;
size_t frontier_size;
hashbag<NodeId> bag;
sequence<EdgeTy> dist;
sequence<NodeId> frontier;
sequence<atomic<bool>> in_frontier;
sequence<atomic<bool>> in_next_frontier;
void add_to_frontier(NodeId v) {
if (sparse) {
if (!in_frontier[v] &&
compare_and_swap(&in_next_frontier[v], false, true)) {
bag.insert(v);
}
} else { // dense
if (!in_frontier[v] && !in_next_frontier[v]) {
in_next_frontier[v] = true;
}
}
}
size_t estimate_size() {
static uint32_t seed = 10086;
size_t hits = 0;
for (size_t i = 0; i < SSSP_SAMPLES; i++) {
NodeId u = hash32(seed) % G.n;
if (in_frontier[u]) {
hits++;
}
seed++;
}
return hits * G.n / SSSP_SAMPLES;
}
size_t sparse_relax() {
// static uint32_t seed = 353442899;
// size_t sum_deg = 0;
// for (size_t i = 0; i < SSSP_SAMPLES; i++) {
// NodeId u = frontier[hash32(seed) % size];
// sum_deg += G.offset[u + 1] - G.offset[u];
// seed++;
//}
// size_t avg_deg = sum_deg / SSSP_SAMPLES;
// bool super_sparse = (avg_deg <= DEG_THLD);
bool super_sparse = true;
EdgeTy th = get_threshold();
parallel_for(0, frontier_size, [&](size_t i) {
NodeId f = frontier[i];
in_frontier[f] = false;
if (dist[f] > th) {
add_to_frontier(f);
} else {
size_t _n = G.offset[f + 1] - G.offset[f];
if (super_sparse && _n < LOCAL_QUEUE_SIZE) {
NodeId local_queue[LOCAL_QUEUE_SIZE];
size_t front = 0, rear = 0;
local_queue[rear++] = f;
while (front < rear && rear < LOCAL_QUEUE_SIZE) {
NodeId u = local_queue[front++];
size_t deg = G.offset[u + 1] - G.offset[u];
if (deg >= LOCAL_QUEUE_SIZE || dist[u] > th) {
add_to_frontier(u);
continue;
}
if (G.symmetrized) {
EdgeTy temp_dist = dist[u];
for (EdgeId es = G.offset[u]; es < G.offset[u + 1]; es++) {
NodeId v = G.edge[es].v;
EdgeTy w = G.edge[es].w;
if (dist[v] != DIST_MAX) {
temp_dist = min(temp_dist, dist[v] + w);
}
}
write_min(&dist[u], temp_dist,
[](EdgeTy w1, EdgeTy w2) { return w1 < w2; });
}
for (EdgeId es = G.offset[u]; es < G.offset[u + 1]; es++) {
NodeId v = G.edge[es].v;
EdgeTy w = G.edge[es].w;
if (write_min(&dist[v], dist[u] + w,
[](EdgeTy w1, EdgeTy w2) { return w1 < w2; })) {
if (rear < LOCAL_QUEUE_SIZE) {
local_queue[rear++] = v;
} else {
add_to_frontier(v);
}
}
}
}
for (size_t j = front; j < rear; j++) {
add_to_frontier(local_queue[j]);
}
} else {
blocked_for(
G.offset[f], G.offset[f + 1], BLOCK_SIZE,
[&](size_t, size_t _s, size_t _e) {
if (G.symmetrized) {
EdgeTy temp_dist = dist[f];
for (EdgeId es = _s; es < _e; es++) {
NodeId v = G.edge[es].v;
EdgeTy w = G.edge[es].w;
if (dist[v] != DIST_MAX) {
temp_dist = min(temp_dist, dist[v] + w);
}
}
if (write_min(&dist[f], temp_dist,
[](EdgeTy w1, EdgeTy w2) { return w1 < w2; })) {
add_to_frontier(f);
}
}
for (EdgeId es = _s; es < _e; es++) {
NodeId v = G.edge[es].v;
EdgeTy w = G.edge[es].w;
if (write_min(&dist[v], dist[f] + w,
[](EdgeTy w1, EdgeTy w2) { return w1 < w2; })) {
add_to_frontier(v);
}
}
});
}
}
});
swap(in_frontier, in_next_frontier);
return bag.pack_into(make_slice(frontier));
}
size_t dense_relax() {
while (estimate_size() >= G.n / sd_scale) {
EdgeTy th = get_threshold();
parallel_for(0, G.n, [&](NodeId u) {
if (in_frontier[u]) {
in_frontier[u] = false;
if (dist[u] > th) {
in_next_frontier[u] = true;
} else {
blocked_for(G.offset[u], G.offset[u + 1], BLOCK_SIZE,
[&](size_t, size_t _s, size_t _e) {
if (G.symmetrized) {
EdgeTy temp_dist = dist[u];
for (size_t es = _s; es < _e; es++) {
NodeId v = G.edge[es].v;
EdgeTy w = G.edge[es].w;
if (dist[v] != DIST_MAX) {
temp_dist = min(temp_dist, dist[v] + w);
}
}
if (write_min(&dist[u], temp_dist,
[](EdgeTy w1, EdgeTy w2) {
return w1 < w2;
})) {
add_to_frontier(u);
}
}
for (size_t es = _s; es < _e; es++) {
NodeId v = G.edge[es].v;
EdgeTy w = G.edge[es].w;
if (write_min(&dist[v], dist[u] + w,
[](EdgeTy w1, EdgeTy w2) {
return w1 < w2;
})) {
add_to_frontier(v);
}
}
});
}
}
});
swap(in_frontier, in_next_frontier);
}
return count(in_frontier, true);
}
void sparse2dense() {
// parallel_for(0, frontier_size, [&](size_t i) {
// NodeId u = frontier[i];
// assert(in_frontier[u] == true);
// in_frontier[u] = true;
//});
}
void dense2sparse() {
auto identity = delayed_seq<NodeId>(G.n, [&](NodeId i) { return i; });
pack_into_uninitialized(identity, in_frontier, frontier);
}
function<void()> init;
function<EdgeTy()> get_threshold;
public:
SSSP() = delete;
SSSP(const Graph &_G) : G(_G), sd_scale(G.m / G.n), bag(G.n) {
dist = sequence<EdgeTy>::uninitialized(G.n);
frontier = sequence<NodeId>::uninitialized(G.n);
in_frontier = sequence<atomic<bool>>::uninitialized(G.n);
in_next_frontier = sequence<atomic<bool>>::uninitialized(G.n);
}
sequence<EdgeTy> sssp(NodeId s) {
if (!G.weighted) {
fprintf(stderr, "Error: Input graph is unweighted\n");
exit(EXIT_FAILURE);
}
init();
// parallel_for(0, G.n, [&](NodeId i) {
// dist[i] = DIST_MAX;
// in_frontier[i] = in_next_frontier[i] = false;
// });
frontier_size = 1;
dist[s] = 0;
frontier[0] = s;
in_frontier[s] = true;
sparse = true;
// int round = 0;
while (frontier_size) {
// printf("Round %d: %s, size: %zu, ", round++, sparse ? "sparse" :
// "dense", frontier_size); internal::timer t;
if (sparse) {
frontier_size = sparse_relax();
} else {
frontier_size = dense_relax();
}
// printf("relax: %f, ", t.next_time());
bool next_sparse = (frontier_size < G.n / sd_scale) ? true : false;
if (sparse && !next_sparse) {
sparse2dense();
} else if (!sparse && next_sparse) {
dense2sparse();
}
// printf("pack: %f\n", t.next_time());
sparse = next_sparse;
}
return dist;
}
void reset() {
parallel_for(0, G.n, [&](NodeId i) {
dist[i] = DIST_MAX;
in_frontier[i] = in_next_frontier[i] = false;
});
}
};
class Rho_Stepping : public SSSP {
size_t rho;
uint32_t seed;
public:
Rho_Stepping(const Graph &_G, size_t _rho = 1 << 20) : SSSP(_G), rho(_rho) {
seed = 0;
init = []() {};
get_threshold = [&]() {
if (frontier_size <= rho) {
if (sparse) {
auto _dist = delayed_seq<EdgeTy>(
frontier_size, [&](size_t i) { return dist[frontier[i]]; });
return *max_element(_dist);
} else {
return DIST_MAX;
}
}
EdgeTy sample_dist[SSSP_SAMPLES + 1];
for (size_t i = 0; i <= SSSP_SAMPLES; i++) {
if (sparse) {
NodeId v = frontier[hash32(seed + i) % frontier_size];
sample_dist[i] = dist[v];
} else {
NodeId v = hash32(seed + i) % G.n;
if (in_frontier[v]) {
sample_dist[i] = dist[v];
} else {
sample_dist[i] = DIST_MAX;
}
}
}
seed += SSSP_SAMPLES + 1;
size_t id = 1.0 * rho / frontier_size * SSSP_SAMPLES;
sort(sample_dist, sample_dist + SSSP_SAMPLES + 1);
return sample_dist[id];
};
}
};
class Delta_Stepping : public SSSP {
EdgeTy delta;
EdgeTy thres;
public:
Delta_Stepping(const Graph &_G, EdgeTy _delta = 1 << 15)
: SSSP(_G), delta(_delta) {
init = [&]() { thres = 0; };
get_threshold = [&]() {
thres += delta;
return thres;
};
}
};
class Bellman_Ford : public SSSP {
public:
Bellman_Ford(const Graph &_G) : SSSP(_G) {
init = []() {};
get_threshold = []() { return DIST_MAX; };
}
};