-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.cpp
77 lines (59 loc) · 1.96 KB
/
demo.cpp
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
// Usage:
// ./demo /path/to/graph.txt <start-vertex-idx> (cpu | gpu)
#include <iostream>
#include <fstream>
#include <assert.h>
#include <chrono>
#include "bfs.hpp"
class ScopedTimer {
std::chrono::time_point<std::chrono::high_resolution_clock> start_time;
std::string name;
public:
ScopedTimer(const std::string & name = "time"):
start_time(std::chrono::high_resolution_clock::now()),
name(name) {}
~ScopedTimer() {
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration<double, std::milli>(end_time - start_time).count();
std::cerr << name << ": " << duration << " ms" << std::endl;
}
};
Graph loadGraphFromFile(std::string filePath) {
std::ifstream inputFile(filePath);
int n; inputFile >> n;
Graph graph(n);
int m; inputFile >> m;
std::string isDirectedStr; inputFile >> isDirectedStr;
assert(isDirectedStr == "directed" or isDirectedStr == "undirected");
bool isDirected = isDirectedStr == "directed";
for (int i = 0; i < m; ++i) {
int a, b;
inputFile >> a >> b;
graph[a].push_back(b);
if (not isDirected) graph[b].push_back(a);
}
return graph;
}
int main(int argc, char *argv[]) {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
assert(argc == 4);
Graph graph = loadGraphFromFile(argv[1]);
const int sourceVertex = std::atoi(argv[2]);
std::string processor = argv[3];
assert(processor == "cpu" or processor == "gpu");
std::cerr << "graph size: " << graph.size() << std::endl;
std::vector<unsigned> distances;
{
ScopedTimer _("BFS on " + processor + " took");
if (processor == "cpu") {
bfsCPU(graph, sourceVertex, distances);
} else {
bfsCUDA(graph, sourceVertex, distances);
}
}
for (int i = 0; i < distances.size(); ++i) {
std::cout << distances[i] << std::endl;
}
return 0;
}