diff --git a/modules/core/src/arithm.cpp b/modules/core/src/arithm.cpp index 1752487aae..229a85eea0 100644 --- a/modules/core/src/arithm.cpp +++ b/modules/core/src/arithm.cpp @@ -638,7 +638,8 @@ static void arithm_op(ew::TOp op, InputArray src1, InputArray src2, OutputArray // broadcast, cn>4 masked, ...) - then the CPU engine below handles it (mapping the UMat via getMat). // oclop < 0 is a sentinel: the caller (e.g. cv::compare) already ran its own OpenCL path, so this // helper only runs the CPU engine (a UMat operand is mapped via getMat). - if (oclop >= 0 && (src1.isUMat() || src2.isUMat() || dst.isUMat())) + if (oclop >= 0 && cv::ocl::isOpenCLActivated() && + (src1.isUMat() || src2.isUMat() || dst.isUMat())) { double abg[3] = { params[0], params[1], params[2] }, scale = params[0]; void* usrdata = (op == ew::OP_ADDW) ? (void*)abg : (muldiv ? (void*)&scale : nullptr); diff --git a/modules/core/src/dxt.cpp b/modules/core/src/dxt.cpp index 59a4c793aa..6ebd399204 100644 --- a/modules/core/src/dxt.cpp +++ b/modules/core/src/dxt.cpp @@ -2612,6 +2612,8 @@ static bool ocl_dft_amdfft(InputArray _src, OutputArray _dst, int flags) } UMat src = _src.getUMat(), dst = _dst.getUMat(); + if (!ocl::internal::isCLBuffer(src) || !ocl::internal::isCLBuffer(dst)) + return false; bool inplace = src.u == dst.u; clfftPlanHandle plHandle = PlanCache::getInstance(). diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index 7757bdf3f6..da63dc8e9a 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -1145,9 +1145,7 @@ void Mat::fit(Size _sz, int _type) void Mat::fitSameSize(InputArray m, int _type) { - int _sizes[CV_MAX_DIM]; - int _dims = m.sizend(_sizes); - fit(_dims, _sizes, _type); + fit(m.shape(), _type); } void Mat::addref() diff --git a/modules/core/src/matrix_wrap.cpp b/modules/core/src/matrix_wrap.cpp index e99adde1b5..8a0d160eac 100644 --- a/modules/core/src/matrix_wrap.cpp +++ b/modules/core/src/matrix_wrap.cpp @@ -1941,6 +1941,47 @@ void _OutputArray::createSameSize(const _InputArray& arr, int mtype) const create(arr.shape(), mtype); } +void _OutputArray::fit(Size _sz, int mtype, int i, + bool allowTransposed, _OutputArray::DepthMask fixedDepthMask) const +{ + int sizes[] = {_sz.height, _sz.width}; + fit(2, sizes, mtype, i, allowTransposed, fixedDepthMask); +} + +void _OutputArray::fit(int rows, int cols, int mtype, int i, + bool allowTransposed, _OutputArray::DepthMask fixedDepthMask) const +{ + int sizes[] = {rows, cols}; + fit(2, sizes, mtype, i, allowTransposed, fixedDepthMask); +} + +void _OutputArray::fit(const MatShape& shape, int mtype, int i, + bool allowTransposed, _OutputArray::DepthMask fixedDepthMask) const +{ + if (shape.dims < 0) { + release(); + return; + } + fit(shape.dims, shape.p, mtype, i, allowTransposed, fixedDepthMask); + + // fit(dims, sizes) resets the layout, so carry it over the same way Mat::fit(const MatShape&) does + _InputArray::KindFlag k = kind(); + if ((k == MAT && i < 0) || (k == STD_VECTOR_MAT && i >= 0)) { + MatSize& msize = getMatRef(i).size; + msize.layout = shape.layout; + msize.C = shape.C; + } else if ((k == UMAT && i < 0) || (k == STD_VECTOR_UMAT && i >= 0)) { + MatSize& msize = getUMatRef(i).size; + msize.layout = shape.layout; + msize.C = shape.C; + } +} + +void _OutputArray::fitSameSize(const _InputArray& arr, int mtype) const +{ + fit(arr.shape(), mtype); +} + void _OutputArray::fit(int d, const int* sizes, int mtype, int i, bool allowTransposed, _OutputArray::DepthMask fixedDepthMask) const { diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp index 00c4d51d1e..063075418a 100644 --- a/modules/core/src/ocl.cpp +++ b/modules/core/src/ocl.cpp @@ -3735,6 +3735,13 @@ int Kernel::set(int i, const UMat& m) return set(i, KernelArg(KernelArg::READ_WRITE, (UMat*)&m)); } +// a CUDA-backed UMat carries a device pointer in u->handle, not a cl_mem +static bool isCudaBacked(const UMatData* u) +{ + const MatAllocator* cudaAllocator = cv::cuda::getCudaAllocator(); + return u && cudaAllocator && u->currAllocator == cudaAllocator; +} + int Kernel::set(int i, const KernelArg& arg) { if( !p || !p->handle ) @@ -3760,6 +3767,16 @@ int Kernel::set(int i, const KernelArg& arg) CV_OCL_DBG_CHECK_RESULT(status, cv::format("clSetKernelArg('%s', arg_index=%d, cl_mem=NULL)", p->name.c_str(), (int)i).c_str()); return i + 1; } + // a CUDA-backed UMat has no cl_mem; decline so the caller falls back to CPU + if (isCudaBacked(arg.m->u)) + { + CV_LOG_DEBUG(NULL, cv::format("OpenCL: Kernel(%s)::set(arg_index=%d): UMat is backed by the CUDA allocator, declining", + p->name.c_str(), (int)i)); + p->cleanupUMats(); + p->release(); + p = 0; + return -1; + } cl_mem h = (cl_mem)arg.m->handle(accessFlags); if (!h) @@ -7598,6 +7615,8 @@ bool internal::isPerformanceCheckBypassed() bool internal::isCLBuffer(UMat& u) { + if (isCudaBacked(u.u)) + return false; void* h = u.handle(ACCESS_RW); if (!h) return true; diff --git a/modules/core/src/umatrix.cpp b/modules/core/src/umatrix.cpp index 010e60409a..cef32652a6 100644 --- a/modules/core/src/umatrix.cpp +++ b/modules/core/src/umatrix.cpp @@ -733,9 +733,7 @@ void UMat::fit(Size _sz, int _type, UMatUsageFlags _usageFlags) void UMat::fitSameSize(InputArray m, int _type, UMatUsageFlags _usageFlags) { - int _sizes[CV_MAX_DIM]; - int _dims = m.sizend(_sizes); - fit(_dims, _sizes, _type, _usageFlags); + fit(m.shape(), _type, _usageFlags); } void UMat::copySize(const UMat& m) @@ -1289,6 +1287,22 @@ void UMat::copyTo(OutputArray _dst) const u->currAllocator->copy(u, dst.u, d, sz, srcofs, step.p, dstofs, dst.step.p, false); return; } + + // different allocators: never map a device destination, upload into it instead + // (through a host Mat when both sides are device memory) + MatAllocator* hostAllocator = Mat::getDefaultAllocator(); + if (dst.u->currAllocator != hostAllocator) + { + if (u->currAllocator == hostAllocator) + getMat(ACCESS_READ).copyTo(_dst); + else + { + Mat tmp; + copyTo(tmp); + tmp.copyTo(_dst); + } + return; + } } Mat dst = _dst.getMat(); diff --git a/modules/core/test/test_cuda_umat.cpp b/modules/core/test/test_cuda_umat.cpp index 8445cde9d2..1b44747572 100644 --- a/modules/core/test/test_cuda_umat.cpp +++ b/modules/core/test/test_cuda_umat.cpp @@ -226,6 +226,58 @@ TEST(Core_CudaUMat, op_addWeighted_host_fallback) EXPECT_LE(cvtest::norm(got, expected, NORM_INF), 1e-4); } +#ifdef HAVE_OPENCL +// turns OpenCL on for one test and restores the previous state afterwards +struct WithOpenCL +{ + bool prev; + WithOpenCL() : prev(cv::ocl::useOpenCL()) { cv::ocl::setUseOpenCL(true); } + ~WithOpenCL() { cv::ocl::setUseOpenCL(prev); } +}; + +// with OpenCL on, the OpenCL kernel path declines CUDA UMats and the op finishes on the CPU +TEST(Core_CudaUMat, op_opencl_on_falls_back) +{ + skipIfNoCudaDevice(); + WithOpenCL guard; + if (!cv::ocl::useOpenCL()) + throw SkipTestException("OpenCL is not available"); + + Mat a(50, 40, CV_32FC1), b(50, 40, CV_32FC1); + randu(a, 0, 10); randu(b, 0, 10); + + UMat ua = makeCudaUMat(), ub = makeCudaUMat(), usum = makeCudaUMat(); + a.copyTo(ua); b.copyTo(ub); + cv::add(ua, ub, usum); + + Mat sum; + usum.copyTo(sum); + EXPECT_LE(cvtest::norm(sum, a + b, NORM_INF), 1e-5); +} + +// device to device across allocators (CUDA -> OpenCL -> CUDA) goes through a host copy +TEST(Core_CudaUMat, copy_cuda_opencl_cuda) +{ + skipIfNoCudaDevice(); + WithOpenCL guard; + if (!cv::ocl::useOpenCL()) + throw SkipTestException("OpenCL is not available"); + + Mat src(30, 20, CV_32FC1); + randu(src, 0, 10); + + UMat cu = makeCudaUMat(), cl, cu2 = makeCudaUMat(); + src.copyTo(cu); + cu.copyTo(cl); + EXPECT_EQ(cv::ocl::getOpenCLAllocator(), cl.u->currAllocator); + cl.copyTo(cu2); + + Mat back; + cu2.copyTo(back); + EXPECT_EQ(0.0, cvtest::norm(src, back, NORM_INF)); +} +#endif // HAVE_OPENCL + }} // namespace #endif // HAVE_CUDA diff --git a/modules/core/test/test_mat.cpp b/modules/core/test/test_mat.cpp index c28c863a9c..3ac5464433 100644 --- a/modules/core/test/test_mat.cpp +++ b/modules/core/test/test_mat.cpp @@ -3240,4 +3240,26 @@ TEST(Core_InputOutputArray, std_vector_vector) EXPECT_EQ(0., err2); } +// fitSameSize carries the layout; MatShape::operator== ignores it, so layout and C are checked directly +TEST(Core_Mat, fitSameSize_keeps_layout) +{ + const MatShape blk({2, 2, 5, 5, 8}, DATA_LAYOUT_BLOCK, 12); + Mat src; + src.fit(blk, CV_32F); + + Mat m, om; + UMat u, ou; + m.fitSameSize(src, CV_32F); + u.fitSameSize(src, CV_32F); + _OutputArray(om).fitSameSize(src, CV_32F); + _OutputArray(ou).fitSameSize(src, CV_32F); + + for (const MatShape& s : {m.shape(), u.shape(), om.shape(), ou.shape()}) + { + EXPECT_EQ(blk, s); + EXPECT_EQ(blk.layout, s.layout); + EXPECT_EQ(blk.C, s.C); + } +} + }} // namespace diff --git a/modules/core/test/test_umat.cpp b/modules/core/test/test_umat.cpp index 532a4e5400..8a06c77836 100644 --- a/modules/core/test/test_umat.cpp +++ b/modules/core/test/test_umat.cpp @@ -1472,4 +1472,23 @@ TEST(UMat, copy_scalar) EXPECT_EQ(5.f, m2.at(0)); } +// a host UMat copied into a UMat of another allocator is uploaded into it +TEST(UMat, copyTo_across_allocators) +{ + Mat src(20, 30, CV_32FC3); + randu(src, 0, 10); + + UMat host; + host.allocator = Mat::getDefaultAllocator(); + src.copyTo(host); + + UMat dev; + dev.fitSameSize(host, host.type()); + host.copyTo(dev); + + Mat back; + dev.copyTo(back); + EXPECT_EQ(0, cvtest::norm(src, back, NORM_INF)); +} + } } // namespace opencv_test::ocl