/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // // By downloading, copying, installing or using the software you agree to this license. // If you do not agree to this license, do not download, install, // copy or use the software. // // // License Agreement // For Open Source Computer Vision Library // // Copyright (C) 2013, OpenCV Foundation, all rights reserved. // Third party copyrights are property of their respective owners. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistribution's in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // * The name of the copyright holders may not be used to endorse or promote products // derived from this software without specific prior written permission. // // This software is provided by the copyright holders and contributors "as is" and // any express or implied warranties, including, but not limited to, the implied // warranties of merchantability and fitness for a particular purpose are disclaimed. // In no event shall the OpenCV Foundation or contributors be liable for any direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused // and on any theory of liability, whether in contract, strict liability, // or tort (including negligence or otherwise) arising in any way out of // the use of this software, even if advised of the possibility of such damage. // //M*/ #include "test_precomp.hpp" #include "opencv2/ts/ocl_test.hpp" using namespace cvtest; using namespace testing; using namespace cv; namespace cvtest { namespace ocl { #define UMAT_TEST_SIZES testing::Values(cv::Size(1, 1), cv::Size(1,128), cv::Size(128, 1), \ cv::Size(128, 128), cv::Size(640, 480), cv::Size(751, 373), cv::Size(1200, 1200)) /////////////////////////////// Basic Tests //////////////////////////////// PARAM_TEST_CASE(UMatBasicTests, int, int, Size, bool) { Mat a; UMat ua; int type; int depth; int cn; Size size; bool useRoi; Size roi_size; Rect roi; virtual void SetUp() { depth = GET_PARAM(0); cn = GET_PARAM(1); size = GET_PARAM(2); useRoi = GET_PARAM(3); type = CV_MAKE_TYPE(depth, cn); a = randomMat(size, type, -100, 100); a.copyTo(ua); int roi_shift_x = randomInt(0, size.width-1); int roi_shift_y = randomInt(0, size.height-1); roi_size = Size(size.width - roi_shift_x, size.height - roi_shift_y); roi = Rect(roi_shift_x, roi_shift_y, roi_size.width, roi_size.height); } }; TEST_P(UMatBasicTests, createUMat) { if(useRoi) { ua = UMat(ua, roi); } int dims = randomInt(2,6); int _sz[CV_MAX_DIM]; for( int i = 0; i GetMat /////////////////////////////////////////////////////////////////// PARAM_TEST_CASE(getUMat, int, int, Size, bool) { int type; Size size; virtual void SetUp() { int depth = GET_PARAM(0); int cn = GET_PARAM(1); size = GET_PARAM(2); useOpenCL = GET_PARAM(3); type = CV_MAKE_TYPE(depth, cn); isOpenCL_enabled = cv::ocl::useOpenCL(); cv::ocl::setUseOpenCL(useOpenCL); } virtual void TearDown() { cv::ocl::setUseOpenCL(isOpenCL_enabled); } // UMat created from user allocated host memory (USE_HOST_PTR) void custom_ptr_test(size_t align_base, size_t align_offset) { void* pData_allocated = new unsigned char [size.area() * CV_ELEM_SIZE(type) + (align_base + align_offset)]; void* pData = (char*)alignPtr(pData_allocated, (int)align_base) + align_offset; size_t step = size.width * CV_ELEM_SIZE(type); { Mat m = Mat(size, type, pData, step); m.setTo(cv::Scalar::all(2)); UMat u = m.getUMat(ACCESS_RW); cv::add(u, cv::Scalar::all(2), u); Mat d = u.getMat(ACCESS_READ); Mat expected(m.size(), m.type(), cv::Scalar::all(4)); double norm = cvtest::norm(d, expected, NORM_INF); EXPECT_EQ(0, norm); } delete[] (unsigned char*)pData_allocated; } private: bool useOpenCL; bool isOpenCL_enabled; }; TEST_P(getUMat, custom_ptr_align_4Kb) { custom_ptr_test(4096, 0); } TEST_P(getUMat, custom_ptr_align_64b) { custom_ptr_test(4096, 64); } TEST_P(getUMat, custom_ptr_align_none) { custom_ptr_test(4096, cv::alignSize(CV_ELEM_SIZE(type), 4)); } TEST_P(getUMat, self_allocated) { Mat m = Mat(size, type); m.setTo(cv::Scalar::all(2)); UMat u = m.getUMat(ACCESS_RW); cv::add(u, cv::Scalar::all(2), u); Mat d = u.getMat(ACCESS_READ); Mat expected(m.size(), m.type(), cv::Scalar::all(4)); double norm = cvtest::norm(d, expected, NORM_INF); EXPECT_EQ(0, norm); } INSTANTIATE_TEST_CASE_P(UMat, getUMat, Combine( Values(CV_8U, CV_64F), // depth Values(1, 3), // channels Values(cv::Size(1, 1), cv::Size(255, 255), cv::Size(256, 256)), // Size Bool() // useOpenCL )); ///////////////////////////////////////////////////////////////// OpenCL //////////////////////////////////////////////////////////////////////////// #ifdef HAVE_OPENCL TEST(UMat, BufferPoolGrowing) { #ifdef _DEBUG const int ITERATIONS = 100; #else const int ITERATIONS = 200; #endif const Size sz(1920, 1080); BufferPoolController* c = cv::ocl::getOpenCLAllocator()->getBufferPoolController(); if (c) { size_t oldMaxReservedSize = c->getMaxReservedSize(); c->freeAllReservedBuffers(); c->setMaxReservedSize(sz.area() * 10); for (int i = 0; i < ITERATIONS; i++) { UMat um(Size(sz.width + i, sz.height + i), CV_8UC1); UMat um2(Size(sz.width + 2 * i, sz.height + 2 * i), CV_8UC1); } c->setMaxReservedSize(oldMaxReservedSize); c->freeAllReservedBuffers(); } else std::cout << "Skipped, no OpenCL" << std::endl; } #endif class CV_UMatTest : public cvtest::BaseTest { public: CV_UMatTest() {} ~CV_UMatTest() {} protected: void run(int); struct test_excep { test_excep(const string& _s=string("")) : s(_s) { } string s; }; bool TestUMat(); void checkDiff(const Mat& m1, const Mat& m2, const string& s) { if (cvtest::norm(m1, m2, NORM_INF) != 0) throw test_excep(s); } void checkDiffF(const Mat& m1, const Mat& m2, const string& s) { if (cvtest::norm(m1, m2, NORM_INF) > 1e-5) throw test_excep(s); } }; #define STR(a) STR2(a) #define STR2(a) #a #define CHECK_DIFF(a, b) checkDiff(a, b, "(" #a ") != (" #b ") at l." STR(__LINE__)) #define CHECK_DIFF_FLT(a, b) checkDiffF(a, b, "(" #a ") !=(eps) (" #b ") at l." STR(__LINE__)) bool CV_UMatTest::TestUMat() { try { Mat a(100, 100, CV_16SC2), b, c; randu(a, Scalar::all(-100), Scalar::all(100)); Rect roi(1, 3, 5, 4); Mat ra(a, roi), rb, rc, rc0; UMat ua, ura, ub, urb, uc, urc; a.copyTo(ua); ua.copyTo(b); CHECK_DIFF(a, b); ura = ua(roi); ura.copyTo(rb); CHECK_DIFF(ra, rb); ra += Scalar::all(1.f); { Mat temp = ura.getMat(ACCESS_RW); temp += Scalar::all(1.f); } ra.copyTo(rb); CHECK_DIFF(ra, rb); b = a.clone(); ra = a(roi); rb = b(roi); randu(b, Scalar::all(-100), Scalar::all(100)); b.copyTo(ub); urb = ub(roi); /*std::cout << "==============================================\nbefore op (CPU):\n"; std::cout << "ra: " << ra << std::endl; std::cout << "rb: " << rb << std::endl;*/ ra.copyTo(ura); rb.copyTo(urb); ra.release(); rb.release(); ura.copyTo(ra); urb.copyTo(rb); /*std::cout << "==============================================\nbefore op (GPU):\n"; std::cout << "ra: " << ra << std::endl; std::cout << "rb: " << rb << std::endl;*/ cv::max(ra, rb, rc); cv::max(ura, urb, urc); urc.copyTo(rc0); /*std::cout << "==============================================\nafter op:\n"; std::cout << "rc: " << rc << std::endl; std::cout << "rc0: " << rc0 << std::endl;*/ CHECK_DIFF(rc0, rc); { UMat tmp = rc0.getUMat(ACCESS_WRITE); cv::max(ura, urb, tmp); } CHECK_DIFF(rc0, rc); ura.copyTo(urc); cv::max(urc, urb, urc); urc.copyTo(rc0); CHECK_DIFF(rc0, rc); rc = ra ^ rb; cv::bitwise_xor(ura, urb, urc); urc.copyTo(rc0); /*std::cout << "==============================================\nafter op:\n"; std::cout << "ra: " << rc0 << std::endl; std::cout << "rc: " << rc << std::endl;*/ CHECK_DIFF(rc0, rc); rc = ra + rb; cv::add(ura, urb, urc); urc.copyTo(rc0); CHECK_DIFF(rc0, rc); cv::subtract(ra, Scalar::all(5), rc); cv::subtract(ura, Scalar::all(5), urc); urc.copyTo(rc0); CHECK_DIFF(rc0, rc); } catch (const test_excep& e) { ts->printf(cvtest::TS::LOG, "%s\n", e.s.c_str()); ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH); return false; } return true; } void CV_UMatTest::run( int /* start_from */) { printf("Use OpenCL: %s\nHave OpenCL: %s\n", cv::ocl::useOpenCL() ? "TRUE" : "FALSE", cv::ocl::haveOpenCL() ? "TRUE" : "FALSE" ); if (!TestUMat()) return; ts->set_failed_test_info(cvtest::TS::OK); } TEST(Core_UMat, base) { CV_UMatTest test; test.safe_run(); } TEST(Core_UMat, getUMat) { { int a[3] = { 1, 2, 3 }; Mat m = Mat(1, 1, CV_32SC3, a); UMat u = m.getUMat(ACCESS_READ); EXPECT_NE((void*)NULL, u.u); } { Mat m(10, 10, CV_8UC1), ref; for (int y = 0; y < m.rows; ++y) { uchar * const ptr = m.ptr(y); for (int x = 0; x < m.cols; ++x) ptr[x] = (uchar)(x + y * 2); } ref = m.clone(); Rect r(1, 1, 8, 8); ref(r).setTo(17); { UMat um = m(r).getUMat(ACCESS_WRITE); um.setTo(17); } double err = cvtest::norm(m, ref, NORM_INF); if (err > 0) { std::cout << "m: " << std::endl << m << std::endl; std::cout << "ref: " << std::endl << ref << std::endl; } EXPECT_EQ(0., err); } } TEST(UMat, Sync) { UMat um(10, 10, CV_8UC1); { Mat m = um.getMat(ACCESS_WRITE); m.setTo(cv::Scalar::all(17)); } um.setTo(cv::Scalar::all(19)); EXPECT_EQ(0, cvtest::norm(um.getMat(ACCESS_READ), cv::Mat(um.size(), um.type(), 19), NORM_INF)); } TEST(UMat, SyncTemp) { Mat m(10, 10, CV_8UC1); { UMat um = m.getUMat(ACCESS_WRITE); { Mat m2 = um.getMat(ACCESS_WRITE); m2.setTo(cv::Scalar::all(17)); } um.setTo(cv::Scalar::all(19)); EXPECT_EQ(0, cvtest::norm(um.getMat(ACCESS_READ), cv::Mat(um.size(), um.type(), 19), NORM_INF)); } } TEST(UMat, CopyToIfDeviceCopyIsObsolete) { UMat um(7, 2, CV_8UC1); Mat m(um.size(), um.type()); m.setTo(Scalar::all(0)); { // make obsolete device copy of UMat Mat temp = um.getMat(ACCESS_WRITE); temp.setTo(Scalar::all(10)); } m.copyTo(um); um.setTo(Scalar::all(17)); EXPECT_EQ(0, cvtest::norm(um.getMat(ACCESS_READ), Mat(um.size(), um.type(), 17), NORM_INF)); } TEST(UMat, setOpenCL) { #ifndef HAVE_OPENCL return; // test skipped #else // save the current state bool useOCL = cv::ocl::useOpenCL(); Mat m = (Mat_(3,3)<<0,1,2,3,4,5,6,7,8); cv::ocl::setUseOpenCL(true); UMat um1; m.copyTo(um1); cv::ocl::setUseOpenCL(false); UMat um2; m.copyTo(um2); cv::ocl::setUseOpenCL(true); countNonZero(um1); countNonZero(um2); um1.copyTo(um2); EXPECT_MAT_NEAR(um1, um2, 0); EXPECT_MAT_NEAR(um1, m, 0); um2.copyTo(um1); EXPECT_MAT_NEAR(um1, m, 0); EXPECT_MAT_NEAR(um1, um2, 0); cv::ocl::setUseOpenCL(false); countNonZero(um1); countNonZero(um2); um1.copyTo(um2); EXPECT_MAT_NEAR(um1, um2, 0); EXPECT_MAT_NEAR(um1, m, 0); um2.copyTo(um1); EXPECT_MAT_NEAR(um1, um2, 0); EXPECT_MAT_NEAR(um1, m, 0); // reset state to the previous one cv::ocl::setUseOpenCL(useOCL); #endif } TEST(UMat, ReadBufferRect) { UMat m(1, 10000, CV_32FC2, Scalar::all(-1)); Mat t(1, 9000, CV_32FC2, Scalar::all(-200)), t2(1, 9000, CV_32FC2, Scalar::all(-1)); m.colRange(0, 9000).copyTo(t); EXPECT_MAT_NEAR(t, t2, 0); } // Use iGPU or OPENCV_OPENCL_DEVICE=:CPU: to catch problem TEST(UMat, synchronization_map_unmap) { class TestParallelLoopBody : public cv::ParallelLoopBody { UMat u_; public: TestParallelLoopBody(const UMat& u) : u_(u) { } void operator() (const cv::Range& range) const { printf("range: %d, %d -- begin\n", range.start, range.end); for (int i = 0; i < 10; i++) { printf("%d: %d map...\n", range.start, i); Mat m = u_.getMat(cv::ACCESS_READ); printf("%d: %d unmap...\n", range.start, i); m.release(); } printf("range: %d, %d -- end\n", range.start, range.end); } }; try { UMat u(1000, 1000, CV_32FC1, Scalar::all(0)); parallel_for_(cv::Range(0, 2), TestParallelLoopBody(u)); } catch (const cv::Exception& e) { FAIL() << "Exception: " << e.what(); ADD_FAILURE(); } catch (...) { FAIL() << "Exception!"; } } TEST(UMat, async_unmap) { for (int i = 0; i < 20; i++) { try { Mat m = Mat(1000, 1000, CV_8UC1, Scalar::all(0)); UMat u = m.getUMat(ACCESS_READ); UMat dst; add(u, Scalar::all(0), dst); // start async operation u.release(); m.release(); } catch (const cv::Exception& e) { printf("i = %d... %s\n", i, e.what()); ADD_FAILURE(); } catch (...) { printf("i = %d...\n", i); ADD_FAILURE(); } } } TEST(UMat, unmap_in_class) { class Logic { public: Logic() {} void processData(InputArray input) { Mat m = input.getMat(); { Mat dst; m.convertTo(dst, CV_32FC1); // some additional CPU-based per-pixel processing into dst intermediateResult = dst.getUMat(ACCESS_READ); // this violates lifetime of base(dst) / derived (intermediateResult) objects. Use copyTo? std::cout << "data processed..." << std::endl; } // problem is here: dst::~Mat() std::cout << "leave ProcessData()" << std::endl; } UMat getResult() const { return intermediateResult; } protected: UMat intermediateResult; }; try { Mat m = Mat(1000, 1000, CV_8UC1, Scalar::all(0)); Logic l; l.processData(m); UMat result = l.getResult(); } catch (const cv::Exception& e) { printf("exception... %s\n", e.what()); ADD_FAILURE(); } catch (...) { printf("exception... \n"); ADD_FAILURE(); } } TEST(UMat, map_unmap_counting) { if (!cv::ocl::useOpenCL()) { std::cout << "OpenCL is not enabled. Skip test" << std::endl; return; } std::cout << "Host memory: " << cv::ocl::Device::getDefault().hostUnifiedMemory() << std::endl; Mat m(Size(10, 10), CV_8UC1, Scalar::all(0)); UMat um = m.getUMat(ACCESS_RW); { Mat d1 = um.getMat(ACCESS_RW); Mat d2 = um.getMat(ACCESS_RW); d1.release(); } void* h = NULL; EXPECT_NO_THROW(h = um.handle(ACCESS_RW)); std::cout << "Handle: " << h << std::endl; } ///////////// oclCleanupCallback threadsafe check (#5062) ///////////////////// // Case 1: reuse of old src Mat in OCL pipe. Hard to catch! OCL_TEST(UMat, DISABLED_OCL_ThreadSafe_CleanupCallback_1_VeryLongTest) { if (!cv::ocl::useOpenCL()) { std::cout << "OpenCL is not enabled. Skip test" << std::endl; return; } for (int j = 0; j < 100; j++) { const Size srcSize(320, 240); const int type = CV_8UC1; const int dtype = CV_16UC1; Mat src(srcSize, type, Scalar::all(0)); Mat dst_ref(srcSize, dtype); // Generate reference data as additional check OCL_OFF(src.convertTo(dst_ref, dtype)); cv::ocl::setUseOpenCL(true); // restore OpenCL state UMat dst(srcSize, dtype); // Use multiple iterations to increase chance of data race catching for(int k = 0; k < 10000; k++) { UMat tmpUMat = src.getUMat(ACCESS_RW); tmpUMat.convertTo(dst, dtype); ::cv::ocl::finish(); // force kernel to complete to start cleanup sooner } EXPECT_MAT_NEAR(dst_ref, dst, 1); printf(".\n"); fflush(stdout); } } // Case 2: concurent deallocation of UMatData between UMat and Mat deallocators. Hard to catch! OCL_TEST(UMat, DISABLED_OCL_ThreadSafe_CleanupCallback_2_VeryLongTest) { if (!cv::ocl::useOpenCL()) { std::cout << "OpenCL is not enabled. Skip test" << std::endl; return; } for (int j = 0; j < 100; j++) { const Size srcSize(320, 240); const int type = CV_8UC1; const int dtype = CV_16UC1; // This test is only relevant for OCL UMat dst(srcSize, dtype); // Use multiple iterations to increase chance of data race catching for(int k = 0; k < 10000; k++) { Mat src(srcSize, type, Scalar::all(0)); // Declare src inside loop now to catch its destruction on stack { UMat tmpUMat = src.getUMat(ACCESS_RW); tmpUMat.convertTo(dst, dtype); } ::cv::ocl::finish(); // force kernel to complete to start cleanup sooner } printf(".\n"); fflush(stdout); } } TEST(UMat, DISABLED_Test_same_behaviour_read_and_read) { bool exceptionDetected = false; try { UMat u(Size(10, 10), CV_8UC1, Scalar::all(0)); Mat m = u.getMat(ACCESS_READ); UMat dst; add(u, Scalar::all(1), dst); } catch (...) { exceptionDetected = true; } ASSERT_FALSE(exceptionDetected); // no data race, 2+ reads are valid } // VP: this test (and probably others from same_behaviour series) is not valid in my opinion. TEST(UMat, DISABLED_Test_same_behaviour_read_and_write) { bool exceptionDetected = false; try { UMat u(Size(10, 10), CV_8UC1, Scalar::all(0)); Mat m = u.getMat(ACCESS_READ); add(u, Scalar::all(1), u); } catch (...) { exceptionDetected = true; } ASSERT_TRUE(exceptionDetected); // data race } TEST(UMat, DISABLED_Test_same_behaviour_write_and_read) { bool exceptionDetected = false; try { UMat u(Size(10, 10), CV_8UC1, Scalar::all(0)); Mat m = u.getMat(ACCESS_WRITE); UMat dst; add(u, Scalar::all(1), dst); } catch (...) { exceptionDetected = true; } ASSERT_TRUE(exceptionDetected); // data race } TEST(UMat, DISABLED_Test_same_behaviour_write_and_write) { bool exceptionDetected = false; try { UMat u(Size(10, 10), CV_8UC1, Scalar::all(0)); Mat m = u.getMat(ACCESS_WRITE); add(u, Scalar::all(1), u); } catch (...) { exceptionDetected = true; } ASSERT_TRUE(exceptionDetected); // data race } TEST(UMat, mat_umat_sync) { UMat u(10, 10, CV_8UC1, Scalar(1)); { Mat m = u.getMat(ACCESS_RW).reshape(1); m.setTo(Scalar(255)); } UMat uDiff; compare(u, 255, uDiff, CMP_NE); ASSERT_EQ(0, countNonZero(uDiff)); } TEST(UMat, testTempObjects_UMat) { UMat u(10, 10, CV_8UC1, Scalar(1)); { UMat u2 = u.getMat(ACCESS_RW).getUMat(ACCESS_RW); u2.setTo(Scalar(255)); } UMat uDiff; compare(u, 255, uDiff, CMP_NE); ASSERT_EQ(0, countNonZero(uDiff)); } TEST(UMat, testTempObjects_Mat) { Mat m(10, 10, CV_8UC1, Scalar(1)); { Mat m2; ASSERT_ANY_THROW({ // Below is unwrapped version of this invalid expression: // m2 = m.getUMat(ACCESS_RW).getMat(ACCESS_RW) UMat u = m.getUMat(ACCESS_RW); m2 = u.getMat(ACCESS_RW); u.release(); }); } } TEST(UMat, testWrongLifetime_UMat) { UMat u(10, 10, CV_8UC1, Scalar(1)); { UMat u2 = u.getMat(ACCESS_RW).getUMat(ACCESS_RW); u.release(); // base object u2.release(); // derived object, should show warning message } } TEST(UMat, testWrongLifetime_Mat) { Mat m(10, 10, CV_8UC1, Scalar(1)); { UMat u = m.getUMat(ACCESS_RW); Mat m2 = u.getMat(ACCESS_RW); m.release(); // base object m2.release(); // map of derived object u.release(); // derived object, should show warning message } } TEST(UMat, DISABLED_regression_5991) { int sz[] = {2,3,2}; UMat mat(3, sz, CV_32F, Scalar(1)); ASSERT_NO_THROW(mat.convertTo(mat, CV_8U)); EXPECT_EQ(sz[0], mat.size[0]); EXPECT_EQ(sz[1], mat.size[1]); EXPECT_EQ(sz[2], mat.size[2]); EXPECT_EQ(0, cvtest::norm(mat.getMat(ACCESS_READ), Mat(3, sz, CV_8U, Scalar(1)), NORM_INF)); } } } // namespace cvtest::ocl