perf_reductions.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  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.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  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 materials provided with the distribution.
  26. //
  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.
  29. //
  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.
  40. //
  41. //M*/
  42. #include "perf_precomp.hpp"
  43. namespace opencv_test { namespace {
  44. //////////////////////////////////////////////////////////////////////
  45. // Norm
  46. DEF_PARAM_TEST(Sz_Depth_Norm, cv::Size, MatDepth, NormType);
  47. PERF_TEST_P(Sz_Depth_Norm, Norm,
  48. Combine(CUDA_TYPICAL_MAT_SIZES,
  49. Values(CV_8U, CV_16U, CV_32S, CV_32F),
  50. Values(NormType(cv::NORM_INF), NormType(cv::NORM_L1), NormType(cv::NORM_L2))))
  51. {
  52. const cv::Size size = GET_PARAM(0);
  53. const int depth = GET_PARAM(1);
  54. const int normType = GET_PARAM(2);
  55. cv::Mat src(size, depth);
  56. if (depth == CV_8U)
  57. cv::randu(src, 0, 254);
  58. else
  59. declare.in(src, WARMUP_RNG);
  60. if (PERF_RUN_CUDA())
  61. {
  62. const cv::cuda::GpuMat d_src(src);
  63. cv::cuda::GpuMat d_buf;
  64. double gpu_dst;
  65. TEST_CYCLE() gpu_dst = cv::cuda::norm(d_src, normType, d_buf);
  66. SANITY_CHECK(gpu_dst, 1e-6, ERROR_RELATIVE);
  67. }
  68. else
  69. {
  70. double cpu_dst;
  71. TEST_CYCLE() cpu_dst = cv::norm(src, normType);
  72. SANITY_CHECK(cpu_dst, 1e-6, ERROR_RELATIVE);
  73. }
  74. }
  75. //////////////////////////////////////////////////////////////////////
  76. // NormDiff
  77. DEF_PARAM_TEST(Sz_Norm, cv::Size, NormType);
  78. PERF_TEST_P(Sz_Norm, NormDiff,
  79. Combine(CUDA_TYPICAL_MAT_SIZES,
  80. Values(NormType(cv::NORM_INF), NormType(cv::NORM_L1), NormType(cv::NORM_L2))))
  81. {
  82. const cv::Size size = GET_PARAM(0);
  83. const int normType = GET_PARAM(1);
  84. cv::Mat src1(size, CV_8UC1);
  85. declare.in(src1, WARMUP_RNG);
  86. cv::Mat src2(size, CV_8UC1);
  87. declare.in(src2, WARMUP_RNG);
  88. if (PERF_RUN_CUDA())
  89. {
  90. const cv::cuda::GpuMat d_src1(src1);
  91. const cv::cuda::GpuMat d_src2(src2);
  92. double gpu_dst;
  93. TEST_CYCLE() gpu_dst = cv::cuda::norm(d_src1, d_src2, normType);
  94. SANITY_CHECK(gpu_dst);
  95. }
  96. else
  97. {
  98. double cpu_dst;
  99. TEST_CYCLE() cpu_dst = cv::norm(src1, src2, normType);
  100. SANITY_CHECK(cpu_dst);
  101. }
  102. }
  103. //////////////////////////////////////////////////////////////////////
  104. // Sum
  105. DEF_PARAM_TEST(Sz_Depth_Cn, cv::Size, MatDepth, MatCn);
  106. PERF_TEST_P(Sz_Depth_Cn, Sum,
  107. Combine(CUDA_TYPICAL_MAT_SIZES,
  108. Values(CV_8U, CV_16U, CV_32F),
  109. CUDA_CHANNELS_1_3_4))
  110. {
  111. const cv::Size size = GET_PARAM(0);
  112. const int depth = GET_PARAM(1);
  113. const int channels = GET_PARAM(2);
  114. const int type = CV_MAKE_TYPE(depth, channels);
  115. cv::Mat src(size, type);
  116. declare.in(src, WARMUP_RNG);
  117. if (PERF_RUN_CUDA())
  118. {
  119. const cv::cuda::GpuMat d_src(src);
  120. cv::Scalar gpu_dst;
  121. TEST_CYCLE() gpu_dst = cv::cuda::sum(d_src);
  122. SANITY_CHECK(gpu_dst, 1e-5, ERROR_RELATIVE);
  123. }
  124. else
  125. {
  126. cv::Scalar cpu_dst;
  127. TEST_CYCLE() cpu_dst = cv::sum(src);
  128. SANITY_CHECK(cpu_dst, 1e-6, ERROR_RELATIVE);
  129. }
  130. }
  131. //////////////////////////////////////////////////////////////////////
  132. // SumAbs
  133. PERF_TEST_P(Sz_Depth_Cn, SumAbs,
  134. Combine(CUDA_TYPICAL_MAT_SIZES,
  135. Values(CV_8U, CV_16U, CV_32F),
  136. CUDA_CHANNELS_1_3_4))
  137. {
  138. const cv::Size size = GET_PARAM(0);
  139. const int depth = GET_PARAM(1);
  140. const int channels = GET_PARAM(2);
  141. const int type = CV_MAKE_TYPE(depth, channels);
  142. cv::Mat src(size, type);
  143. declare.in(src, WARMUP_RNG);
  144. if (PERF_RUN_CUDA())
  145. {
  146. const cv::cuda::GpuMat d_src(src);
  147. cv::Scalar gpu_dst;
  148. TEST_CYCLE() gpu_dst = cv::cuda::absSum(d_src);
  149. SANITY_CHECK(gpu_dst, 1e-6, ERROR_RELATIVE);
  150. }
  151. else
  152. {
  153. FAIL_NO_CPU();
  154. }
  155. }
  156. //////////////////////////////////////////////////////////////////////
  157. // SumSqr
  158. PERF_TEST_P(Sz_Depth_Cn, SumSqr,
  159. Combine(CUDA_TYPICAL_MAT_SIZES,
  160. Values<MatDepth>(CV_8U, CV_16U, CV_32F),
  161. CUDA_CHANNELS_1_3_4))
  162. {
  163. const cv::Size size = GET_PARAM(0);
  164. const int depth = GET_PARAM(1);
  165. const int channels = GET_PARAM(2);
  166. const int type = CV_MAKE_TYPE(depth, channels);
  167. cv::Mat src(size, type);
  168. declare.in(src, WARMUP_RNG);
  169. if (PERF_RUN_CUDA())
  170. {
  171. const cv::cuda::GpuMat d_src(src);
  172. cv::Scalar gpu_dst;
  173. TEST_CYCLE() gpu_dst = cv::cuda::sqrSum(d_src);
  174. SANITY_CHECK(gpu_dst, 1e-6, ERROR_RELATIVE);
  175. }
  176. else
  177. {
  178. FAIL_NO_CPU();
  179. }
  180. }
  181. //////////////////////////////////////////////////////////////////////
  182. // MinMax
  183. DEF_PARAM_TEST(Sz_Depth, cv::Size, MatDepth);
  184. PERF_TEST_P(Sz_Depth, MinMax,
  185. Combine(CUDA_TYPICAL_MAT_SIZES,
  186. Values(CV_8U, CV_16U, CV_32F, CV_64F)))
  187. {
  188. const cv::Size size = GET_PARAM(0);
  189. const int depth = GET_PARAM(1);
  190. cv::Mat src(size, depth);
  191. if (depth == CV_8U)
  192. cv::randu(src, 0, 254);
  193. else
  194. declare.in(src, WARMUP_RNG);
  195. if (PERF_RUN_CUDA())
  196. {
  197. const cv::cuda::GpuMat d_src(src);
  198. double gpu_minVal, gpu_maxVal;
  199. TEST_CYCLE() cv::cuda::minMax(d_src, &gpu_minVal, &gpu_maxVal, cv::cuda::GpuMat());
  200. SANITY_CHECK(gpu_minVal, 1e-10);
  201. SANITY_CHECK(gpu_maxVal, 1e-10);
  202. }
  203. else
  204. {
  205. double cpu_minVal, cpu_maxVal;
  206. TEST_CYCLE() cv::minMaxLoc(src, &cpu_minVal, &cpu_maxVal);
  207. SANITY_CHECK(cpu_minVal);
  208. SANITY_CHECK(cpu_maxVal);
  209. }
  210. }
  211. //////////////////////////////////////////////////////////////////////
  212. // MinMaxLoc
  213. PERF_TEST_P(Sz_Depth, MinMaxLoc,
  214. Combine(CUDA_TYPICAL_MAT_SIZES,
  215. Values(CV_8U, CV_16U, CV_32F, CV_64F)))
  216. {
  217. const cv::Size size = GET_PARAM(0);
  218. const int depth = GET_PARAM(1);
  219. cv::Mat src(size, depth);
  220. if (depth == CV_8U)
  221. cv::randu(src, 0, 254);
  222. else
  223. declare.in(src, WARMUP_RNG);
  224. if (PERF_RUN_CUDA())
  225. {
  226. const cv::cuda::GpuMat d_src(src);
  227. double gpu_minVal, gpu_maxVal;
  228. cv::Point gpu_minLoc, gpu_maxLoc;
  229. TEST_CYCLE() cv::cuda::minMaxLoc(d_src, &gpu_minVal, &gpu_maxVal, &gpu_minLoc, &gpu_maxLoc);
  230. SANITY_CHECK(gpu_minVal, 1e-10);
  231. SANITY_CHECK(gpu_maxVal, 1e-10);
  232. }
  233. else
  234. {
  235. double cpu_minVal, cpu_maxVal;
  236. cv::Point cpu_minLoc, cpu_maxLoc;
  237. TEST_CYCLE() cv::minMaxLoc(src, &cpu_minVal, &cpu_maxVal, &cpu_minLoc, &cpu_maxLoc);
  238. SANITY_CHECK(cpu_minVal);
  239. SANITY_CHECK(cpu_maxVal);
  240. }
  241. }
  242. //////////////////////////////////////////////////////////////////////
  243. // CountNonZero
  244. PERF_TEST_P(Sz_Depth, CountNonZero,
  245. Combine(CUDA_TYPICAL_MAT_SIZES,
  246. Values(CV_8U, CV_16U, CV_32F, CV_64F)))
  247. {
  248. const cv::Size size = GET_PARAM(0);
  249. const int depth = GET_PARAM(1);
  250. cv::Mat src(size, depth);
  251. declare.in(src, WARMUP_RNG);
  252. if (PERF_RUN_CUDA())
  253. {
  254. const cv::cuda::GpuMat d_src(src);
  255. int gpu_dst = 0;
  256. TEST_CYCLE() gpu_dst = cv::cuda::countNonZero(d_src);
  257. SANITY_CHECK(gpu_dst);
  258. }
  259. else
  260. {
  261. int cpu_dst = 0;
  262. TEST_CYCLE() cpu_dst = cv::countNonZero(src);
  263. SANITY_CHECK(cpu_dst);
  264. }
  265. }
  266. //////////////////////////////////////////////////////////////////////
  267. // Reduce
  268. CV_ENUM(ReduceCode, REDUCE_SUM, REDUCE_AVG, REDUCE_MAX, REDUCE_MIN)
  269. enum {Rows = 0, Cols = 1};
  270. CV_ENUM(ReduceDim, Rows, Cols)
  271. DEF_PARAM_TEST(Sz_Depth_Cn_Code_Dim, cv::Size, MatDepth, MatCn, ReduceCode, ReduceDim);
  272. PERF_TEST_P(Sz_Depth_Cn_Code_Dim, Reduce,
  273. Combine(CUDA_TYPICAL_MAT_SIZES,
  274. Values(CV_8U, CV_16U, CV_16S, CV_32F),
  275. Values(1, 2, 3, 4),
  276. ReduceCode::all(),
  277. ReduceDim::all()))
  278. {
  279. const cv::Size size = GET_PARAM(0);
  280. const int depth = GET_PARAM(1);
  281. const int channels = GET_PARAM(2);
  282. const int reduceOp = GET_PARAM(3);
  283. const int dim = GET_PARAM(4);
  284. const int type = CV_MAKE_TYPE(depth, channels);
  285. cv::Mat src(size, type);
  286. declare.in(src, WARMUP_RNG);
  287. if (PERF_RUN_CUDA())
  288. {
  289. const cv::cuda::GpuMat d_src(src);
  290. cv::cuda::GpuMat dst;
  291. TEST_CYCLE() cv::cuda::reduce(d_src, dst, dim, reduceOp, CV_32F);
  292. dst = dst.reshape(dst.channels(), 1);
  293. CUDA_SANITY_CHECK(dst);
  294. }
  295. else
  296. {
  297. cv::Mat dst;
  298. TEST_CYCLE() cv::reduce(src, dst, dim, reduceOp, CV_32F);
  299. CPU_SANITY_CHECK(dst);
  300. }
  301. }
  302. //////////////////////////////////////////////////////////////////////
  303. // Normalize
  304. DEF_PARAM_TEST(Sz_Depth_NormType, cv::Size, MatDepth, NormType);
  305. PERF_TEST_P(Sz_Depth_NormType, Normalize,
  306. Combine(CUDA_TYPICAL_MAT_SIZES,
  307. Values(CV_8U, CV_16U, CV_32F, CV_64F),
  308. Values(NormType(cv::NORM_INF),
  309. NormType(cv::NORM_L1),
  310. NormType(cv::NORM_L2),
  311. NormType(cv::NORM_MINMAX))))
  312. {
  313. const cv::Size size = GET_PARAM(0);
  314. const int type = GET_PARAM(1);
  315. const int norm_type = GET_PARAM(2);
  316. const double alpha = 1;
  317. const double beta = 0;
  318. cv::Mat src(size, type);
  319. declare.in(src, WARMUP_RNG);
  320. if (PERF_RUN_CUDA())
  321. {
  322. const cv::cuda::GpuMat d_src(src);
  323. cv::cuda::GpuMat dst;
  324. TEST_CYCLE() cv::cuda::normalize(d_src, dst, alpha, beta, norm_type, type, cv::cuda::GpuMat());
  325. CUDA_SANITY_CHECK(dst, 1e-6);
  326. }
  327. else
  328. {
  329. cv::Mat dst;
  330. TEST_CYCLE() cv::normalize(src, dst, alpha, beta, norm_type, type);
  331. CPU_SANITY_CHECK(dst);
  332. }
  333. }
  334. //////////////////////////////////////////////////////////////////////
  335. // MeanStdDev
  336. PERF_TEST_P(Sz, MeanStdDev,
  337. CUDA_TYPICAL_MAT_SIZES)
  338. {
  339. const cv::Size size = GetParam();
  340. cv::Mat src(size, CV_8UC1);
  341. declare.in(src, WARMUP_RNG);
  342. if (PERF_RUN_CUDA())
  343. {
  344. const cv::cuda::GpuMat d_src(src);
  345. cv::Scalar gpu_mean;
  346. cv::Scalar gpu_stddev;
  347. TEST_CYCLE() cv::cuda::meanStdDev(d_src, gpu_mean, gpu_stddev);
  348. SANITY_CHECK(gpu_mean);
  349. SANITY_CHECK(gpu_stddev);
  350. }
  351. else
  352. {
  353. cv::Scalar cpu_mean;
  354. cv::Scalar cpu_stddev;
  355. TEST_CYCLE() cv::meanStdDev(src, cpu_mean, cpu_stddev);
  356. SANITY_CHECK(cpu_mean);
  357. SANITY_CHECK(cpu_stddev);
  358. }
  359. }
  360. //////////////////////////////////////////////////////////////////////
  361. // Integral
  362. PERF_TEST_P(Sz, Integral,
  363. CUDA_TYPICAL_MAT_SIZES)
  364. {
  365. const cv::Size size = GetParam();
  366. cv::Mat src(size, CV_8UC1);
  367. declare.in(src, WARMUP_RNG);
  368. if (PERF_RUN_CUDA())
  369. {
  370. const cv::cuda::GpuMat d_src(src);
  371. cv::cuda::GpuMat dst;
  372. TEST_CYCLE() cv::cuda::integral(d_src, dst);
  373. CUDA_SANITY_CHECK(dst);
  374. }
  375. else
  376. {
  377. cv::Mat dst;
  378. TEST_CYCLE() cv::integral(src, dst);
  379. CPU_SANITY_CHECK(dst);
  380. }
  381. }
  382. //////////////////////////////////////////////////////////////////////
  383. // IntegralSqr
  384. PERF_TEST_P(Sz, IntegralSqr,
  385. CUDA_TYPICAL_MAT_SIZES)
  386. {
  387. const cv::Size size = GetParam();
  388. cv::Mat src(size, CV_8UC1);
  389. declare.in(src, WARMUP_RNG);
  390. if (PERF_RUN_CUDA())
  391. {
  392. const cv::cuda::GpuMat d_src(src);
  393. cv::cuda::GpuMat dst;
  394. TEST_CYCLE() cv::cuda::sqrIntegral(d_src, dst);
  395. CUDA_SANITY_CHECK(dst);
  396. }
  397. else
  398. {
  399. FAIL_NO_CPU();
  400. }
  401. }
  402. }} // namespace