-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastar.h
More file actions
475 lines (442 loc) · 14.2 KB
/
Copy pathastar.h
File metadata and controls
475 lines (442 loc) · 14.2 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
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
#pragma once
#include "graph.h"
#include "hashbag.h"
#include "parlay/internal/get_time.h"
using namespace std;
using namespace parlay;
constexpr size_t LOCAL_QUEUE_SIZE = 4096;
constexpr size_t DEG_THLD = 20;
constexpr size_t NUM_SAMPLES = 1000;
enum Algorithm { rho_stepping = 0, delta_stepping, bellman_ford };
class ASTAR {
protected:
const Graph &G;
const sequence<double> &coords;
const bool bidirection;
size_t n;
size_t dimension;
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;
//sequence<double> heuristic_seq;
NodeId source;
NodeId destination;
EdgeTy gth;
inline NodeId Node(NodeId u) { return u << bidirection; }
inline NodeId get_vertex(NodeId u) { return u >> bidirection; }
inline NodeId get_source(NodeId u) { return bidirection && (u & 1); }
#ifdef OSM
inline double toRadians(const double °ree) {
double one_deg = (M_PI) / 180;
return (one_deg * degree);
}
inline double distance(double lat1, double long1, double lat2, double long2) {
lat1 = toRadians(lat1);
long1 = toRadians(long1);
lat2 = toRadians(lat2);
long2 = toRadians(long2);
double dlong = long2 - long1;
double dlat = lat2 - lat1;
double ans =
pow(sin(dlat / 2), 2) + cos(lat1) * cos(lat2) * pow(sin(dlong / 2), 2);
ans = 2 * asin(sqrt(ans));
double R = 6371;
ans = ans * R * 1000;
return ans;
}
inline EdgeTy heuristic_func(NodeId u, NodeId v) {
return distance(coords[u * 2 + 1], coords[u * 2], coords[v * 2 + 1],
coords[v * 2]);
}
#else
inline EdgeTy heuristic_func(NodeId u, NodeId v) {
double diff = 0;
for (size_t i = 0; i < dimension; ++i) {
diff += (coords[u * dimension + i] - coords[v * dimension + i]) *
(coords[u * dimension + i] - coords[v * dimension + i]);
}
return sqrt(diff);
}
#endif
inline EdgeTy heuristic(NodeId u, NodeId v) {
NodeId s = source;
NodeId t = destination;
if (v == t) {
return heuristic_func(u, t);
//if (heuristic_seq[u] == DIST_MAX) {
//heuristic_seq[u] = heuristic_func(u, t);
//}
//return heuristic_seq[u];
} else {
return heuristic_func(u, s);
//if (heuristic_seq[u + G.n] == DIST_MAX) {
//heuristic_seq[u + G.n] = heuristic_func(u, s);
//}
//return heuristic_seq[u + G.n];
}
}
inline EdgeTy estimate_distance(NodeId u) {
if (bidirection) {
NodeId s = source;
NodeId t = destination;
NodeId v = get_vertex(u);
if (get_source(u) == false) {
return dist[u] + (heuristic(v, t) - heuristic(v, s)) / 2;
} else {
return dist[u] + (heuristic(v, s) - heuristic(v, t)) / 2;
}
} else {
return dist[u] + heuristic(u, destination);
}
}
bool get_processing_status() {
if (gth != DIST_MAX) {
return true;
}
NodeId processing_set = 0;
if (sparse) {
auto state = delayed_seq<NodeId>(frontier_size, [&](size_t i) {
return static_cast<NodeId>(1 << get_source(frontier[i]));
});
processing_set = reduce(state, parlay::bit_or<NodeId>());
} else {
auto state = delayed_seq<NodeId>(n, [&](size_t i) {
if (!in_frontier[i]) {
return static_cast<NodeId>(0);
}
return static_cast<NodeId>(1 << get_source(i));
});
processing_set = reduce(state, parlay::bit_or<NodeId>());
}
return processing_set == 3;
}
void update_global_threshold(NodeId v) {
if (bidirection) {
if (dist[v | 1] != DIST_MAX && dist[v & ~1] != DIST_MAX) {
write_min(>h, dist[v | 1] + dist[v & ~1]);
}
} else if (v == destination) {
write_min(>h, dist[v]);
}
}
bool prune(NodeId v) {
if (bidirection) {
return estimate_distance(v) * 2 >= gth;
} else {
return estimate_distance(v) >= gth;
}
}
void add_to_frontier(NodeId v) {
if (prune(v)) {
return;
}
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 < NUM_SAMPLES; i++) {
NodeId u = hash32(seed) % n;
if (in_frontier[u]) {
hits++;
}
seed++;
}
return hits * n / NUM_SAMPLES;
}
size_t sparse_relax() {
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 (estimate_distance(f) > th) {
add_to_frontier(f);
} else {
size_t _n = G.offset[get_vertex(f) + 1] - G.offset[get_vertex(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[get_vertex(u) + 1] - G.offset[get_vertex(u)];
if (deg >= LOCAL_QUEUE_SIZE || estimate_distance(u) > th) {
add_to_frontier(u);
continue;
}
if (G.symmetrized) {
EdgeTy temp_dist = dist[u];
for (EdgeId es = G.offset[get_vertex(u)];
es < G.offset[get_vertex(u) + 1]; es++) {
NodeId v = G.edge[es].v;
v = Node(v) + get_source(u);
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)) {
update_global_threshold(u);
}
}
for (EdgeId es = G.offset[get_vertex(u)];
es < G.offset[get_vertex(u) + 1]; es++) {
NodeId v = G.edge[es].v;
v = Node(v) + get_source(u);
EdgeTy w = G.edge[es].w;
if (write_min(&dist[v], dist[u] + w)) {
update_global_threshold(v);
if (rear < LOCAL_QUEUE_SIZE) {
if (prune(v)) {
continue;
}
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[get_vertex(f)], G.offset[get_vertex(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;
v = Node(v) + get_source(f);
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)) {
update_global_threshold(f);
add_to_frontier(f);
}
}
for (EdgeId es = _s; es < _e; es++) {
NodeId v = G.edge[es].v;
v = Node(v) + get_source(f);
EdgeTy w = G.edge[es].w;
if (write_min(&dist[v], dist[f] + w)) {
update_global_threshold(v);
add_to_frontier(v);
}
}
});
}
}
});
swap(in_frontier, in_next_frontier);
return bag.pack_into(make_slice(frontier));
}
size_t dense_relax() {
while (estimate_size() >= n / sd_scale) {
EdgeTy th = get_threshold();
parallel_for(0, n, [&](NodeId u) {
if (in_frontier[u]) {
in_frontier[u] = false;
if (estimate_distance(u) > th) {
in_next_frontier[u] = true;
} else {
blocked_for(G.offset[get_vertex(u)], G.offset[get_vertex(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;
v = Node(v) + get_source(u);
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)) {
update_global_threshold(u);
add_to_frontier(u);
}
}
for (size_t es = _s; es < _e; es++) {
NodeId v = G.edge[es].v;
v = Node(v) + get_source(u);
EdgeTy w = G.edge[es].w;
if (write_min(&dist[v], dist[u] + w)) {
update_global_threshold(v);
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>(n, [&](NodeId i) { return i; });
pack_into_uninitialized(identity, in_frontier, frontier);
}
virtual void init() = 0;
virtual EdgeTy get_threshold() = 0;
public:
ASTAR() = delete;
ASTAR(const Graph &_G, const sequence<double> &_coords, bool _bidirection)
: G(_G),
coords(_coords),
bidirection(_bidirection),
n(bidirection ? 2 * G.n : G.n),
sd_scale(G.m / G.n),
bag(n) {
dist = sequence<EdgeTy>::uninitialized(n);
frontier = sequence<NodeId>::uninitialized(n);
in_frontier = sequence<atomic<bool>>::uninitialized(n);
in_next_frontier = sequence<atomic<bool>>::uninitialized(n);
//heuristic_seq = sequence<double>::uninitialized(n);
if (!coords.empty()) {
dimension = coords.size() / G.n;
}
}
EdgeTy astar(NodeId s, NodeId t) {
if (!G.weighted) {
fprintf(stderr, "Error: Input graph is unweighted\n");
exit(EXIT_FAILURE);
}
gth = DIST_MAX;
source = s;
destination = t;
init();
// parallel_for(0, n, [&](NodeId i) {
// dist[i] = DIST_MAX;
// in_frontier[i] = in_next_frontier[i] = false;
// });
frontier_size = 0;
if (bidirection) {
NodeId s_d = Node(s), t_d = Node(t) + 1;
dist[s_d] = dist[t_d] = 0;
in_frontier[s_d] = in_frontier[t_d] = true;
frontier[frontier_size++] = s_d;
frontier[frontier_size++] = t_d;
} else {
dist[s] = 0;
in_frontier[s] = true;
frontier[frontier_size++] = s;
}
sparse = true;
while (frontier_size) {
if (bidirection) {
if (!get_processing_status()) {
break;
}
}
if (sparse) {
frontier_size = sparse_relax();
} else {
frontier_size = dense_relax();
}
bool next_sparse = (frontier_size < n / sd_scale) ? true : false;
if (sparse && !next_sparse) {
sparse2dense();
} else if (!sparse && next_sparse) {
dense2sparse();
}
sparse = next_sparse;
}
return gth;
}
void reset() {
parallel_for(0, n, [&](NodeId i) {
dist[i] = DIST_MAX;
//heuristic_seq[i] = DIST_MAX;
in_frontier[i] = in_next_frontier[i] = false;
});
}
};
class Rho_Stepping : public ASTAR {
size_t rho;
uint32_t seed;
public:
Rho_Stepping(const Graph &_G, const sequence<double> &_coords,
bool _bidirection, size_t _rho = 1 << 20)
: ASTAR(_G, _coords, _bidirection), rho(_rho) {
seed = 0;
}
void init() override {}
EdgeTy get_threshold() override {
if (frontier_size <= rho) {
if (sparse) {
auto _dist = delayed_seq<EdgeTy>(frontier_size, [&](size_t i) {
return estimate_distance(frontier[i]);
});
return *max_element(_dist);
} else {
return DIST_MAX;
}
}
EdgeTy sample_dist[NUM_SAMPLES + 1];
for (size_t i = 0; i <= NUM_SAMPLES; i++) {
if (sparse) {
NodeId v = frontier[hash32(seed + i) % frontier_size];
sample_dist[i] = estimate_distance(v);
} else {
NodeId v = hash32(seed + i) % n;
if (in_frontier[v]) {
sample_dist[i] = estimate_distance(v);
} else {
sample_dist[i] = DIST_MAX;
}
}
}
seed += NUM_SAMPLES + 1;
size_t id = 1.0 * rho / frontier_size * NUM_SAMPLES;
sort(sample_dist, sample_dist + NUM_SAMPLES + 1);
return sample_dist[id];
}
};
class Delta_Stepping : public ASTAR {
EdgeTy delta;
EdgeTy thres;
public:
Delta_Stepping(const Graph &_G, const sequence<double> &_coords,
bool _bidirection, EdgeTy _delta = 1 << 15)
: ASTAR(_G, _coords, _bidirection), delta(_delta) {}
void init() override { thres = 0; }
EdgeTy get_threshold() override {
thres += delta;
return thres;
}
};
class Bellman_Ford : public ASTAR {
public:
Bellman_Ford(const Graph &_G, const sequence<double> &_coords,
bool _bidirection)
: ASTAR(_G, _coords, _bidirection) {}
void init() override {}
EdgeTy get_threshold() override { return DIST_MAX; }
};