From eacadf0e734427279f6a76bb2974ff809e1ceba5 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Tue, 24 Sep 2019 13:03:29 +0300 Subject: [PATCH] core(ocl): add flag OPENCV_OPENCL_ENABLE_MEM_USE_HOST_PTR to control CL_MEM_USE_HOST_PTR usage --- modules/core/src/ocl.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp index 7b6bcd7118..22408df723 100644 --- a/modules/core/src/ocl.cpp +++ b/modules/core/src/ocl.cpp @@ -267,6 +267,9 @@ static const String getBuildExtraOptions() return param_buildExtraOptions; } +static const bool CV_OPENCL_ENABLE_MEM_USE_HOST_PTR = utils::getConfigurationParameterBool("OPENCV_OPENCL_ENABLE_MEM_USE_HOST_PTR", true); +static const size_t CV_OPENCL_ALIGNMENT_MEM_USE_HOST_PTR = utils::getConfigurationParameterSizeT("OPENCV_OPENCL_ALIGNMENT_MEM_USE_HOST_PTR", 4); + #endif // HAVE_OPENCL struct UMat2D @@ -4675,6 +4678,9 @@ public: bool allocate(UMatData* u, int accessFlags, UMatUsageFlags usageFlags) const CV_OVERRIDE { +#ifndef HAVE_OPENCL + return false; +#else if(!u) return false; @@ -4749,8 +4755,12 @@ public: #endif { tempUMatFlags = UMatData::TEMP_UMAT; - if (u->origdata == cv::alignPtr(u->origdata, 4) // There are OpenCL runtime issues for less aligned data - && !(u->originalUMatData && u->originalUMatData->handle) // Avoid sharing of host memory between OpenCL buffers + if (CV_OPENCL_ENABLE_MEM_USE_HOST_PTR + // There are OpenCL runtime issues for less aligned data + && (CV_OPENCL_ALIGNMENT_MEM_USE_HOST_PTR != 0 + && u->origdata == cv::alignPtr(u->origdata, (int)CV_OPENCL_ALIGNMENT_MEM_USE_HOST_PTR)) + // Avoid sharing of host memory between OpenCL buffers + && !(u->originalUMatData && u->originalUMatData->handle) ) { handle = clCreateBuffer(ctx_handle, CL_MEM_USE_HOST_PTR|createFlags, @@ -4780,6 +4790,7 @@ public: u->markHostCopyObsolete(true); opencl_allocator_stats.onAllocate(u->size); return true; +#endif // HAVE_OPENCL } /*void sync(UMatData* u) const @@ -4908,7 +4919,7 @@ public: (CL_MAP_READ | CL_MAP_WRITE), 0, u->size, 0, 0, 0, &retval); CV_OCL_CHECK_RESULT(retval, cv::format("clEnqueueMapBuffer(handle=%p, sz=%lld) => %p", (void*)u->handle, (long long int)u->size, data).c_str()); - CV_Assert(u->origdata == data); + CV_Assert(u->origdata == data && "Details: https://github.com/opencv/opencv/issues/6293"); if (u->originalUMatData) { CV_Assert(u->originalUMatData->data == data);