1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other oclMaterials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors as is and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #ifndef __OPENCV_PERF_PRECOMP_HPP__
44 #define __OPENCV_PERF_PRECOMP_HPP__
53 #include "opencv2/core/core.hpp"
54 #include "opencv2/imgproc/imgproc.hpp"
55 #include "opencv2/highgui/highgui.hpp"
56 #include "opencv2/calib3d/calib3d.hpp"
57 #include "opencv2/video/video.hpp"
58 #include "opencv2/objdetect/objdetect.hpp"
59 #include "opencv2/features2d/features2d.hpp"
60 #include "opencv2/ocl/ocl.hpp"
61 #include "opencv2/ts/ts.hpp"
62 #include "opencv2/ts/ts_perf.hpp"
63 #include "opencv2/ts/ts_gtest.h"
74 void gen(Mat &mat, int rows, int cols, int type, Scalar low, Scalar high);
75 void gen(Mat &mat, int rows, int cols, int type, int low, int high, int n);
77 string abspath(const string &relpath);
78 int CV_CDECL cvErrorCallback(int, const char *, const char *, const char *, int, void *);
84 COOR do_meanShift(int x0, int y0, uchar *sptr, uchar *dptr, int sstep,
85 cv::Size size, int sp, int sr, int maxIter, float eps, int *tab);
86 void meanShiftProc_(const Mat &src_roi, Mat &dst_roi, Mat &dstCoor_roi,
87 int sp, int sr, cv::TermCriteria crit);
90 template<class T1, class T2>
91 int ExpectedEQ(T1 expected, T2 actual)
93 if(expected == actual)
100 int EeceptDoubleEQ(T1 expected, T1 actual)
102 testing::internal::Double lhs(expected);
103 testing::internal::Double rhs(actual);
105 if (lhs.AlmostEquals(rhs))
114 int AssertEQ(T expected, T actual)
116 if(expected == actual)
123 int ExceptDoubleNear(double val1, double val2, double abs_error);
124 bool match_rect(cv::Rect r1, cv::Rect r2, int threshold);
126 double checkNorm(const cv::Mat &m);
127 double checkNorm(const cv::Mat &m1, const cv::Mat &m2);
128 double checkSimilarity(const cv::Mat &m1, const cv::Mat &m2);
130 int ExpectedMatNear(cv::Mat dst, cv::Mat cpu_dst, double eps);
131 int ExceptedMatSimilar(cv::Mat dst, cv::Mat cpu_dst, double eps);
136 explicit Runnable(const std::string &runname): name_(runname) {}
137 virtual ~Runnable() {}
139 const std::string &name() const
144 virtual void run() = 0;
153 static TestSystem &instance()
155 static TestSystem me;
159 void setWorkingDir(const std::string &val)
163 const std::string &workingDir() const
168 void setTestFilter(const std::string &val)
172 const std::string &testFilter() const
177 void setNumIters(int num_iters)
179 num_iters_ = num_iters;
181 void setGPUWarmupIters(int num_iters)
183 gpu_warmup_iters_ = num_iters;
185 void setCPUIters(int num_iters)
187 cpu_num_iters_ = num_iters;
190 void setTopThreshold(double top)
194 void setBottomThreshold(double bottom)
199 void addInit(Runnable *init)
201 inits_.push_back(init);
203 void addTest(Runnable *test)
205 tests_.push_back(test);
209 // It's public because OpenCV callback uses it
210 void printError(const std::string &msg);
212 std::stringstream &startNewSubtest()
214 finishCurrentSubtest();
215 return cur_subtest_description_;
220 return cur_iter_idx_ >= num_iters_;
223 bool cpu_stop() const
225 return cur_iter_idx_ >= cpu_num_iters_;
228 int get_cur_iter_idx()
230 return cur_iter_idx_;
233 int get_cpu_num_iters()
235 return cpu_num_iters_;
240 return cur_warmup_idx_++ >= gpu_warmup_iters_;
243 void warmupComplete()
250 cpu_started_ = cv::getTickCount();
254 int64 delta = cv::getTickCount() - cpu_started_;
255 cpu_times_.push_back(delta);
260 cpu_elapsed_ += meanTime(cpu_times_);
261 cur_subtest_is_empty_ = false;
267 gpu_started_ = cv::getTickCount();
271 int64 delta = cv::getTickCount() - gpu_started_;
272 gpu_times_.push_back(delta);
277 gpu_elapsed_ += meanTime(gpu_times_);
278 cur_subtest_is_empty_ = false;
284 gpu_full_started_ = cv::getTickCount();
288 int64 delta = cv::getTickCount() - gpu_full_started_;
289 gpu_full_times_.push_back(delta);
292 void gpufullComplete()
294 gpu_full_elapsed_ += meanTime(gpu_full_times_);
295 cur_subtest_is_empty_ = false;
299 bool isListMode() const
301 return is_list_mode_;
303 void setListMode(bool value)
305 is_list_mode_ = value;
308 void setRecordName(const std::string &name)
313 void setCurrentTest(const std::string &name)
316 itname_changed_ = true;
319 void setAccurate(int accurate, double diff)
321 is_accurate_ = accurate;
322 accurate_diff_ = diff;
325 void ExpectMatsNear(vector<Mat>& dst, vector<Mat>& cpu_dst, vector<double>& eps)
327 assert(dst.size() == cpu_dst.size());
328 assert(cpu_dst.size() == eps.size());
330 for(size_t i=0; i<dst.size(); i++)
332 double cur_diff = checkNorm(dst[i], cpu_dst[i]);
333 accurate_diff_ = max(accurate_diff_, cur_diff);
334 if(cur_diff > eps[i])
339 void ExpectedMatNear(cv::Mat& dst, cv::Mat& cpu_dst, double eps)
341 assert(dst.type() == cpu_dst.type());
342 assert(dst.size() == cpu_dst.size());
343 accurate_diff_ = checkNorm(dst, cpu_dst);
344 if(accurate_diff_ <= eps)
350 void ExceptedMatSimilar(cv::Mat& dst, cv::Mat& cpu_dst, double eps)
352 assert(dst.type() == cpu_dst.type());
353 assert(dst.size() == cpu_dst.size());
354 accurate_diff_ = checkSimilarity(cpu_dst, dst);
355 if(accurate_diff_ <= eps)
361 std::stringstream &getCurSubtestDescription()
363 return cur_subtest_description_;
368 cur_subtest_is_empty_(true), cpu_elapsed_(0),
369 gpu_elapsed_(0), gpu_full_elapsed_(0), speedup_total_(0.0),
370 num_subtests_called_(0),
371 speedup_faster_count_(0), speedup_slower_count_(0), speedup_equal_count_(0),
372 speedup_full_faster_count_(0), speedup_full_slower_count_(0), speedup_full_equal_count_(0), is_list_mode_(false),
373 num_iters_(10), cpu_num_iters_(2),
374 gpu_warmup_iters_(1), cur_iter_idx_(0), cur_warmup_idx_(0),
375 record_(0), recordname_("performance"), itname_changed_(true),
376 is_accurate_(-1), accurate_diff_(0.)
378 cpu_times_.reserve(num_iters_);
379 gpu_times_.reserve(num_iters_);
380 gpu_full_times_.reserve(num_iters_);
383 void finishCurrentSubtest();
384 void resetCurrentSubtest()
388 gpu_full_elapsed_ = 0;
389 cur_subtest_description_.str("");
390 cur_subtest_is_empty_ = true;
395 gpu_full_times_.clear();
400 double meanTime(const std::vector<int64> &samples);
404 void printMetrics(int is_accurate, double cpu_time, double gpu_time = 0.0f, double gpu_full_time = 0.0f, double speedup = 0.0f, double fullspeedup = 0.0f);
408 void writeMetrics(double cpu_time, double gpu_time = 0.0f, double gpu_full_time = 0.0f,
409 double speedup = 0.0f, double fullspeedup = 0.0f,
410 double gpu_min = 0.0f, double gpu_max = 0.0f, double std_dev = 0.0f);
412 std::string working_dir_;
413 std::string test_filter_;
415 std::vector<Runnable *> inits_;
416 std::vector<Runnable *> tests_;
418 std::stringstream cur_subtest_description_;
419 bool cur_subtest_is_empty_;
423 int64 gpu_full_started_;
426 double gpu_full_elapsed_;
428 double speedup_total_;
429 double speedup_full_total_;
430 int num_subtests_called_;
432 int speedup_faster_count_;
433 int speedup_slower_count_;
434 int speedup_equal_count_;
436 int speedup_full_faster_count_;
437 int speedup_full_slower_count_;
438 int speedup_full_equal_count_;
446 int cpu_num_iters_; //there's no need to set cpu running same times with gpu
447 int gpu_warmup_iters_; //gpu warm up times, default is 1
449 int cur_warmup_idx_; //current gpu warm up times
450 std::vector<int64> cpu_times_;
451 std::vector<int64> gpu_times_;
452 std::vector<int64> gpu_full_times_;
455 std::string recordname_;
457 bool itname_changed_;
460 double accurate_diff_;
464 #define GLOBAL_INIT(name) \
465 struct name##_init: Runnable { \
466 name##_init(): Runnable(#name) { \
467 TestSystem::instance().addInit(this); \
470 } name##_init_instance; \
471 void name##_init::run()
474 #define PERFTEST(name) \
475 struct name##_test: Runnable { \
476 name##_test(): Runnable(#name) { \
477 TestSystem::instance().addTest(this); \
480 } name##_test_instance; \
481 void name##_test::run()
483 #define SUBTEST TestSystem::instance().startNewSubtest()
486 while (!TestSystem::instance().cpu_stop()) { \
487 TestSystem::instance().cpuOn()
489 TestSystem::instance().cpuOff(); \
490 } TestSystem::instance().cpuComplete()
493 while (!TestSystem::instance().stop()) { \
494 TestSystem::instance().gpuOn()
497 TestSystem::instance().gpuOff(); \
498 } TestSystem::instance().gpuComplete()
500 #define GPU_FULL_ON \
501 while (!TestSystem::instance().stop()) { \
502 TestSystem::instance().gpufullOn()
503 #define GPU_FULL_OFF \
504 TestSystem::instance().gpufullOff(); \
505 } TestSystem::instance().gpufullComplete()
508 while (!TestSystem::instance().warmupStop()) {
511 } TestSystem::instance().warmupComplete()