1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
// 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 {
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);
}
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)";
}
TEST(OCL_OpenCLExecutionContextDefault, basic)
{
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());
}
TEST(OCL_OpenCLExecutionContextDefault, createAndBind)
{
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;
}
}
typedef testing::TestWithParam<std::string> OCL_OpenCLExecutionContext_P;
TEST_P(OCL_OpenCLExecutionContext_P, multipleBindAndExecute)
{
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());
std::string opencl_device = GetParam();
ocl::Context context = ocl::Context::create(opencl_device);
if (context.empty())
{
throw SkipTestException(std::string("OpenCL device is not available: '") + opencl_device + "'");
}
ocl::Device device = context.device(0);
OpenCLExecutionContext ctx2 = OpenCLExecutionContext::create(context, device);
ASSERT_FALSE(ctx2.empty());
try
{
std::cout << "ctx2..." << std::endl;
ctx2.bind();
executeUMatCall();
std::cout << "ctx..." << std::endl;
ctx.bind();
executeUMatCall();
}
catch (...)
{
ctx.bind(); // restore
throw;
}
}
TEST_P(OCL_OpenCLExecutionContext_P, ScopeTest)
{
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());
std::string opencl_device = GetParam();
ocl::Context context = ocl::Context::create(opencl_device);
if (context.empty())
{
throw SkipTestException(std::string("OpenCL device is not available: '") + opencl_device + "'");
}
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();
}
INSTANTIATE_TEST_CASE_P(/*nothing*/, OCL_OpenCLExecutionContext_P, getOpenCLTestConfigurations());
typedef testing::TestWithParam<UMatUsageFlags> UsageFlagsFixture;
OCL_TEST_P(UsageFlagsFixture, UsageFlagsRetained)
{
if (!cv::ocl::useOpenCL())
{
throw SkipTestException("OpenCL is not available / disabled");
}
const UMatUsageFlags usage = GetParam();
cv::UMat flip_in(10, 10, CV_32F, usage);
cv::UMat flip_out(usage);
cv::flip(flip_in, flip_out, 1);
cv::ocl::finish();
ASSERT_EQ(usage, flip_in.usageFlags);
ASSERT_EQ(usage, flip_out.usageFlags);
}
INSTANTIATE_TEST_CASE_P(
/*nothing*/,
UsageFlagsFixture,
testing::Values(USAGE_DEFAULT, USAGE_ALLOCATE_HOST_MEMORY, USAGE_ALLOCATE_DEVICE_MEMORY)
);
} } // namespace opencv_test::ocl
|