perf_arithm.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. // GEMM
  46. #ifdef HAVE_CUBLAS
  47. CV_FLAGS(GemmFlags, 0, cv::GEMM_1_T, cv::GEMM_2_T, cv::GEMM_3_T)
  48. #define ALL_GEMM_FLAGS Values(GemmFlags(0), GemmFlags(cv::GEMM_1_T), GemmFlags(cv::GEMM_2_T), GemmFlags(cv::GEMM_3_T), \
  49. GemmFlags(cv::GEMM_1_T | cv::GEMM_2_T), GemmFlags(cv::GEMM_1_T | cv::GEMM_3_T), GemmFlags(cv::GEMM_1_T | cv::GEMM_2_T | cv::GEMM_3_T))
  50. DEF_PARAM_TEST(Sz_Type_Flags, cv::Size, MatType, GemmFlags);
  51. PERF_TEST_P(Sz_Type_Flags, GEMM,
  52. Combine(Values(cv::Size(512, 512), cv::Size(1024, 1024)),
  53. Values(CV_32FC1, CV_32FC2, CV_64FC1),
  54. ALL_GEMM_FLAGS))
  55. {
  56. const cv::Size size = GET_PARAM(0);
  57. const int type = GET_PARAM(1);
  58. const int flags = GET_PARAM(2);
  59. cv::Mat src1(size, type);
  60. declare.in(src1, WARMUP_RNG);
  61. cv::Mat src2(size, type);
  62. declare.in(src2, WARMUP_RNG);
  63. cv::Mat src3(size, type);
  64. declare.in(src3, WARMUP_RNG);
  65. if (PERF_RUN_CUDA())
  66. {
  67. declare.time(5.0);
  68. const cv::cuda::GpuMat d_src1(src1);
  69. const cv::cuda::GpuMat d_src2(src2);
  70. const cv::cuda::GpuMat d_src3(src3);
  71. cv::cuda::GpuMat dst;
  72. TEST_CYCLE() cv::cuda::gemm(d_src1, d_src2, 1.0, d_src3, 1.0, dst, flags);
  73. CUDA_SANITY_CHECK(dst, 1e-6, ERROR_RELATIVE);
  74. }
  75. else
  76. {
  77. declare.time(50.0);
  78. cv::Mat dst;
  79. TEST_CYCLE() cv::gemm(src1, src2, 1.0, src3, 1.0, dst, flags);
  80. CPU_SANITY_CHECK(dst);
  81. }
  82. }
  83. #endif
  84. //////////////////////////////////////////////////////////////////////
  85. // MulSpectrums
  86. CV_FLAGS(DftFlags, 0, cv::DFT_INVERSE, cv::DFT_SCALE, cv::DFT_ROWS, cv::DFT_COMPLEX_OUTPUT, cv::DFT_REAL_OUTPUT)
  87. DEF_PARAM_TEST(Sz_Flags, cv::Size, DftFlags);
  88. PERF_TEST_P(Sz_Flags, MulSpectrums,
  89. Combine(CUDA_TYPICAL_MAT_SIZES,
  90. Values(0, DftFlags(cv::DFT_ROWS))))
  91. {
  92. const cv::Size size = GET_PARAM(0);
  93. const int flag = GET_PARAM(1);
  94. cv::Mat a(size, CV_32FC2);
  95. cv::Mat b(size, CV_32FC2);
  96. declare.in(a, b, WARMUP_RNG);
  97. if (PERF_RUN_CUDA())
  98. {
  99. const cv::cuda::GpuMat d_a(a);
  100. const cv::cuda::GpuMat d_b(b);
  101. cv::cuda::GpuMat dst;
  102. TEST_CYCLE() cv::cuda::mulSpectrums(d_a, d_b, dst, flag);
  103. CUDA_SANITY_CHECK(dst, 1e-6, ERROR_RELATIVE);
  104. }
  105. else
  106. {
  107. cv::Mat dst;
  108. TEST_CYCLE() cv::mulSpectrums(a, b, dst, flag);
  109. CPU_SANITY_CHECK(dst);
  110. }
  111. }
  112. //////////////////////////////////////////////////////////////////////
  113. // MulAndScaleSpectrums
  114. PERF_TEST_P(Sz, MulAndScaleSpectrums,
  115. CUDA_TYPICAL_MAT_SIZES)
  116. {
  117. const cv::Size size = GetParam();
  118. const float scale = 1.f / size.area();
  119. cv::Mat src1(size, CV_32FC2);
  120. cv::Mat src2(size, CV_32FC2);
  121. declare.in(src1,src2, WARMUP_RNG);
  122. if (PERF_RUN_CUDA())
  123. {
  124. const cv::cuda::GpuMat d_src1(src1);
  125. const cv::cuda::GpuMat d_src2(src2);
  126. cv::cuda::GpuMat dst;
  127. TEST_CYCLE() cv::cuda::mulAndScaleSpectrums(d_src1, d_src2, dst, cv::DFT_ROWS, scale, false);
  128. CUDA_SANITY_CHECK(dst, 1e-6, ERROR_RELATIVE);
  129. }
  130. else
  131. {
  132. FAIL_NO_CPU();
  133. }
  134. }
  135. //////////////////////////////////////////////////////////////////////
  136. // Dft
  137. PERF_TEST_P(Sz_Flags, Dft,
  138. Combine(CUDA_TYPICAL_MAT_SIZES,
  139. Values(0, DftFlags(cv::DFT_ROWS), DftFlags(cv::DFT_INVERSE))))
  140. {
  141. declare.time(10.0);
  142. const cv::Size size = GET_PARAM(0);
  143. const int flag = GET_PARAM(1);
  144. cv::Mat src(size, CV_32FC2);
  145. declare.in(src, WARMUP_RNG);
  146. if (PERF_RUN_CUDA())
  147. {
  148. const cv::cuda::GpuMat d_src(src);
  149. cv::cuda::GpuMat dst;
  150. TEST_CYCLE() cv::cuda::dft(d_src, dst, size, flag);
  151. CUDA_SANITY_CHECK(dst, 1e-6, ERROR_RELATIVE);
  152. }
  153. else
  154. {
  155. cv::Mat dst;
  156. TEST_CYCLE() cv::dft(src, dst, flag);
  157. CPU_SANITY_CHECK(dst);
  158. }
  159. }
  160. //////////////////////////////////////////////////////////////////////
  161. // Convolve
  162. DEF_PARAM_TEST(Sz_KernelSz_Ccorr, cv::Size, int, bool);
  163. PERF_TEST_P(Sz_KernelSz_Ccorr, Convolve,
  164. Combine(CUDA_TYPICAL_MAT_SIZES,
  165. Values(17, 27, 32, 64),
  166. Bool()))
  167. {
  168. declare.time(10.0);
  169. const cv::Size size = GET_PARAM(0);
  170. const int templ_size = GET_PARAM(1);
  171. const bool ccorr = GET_PARAM(2);
  172. const cv::Mat image(size, CV_32FC1);
  173. const cv::Mat templ(templ_size, templ_size, CV_32FC1);
  174. declare.in(image, templ, WARMUP_RNG);
  175. if (PERF_RUN_CUDA())
  176. {
  177. cv::cuda::GpuMat d_image = cv::cuda::createContinuous(size, CV_32FC1);
  178. d_image.upload(image);
  179. cv::cuda::GpuMat d_templ = cv::cuda::createContinuous(templ_size, templ_size, CV_32FC1);
  180. d_templ.upload(templ);
  181. cv::Ptr<cv::cuda::Convolution> convolution = cv::cuda::createConvolution();
  182. cv::cuda::GpuMat dst;
  183. TEST_CYCLE() convolution->convolve(d_image, d_templ, dst, ccorr);
  184. CUDA_SANITY_CHECK(dst, 1e-6, ERROR_RELATIVE);
  185. }
  186. else
  187. {
  188. if (ccorr)
  189. FAIL_NO_CPU();
  190. cv::Mat dst;
  191. TEST_CYCLE() cv::filter2D(image, dst, image.depth(), templ);
  192. CPU_SANITY_CHECK(dst);
  193. }
  194. }
  195. }} // namespace