forked from smistad/OpenCLUtilityLibrary
/
RuntimeMeasurementManager.cpp
151 lines (118 loc) · 3.84 KB
/
RuntimeMeasurementManager.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
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
#include "RuntimeMeasurementManager.hpp"
#include "Exceptions.hpp"
namespace oul {
void RuntimeMeasurementsManager::enable() {
enabled = true;
}
void RuntimeMeasurementsManager::disable() {
enabled = false;
}
void RuntimeMeasurementsManager::startCLTimer(std::string name, cl::CommandQueue queue) {
if (!enabled)
return;
this->verifyQueueProfilingIsEnabled(queue);
cl::Event startEvent = this->enqueueNewMarker(queue);
startEvents.insert(std::make_pair(name, startEvent));
}
void RuntimeMeasurementsManager::stopCLTimer(std::string name, cl::CommandQueue queue) {
if (!enabled)
return;
this->verifyQueueProfilingIsEnabled(queue);
this->verifyThatEventExists(name);
cl::Event startEvent = startEvents.at(name);
cl_ulong start = startEvent.getProfilingInfo<CL_PROFILING_COMMAND_START>();
startEvents.erase(name);
cl::Event endEvent = this->enqueueNewMarker(queue);
cl_ulong end = endEvent.getProfilingInfo<CL_PROFILING_COMMAND_START>();
double runtime_ms = (end - start) * 1.0e-6; //converting from nano- to milliseconds
this->addSampleToRuntimeMeasurement(name, runtime_ms);
}
void RuntimeMeasurementsManager::startRegularTimer(std::string name) {
if (!enabled)
return;
}
void RuntimeMeasurementsManager::stopRegularTimer(std::string name) {
if (!enabled)
return;
}
void RuntimeMeasurementsManager::startNumberedCLTimer(std::string name, cl::CommandQueue queue) {
if (!enabled)
return;
}
void RuntimeMeasurementsManager::stopNumberedCLTimer(std::string name, cl::CommandQueue queue) {
if (!enabled)
return;
}
void RuntimeMeasurementsManager::startNumberedRegularTimer(std::string name) {
if (!enabled)
return;
}
void RuntimeMeasurementsManager::stopNumberedRegularTimer(std::string name) {
if (!enabled)
return;
}
RuntimeMeasurement RuntimeMeasurementsManager::getTiming(std::string name) {
return *timings.at(name).get();
}
void RuntimeMeasurementsManager::print(std::string name) {
if (!enabled)
return;
timings.at(name)->print();
}
void RuntimeMeasurementsManager::printAll() {
if (!enabled)
return;
std::map<std::string, RuntimeMeasurementPtr>::iterator it;
for (it = timings.begin(); it != timings.end(); it++) {
it->second->print();
}
}
RuntimeMeasurementsManager::RuntimeMeasurementsManager() {
}
double RuntimeMeasurement::getSum() const {
return sum;
}
double RuntimeMeasurement::getAverage() const {
return sum / samples;
}
double RuntimeMeasurement::getStdDeviation() const {
// TODO: implement
return -1;
}
bool RuntimeMeasurementsManager::isEnabled() {
return enabled;
}
cl::Event RuntimeMeasurementsManager::enqueueNewMarker(cl::CommandQueue queue) {
cl::Event event;
#if !defined(CL_VERSION_1_2) || defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS)
// Use deprecated API
queue.enqueueMarker(&event);
#else
queue.enqueueMarkerWithWaitList(NULL, &event)
#endif
queue.finish();
return event;
}
void RuntimeMeasurementsManager::verifyThatEventExists(std::string name) {
if (startEvents.count(name) == 0) {
throw oul::Exception("Unknown CL timer");
}
}
void RuntimeMeasurementsManager::verifyQueueProfilingIsEnabled(cl::CommandQueue queue) {
if (queue.getInfo<CL_QUEUE_PROPERTIES>() != CL_QUEUE_PROFILING_ENABLE) {
throw oul::Exception(
"Failed to get profiling info. Make sure that RuntimeMeasurementManager::enable() is called before the OpenCL context is created.",
__LINE__, __FILE__);
}
}
void RuntimeMeasurementsManager::addSampleToRuntimeMeasurement(std::string name, double runtime) {
if (timings.count(name) == 0) {
// No timings with this name exists, create a new one
RuntimeMeasurementPtr runtimeMeasurement(new RuntimeMeasurement(name));
runtimeMeasurement->addSample(runtime);
timings[name] = runtimeMeasurement;
} else {
timings.at(name)->addSample(runtime);
}
}
} //namespace oul