-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
112 lines (81 loc) · 3.22 KB
/
main.cpp
File metadata and controls
112 lines (81 loc) · 3.22 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
#include "graph.hpp"
#include <array>
#include <functional>
#include <string>
#include <vector>
#include <chrono>
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
using std::chrono::milliseconds;
std::string bench_traverse(std::function<void()> traverse_fn)
{
auto start = high_resolution_clock::now();
traverse_fn();
auto stop = high_resolution_clock::now();
// Subtract stop and start timepoints and cast it to required unit.
// Predefined units are nanoseconds, microseconds, milliseconds, seconds,
// minutes, hours. Use duration_cast() function.
auto duration = duration_cast<milliseconds>(stop - start);
// To get the value of duration use the count() member function on the
// duration object
return std::to_string(duration.count());
}
void full_bench(Graph& graph)
{
int num_test = 6;
std::array<int, 6> num_threads { {1, 2, 4, 8, 16, 32 } };
std::vector<Graph::Node> visited(graph.size(), false);
Graph::Node src = 0;
// Explicitly disable dynamic teams as we are going to set a fixed number of
// threads
omp_set_dynamic(0);
// TODO: find a better way to avoid code repetition
std::cout << "Number of nodes: " << graph.size() << "\n\n";
for (int i = 0; i < num_test; i++) {
std::cout << "\t"
<< "Execution " << i + 1 << std::endl;
std::cout << "Sequential iterative DFS: "
<< bench_traverse([&] { graph.dfs(src, visited); })
<< "ms\n";
// We cannot pass a copy of the vector, so we "reset" it every time
std::fill(visited.begin(), visited.end(), false);
std::cout << "Sequential recursive DFS: "
<< bench_traverse([&]() { graph.rdfs(src, visited); })
<< "ms\n";
// std::cout << "Dijkstra: "
// << bench_traverse([&] { graph.dijkstra(0); })
// << "ms\n";
for (const auto n : num_threads) {
std::fill(visited.begin(), visited.end(), false);
std::cout << "Using " << n << " threads..." << std::endl;
// Set to use N threads
omp_set_num_threads(n);
// Should we change also this?
// graph.task_threshold = n;
std::cout << "Parallel iterative DFS: "
<< bench_traverse([&] { graph.p_dfs(src, visited); })
<< "ms\n";
std::fill(visited.begin(), visited.end(), false);
std::cout << "Parallel recursive DFS: "
<< bench_traverse([&] { graph.p_rdfs(src, visited); })
<< "ms\n";
// std::cout << "Parallel Dijkstra: "
// << bench_traverse([&] { graph.p_dijkstra(0); })
// << "ms\n";
}
std::fill(visited.begin(), visited.end(), false);
std::cout << std::endl;
}
}
int main(int argc, const char** argv)
{
// TODO: Add a CLI? Also, we should accept more input files and process them separately
if (argc < 2) {
std::cout << "Input file not specified.\n";
return 1;
}
std::string file_path = argv[1];
auto graph = import_graph(file_path);
full_bench(graph);
return 0;
}