Commit e5b563b3 authored by Alexey Spizhevoy's avatar Alexey Spizhevoy

refactored GPU performance sample, added filter suport

parent 12c2ead8
...@@ -5,13 +5,27 @@ ...@@ -5,13 +5,27 @@
using namespace std; using namespace std;
using namespace cv; using namespace cv;
void TestSystem::setWorkingDir(const string& val)
{
working_dir_ = val;
}
void TestSystem::setTestFilter(const string& val)
{
test_filter_ = val;
}
void TestSystem::run() void TestSystem::run()
{ {
// Run initializers // Run test initializers
vector<Runnable*>::iterator it = inits_.begin(); vector<Runnable*>::iterator it = inits_.begin();
for (; it != inits_.end(); ++it) for (; it != inits_.end(); ++it)
{ {
(*it)->run(); if ((*it)->name().find(test_filter_, 0) != string::npos)
(*it)->run();
} }
printHeading(); printHeading();
...@@ -20,21 +34,24 @@ void TestSystem::run() ...@@ -20,21 +34,24 @@ void TestSystem::run()
it = tests_.begin(); it = tests_.begin();
for (; it != tests_.end(); ++it) for (; it != tests_.end(); ++it)
{ {
cout << endl << (*it)->name() << ":\n";
try try
{ {
(*it)->run(); if ((*it)->name().find(test_filter_, 0) != string::npos)
flushSubtestData(); {
cout << endl << (*it)->name() << ":\n";
(*it)->run();
finishCurrentSubtest();
}
} }
catch (const Exception&) catch (const Exception&)
{ {
// Message is printed via callback // Message is printed via callback
resetSubtestData(); resetCurrentSubtest();
} }
catch (const runtime_error& e) catch (const runtime_error& e)
{ {
printError(e.what()); printError(e.what());
resetSubtestData(); resetCurrentSubtest();
} }
} }
...@@ -42,27 +59,23 @@ void TestSystem::run() ...@@ -42,27 +59,23 @@ void TestSystem::run()
} }
void TestSystem::setWorkingDir(const string& val) void TestSystem::finishCurrentSubtest()
{
working_dir_ = val;
}
void TestSystem::flushSubtestData()
{ {
if (!can_flush_) if (cur_subtest_is_empty_)
// There is no need to print subtest statistics
return; return;
int cpu_time = static_cast<int>(cpu_elapsed_ / getTickFrequency() * 1000.0); int cpu_time = static_cast<int>(cpu_elapsed_ / getTickFrequency() * 1000.0);
int gpu_time = static_cast<int>(gpu_elapsed_ / getTickFrequency() * 1000.0); int gpu_time = static_cast<int>(gpu_elapsed_ / getTickFrequency() * 1000.0);
double speedup = static_cast<double>(cpu_elapsed_) / std::max((int64)1, gpu_elapsed_); double speedup = static_cast<double>(cpu_elapsed_) /
std::max((int64)1, gpu_elapsed_);
speedup_total_ += speedup; speedup_total_ += speedup;
printItem(cpu_time, gpu_time, speedup); printMetrics(cpu_time, gpu_time, speedup);
num_subtests_called_++; num_subtests_called_++;
resetSubtestData(); resetCurrentSubtest();
} }
...@@ -86,7 +99,7 @@ void TestSystem::printSummary() ...@@ -86,7 +99,7 @@ void TestSystem::printSummary()
} }
void TestSystem::printItem(double cpu_time, double gpu_time, double speedup) void TestSystem::printMetrics(double cpu_time, double gpu_time, double speedup)
{ {
cout << TAB << setiosflags(ios_base::left); cout << TAB << setiosflags(ios_base::left);
stringstream stream; stringstream stream;
...@@ -102,14 +115,14 @@ void TestSystem::printItem(double cpu_time, double gpu_time, double speedup) ...@@ -102,14 +115,14 @@ void TestSystem::printItem(double cpu_time, double gpu_time, double speedup)
stream << "x" << setprecision(3) << speedup; stream << "x" << setprecision(3) << speedup;
cout << setw(14) << stream.str(); cout << setw(14) << stream.str();
cout << description_.str(); cout << cur_subtest_description_.str();
cout << resetiosflags(ios_base::left) << endl; cout << resetiosflags(ios_base::left) << endl;
} }
void TestSystem::printError(const std::string& msg) void TestSystem::printError(const std::string& msg)
{ {
cout << TAB << "[error: " << msg << "] " << description_.str() << endl; cout << TAB << "[error: " << msg << "] " << cur_subtest_description_.str() << endl;
} }
...@@ -138,13 +151,15 @@ int CV_CDECL cvErrorCallback(int /*status*/, const char* /*func_name*/, ...@@ -138,13 +151,15 @@ int CV_CDECL cvErrorCallback(int /*status*/, const char* /*func_name*/,
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (argc < 2) if (argc < 3)
cout << "Usage: performance_gpu <working_dir_with_slash>\n\n"; cout << "Usage: performance_gpu <test_filter> <working_dir_with_slash>\n\n";
else if (argc >= 2)
TestSystem::instance().setWorkingDir(argv[1]); TestSystem::instance().setTestFilter(argv[1]);
if (argc >= 3)
TestSystem::instance().setWorkingDir(argv[2]);
redirectError(cvErrorCallback); redirectError(cvErrorCallback);
TestSystem::instance().run(); TestSystem::instance().run();
return 0; return 0;
} }
\ No newline at end of file
...@@ -13,9 +13,7 @@ class Runnable ...@@ -13,9 +13,7 @@ class Runnable
{ {
public: public:
explicit Runnable(const std::string& name): name_(name) {} explicit Runnable(const std::string& name): name_(name) {}
const std::string& name() const { return name_; } const std::string& name() const { return name_; }
virtual void run() = 0; virtual void run() = 0;
private: private:
...@@ -32,70 +30,67 @@ public: ...@@ -32,70 +30,67 @@ public:
return me; return me;
} }
void addInit(Runnable* init) { inits_.push_back(init); } void setWorkingDir(const std::string& val);
const std::string& workingDir() const { return working_dir_; }
void addTest(Runnable* test) { tests_.push_back(test); } void setTestFilter(const std::string& val);
const std::string& testFilter() const { return test_filter_; }
void addInit(Runnable* init) { inits_.push_back(init); }
void addTest(Runnable* test) { tests_.push_back(test); }
void run(); void run();
// Ends current subtest and starts new one // It's public because OpenCV callback uses it
std::stringstream& subtest() void printError(const std::string& msg);
std::stringstream& startNewSubtest()
{ {
flushSubtestData(); finishCurrentSubtest();
return description_; return cur_subtest_description_;
} }
void cpuOn() { cpu_started_ = cv::getTickCount(); } void cpuOn() { cpu_started_ = cv::getTickCount(); }
void cpuOff() void cpuOff()
{ {
int64 delta = cv::getTickCount() - cpu_started_; int64 delta = cv::getTickCount() - cpu_started_;
cpu_elapsed_ += delta; cpu_elapsed_ += delta;
can_flush_ = true; cur_subtest_is_empty_ = false;
} }
void gpuOn() { gpu_started_ = cv::getTickCount(); } void gpuOn() { gpu_started_ = cv::getTickCount(); }
void gpuOff() void gpuOff()
{ {
int64 delta = cv::getTickCount() - gpu_started_; int64 delta = cv::getTickCount() - gpu_started_;
gpu_elapsed_ += delta; gpu_elapsed_ += delta;
can_flush_ = true; cur_subtest_is_empty_ = false;
} }
void setWorkingDir(const std::string& val);
const std::string& workingDir() const { return working_dir_; }
void printError(const std::string& msg);
private: private:
TestSystem(): can_flush_(false), cpu_elapsed_(0), gpu_elapsed_(0), TestSystem(): cur_subtest_is_empty_(true), cpu_elapsed_(0),
speedup_total_(0.0), num_subtests_called_(0) {} gpu_elapsed_(0), speedup_total_(0.0),
num_subtests_called_(0) {}
void flushSubtestData(); void finishCurrentSubtest();
void resetCurrentSubtest()
void resetSubtestData()
{ {
cpu_elapsed_ = 0; cpu_elapsed_ = 0;
gpu_elapsed_ = 0; gpu_elapsed_ = 0;
description_.str(""); cur_subtest_description_.str("");
can_flush_ = false; cur_subtest_is_empty_ = true;
} }
void printHeading(); void printHeading();
void printSummary(); void printSummary();
void printItem(double cpu_time, double gpu_time, double speedup); void printMetrics(double cpu_time, double gpu_time, double speedup);
std::string working_dir_; std::string working_dir_;
std::string test_filter_;
std::vector<Runnable*> inits_; std::vector<Runnable*> inits_;
std::vector<Runnable*> tests_; std::vector<Runnable*> tests_;
// Current test (subtest) description std::stringstream cur_subtest_description_;
std::stringstream description_; bool cur_subtest_is_empty_;
bool can_flush_;
int64 cpu_started_, cpu_elapsed_; int64 cpu_started_, cpu_elapsed_;
int64 gpu_started_, gpu_elapsed_; int64 gpu_started_, gpu_elapsed_;
...@@ -124,15 +119,17 @@ private: ...@@ -124,15 +119,17 @@ private:
} name##_test_instance; \ } name##_test_instance; \
void name##_test::run() void name##_test::run()
#define SUBTEST TestSystem::instance().subtest() #define SUBTEST TestSystem::instance().startNewSubtest()
#define CPU_ON TestSystem::instance().cpuOn() #define CPU_ON TestSystem::instance().cpuOn()
#define GPU_ON TestSystem::instance().gpuOn() #define GPU_ON TestSystem::instance().gpuOn()
#define CPU_OFF TestSystem::instance().cpuOff() #define CPU_OFF TestSystem::instance().cpuOff()
#define GPU_OFF TestSystem::instance().gpuOff() #define GPU_OFF TestSystem::instance().gpuOff()
// Generates matrix
void gen(cv::Mat& mat, int rows, int cols, int type, cv::Scalar low, void gen(cv::Mat& mat, int rows, int cols, int type, cv::Scalar low,
cv::Scalar high); cv::Scalar high);
// Returns abs path taking into account test system working dir
std::string abspath(const std::string& relpath); std::string abspath(const std::string& relpath);
#endif // OPENCV_GPU_SAMPLE_PERFORMANCE_H_ #endif // OPENCV_GPU_SAMPLE_PERFORMANCE_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment