-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathgraph.hpp
375 lines (300 loc) · 12.7 KB
/
graph.hpp
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
#pragma once
#include <omp.h>
#include <fstream>
#include <functional>
#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
#include <algorithm>
// Generic representation of a graph implemented with an adjacency matrix
struct Graph {
using Node = int;
int task_threshold = 60;
int max_depth_rdfs = 10'000;
std::vector<std::vector<int>> adj_matrix;
// Returns if an edge between two nodes exists
bool edge_exists(Node n1, Node n2) { return adj_matrix[n1][n2] > 0; }
// Returns the number of nodes of the graph
int n_nodes() { return adj_matrix.size(); }
// Returns the number of nodes of the graph
int size() { return n_nodes(); }
// Sequential implementation of the iterative version of depth first search.
void dfs(Node src, std::vector<int>& visited) {
std::vector<Node> queue{src};
while (!queue.empty()) {
Node node = queue.back();
queue.pop_back();
if (!visited[node]) {
visited[node] = true;
for (int next_node = 0; next_node < n_nodes(); next_node++)
if (edge_exists(node, next_node) && !visited[next_node])
queue.push_back(next_node);
}
}
}
// Sequential implementation of the recursive version of depth first search.
void rdfs(Node src, std::vector<int>& visited, int depth = 0) {
visited[src] = true;
for (int node = 0; node < n_nodes(); node++) {
if (edge_exists(src, node) && !visited[node]) {
// Limit recursion depth to avoid stack overflow error
if (depth <= max_depth_rdfs)
rdfs(node, visited, depth + 1);
else
dfs(node, visited);
}
}
}
// Parallel implementation of the iterative version of depth first search.
//
// The general idea is that the main thread extracts the last node from the
// queue and check the neighbors of the node in parallel. Each of these threads
// have a private queue where neighbors still not visited are added. At the end,
// threads concatenate their private queue to the main queue.
void p_dfs(Node src, std::vector<int>& visited) {
std::vector<Node> queue{src};
while (!queue.empty()) {
Node node = queue.back();
queue.pop_back();
if (!visited[node]) {
visited[node] = true;
#pragma omp parallel shared(queue, visited)
{
// Every thread has a private_queue to avoid continuous lock
// checking to update the main one
std::vector<Node> private_queue;
#pragma omp for nowait schedule(static)
for (int next_node = 0; next_node < n_nodes(); next_node++)
if (edge_exists(node, next_node) && !visited[next_node])
private_queue.push_back(next_node);
// Append at the end of master queue the private queue of the thread
#pragma omp critical(queue_update)
queue.insert(queue.end(), private_queue.begin(), private_queue.end());
}
}
}
}
// Parallel implementation of the iterative version of depth first search.
//
// The general idea is that the main thread extracts the last node from the
// queue and check the neighbors of the node in parallel. Each of these
// threads have a private queue where neighbors still not visited are added.
// At the end, threads concatenate their private queue to the main queue.
//
// **Important**: this version implements node level locks.
void p_dfs_with_locks(Node src, std::vector<int>& visited,
std::vector<omp_lock_t>& node_locks) {
// Note: Since C++11, different elements in the same container can be
// modified concurrently by different threads, except for the elements
// of std::vector<bool>
//
// Possible explanation of why here:
// https://stackoverflow.com/a/33617530/2691946
//
// This is why we use a vector of int.
std::vector<Node> queue{src};
while (!queue.empty()) {
Node node = queue.back();
queue.pop_back();
bool already_visited = atomic_test_visited(node, visited, &node_locks[node]);
if (!already_visited) {
atomic_set_visited(node, visited, &node_locks[node]);
#pragma omp parallel shared(queue, visited)
{
// Every thread has a private queue to avoid continuos lock
// checking to update the main one
std::vector<Node> private_queue;
#pragma omp for nowait
for (int next_node = 0; next_node < n_nodes(); next_node++) {
// Check if the edge exists is a non-blocking request,
// so it's better to do it before than checking if the
// node is already visited
if (edge_exists(node, next_node)) {
if (atomic_test_visited(next_node, visited, &node_locks[next_node])) {
private_queue.push_back(next_node);
}
}
}
// Append at the end of master queue the private queue of the thread
#pragma omp critical(queue_update)
queue.insert(queue.end(), private_queue.begin(), private_queue.end());
}
}
}
}
// Parallel implementation of the recursive version of depth first search.
//
// This version automatically initialize locks
void p_rdfs(Node src, std::vector<int>& visited) {
// Initialize locks
std::vector<omp_lock_t> node_locks;
node_locks.reserve(size());
for (int node = 0; node < n_nodes(); node++) {
omp_lock_t lock;
node_locks[node] = lock;
omp_init_lock(&(node_locks[node]));
}
#pragma omp parallel shared(src, visited, node_locks)
#pragma omp single
p_rdfs(src, visited, node_locks);
// Destory locks
for (int node = 0; node < n_nodes(); node++) omp_destroy_lock(&(node_locks[node]));
}
// Parallel implementation of the recursive version of depth first search,
// full version with locks
void p_rdfs(Node src, std::vector<int>& visited, std::vector<omp_lock_t>& node_locks,
int depth = 0) {
atomic_set_visited(src, visited, &node_locks[src]);
// Number of tasks in parallel executing at this level of depth
int task_count = 0;
for (int node = 0; node < n_nodes(); node++) {
if (edge_exists(src, node) && !atomic_test_visited(node, visited, &node_locks[node])) {
// Limit the number of parallel tasks both horizontally (for
// checking neighbors) and vertically (between recursive
// calls).
//
// Fallback to iterative version if one of these limits are
// reached
if (depth <= max_depth_rdfs && task_count <= task_threshold) {
task_count++;
#pragma omp task untied default(shared) firstprivate(node)
{
p_rdfs(node, visited, node_locks, depth + 1);
task_count--;
}
} else {
// Fallback to parallel iterative version
p_dfs_with_locks(node, visited, node_locks);
}
}
}
#pragma omp taskwait
}
// Serial implementation of the Dijkstra algorithm without early exit condition.
//
// Note: It does not use a priority queue.
std::pair<std::vector<Node>, std::vector<Node>> dijkstra(Node src) {
std::vector<Node> queue;
queue.push_back(src);
std::vector<Node> came_from(size(), -1);
std::vector<Node> cost_so_far(size(), -1);
came_from[src] = src;
cost_so_far[src] = 0;
while (!queue.empty()) {
Node current = queue.back();
queue.pop_back();
for (int next = 0; next < n_nodes(); next++) {
if (edge_exists(current, next)) {
int new_cost = cost_so_far[current] + adj_matrix[current][next];
if (cost_so_far[next] == -1 || new_cost < cost_so_far[next]) {
cost_so_far[next] = new_cost;
queue.push_back(next);
came_from[next] = current;
}
}
}
}
return std::make_pair(came_from, cost_so_far);
}
inline std::vector<omp_lock_t> initialize_locks() {
std::vector<omp_lock_t> node_locks;
node_locks.reserve(n_nodes());
for (int node = 0; node < n_nodes(); node++) {
omp_lock_t lock;
node_locks[node] = lock;
omp_init_lock(&(node_locks[node]));
}
return node_locks;
}
// Parallel implementation of the Dijkstra algorithm without early exit
// condition using node level locks. As expected, it performs very poorly
//
// Note: It does not use a priority queue.
std::pair<std::vector<Node>, std::vector<Node>> p_dijkstra(Node src) {
std::vector<Node> queue;
queue.push_back(src);
std::vector<Node> came_from(size(), -1);
std::vector<Node> cost_so_far(size(), -1);
came_from[src] = src;
cost_so_far[src] = 0;
auto node_locks = initialize_locks();
while (!queue.empty()) {
Node current = queue.back();
queue.pop_back();
#pragma omp parallel shared(queue, node_locks)
#pragma omp for
for (int next = 0; next < n_nodes(); next++) {
if (edge_exists(current, next)) {
omp_set_lock(&node_locks[current]);
auto cost_so_far_current = cost_so_far[current];
omp_unset_lock(&node_locks[current]);
int new_cost = cost_so_far_current + adj_matrix[current][next];
omp_set_lock(&node_locks[next]);
auto cost_so_far_next = cost_so_far[next];
omp_unset_lock(&node_locks[next]);
if (cost_so_far_next == -1 || new_cost < cost_so_far_next) {
omp_set_lock(&node_locks[next]);
cost_so_far[next] = new_cost;
came_from[next] = current;
omp_unset_lock(&node_locks[next]);
#pragma omp critical(queue_update)
queue.push_back(next);
}
}
}
}
// Destory locks
for (int node = 0; node < n_nodes(); node++) omp_destroy_lock(&(node_locks[node]));
return std::make_pair(came_from, cost_so_far);
}
// Reconstruct path from the destination to the source
std::vector<Node> reconstruct_path(Node src, Node dst, std::vector<Node> origins) {
auto current_node = dst;
std::vector<Node> path;
while (current_node != src) {
path.push_back(current_node);
current_node = origins.at(current_node);
}
path.push_back(src);
reverse(path.begin(), path.end());
return path;
}
private:
// Return true if a node is already visited using a node level lock
inline bool atomic_test_visited(Node node, const std::vector<int>& visited, omp_lock_t* lock) {
omp_set_lock(lock);
bool already_visited = visited.at(node);
omp_unset_lock(lock);
return already_visited;
}
// Set that a node is already visited using a node level lock
inline void atomic_set_visited(Node node, std::vector<int>& visited, omp_lock_t* lock) {
omp_set_lock(lock);
visited[node] = true;
omp_unset_lock(lock);
}
};
// Import graph from a file
Graph import_graph(std::string& path) {
Graph graph;
std::ifstream file(path);
if (!file.is_open()) {
throw std::invalid_argument("Input file does not exist or is not readable.");
}
std::string line;
// Read one line at a time into the variable line
while (getline(file, line)) {
std::vector<int> lineData;
std::stringstream lineStream(line);
// Read an integer at a time from the line
int value;
while (lineStream >> value) lineData.push_back(value);
lineData.shrink_to_fit(); // Usefull?
graph.adj_matrix.push_back(lineData);
}
graph.adj_matrix.shrink_to_fit();
return graph;
}