perf_matop.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. // Copyright (C) 2014, Advanced Micro Devices, Inc., all rights reserved.
  5. // Third party copyrights are property of their respective owners.
  6. #include "../perf_precomp.hpp"
  7. #include "opencv2/ts/ocl_perf.hpp"
  8. #ifdef HAVE_OPENCL
  9. namespace opencv_test {
  10. namespace ocl {
  11. ///////////// SetTo ////////////////////////
  12. typedef Size_MatType SetToFixture;
  13. OCL_PERF_TEST_P(SetToFixture, SetTo,
  14. ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES))
  15. {
  16. const Size_MatType_t params = GetParam();
  17. const Size srcSize = get<0>(params);
  18. const int type = get<1>(params);
  19. const Scalar s = Scalar::all(17);
  20. checkDeviceMaxMemoryAllocSize(srcSize, type);
  21. UMat src(srcSize, type);
  22. declare.in(src, WARMUP_RNG).out(src);
  23. OCL_TEST_CYCLE() src.setTo(s);
  24. SANITY_CHECK(src);
  25. }
  26. ///////////// SetTo with mask ////////////////////////
  27. typedef Size_MatType SetToFixture;
  28. OCL_PERF_TEST_P(SetToFixture, SetToWithMask,
  29. ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES))
  30. {
  31. const Size_MatType_t params = GetParam();
  32. const Size srcSize = get<0>(params);
  33. const int type = get<1>(params);
  34. const Scalar s = Scalar::all(17);
  35. checkDeviceMaxMemoryAllocSize(srcSize, type);
  36. UMat src(srcSize, type), mask(srcSize, CV_8UC1);
  37. declare.in(src, mask, WARMUP_RNG).out(src);
  38. OCL_TEST_CYCLE() src.setTo(s, mask);
  39. SANITY_CHECK(src);
  40. }
  41. ///////////// ConvertTo ////////////////////////
  42. typedef Size_MatType ConvertToFixture;
  43. OCL_PERF_TEST_P(ConvertToFixture, ConvertTo,
  44. ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES))
  45. {
  46. const Size_MatType_t params = GetParam();
  47. const Size srcSize = get<0>(params);
  48. const int type = get<1>(params), ddepth = CV_MAT_DEPTH(type) == CV_8U ? CV_32F : CV_8U,
  49. cn = CV_MAT_CN(type), dtype = CV_MAKE_TYPE(ddepth, cn);
  50. checkDeviceMaxMemoryAllocSize(srcSize, type);
  51. checkDeviceMaxMemoryAllocSize(srcSize, dtype);
  52. UMat src(srcSize, type), dst(srcSize, dtype);
  53. declare.in(src, WARMUP_RNG).out(dst);
  54. OCL_TEST_CYCLE() src.convertTo(dst, dtype);
  55. SANITY_CHECK(dst);
  56. }
  57. ///////////// CopyTo ////////////////////////
  58. typedef Size_MatType CopyToFixture;
  59. OCL_PERF_TEST_P(CopyToFixture, CopyTo,
  60. ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES))
  61. {
  62. const Size_MatType_t params = GetParam();
  63. const Size srcSize = get<0>(params);
  64. const int type = get<1>(params);
  65. checkDeviceMaxMemoryAllocSize(srcSize, type);
  66. UMat src(srcSize, type), dst(srcSize, type);
  67. declare.in(src, WARMUP_RNG).out(dst);
  68. OCL_TEST_CYCLE() src.copyTo(dst);
  69. SANITY_CHECK(dst);
  70. }
  71. ///////////// CopyTo with mask ////////////////////////
  72. typedef Size_MatType CopyToFixture;
  73. OCL_PERF_TEST_P(CopyToFixture, CopyToWithMask,
  74. ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES))
  75. {
  76. const Size_MatType_t params = GetParam();
  77. const Size srcSize = get<0>(params);
  78. const int type = get<1>(params);
  79. checkDeviceMaxMemoryAllocSize(srcSize, type);
  80. UMat src(srcSize, type), dst(srcSize, type), mask(srcSize, CV_8UC1);
  81. declare.in(src, mask, WARMUP_RNG).out(dst);
  82. OCL_TEST_CYCLE() src.copyTo(dst, mask);
  83. SANITY_CHECK(dst);
  84. }
  85. OCL_PERF_TEST_P(CopyToFixture, CopyToWithMaskUninit,
  86. ::testing::Combine(OCL_PERF_ENUM(OCL_SIZE_1, OCL_SIZE_2, OCL_SIZE_3), OCL_TEST_TYPES))
  87. {
  88. const Size_MatType_t params = GetParam();
  89. const Size srcSize = get<0>(params);
  90. const int type = get<1>(params);
  91. checkDeviceMaxMemoryAllocSize(srcSize, type);
  92. UMat src(srcSize, type), dst, mask(srcSize, CV_8UC1);
  93. declare.in(src, mask, WARMUP_RNG);
  94. for ( ; next(); )
  95. {
  96. dst.release();
  97. startTimer();
  98. src.copyTo(dst, mask);
  99. cvtest::ocl::perf::safeFinish();
  100. stopTimer();
  101. }
  102. SANITY_CHECK(dst);
  103. }
  104. enum ROIType {
  105. ROI_FULL,
  106. ROI_2_RECT,
  107. ROI_2_TOP, // contiguous memory block
  108. ROI_2_LEFT,
  109. ROI_4,
  110. ROI_16,
  111. };
  112. static Rect getROI(enum ROIType t, const Size& sz)
  113. {
  114. switch (t)
  115. {
  116. case ROI_FULL: return Rect(0, 0, sz.width, sz.height);
  117. case ROI_2_RECT: return Rect(0, 0, sz.width * 71 / 100, sz.height * 71 / 100); // 71 = sqrt(1/2) * 100
  118. case ROI_2_TOP: return Rect(0, 0, sz.width, sz.height / 2); // 71 = sqrt(1/2) * 100
  119. case ROI_2_LEFT: return Rect(0, 0, sz.width / 2, sz.height); // 71 = sqrt(1/2) * 100
  120. case ROI_4: return Rect(0, 0, sz.width / 2, sz.height / 2);
  121. case ROI_16: return Rect(0, 0, sz.width / 4, sz.height / 4);
  122. }
  123. CV_Assert(false);
  124. }
  125. typedef TestBaseWithParam< tuple<cv::Size, MatType, ROIType> > OpenCLBuffer;
  126. static inline void PrintTo(const tuple<cv::Size, MatType, enum ROIType>& v, std::ostream* os)
  127. {
  128. *os << "(" << get<0>(v) << ", " << typeToString(get<1>(v)) << ", ";
  129. enum ROIType roiType = get<2>(v);
  130. if (roiType == ROI_FULL)
  131. *os << "ROI_100_FULL";
  132. else if (roiType == ROI_2_RECT)
  133. *os << "ROI_050_RECT_HALF";
  134. else if (roiType == ROI_2_TOP)
  135. *os << "ROI_050_TOP_HALF";
  136. else if (roiType == ROI_2_LEFT)
  137. *os << "ROI_050_LEFT_HALF";
  138. else if (roiType == ROI_4)
  139. *os << "ROI_025_1/4";
  140. else
  141. *os << "ROI_012_1/16";
  142. *os << ")";
  143. }
  144. PERF_TEST_P_(OpenCLBuffer, cpu_write)
  145. {
  146. const Size srcSize = get<0>(GetParam());
  147. const int type = get<1>(GetParam());
  148. const Rect roi = getROI(get<2>(GetParam()), srcSize);
  149. checkDeviceMaxMemoryAllocSize(srcSize, type);
  150. UMat src(srcSize, type);
  151. declare.in(src(roi), WARMUP_NONE);
  152. OCL_TEST_CYCLE()
  153. {
  154. Mat m = src(roi).getMat(ACCESS_WRITE);
  155. m.setTo(Scalar(1, 2, 3, 4));
  156. }
  157. SANITY_CHECK_NOTHING();
  158. }
  159. PERF_TEST_P_(OpenCLBuffer, cpu_read)
  160. {
  161. const Size srcSize = get<0>(GetParam());
  162. const int type = get<1>(GetParam());
  163. const Rect roi = getROI(get<2>(GetParam()), srcSize);
  164. checkDeviceMaxMemoryAllocSize(srcSize, type);
  165. UMat src(srcSize, type, Scalar(1, 2, 3, 4));
  166. declare.in(src(roi), WARMUP_NONE);
  167. OCL_TEST_CYCLE()
  168. {
  169. unsigned counter = 0;
  170. Mat m = src(roi).getMat(ACCESS_READ);
  171. for (int y = 0; y < m.rows; y++)
  172. {
  173. uchar* ptr = m.ptr(y);
  174. size_t width_bytes = m.cols * m.elemSize();
  175. for (size_t x_bytes = 0; x_bytes < width_bytes; x_bytes++)
  176. counter += (unsigned)(ptr[x_bytes]);
  177. }
  178. }
  179. SANITY_CHECK_NOTHING();
  180. }
  181. PERF_TEST_P_(OpenCLBuffer, cpu_update)
  182. {
  183. const Size srcSize = get<0>(GetParam());
  184. const int type = get<1>(GetParam());
  185. const Rect roi = getROI(get<2>(GetParam()), srcSize);
  186. checkDeviceMaxMemoryAllocSize(srcSize, type);
  187. UMat src(srcSize, type, Scalar(1, 2, 3, 4));
  188. declare.in(src(roi), WARMUP_NONE);
  189. OCL_TEST_CYCLE()
  190. {
  191. Mat m = src(roi).getMat(ACCESS_READ | ACCESS_WRITE);
  192. for (int y = 0; y < m.rows; y++)
  193. {
  194. uchar* ptr = m.ptr(y);
  195. size_t width_bytes = m.cols * m.elemSize();
  196. for (size_t x_bytes = 0; x_bytes < width_bytes; x_bytes++)
  197. ptr[x_bytes] += 1;
  198. }
  199. }
  200. SANITY_CHECK_NOTHING();
  201. }
  202. INSTANTIATE_TEST_CASE_P(/*FULL*/, OpenCLBuffer,
  203. testing::Combine(
  204. testing::Values(szVGA, sz720p, sz1080p, sz2160p),
  205. testing::Values(CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4),
  206. testing::Values(ROI_FULL)
  207. )
  208. );
  209. INSTANTIATE_TEST_CASE_P(ROI, OpenCLBuffer,
  210. testing::Combine(
  211. testing::Values(sz1080p, sz2160p),
  212. testing::Values(CV_8UC1),
  213. testing::Values(ROI_16, ROI_4, ROI_2_RECT, ROI_2_LEFT, ROI_2_TOP, ROI_FULL)
  214. )
  215. );
  216. } } // namespace opencv_test::ocl
  217. #endif // HAVE_OPENCL