-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelta_step_static.cpp
More file actions
395 lines (358 loc) · 12.1 KB
/
Copy pathdelta_step_static.cpp
File metadata and controls
395 lines (358 loc) · 12.1 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
#include "graph.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <utility>
#include <unordered_set>
#include <queue>
#include <random>
#include <algorithm>
#include <thread>
#include <chrono>
#include <mutex>
#include <atomic>
#include <barrier>
#include <climits>
class DeltaSteppingSequential
{
public:
double delta;
Graph graph;
std::vector<double> tent;
std::vector<std::unordered_set<int>> buckets;
DeltaSteppingSequential(Graph graph, double delta)
{
this->graph = graph;
this->delta = delta;
int n_buckets = graph.maxDist / delta + 1;
this->buckets = std::vector<std::unordered_set<int>>(n_buckets);
this->tent.resize(graph.n);
std::fill(tent.begin(), tent.end(), 2e9);
}
void findShortest(int source)
{
relax(source, 0);
int i;
int maxiter = 10;
while (!BucketsEmpty(i))
{
std::unordered_set<int> R;
std::vector<std::pair<int, double>> Req;
while (!buckets[i].empty())
{
Req = findRequests(buckets[i], true);
for (auto vertex : buckets[i])
R.insert(vertex);
buckets[i].clear();
relaxRequests(Req);
}
Req = findRequests(R, false);
relaxRequests(Req);
}
}
// this works
bool BucketsEmpty(int &non_empty_index)
{
for (int i = 0; i != buckets.size(); i++)
{
if (!buckets[i].empty())
{
non_empty_index = i;
return false;
}
}
return true;
}
// kind values:
// 0 - heavy edges
// 1 - light edges
std::vector<std::pair<int, double>> findRequests(std::unordered_set<int> vertices, bool kind)
{
std::vector<std::pair<int, double>> reqs;
for (auto vertex : vertices)
{
for (auto connected_vertex : graph.adj_lists[vertex])
{
if (kind && connected_vertex.second <= delta)
reqs.push_back(std::make_pair(connected_vertex.first, tent[vertex] + connected_vertex.second));
else if (!kind && connected_vertex.second > delta)
reqs.push_back(std::make_pair(connected_vertex.first, tent[vertex] + connected_vertex.second));
}
}
return reqs;
}
void relaxRequests(std::vector<std::pair<int, double>> reqs)
{
for (auto req : reqs)
{
relax(req.first, req.second);
}
}
void relax(int vertex, double distance)
{
if (distance < tent[vertex])
{
// remove only if we are sure it is in a bucket
if (tent[vertex] != 2e9)
buckets[tent[vertex] / delta].erase(vertex);
buckets[distance / delta].insert(vertex);
tent[vertex] = distance;
}
}
};
class DeltaSteppingParallelStatic
{
public:
double delta;
int num_threads;
Graph graph;
std::vector<double> tent;
std::vector<std::vector<std::unordered_set<int>>> buckets; // bucket - thread - set of elements
std::vector<int> owner;
std::vector<std::vector<std::pair<int, double>>> NeighborsLight, NeighborsHeavy; // element - list of neighbors
std::vector<std::vector<std::vector<std::pair<int, double>>>> ReqLight, ReqHeavy; // thread - list of requests for this thread
DeltaSteppingParallelStatic(const Graph &graph, double delta, int num_threads) : graph(graph),
delta(delta),
num_threads(num_threads),
tent(graph.n, 2e9),
buckets(
graph.maxDist / delta + 1,
std::vector<std::unordered_set<int>>(num_threads)),
owner(graph.n, 0),
NeighborsLight(graph.n),
NeighborsHeavy(graph.n),
ReqLight(num_threads, std::vector<std::vector<std::pair<int, double>>>(num_threads)),
ReqHeavy(num_threads, std::vector<std::vector<std::pair<int, double>>>(num_threads))
{
}
inline std::size_t bucketIndex(double d) const
{
/* magic constant to keep from numerical issue */
double q = d / delta;
double eps = std::ldexp(1.0, -52);
return static_cast<std::size_t>(std::floor(q + 0.5 * eps * std::abs(q)));
}
void fillNeighbors()
{
for (int i = 0; i < graph.n; i++)
{
for (auto edge : graph.adj_lists[i])
{
//magic constant to keep from numerical issues
const double EPS = 1e-12 * delta;
if (edge.second < delta + EPS)
NeighborsLight[i].push_back(edge);
else
NeighborsHeavy[i].push_back(edge);
}
}
}
// assign owners to vertices
void assignThreads()
{
int iter = 0;
for (int t = 0; t < num_threads; t++)
{
int count = graph.n / num_threads + (t < (graph.n % num_threads) ? 1 : 0);
for (int i = 0; i < count; ++i)
{
owner[iter++] = t;
}
}
std::random_device rd;
std::mt19937 gen(rd());
std::shuffle(owner.begin(), owner.end(), gen);
}
// thread, index of bucket
void loop1(int t, int i)
{
// generate Req_L(B_i_t) and Req_H(B_i_t)
for (auto from : buckets[i][t])
{
// std::cout << "generating requests from " << from << std::endl;
generateRequests(from, true);
generateRequests(from, false);
}
buckets[i][t].clear();
}
void loop2(int t)
{
Relax(true, t);
for (int src = 0; src < num_threads; ++src)
{
ReqLight[src][t].clear();
}
}
void loop3(int t)
{
Relax(false, t);
for (int src = 0; src < num_threads; ++src)
{
ReqHeavy[src][t].clear();
}
}
// kind values:
// 0 - heavy requests
// 1 - light requests
void generateRequests(int from, bool kind)
{
if (kind)
{
for (auto vertex : NeighborsLight[from])
{
ReqLight[owner[from]][owner[vertex.first]].push_back(std::make_pair(vertex.first, tent[from] + vertex.second));
}
}
else
{
for (auto vertex : NeighborsHeavy[from])
{
ReqHeavy[owner[from]][owner[vertex.first]].push_back(std::make_pair(vertex.first, tent[from] + vertex.second));
}
}
}
// slightly different - relaxes the whole sublist for thread t instead of splitting it.
void Relax(bool kind, int to_thread)
{
if (kind)
{
for (int from_thread = 0; from_thread < num_threads; from_thread++)
{
for (auto request : ReqLight[from_thread][to_thread])
{
int vertex = request.first;
double propDist = request.second;
if (propDist < tent[vertex])
{
double olddist = tent[vertex];
size_t oldbucket_ind = bucketIndex(tent[vertex]);
tent[vertex] = propDist;
size_t newbucket_ind = bucketIndex(propDist);
if (olddist < 2e9)
buckets[oldbucket_ind][to_thread].erase(vertex);
buckets[newbucket_ind][to_thread].insert(vertex);
}
}
}
}
else
{
for (int from_thread = 0; from_thread < num_threads; from_thread++)
{
for (auto request : ReqHeavy[from_thread][to_thread])
{
int vertex = request.first;
double propDist = request.second;
if (propDist < tent[vertex])
{
double olddist = tent[vertex];
size_t oldbucket_ind = bucketIndex(tent[vertex]);
tent[vertex] = propDist;
size_t newbucket_ind = bucketIndex(propDist);
if (olddist < 2e9)
buckets[oldbucket_ind][to_thread].erase(vertex);
buckets[newbucket_ind][to_thread].insert(vertex);
}
}
}
}
}
bool BucketsEmpty(int &non_empty_index, int prev_index)
{
for (int i = prev_index + 1; i != buckets.size(); i++)
{
for (int j = 0; j != buckets[i].size(); j++)
{
if (!buckets[i][j].empty())
{
non_empty_index = i;
return false;
}
}
}
return true;
}
bool BucketEmpty(int index)
{
for (int j = 0; j != buckets[index].size(); j++)
{
if (!buckets[index][j].empty())
{
return false;
}
}
return true;
}
private:
enum class Phase
{
RUN_LOOP1,
RUN_LOOP2,
RUN_LOOP3,
TERMINATE
};
std::atomic<Phase> phase = Phase::RUN_LOOP1;
std::atomic<int> current_bucket = 0;
//either do some weird cast or intialise with brackets like this
std::barrier<> sync{num_threads + 1};
// this handles how the thread works - between phases, it doesnt disappear, but it kind of sleeps
void worker(int thread_id)
{
while(true)
{
sync.arrive_and_wait();
Phase p = phase.load(std::memory_order_relaxed);
if (p == Phase::TERMINATE)
{
sync.arrive_and_wait();
return;
}
int bucket_id = current_bucket.load(std::memory_order_relaxed);
if (p == Phase::RUN_LOOP1)
loop1(thread_id, bucket_id);
else if (p == Phase::RUN_LOOP2)
loop2(thread_id);
else
loop3(thread_id);
sync.arrive_and_wait();
}
}
//tells the threads which phase to perform
void do_phase(Phase p, int bucket_id = 0)
{
phase.store(p);
current_bucket.store(bucket_id);
/* starting the phase */
sync.arrive_and_wait();
/* wait for all threads to finish the phase */
sync.arrive_and_wait();
}
public:
void findShortest(int source)
{
fillNeighbors();
assignThreads();
buckets[0][owner[source]].insert(source);
tent[source] = 0;
std::vector<std::thread> workers(num_threads);
for (int t = 0; t < num_threads; ++t)
workers[t] = std::thread(&DeltaSteppingParallelStatic::worker, this, t);
int prev_index = -1;
int index;
while (!BucketsEmpty(index, prev_index))
{
prev_index = index;
while (!BucketEmpty(index))
{
do_phase(Phase::RUN_LOOP1, index); // 1st loop
do_phase(Phase::RUN_LOOP2); // 2nd loop
}
do_phase(Phase::RUN_LOOP3); //3rd loop
}
do_phase(Phase::TERMINATE);
for (auto &w : workers)
w.join();
}
};