1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 07:43:03 +04:00

Merge remote-tracking branch 'origin/2.4' into merge-2.4

Conflicts:
	.gitignore
	modules/contrib/src/detection_based_tracker.cpp
	modules/core/include/opencv2/core/core.hpp
	modules/core/include/opencv2/core/internal.hpp
	modules/core/src/gpumat.cpp
	modules/core/src/opengl.cpp
	modules/gpu/src/cuda/safe_call.hpp
	modules/highgui/src/cap.cpp
	modules/imgproc/include/opencv2/imgproc/imgproc.hpp
	modules/ocl/doc/image_processing.rst
	modules/ocl/include/opencv2/ocl/ocl.hpp
	modules/ocl/perf/perf_haar.cpp
	modules/ocl/src/haar.cpp
	modules/ocl/src/imgproc.cpp
	modules/ocl/src/kmeans.cpp
	modules/ocl/src/svm.cpp
	modules/ocl/test/test_objdetect.cpp
	samples/ocl/adaptive_bilateral_filter.cpp
This commit is contained in:
Roman Donchenko
2013-11-11 16:55:36 +04:00
59 changed files with 961 additions and 755 deletions
+72 -40
View File
@@ -109,12 +109,15 @@ cl_mem openCLCreateBuffer(Context *ctx, size_t flag , size_t size)
return buffer;
}
#define MEMORY_CORRUPTION_GUARD
#ifdef MEMORY_CORRUPTION_GUARD
//#define CHECK_MEMORY_CORRUPTION
#ifdef CHECK_MEMORY_CORRUPTION
//#define CHECK_MEMORY_CORRUPTION_PRINT_ERROR
#define CHECK_MEMORY_CORRUPTION_PRINT_ERROR
#define CHECK_MEMORY_CORRUPTION_RAISE_ERROR
static const int __memory_corruption_check_bytes = 1024*1024;
static const int __memory_corruption_guard_bytes = 64*1024;
#ifdef CHECK_MEMORY_CORRUPTION
static const int __memory_corruption_check_pattern = 0x14326547; // change pattern for sizeof(int)==8
#endif
struct CheckBuffers
{
cl_mem mainBuffer;
@@ -128,7 +131,7 @@ struct CheckBuffers
CheckBuffers(cl_mem _mainBuffer, size_t _size, size_t _widthInBytes, size_t _height)
: mainBuffer(_mainBuffer), size(_size), widthInBytes(_widthInBytes), height(_height)
{
// notihng
// nothing
}
};
static std::map<cl_mem, CheckBuffers> __check_buffers;
@@ -145,32 +148,52 @@ void openCLMallocPitchEx(Context *ctx, void **dev_ptr, size_t *pitch,
{
cl_int status;
size_t size = widthInBytes * height;
#ifndef CHECK_MEMORY_CORRUPTION
*dev_ptr = clCreateBuffer(getClContext(ctx), gDevMemRWValueMap[rw_type]|gDevMemTypeValueMap[mem_type],
size, 0, &status);
openCLVerifyCall(status);
bool useSubBuffers =
#ifndef MEMORY_CORRUPTION_GUARD
false;
#else
size_t allocSize = size + __memory_corruption_check_bytes * 2;
cl_mem mainBuffer = clCreateBuffer(getClContext(ctx), gDevMemRWValueMap[rw_type]|gDevMemTypeValueMap[mem_type],
allocSize, 0, &status);
openCLVerifyCall(status);
cl_buffer_region r = {__memory_corruption_check_bytes, size};
*dev_ptr = clCreateSubBuffer(mainBuffer,
gDevMemRWValueMap[rw_type]|gDevMemTypeValueMap[mem_type],
CL_BUFFER_CREATE_TYPE_REGION, &r,
&status);
openCLVerifyCall(status);
std::vector<int> tmp(__memory_corruption_check_bytes / sizeof(int),
__memory_corruption_check_pattern);
CV_Assert(tmp.size() * sizeof(int) == __memory_corruption_check_bytes);
openCLVerifyCall(clEnqueueWriteBuffer(getClCommandQueue(ctx),
mainBuffer, CL_TRUE, 0, __memory_corruption_check_bytes, &tmp[0],
0, NULL, NULL));
openCLVerifyCall(clEnqueueWriteBuffer(getClCommandQueue(ctx),
mainBuffer, CL_TRUE, __memory_corruption_check_bytes + size, __memory_corruption_check_bytes, &tmp[0],
0, NULL, NULL));
CheckBuffers data(mainBuffer, size, widthInBytes, height);
__check_buffers.insert(std::pair<cl_mem, CheckBuffers>((cl_mem)*dev_ptr, data));
true;
#endif
const DeviceInfo& devInfo = ctx->getDeviceInfo();
if (useSubBuffers && devInfo.isIntelDevice)
{
useSubBuffers = false; // TODO FIXIT We observe memory leaks then we working with sub-buffers
// on the CPU device of Intel OpenCL SDK (Linux). We will investigate this later.
}
if (!useSubBuffers)
{
*dev_ptr = clCreateBuffer(getClContext(ctx), gDevMemRWValueMap[rw_type]|gDevMemTypeValueMap[mem_type],
size, 0, &status);
openCLVerifyCall(status);
}
#ifdef MEMORY_CORRUPTION_GUARD
else
{
size_t allocSize = size + __memory_corruption_guard_bytes * 2;
cl_mem mainBuffer = clCreateBuffer(getClContext(ctx), gDevMemRWValueMap[rw_type]|gDevMemTypeValueMap[mem_type],
allocSize, 0, &status);
openCLVerifyCall(status);
cl_buffer_region r = {__memory_corruption_guard_bytes, size};
*dev_ptr = clCreateSubBuffer(mainBuffer,
gDevMemRWValueMap[rw_type]|gDevMemTypeValueMap[mem_type],
CL_BUFFER_CREATE_TYPE_REGION, &r,
&status);
openCLVerifyCall(status);
#ifdef CHECK_MEMORY_CORRUPTION
std::vector<int> tmp(__memory_corruption_guard_bytes / sizeof(int),
__memory_corruption_check_pattern);
CV_Assert(tmp.size() * sizeof(int) == __memory_corruption_guard_bytes);
openCLVerifyCall(clEnqueueWriteBuffer(getClCommandQueue(ctx),
mainBuffer, CL_FALSE, 0, __memory_corruption_guard_bytes, &tmp[0],
0, NULL, NULL));
openCLVerifyCall(clEnqueueWriteBuffer(getClCommandQueue(ctx),
mainBuffer, CL_FALSE, __memory_corruption_guard_bytes + size, __memory_corruption_guard_bytes, &tmp[0],
0, NULL, NULL));
clFinish(getClCommandQueue(ctx));
#endif
CheckBuffers data(mainBuffer, size, widthInBytes, height);
__check_buffers.insert(std::pair<cl_mem, CheckBuffers>((cl_mem)*dev_ptr, data));
}
#endif
*pitch = widthInBytes;
}
@@ -224,40 +247,48 @@ void openCLCopyBuffer2D(Context *ctx, void *dst, size_t dpitch, int dst_offset,
void openCLFree(void *devPtr)
{
openCLSafeCall(clReleaseMemObject((cl_mem)devPtr));
#ifdef MEMORY_CORRUPTION_GUARD
#ifdef CHECK_MEMORY_CORRUPTION
bool failBefore = false, failAfter = false;
#endif
CheckBuffers data;
std::map<cl_mem, CheckBuffers>::iterator i = __check_buffers.find((cl_mem)devPtr);
if (i != __check_buffers.end())
{
data = i->second;
#ifdef CHECK_MEMORY_CORRUPTION
Context* ctx = Context::getContext();
std::vector<uchar> checkBefore(__memory_corruption_check_bytes);
std::vector<uchar> checkAfter(__memory_corruption_check_bytes);
std::vector<uchar> checkBefore(__memory_corruption_guard_bytes);
std::vector<uchar> checkAfter(__memory_corruption_guard_bytes);
openCLVerifyCall(clEnqueueReadBuffer(getClCommandQueue(ctx),
data.mainBuffer, CL_TRUE, 0, __memory_corruption_check_bytes, &checkBefore[0],
data.mainBuffer, CL_FALSE, 0, __memory_corruption_guard_bytes, &checkBefore[0],
0, NULL, NULL));
openCLVerifyCall(clEnqueueReadBuffer(getClCommandQueue(ctx),
data.mainBuffer, CL_TRUE, __memory_corruption_check_bytes + data.size, __memory_corruption_check_bytes, &checkAfter[0],
data.mainBuffer, CL_FALSE, __memory_corruption_guard_bytes + data.size, __memory_corruption_guard_bytes, &checkAfter[0],
0, NULL, NULL));
clFinish(getClCommandQueue(ctx));
std::vector<int> tmp(__memory_corruption_check_bytes / sizeof(int),
std::vector<int> tmp(__memory_corruption_guard_bytes / sizeof(int),
__memory_corruption_check_pattern);
if (memcmp(&checkBefore[0], &tmp[0], __memory_corruption_check_bytes) != 0)
if (memcmp(&checkBefore[0], &tmp[0], __memory_corruption_guard_bytes) != 0)
{
failBefore = true;
}
if (memcmp(&checkAfter[0], &tmp[0], __memory_corruption_check_bytes) != 0)
if (memcmp(&checkAfter[0], &tmp[0], __memory_corruption_guard_bytes) != 0)
{
failAfter = true;
}
#else
// TODO FIXIT Attach clReleaseMemObject call to event completion callback
Context* ctx = Context::getContext();
clFinish(getClCommandQueue(ctx));
#endif
openCLSafeCall(clReleaseMemObject(data.mainBuffer));
__check_buffers.erase(i);
}
#endif
openCLSafeCall(clReleaseMemObject((cl_mem)devPtr));
#ifdef CHECK_MEMORY_CORRUPTION
#if defined(CHECK_MEMORY_CORRUPTION)
if (failBefore)
{
#ifdef CHECK_MEMORY_CORRUPTION_PRINT_ERROR
@@ -276,7 +307,8 @@ void openCLFree(void *devPtr)
CV_Error(CV_StsInternal, "Memory corruption detected: after buffer");
#endif
}
#endif
#endif // CHECK_MEMORY_CORRUPTION
#endif // MEMORY_CORRUPTION_GUARD
}
cl_kernel openCLGetKernelFromSource(const Context *ctx, const cv::ocl::ProgramEntry* source, String kernelName)