Path: blob/master/modules/core/test/ocl/test_opencl.cpp
16339 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.3#include "../test_precomp.hpp"45#include <opencv2/core/ocl.hpp>67namespace opencv_test { namespace {89static void testOpenCLKernel(cv::ocl::Kernel& k)10{11ASSERT_FALSE(k.empty());12cv::UMat src(cv::Size(4096, 2048), CV_8UC1, cv::Scalar::all(100));13cv::UMat dst(src.size(), CV_8UC1);14size_t globalSize[2] = {(size_t)src.cols, (size_t)src.rows};15size_t localSize[2] = {8, 8};16int64 kernel_time = k.args(17cv::ocl::KernelArg::ReadOnlyNoSize(src), // size is not used (similar to 'dst' size)18cv::ocl::KernelArg::WriteOnly(dst),19(int)520).runProfiling(2, globalSize, localSize);21ASSERT_GE(kernel_time, (int64)0);22std::cout << "Kernel time: " << (kernel_time * 1e-6) << " ms" << std::endl;23cv::Mat res, reference(src.size(), CV_8UC1, cv::Scalar::all(105));24dst.copyTo(res);25EXPECT_EQ(0, cvtest::norm(reference, res, cv::NORM_INF));26}2728TEST(OpenCL, support_binary_programs)29{30cv::ocl::Context ctx = cv::ocl::Context::getDefault();31if (!ctx.ptr())32{33throw cvtest::SkipTestException("OpenCL is not available");34}35cv::ocl::Device device = cv::ocl::Device::getDefault();36if (!device.compilerAvailable())37{38throw cvtest::SkipTestException("OpenCL compiler is not available");39}40std::vector<char> program_binary_code;4142cv::String module_name; // empty to disable OpenCL cache4344{ // Generate program binary from OpenCL C source45static const char* opencl_kernel_src =46"__kernel void test_kernel(__global const uchar* src, int src_step, int src_offset,\n"47" __global uchar* dst, int dst_step, int dst_offset, int dst_rows, int dst_cols,\n"48" int c)\n"49"{\n"50" int x = get_global_id(0);\n"51" int y = get_global_id(1);\n"52" if (x < dst_cols && y < dst_rows)\n"53" {\n"54" int src_idx = y * src_step + x + src_offset;\n"55" int dst_idx = y * dst_step + x + dst_offset;\n"56" dst[dst_idx] = src[src_idx] + c;\n"57" }\n"58"}\n";59cv::ocl::ProgramSource src(module_name, "simple", opencl_kernel_src, "");60cv::String errmsg;61cv::ocl::Program program(src, "", errmsg);62ASSERT_TRUE(program.ptr() != NULL);63cv::ocl::Kernel k("test_kernel", program);64EXPECT_FALSE(k.empty());65program.getBinary(program_binary_code);66std::cout << "Program binary size: " << program_binary_code.size() << " bytes" << std::endl;67}6869cv::ocl::Kernel k;7071{ // Load program from binary (without sources)72ASSERT_FALSE(program_binary_code.empty());73cv::ocl::ProgramSource src = cv::ocl::ProgramSource::fromBinary(module_name, "simple_binary", (uchar*)&program_binary_code[0], program_binary_code.size(), "");74cv::String errmsg;75cv::ocl::Program program(src, "", errmsg);76ASSERT_TRUE(program.ptr() != NULL);77k.create("test_kernel", program);78}7980testOpenCLKernel(k);81}828384TEST(OpenCL, support_SPIR_programs)85{86cv::ocl::Context ctx = cv::ocl::Context::getDefault();87if (!ctx.ptr())88{89throw cvtest::SkipTestException("OpenCL is not available");90}91cv::ocl::Device device = cv::ocl::Device::getDefault();92if (!device.isExtensionSupported("cl_khr_spir"))93{94throw cvtest::SkipTestException("'cl_khr_spir' extension is not supported by OpenCL device");95}96std::vector<char> program_binary_code;97cv::String fname = cv::format("test_kernel.spir%d", device.addressBits());98std::string full_path = cvtest::findDataFile(std::string("opencl/") + fname);99100{101std::fstream f(full_path.c_str(), std::ios::in|std::ios::binary);102ASSERT_TRUE(f.is_open());103size_t pos = (size_t)f.tellg();104f.seekg(0, std::fstream::end);105size_t fileSize = (size_t)f.tellg();106std::cout << "Program SPIR size: " << fileSize << " bytes" << std::endl;107f.seekg(pos, std::fstream::beg);108program_binary_code.resize(fileSize);109f.read(&program_binary_code[0], fileSize);110ASSERT_FALSE(f.fail());111}112113cv::String module_name; // empty to disable OpenCL cache114115cv::ocl::Kernel k;116117{ // Load program from SPIR format118ASSERT_FALSE(program_binary_code.empty());119cv::ocl::ProgramSource src = cv::ocl::ProgramSource::fromSPIR(module_name, "simple_spir", (uchar*)&program_binary_code[0], program_binary_code.size(), "");120cv::String errmsg;121cv::ocl::Program program(src, "", errmsg);122ASSERT_TRUE(program.ptr() != NULL);123k.create("test_kernel", program);124}125126testOpenCLKernel(k);127}128129}} // namespace130131132