From e5b563b3fdeeb0596835d3f57760237970be8ed7 Mon Sep 17 00:00:00 2001 From: Alexey Spizhevoy Date: Thu, 17 Feb 2011 15:25:50 +0000 Subject: [PATCH] refactored GPU performance sample, added filter suport --- samples/gpu/performance/performance.cpp | 67 +++++++++++++++---------- samples/gpu/performance/performance.h | 59 +++++++++++----------- 2 files changed, 69 insertions(+), 57 deletions(-) diff --git a/samples/gpu/performance/performance.cpp b/samples/gpu/performance/performance.cpp index f8a0fbe72b..c265b364e0 100644 --- a/samples/gpu/performance/performance.cpp +++ b/samples/gpu/performance/performance.cpp @@ -5,13 +5,27 @@ using namespace std; using namespace cv; + +void TestSystem::setWorkingDir(const string& val) +{ + working_dir_ = val; +} + + +void TestSystem::setTestFilter(const string& val) +{ + test_filter_ = val; +} + + void TestSystem::run() { - // Run initializers + // Run test initializers vector::iterator it = inits_.begin(); for (; it != inits_.end(); ++it) { - (*it)->run(); + if ((*it)->name().find(test_filter_, 0) != string::npos) + (*it)->run(); } printHeading(); @@ -20,21 +34,24 @@ void TestSystem::run() it = tests_.begin(); for (; it != tests_.end(); ++it) { - cout << endl << (*it)->name() << ":\n"; try { - (*it)->run(); - flushSubtestData(); + if ((*it)->name().find(test_filter_, 0) != string::npos) + { + cout << endl << (*it)->name() << ":\n"; + (*it)->run(); + finishCurrentSubtest(); + } } catch (const Exception&) { // Message is printed via callback - resetSubtestData(); + resetCurrentSubtest(); } catch (const runtime_error& e) { printError(e.what()); - resetSubtestData(); + resetCurrentSubtest(); } } @@ -42,27 +59,23 @@ void TestSystem::run() } -void TestSystem::setWorkingDir(const string& val) -{ - working_dir_ = val; -} - - -void TestSystem::flushSubtestData() +void TestSystem::finishCurrentSubtest() { - if (!can_flush_) + if (cur_subtest_is_empty_) + // There is no need to print subtest statistics return; int cpu_time = static_cast(cpu_elapsed_ / getTickFrequency() * 1000.0); int gpu_time = static_cast(gpu_elapsed_ / getTickFrequency() * 1000.0); - double speedup = static_cast(cpu_elapsed_) / std::max((int64)1, gpu_elapsed_); + double speedup = static_cast(cpu_elapsed_) / + std::max((int64)1, gpu_elapsed_); speedup_total_ += speedup; - printItem(cpu_time, gpu_time, speedup); + printMetrics(cpu_time, gpu_time, speedup); num_subtests_called_++; - resetSubtestData(); + resetCurrentSubtest(); } @@ -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); stringstream stream; @@ -102,14 +115,14 @@ void TestSystem::printItem(double cpu_time, double gpu_time, double speedup) stream << "x" << setprecision(3) << speedup; cout << setw(14) << stream.str(); - cout << description_.str(); + cout << cur_subtest_description_.str(); cout << resetiosflags(ios_base::left) << endl; } 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*/, int main(int argc, char** argv) { - if (argc < 2) - cout << "Usage: performance_gpu \n\n"; - else - TestSystem::instance().setWorkingDir(argv[1]); + if (argc < 3) + cout << "Usage: performance_gpu \n\n"; + if (argc >= 2) + TestSystem::instance().setTestFilter(argv[1]); + if (argc >= 3) + TestSystem::instance().setWorkingDir(argv[2]); redirectError(cvErrorCallback); TestSystem::instance().run(); return 0; -} \ No newline at end of file +} diff --git a/samples/gpu/performance/performance.h b/samples/gpu/performance/performance.h index 4d5283d51b..c698b92c52 100644 --- a/samples/gpu/performance/performance.h +++ b/samples/gpu/performance/performance.h @@ -13,9 +13,7 @@ class Runnable { public: explicit Runnable(const std::string& name): name_(name) {} - const std::string& name() const { return name_; } - virtual void run() = 0; private: @@ -32,70 +30,67 @@ public: 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(); - // Ends current subtest and starts new one - std::stringstream& subtest() + // It's public because OpenCV callback uses it + void printError(const std::string& msg); + + std::stringstream& startNewSubtest() { - flushSubtestData(); - return description_; + finishCurrentSubtest(); + return cur_subtest_description_; } void cpuOn() { cpu_started_ = cv::getTickCount(); } - void cpuOff() { int64 delta = cv::getTickCount() - cpu_started_; cpu_elapsed_ += delta; - can_flush_ = true; + cur_subtest_is_empty_ = false; } void gpuOn() { gpu_started_ = cv::getTickCount(); } - void gpuOff() { int64 delta = cv::getTickCount() - gpu_started_; 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: - TestSystem(): can_flush_(false), cpu_elapsed_(0), gpu_elapsed_(0), - speedup_total_(0.0), num_subtests_called_(0) {} + TestSystem(): cur_subtest_is_empty_(true), cpu_elapsed_(0), + gpu_elapsed_(0), speedup_total_(0.0), + num_subtests_called_(0) {} - void flushSubtestData(); - - void resetSubtestData() + void finishCurrentSubtest(); + void resetCurrentSubtest() { cpu_elapsed_ = 0; gpu_elapsed_ = 0; - description_.str(""); - can_flush_ = false; + cur_subtest_description_.str(""); + cur_subtest_is_empty_ = true; } void printHeading(); 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 test_filter_; std::vector inits_; std::vector tests_; - // Current test (subtest) description - std::stringstream description_; - - bool can_flush_; + std::stringstream cur_subtest_description_; + bool cur_subtest_is_empty_; int64 cpu_started_, cpu_elapsed_; int64 gpu_started_, gpu_elapsed_; @@ -124,15 +119,17 @@ private: } name##_test_instance; \ void name##_test::run() -#define SUBTEST TestSystem::instance().subtest() +#define SUBTEST TestSystem::instance().startNewSubtest() #define CPU_ON TestSystem::instance().cpuOn() #define GPU_ON TestSystem::instance().gpuOn() #define CPU_OFF TestSystem::instance().cpuOff() #define GPU_OFF TestSystem::instance().gpuOff() +// Generates matrix void gen(cv::Mat& mat, int rows, int cols, int type, cv::Scalar low, cv::Scalar high); +// Returns abs path taking into account test system working dir std::string abspath(const std::string& relpath); #endif // OPENCV_GPU_SAMPLE_PERFORMANCE_H_