1
#ifndef OPENCV_CUDA_SAMPLE_PERFORMANCE_H_
2
#define OPENCV_CUDA_SAMPLE_PERFORMANCE_H_
9
#include <opencv2/core/utility.hpp>
16
explicit Runnable(const std::string& nameStr): name_(nameStr) {}
17
virtual ~Runnable() {}
19
const std::string& name() const { return name_; }
21
virtual void run() = 0;
31
static TestSystem& instance()
37
void setWorkingDir(const std::string& val) { working_dir_ = val; }
38
const std::string& workingDir() const { return working_dir_; }
40
void setTestFilter(const std::string& val) { test_filter_ = val; }
41
const std::string& testFilter() const { return test_filter_; }
43
void setNumIters(int num_iters) { num_iters_ = num_iters; }
45
void addInit(Runnable* init) { inits_.push_back(init); }
46
void addTest(Runnable* test) { tests_.push_back(test); }
49
// It's public because OpenCV callback uses it
50
void printError(const std::string& msg);
52
std::stringstream& startNewSubtest()
54
finishCurrentSubtest();
55
return cur_subtest_description_;
58
bool stop() const { return cur_iter_idx_ >= num_iters_; }
60
void cpuOn() { cpu_started_ = cv::getTickCount(); }
63
int64 delta = cv::getTickCount() - cpu_started_;
64
cpu_times_.push_back(delta);
69
cpu_elapsed_ += meanTime(cpu_times_);
70
cur_subtest_is_empty_ = false;
74
void gpuOn() { gpu_started_ = cv::getTickCount(); }
77
int64 delta = cv::getTickCount() - gpu_started_;
78
gpu_times_.push_back(delta);
83
gpu_elapsed_ += meanTime(gpu_times_);
84
cur_subtest_is_empty_ = false;
88
bool isListMode() const { return is_list_mode_; }
89
void setListMode(bool value) { is_list_mode_ = value; }
93
cur_subtest_is_empty_(true), cpu_elapsed_(0),
94
gpu_elapsed_(0), speedup_total_(0.0),
95
num_subtests_called_(0), is_list_mode_(false),
96
num_iters_(10), cur_iter_idx_(0)
98
cpu_times_.reserve(num_iters_);
99
gpu_times_.reserve(num_iters_);
102
void finishCurrentSubtest();
103
void resetCurrentSubtest()
107
cur_subtest_description_.str("");
108
cur_subtest_is_empty_ = true;
114
double meanTime(const std::vector<int64> &samples);
118
void printMetrics(double cpu_time, double gpu_time, double speedup);
120
std::string working_dir_;
121
std::string test_filter_;
123
std::vector<Runnable*> inits_;
124
std::vector<Runnable*> tests_;
126
std::stringstream cur_subtest_description_;
127
bool cur_subtest_is_empty_;
134
double speedup_total_;
135
int num_subtests_called_;
141
std::vector<int64> cpu_times_;
142
std::vector<int64> gpu_times_;
146
#define GLOBAL_INIT(name) \
147
struct name##_init: Runnable { \
148
name##_init(): Runnable(#name) { \
149
TestSystem::instance().addInit(this); \
152
} name##_init_instance; \
153
void name##_init::run()
157
struct name##_test: Runnable { \
158
name##_test(): Runnable(#name) { \
159
TestSystem::instance().addTest(this); \
162
} name##_test_instance; \
163
void name##_test::run()
165
#define SUBTEST TestSystem::instance().startNewSubtest()
168
while (!TestSystem::instance().stop()) { \
169
TestSystem::instance().cpuOn()
171
TestSystem::instance().cpuOff(); \
172
} TestSystem::instance().cpuComplete()
175
while (!TestSystem::instance().stop()) { \
176
TestSystem::instance().gpuOn()
178
TestSystem::instance().gpuOff(); \
179
} TestSystem::instance().gpuComplete()
181
// Generates a matrix
182
void gen(cv::Mat& mat, int rows, int cols, int type, cv::Scalar low,
185
// Returns abs path taking into account test system working dir
186
std::string abspath(const std::string& relpath);
188
#endif // OPENCV_CUDA_SAMPLE_PERFORMANCE_H_