-
Notifications
You must be signed in to change notification settings - Fork 1
/
Algorithm.cpp
107 lines (90 loc) · 2.63 KB
/
Algorithm.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
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
#include <stdexcept>
#include <sstream>
#include <utility>
#include "Algorithm.hpp"
#include "utils/Util.hpp"
Algorithm::Algorithm()
: selectedKernel(nullptr)
{}
Algorithm::Algorithm(const std::string& commit)
: commitHash(commit), selectedKernel(nullptr)
{}
Algorithm::Algorithm(Algorithm&& other)
: commitHash(std::move(other.commitHash))
, selectedKernel(std::move(other.selectedKernel))
, implementations(std::move(other.implementations))
{}
Algorithm&
Algorithm::operator=(Algorithm&& other)
{
commitHash = std::move(other.commitHash);
selectedKernel = std::move(other.selectedKernel);
implementations = std::move(other.implementations);
return *this;
}
void
Algorithm::selectKernel(const std::string& kernelName)
{
bool kernelMissing = false;
std::string errorMsg;
try {
selectedKernel = implementations.at(kernelName).get();
if (!selectedKernel) kernelMissing = true;
} catch (const std::out_of_range &) {
kernelMissing = true;
}
if (commitHash.empty()) reportError("Algorithm version not specified!");
if (kernelMissing) {
if (kernelName.empty()) {
errorMsg = "Kernel name not specified!";
} else {
errorMsg = "No kernel named \"" + kernelName + "\" exists!";
}
reportError(errorMsg, "\n\nSupported kernels:\n", kernelNames());
}
}
void
Algorithm::addImplementation
(std::string name, std::unique_ptr<ImplementationBase>&& impl)
{
auto result = implementations.emplace(name, std::move(impl));
if (!result.second) {
reportError("Implementation with name \"" + name + "\" already exists!");
}
}
void
Algorithm::operator()(const std::string& graphFile, std::ofstream&& output)
{
if (!selectedKernel) reportError("No kernel selected to run!");
selectedKernel->operator()(graphFile, std::move(output));
}
void
Algorithm::operator()(const std::string& graphFile, const std::string& output)
{
if (!selectedKernel) reportError("No kernel selected to run!");
selectedKernel->operator()(graphFile, output);
}
void
Algorithm::help(std::ostream& out, std::string prefix)
{
if (!selectedKernel) reportError("No kernel selected!");
selectedKernel->help(out, prefix);
}
std::vector<std::string>
Algorithm::setup(std::vector<std::string> args)
{
if (!selectedKernel) reportError("No kernel selected!");
return selectedKernel->setup(args);
}
std::string
Algorithm::commit()
{ return commitHash; }
std::string
Algorithm::kernelNames()
{
std::ostringstream names;
for (auto& kernel : implementations) {
names << " " << kernel.first << std::endl;
}
return names.str();
}