test_opencl.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #include "test_precomp.hpp"
  5. #include "opencv2/ts/ocl_test.hpp"
  6. namespace opencv_test {
  7. namespace ocl {
  8. static
  9. testing::internal::ParamGenerator<std::string> getOpenCLTestConfigurations()
  10. {
  11. if (!cv::ocl::useOpenCL())
  12. {
  13. return testing::ValuesIn(std::vector<std::string>());
  14. }
  15. std::vector<std::string> configurations = {
  16. ":GPU:0",
  17. ":GPU:1",
  18. ":CPU:0",
  19. };
  20. return testing::ValuesIn(configurations);
  21. }
  22. static void executeUMatCall(bool requireOpenCL = true)
  23. {
  24. UMat a(100, 100, CV_8UC1, Scalar::all(0));
  25. UMat b;
  26. cv::add(a, Scalar::all(1), b);
  27. Mat b_cpu = b.getMat(ACCESS_READ);
  28. EXPECT_EQ(0, cv::norm(b_cpu - 1, NORM_INF));
  29. if (requireOpenCL)
  30. {
  31. EXPECT_TRUE(cv::ocl::useOpenCL());
  32. }
  33. }
  34. TEST(OCL_Context, createFromDevice)
  35. {
  36. bool useOCL = cv::ocl::useOpenCL();
  37. OpenCLExecutionContext ctx = OpenCLExecutionContext::getCurrent();
  38. if (!useOCL)
  39. {
  40. ASSERT_TRUE(ctx.empty()); // Other tests should not broke global state
  41. throw SkipTestException("OpenCL is not available / disabled");
  42. }
  43. ASSERT_FALSE(ctx.empty());
  44. ocl::Device device = ctx.getDevice();
  45. ASSERT_FALSE(device.empty());
  46. ocl::Context context = ocl::Context::fromDevice(device);
  47. ocl::Context context2 = ocl::Context::fromDevice(device);
  48. EXPECT_TRUE(context.getImpl() == context2.getImpl()) << "Broken cache for OpenCL context (device)";
  49. }
  50. TEST(OCL_OpenCLExecutionContextDefault, basic)
  51. {
  52. bool useOCL = cv::ocl::useOpenCL();
  53. OpenCLExecutionContext ctx = OpenCLExecutionContext::getCurrent();
  54. if (!useOCL)
  55. {
  56. ASSERT_TRUE(ctx.empty()); // Other tests should not broke global state
  57. throw SkipTestException("OpenCL is not available / disabled");
  58. }
  59. ASSERT_FALSE(ctx.empty());
  60. ocl::Context context = ctx.getContext();
  61. ocl::Context context2 = ocl::Context::getDefault();
  62. EXPECT_TRUE(context.getImpl() == context2.getImpl());
  63. ocl::Device device = ctx.getDevice();
  64. ocl::Device device2 = ocl::Device::getDefault();
  65. EXPECT_TRUE(device.getImpl() == device2.getImpl());
  66. ocl::Queue queue = ctx.getQueue();
  67. ocl::Queue queue2 = ocl::Queue::getDefault();
  68. EXPECT_TRUE(queue.getImpl() == queue2.getImpl());
  69. }
  70. TEST(OCL_OpenCLExecutionContextDefault, createAndBind)
  71. {
  72. bool useOCL = cv::ocl::useOpenCL();
  73. OpenCLExecutionContext ctx = OpenCLExecutionContext::getCurrent();
  74. if (!useOCL)
  75. {
  76. ASSERT_TRUE(ctx.empty()); // Other tests should not broke global state
  77. throw SkipTestException("OpenCL is not available / disabled");
  78. }
  79. ASSERT_FALSE(ctx.empty());
  80. ocl::Context context = ctx.getContext();
  81. ocl::Device device = ctx.getDevice();
  82. OpenCLExecutionContext ctx2 = OpenCLExecutionContext::create(context, device);
  83. ASSERT_FALSE(ctx2.empty());
  84. try
  85. {
  86. ctx2.bind();
  87. executeUMatCall();
  88. ctx.bind();
  89. executeUMatCall();
  90. }
  91. catch (...)
  92. {
  93. ctx.bind(); // restore
  94. throw;
  95. }
  96. }
  97. typedef testing::TestWithParam<std::string> OCL_OpenCLExecutionContext_P;
  98. TEST_P(OCL_OpenCLExecutionContext_P, multipleBindAndExecute)
  99. {
  100. bool useOCL = cv::ocl::useOpenCL();
  101. OpenCLExecutionContext ctx = OpenCLExecutionContext::getCurrent();
  102. if (!useOCL)
  103. {
  104. ASSERT_TRUE(ctx.empty()); // Other tests should not broke global state
  105. throw SkipTestException("OpenCL is not available / disabled");
  106. }
  107. ASSERT_FALSE(ctx.empty());
  108. std::string opencl_device = GetParam();
  109. ocl::Context context = ocl::Context::create(opencl_device);
  110. if (context.empty())
  111. {
  112. throw SkipTestException(std::string("OpenCL device is not available: '") + opencl_device + "'");
  113. }
  114. ocl::Device device = context.device(0);
  115. OpenCLExecutionContext ctx2 = OpenCLExecutionContext::create(context, device);
  116. ASSERT_FALSE(ctx2.empty());
  117. try
  118. {
  119. std::cout << "ctx2..." << std::endl;
  120. ctx2.bind();
  121. executeUMatCall();
  122. std::cout << "ctx..." << std::endl;
  123. ctx.bind();
  124. executeUMatCall();
  125. }
  126. catch (...)
  127. {
  128. ctx.bind(); // restore
  129. throw;
  130. }
  131. }
  132. TEST_P(OCL_OpenCLExecutionContext_P, ScopeTest)
  133. {
  134. bool useOCL = cv::ocl::useOpenCL();
  135. OpenCLExecutionContext ctx = OpenCLExecutionContext::getCurrent();
  136. if (!useOCL)
  137. {
  138. ASSERT_TRUE(ctx.empty()); // Other tests should not broke global state
  139. throw SkipTestException("OpenCL is not available / disabled");
  140. }
  141. ASSERT_FALSE(ctx.empty());
  142. std::string opencl_device = GetParam();
  143. ocl::Context context = ocl::Context::create(opencl_device);
  144. if (context.empty())
  145. {
  146. throw SkipTestException(std::string("OpenCL device is not available: '") + opencl_device + "'");
  147. }
  148. ocl::Device device = context.device(0);
  149. OpenCLExecutionContext ctx2 = OpenCLExecutionContext::create(context, device);
  150. ASSERT_FALSE(ctx2.empty());
  151. try
  152. {
  153. OpenCLExecutionContextScope ctx_scope(ctx2);
  154. executeUMatCall();
  155. }
  156. catch (...)
  157. {
  158. ctx.bind(); // restore
  159. throw;
  160. }
  161. executeUMatCall();
  162. }
  163. INSTANTIATE_TEST_CASE_P(/*nothing*/, OCL_OpenCLExecutionContext_P, getOpenCLTestConfigurations());
  164. typedef testing::TestWithParam<UMatUsageFlags> UsageFlagsFixture;
  165. OCL_TEST_P(UsageFlagsFixture, UsageFlagsRetained)
  166. {
  167. if (!cv::ocl::useOpenCL())
  168. {
  169. throw SkipTestException("OpenCL is not available / disabled");
  170. }
  171. const UMatUsageFlags usage = GetParam();
  172. cv::UMat flip_in(10, 10, CV_32F, usage);
  173. cv::UMat flip_out(usage);
  174. cv::flip(flip_in, flip_out, 1);
  175. cv::ocl::finish();
  176. ASSERT_EQ(usage, flip_in.usageFlags);
  177. ASSERT_EQ(usage, flip_out.usageFlags);
  178. }
  179. INSTANTIATE_TEST_CASE_P(
  180. /*nothing*/,
  181. UsageFlagsFixture,
  182. testing::Values(USAGE_DEFAULT, USAGE_ALLOCATE_HOST_MEMORY, USAGE_ALLOCATE_DEVICE_MEMORY)
  183. );
  184. } } // namespace opencv_test::ocl