2020-08-12 02:13:52 +08:00
|
|
|
// This file is part of OpenCV project.
|
|
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
|
|
// of this distribution and at http://opencv.org/license.html.
|
|
|
|
|
|
|
|
#include "test_precomp.hpp"
|
|
|
|
#include "opencv2/ts/ocl_test.hpp"
|
|
|
|
|
|
|
|
namespace opencv_test {
|
|
|
|
namespace ocl {
|
|
|
|
|
2020-12-25 03:25:58 +08:00
|
|
|
static
|
|
|
|
testing::internal::ParamGenerator<std::string> getOpenCLTestConfigurations()
|
|
|
|
{
|
|
|
|
if (!cv::ocl::useOpenCL())
|
|
|
|
{
|
|
|
|
return testing::ValuesIn(std::vector<std::string>());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> configurations = {
|
|
|
|
":GPU:0",
|
|
|
|
":GPU:1",
|
|
|
|
":CPU:0",
|
|
|
|
};
|
|
|
|
return testing::ValuesIn(configurations);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-12 02:13:52 +08:00
|
|
|
static void executeUMatCall(bool requireOpenCL = true)
|
|
|
|
{
|
|
|
|
UMat a(100, 100, CV_8UC1, Scalar::all(0));
|
|
|
|
UMat b;
|
|
|
|
cv::add(a, Scalar::all(1), b);
|
|
|
|
Mat b_cpu = b.getMat(ACCESS_READ);
|
|
|
|
EXPECT_EQ(0, cv::norm(b_cpu - 1, NORM_INF));
|
|
|
|
|
|
|
|
if (requireOpenCL)
|
|
|
|
{
|
|
|
|
EXPECT_TRUE(cv::ocl::useOpenCL());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(OCL_Context, createFromDevice)
|
|
|
|
{
|
|
|
|
bool useOCL = cv::ocl::useOpenCL();
|
|
|
|
|
|
|
|
OpenCLExecutionContext ctx = OpenCLExecutionContext::getCurrent();
|
|
|
|
|
|
|
|
if (!useOCL)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(ctx.empty()); // Other tests should not broke global state
|
|
|
|
throw SkipTestException("OpenCL is not available / disabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_FALSE(ctx.empty());
|
|
|
|
|
|
|
|
ocl::Device device = ctx.getDevice();
|
|
|
|
ASSERT_FALSE(device.empty());
|
|
|
|
|
|
|
|
ocl::Context context = ocl::Context::fromDevice(device);
|
|
|
|
ocl::Context context2 = ocl::Context::fromDevice(device);
|
|
|
|
|
|
|
|
EXPECT_TRUE(context.getImpl() == context2.getImpl()) << "Broken cache for OpenCL context (device)";
|
|
|
|
}
|
|
|
|
|
2020-12-25 03:25:58 +08:00
|
|
|
TEST(OCL_OpenCLExecutionContextDefault, basic)
|
2020-08-12 02:13:52 +08:00
|
|
|
{
|
|
|
|
bool useOCL = cv::ocl::useOpenCL();
|
|
|
|
|
|
|
|
OpenCLExecutionContext ctx = OpenCLExecutionContext::getCurrent();
|
|
|
|
|
|
|
|
if (!useOCL)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(ctx.empty()); // Other tests should not broke global state
|
|
|
|
throw SkipTestException("OpenCL is not available / disabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_FALSE(ctx.empty());
|
|
|
|
|
|
|
|
ocl::Context context = ctx.getContext();
|
|
|
|
ocl::Context context2 = ocl::Context::getDefault();
|
|
|
|
EXPECT_TRUE(context.getImpl() == context2.getImpl());
|
|
|
|
|
|
|
|
ocl::Device device = ctx.getDevice();
|
|
|
|
ocl::Device device2 = ocl::Device::getDefault();
|
|
|
|
EXPECT_TRUE(device.getImpl() == device2.getImpl());
|
|
|
|
|
|
|
|
ocl::Queue queue = ctx.getQueue();
|
|
|
|
ocl::Queue queue2 = ocl::Queue::getDefault();
|
|
|
|
EXPECT_TRUE(queue.getImpl() == queue2.getImpl());
|
|
|
|
}
|
|
|
|
|
2020-12-25 03:25:58 +08:00
|
|
|
TEST(OCL_OpenCLExecutionContextDefault, createAndBind)
|
2020-08-12 02:13:52 +08:00
|
|
|
{
|
|
|
|
bool useOCL = cv::ocl::useOpenCL();
|
|
|
|
|
|
|
|
OpenCLExecutionContext ctx = OpenCLExecutionContext::getCurrent();
|
|
|
|
|
|
|
|
if (!useOCL)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(ctx.empty()); // Other tests should not broke global state
|
|
|
|
throw SkipTestException("OpenCL is not available / disabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_FALSE(ctx.empty());
|
|
|
|
|
|
|
|
ocl::Context context = ctx.getContext();
|
|
|
|
ocl::Device device = ctx.getDevice();
|
|
|
|
|
|
|
|
OpenCLExecutionContext ctx2 = OpenCLExecutionContext::create(context, device);
|
|
|
|
ASSERT_FALSE(ctx2.empty());
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ctx2.bind();
|
|
|
|
executeUMatCall();
|
|
|
|
ctx.bind();
|
|
|
|
executeUMatCall();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
ctx.bind(); // restore
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-25 03:25:58 +08:00
|
|
|
typedef testing::TestWithParam<std::string> OCL_OpenCLExecutionContext_P;
|
|
|
|
|
|
|
|
TEST_P(OCL_OpenCLExecutionContext_P, multipleBindAndExecute)
|
2020-08-12 02:13:52 +08:00
|
|
|
{
|
|
|
|
bool useOCL = cv::ocl::useOpenCL();
|
|
|
|
|
|
|
|
OpenCLExecutionContext ctx = OpenCLExecutionContext::getCurrent();
|
|
|
|
|
|
|
|
if (!useOCL)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(ctx.empty()); // Other tests should not broke global state
|
|
|
|
throw SkipTestException("OpenCL is not available / disabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_FALSE(ctx.empty());
|
|
|
|
|
2020-12-25 03:25:58 +08:00
|
|
|
std::string opencl_device = GetParam();
|
|
|
|
ocl::Context context = ocl::Context::create(opencl_device);
|
2020-08-12 02:13:52 +08:00
|
|
|
if (context.empty())
|
|
|
|
{
|
2020-12-25 03:25:58 +08:00
|
|
|
throw SkipTestException(std::string("OpenCL device is not available: '") + opencl_device + "'");
|
2020-08-12 02:13:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ocl::Device device = context.device(0);
|
|
|
|
|
|
|
|
OpenCLExecutionContext ctx2 = OpenCLExecutionContext::create(context, device);
|
|
|
|
ASSERT_FALSE(ctx2.empty());
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-12-25 03:25:58 +08:00
|
|
|
std::cout << "ctx2..." << std::endl;
|
2020-08-12 02:13:52 +08:00
|
|
|
ctx2.bind();
|
|
|
|
executeUMatCall();
|
2020-12-25 03:25:58 +08:00
|
|
|
std::cout << "ctx..." << std::endl;
|
2020-08-12 02:13:52 +08:00
|
|
|
ctx.bind();
|
|
|
|
executeUMatCall();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
ctx.bind(); // restore
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-25 03:25:58 +08:00
|
|
|
TEST_P(OCL_OpenCLExecutionContext_P, ScopeTest)
|
2020-08-12 02:13:52 +08:00
|
|
|
{
|
|
|
|
bool useOCL = cv::ocl::useOpenCL();
|
|
|
|
|
|
|
|
OpenCLExecutionContext ctx = OpenCLExecutionContext::getCurrent();
|
|
|
|
|
|
|
|
if (!useOCL)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(ctx.empty()); // Other tests should not broke global state
|
|
|
|
throw SkipTestException("OpenCL is not available / disabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_FALSE(ctx.empty());
|
|
|
|
|
2020-12-25 03:25:58 +08:00
|
|
|
std::string opencl_device = GetParam();
|
|
|
|
ocl::Context context = ocl::Context::create(opencl_device);
|
2020-08-12 02:13:52 +08:00
|
|
|
if (context.empty())
|
|
|
|
{
|
2020-12-25 03:25:58 +08:00
|
|
|
throw SkipTestException(std::string("OpenCL device is not available: '") + opencl_device + "'");
|
2020-08-12 02:13:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ocl::Device device = context.device(0);
|
|
|
|
|
|
|
|
OpenCLExecutionContext ctx2 = OpenCLExecutionContext::create(context, device);
|
|
|
|
ASSERT_FALSE(ctx2.empty());
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
OpenCLExecutionContextScope ctx_scope(ctx2);
|
|
|
|
executeUMatCall();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
ctx.bind(); // restore
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
executeUMatCall();
|
|
|
|
}
|
|
|
|
|
2020-12-25 03:25:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(/*nothing*/, OCL_OpenCLExecutionContext_P, getOpenCLTestConfigurations());
|
|
|
|
|
|
|
|
|
2020-08-12 02:13:52 +08:00
|
|
|
} } // namespace opencv_test::ocl
|