From 6f91a29ea2d3ffe0970198c2bfe19b1284c5cc78 Mon Sep 17 00:00:00 2001 From: Alexey Spizhevoy Date: Tue, 25 Jan 2011 14:43:54 +0000 Subject: [PATCH] added initializers into GPU perf. tests --- samples/gpu/performance/performance.cpp | 38 +++++++++++++------------ samples/gpu/performance/performance.h | 37 +++++++++++++++++------- samples/gpu/performance/tests.cpp | 29 ++++++++++++++----- 3 files changed, 68 insertions(+), 36 deletions(-) diff --git a/samples/gpu/performance/performance.cpp b/samples/gpu/performance/performance.cpp index 1da6330e7..caa3fd169 100644 --- a/samples/gpu/performance/performance.cpp +++ b/samples/gpu/performance/performance.cpp @@ -4,41 +4,35 @@ using namespace std; using namespace cv; -void Test::gen(Mat& mat, int rows, int cols, int type, Scalar low, Scalar high) -{ - mat.create(rows, cols, type); - - RNG rng(0); - rng.fill(mat, RNG::UNIFORM, low, high); -} - - void TestSystem::run() { + // Run initializers + vector::iterator it = inits_.begin(); + for (; it != inits_.end(); ++it) + (*it)->run(); + cout << setiosflags(ios_base::left); cout << " " << setw(10) << "CPU, ms" << setw(10) << "GPU, ms" << setw(10) << "SPEEDUP" << "DESCRIPTION\n"; cout << resetiosflags(ios_base::left); - vector::iterator it = tests_.begin(); + // Run tests + it = tests_.begin(); for (; it != tests_.end(); ++it) { - Test* test = *it; - - cout << endl << test->name() << ":\n"; - test->run(); - - flush(); + cout << endl << (*it)->name() << ":\n"; + (*it)->run(); + flush_subtest_data(); } cout << setiosflags(ios_base::fixed | ios_base::left); - cout << "\nAverage GPU Speedup: x" << setprecision(3) + cout << "\naverage GPU speedup: x" << setprecision(3) << speedup_total_ / num_subtests_called_ << endl; cout << resetiosflags(ios_base::fixed | ios_base::left); } -void TestSystem::flush() +void TestSystem::flush_subtest_data() { if (!can_flush_) return; @@ -75,6 +69,14 @@ void TestSystem::flush() } +void gen(Mat& mat, int rows, int cols, int type, Scalar low, Scalar high) +{ + mat.create(rows, cols, type); + RNG rng(0); + rng.fill(mat, RNG::UNIFORM, low, high); +} + + int main() { TestSystem::instance()->run(); diff --git a/samples/gpu/performance/performance.h b/samples/gpu/performance/performance.h index cecec4584..a1feeb3ca 100644 --- a/samples/gpu/performance/performance.h +++ b/samples/gpu/performance/performance.h @@ -7,16 +7,13 @@ #include #include -class Test +class Runnable { public: - explicit Test(const std::string& name): name_(name) {} + explicit Runnable(const std::string& name): name_(name) {} const std::string& name() const { return name_; } - void gen(cv::Mat& mat, int rows, int cols, int type, - cv::Scalar low, cv::Scalar high); - virtual void run() = 0; private: @@ -33,7 +30,8 @@ public: return &me; } - void add(Test* test) { tests_.push_back(test); } + void addInit(Runnable* init) { inits_.push_back(init); } + void addTest(Runnable* test) { tests_.push_back(test); } void run(); @@ -58,7 +56,7 @@ public: // Ends current subtest and starts new one std::stringstream& subtest() { - flush(); + flush_subtest_data(); return description_; } @@ -66,9 +64,10 @@ private: TestSystem(): can_flush_(false), cpu_elapsed_(0), gpu_elapsed_(0), speedup_total_(0.0), num_subtests_called_(0) {}; - void flush(); + void flush_subtest_data(); - std::vector tests_; + std::vector inits_; + std::vector tests_; // Current test (subtest) description std::stringstream description_; @@ -84,14 +83,27 @@ private: #define TEST(name) \ - struct name##_test: Test \ + struct name##_test: Runnable \ { \ - name##_test(): Test(#name) { TestSystem::instance()->add(this); } \ + name##_test(): Runnable(#name) { \ + TestSystem::instance()->addTest(this); \ + } \ void run(); \ } name##_test_instance; \ void name##_test::run() +#define INIT(name) \ + struct name##_init: Runnable \ + { \ + name##_init(): Runnable(#name) { \ + TestSystem::instance()->addInit(this); \ + } \ + void run(); \ + } name##_init_instance; \ + void name##_init::run() + + #define CPU_ON TestSystem::instance()->cpuOn() #define GPU_ON TestSystem::instance()->gpuOn() #define CPU_OFF TestSystem::instance()->cpuOff() @@ -99,4 +111,7 @@ private: #define SUBTEST TestSystem::instance()->subtest() #define DESCRIPTION TestSystem::instance()->subtest() +void gen(cv::Mat& mat, int rows, int cols, int type, cv::Scalar low, + cv::Scalar high); + #endif // OPENCV_GPU_SAMPLE_PERFORMANCE_H_ \ No newline at end of file diff --git a/samples/gpu/performance/tests.cpp b/samples/gpu/performance/tests.cpp index 8aa3bdc32..eac8c9851 100644 --- a/samples/gpu/performance/tests.cpp +++ b/samples/gpu/performance/tests.cpp @@ -6,21 +6,36 @@ using namespace std; using namespace cv; +// This code calls CUFFT DFT and initializes that lib +INIT(CUFFT_library) +{ + Mat src, templ; + gen(src, 500, 500, CV_32F, 0, 1); + gen(templ, 500, 500, CV_32F, 0, 1); + + gpu::GpuMat d_src(src); + gpu::GpuMat d_templ(templ); + gpu::GpuMat d_result; + + gpu::matchTemplate(d_src, d_templ, d_result, CV_TM_CCORR); +} + + TEST(matchTemplate) { - Mat image, templ, result; - gen(image, 3000, 3000, CV_32F, 0, 1); + Mat src, templ, result; + gen(src, 3000, 3000, CV_32F, 0, 1); - gpu::GpuMat d_image(image), d_templ, d_result; + gpu::GpuMat d_image(src), d_templ, d_result; for (int templ_size = 5; templ_size <= 1000; templ_size *= 2) { - SUBTEST << "img " << image.rows << ", templ " << templ_size << ", 32F, CCORR"; + SUBTEST << "src " << src.rows << ", templ " << templ_size << ", 32F, CCORR"; gen(templ, templ_size, templ_size, CV_32F, 0, 1); CPU_ON; - matchTemplate(image, templ, result, CV_TM_CCORR); + matchTemplate(src, templ, result, CV_TM_CCORR); CPU_OFF; d_templ = templ; @@ -42,7 +57,7 @@ TEST(minMaxLoc) for (int size = 2000; size <= 8000; size *= 2) { - SUBTEST << "img " << size << ", 32F, no mask"; + SUBTEST << "src " << size << ", 32F, no mask"; gen(src, size, size, CV_32F, 0, 1); @@ -66,7 +81,7 @@ TEST(remap) for (int size = 1000; size <= 8000; size *= 2) { - SUBTEST << "img " << size << " and 8UC1, 32FC1 maps"; + SUBTEST << "src " << size << " and 8UC1, 32FC1 maps"; gen(src, size, size, CV_8UC1, 0, 256); gen(xmap, size, size, CV_32F, 0, size);