mirror of
https://github.com/opencv/opencv.git
synced 2024-11-24 19:20:28 +08:00
refactored GPU performance sample, added filter suport
This commit is contained in:
parent
12c2ead83f
commit
e5b563b3fd
@ -5,12 +5,26 @@
|
||||
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<Runnable*>::iterator it = inits_.begin();
|
||||
for (; it != inits_.end(); ++it)
|
||||
{
|
||||
if ((*it)->name().find(test_filter_, 0) != string::npos)
|
||||
(*it)->run();
|
||||
}
|
||||
|
||||
@ -20,21 +34,24 @@ void TestSystem::run()
|
||||
it = tests_.begin();
|
||||
for (; it != tests_.end(); ++it)
|
||||
{
|
||||
cout << endl << (*it)->name() << ":\n";
|
||||
try
|
||||
{
|
||||
if ((*it)->name().find(test_filter_, 0) != string::npos)
|
||||
{
|
||||
cout << endl << (*it)->name() << ":\n";
|
||||
(*it)->run();
|
||||
flushSubtestData();
|
||||
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)
|
||||
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;
|
||||
|
||||
int cpu_time = static_cast<int>(cpu_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;
|
||||
|
||||
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,10 +151,12 @@ int CV_CDECL cvErrorCallback(int /*status*/, const char* /*func_name*/,
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc < 2)
|
||||
cout << "Usage: performance_gpu <working_dir_with_slash>\n\n";
|
||||
else
|
||||
TestSystem::instance().setWorkingDir(argv[1]);
|
||||
if (argc < 3)
|
||||
cout << "Usage: performance_gpu <test_filter> <working_dir_with_slash>\n\n";
|
||||
if (argc >= 2)
|
||||
TestSystem::instance().setTestFilter(argv[1]);
|
||||
if (argc >= 3)
|
||||
TestSystem::instance().setWorkingDir(argv[2]);
|
||||
|
||||
redirectError(cvErrorCallback);
|
||||
TestSystem::instance().run();
|
||||
|
@ -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 setWorkingDir(const std::string& val);
|
||||
const std::string& workingDir() const { return working_dir_; }
|
||||
|
||||
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<Runnable*> inits_;
|
||||
std::vector<Runnable*> 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_
|
||||
|
Loading…
Reference in New Issue
Block a user