Commit 34046ec6 authored by Maksim Shabunin's avatar Maksim Shabunin

Merge pull request #9105 from alalek:ocl_update_event_callback

parents f670a992 daee9821
...@@ -1962,7 +1962,7 @@ KernelArg KernelArg::Constant(const Mat& m) ...@@ -1962,7 +1962,7 @@ KernelArg KernelArg::Constant(const Mat& m)
struct Kernel::Impl struct Kernel::Impl
{ {
Impl(const char* kname, const Program& prog) : Impl(const char* kname, const Program& prog) :
refcount(1), e(0), nu(0) refcount(1), isInProgress(false), nu(0)
{ {
cl_program ph = (cl_program)prog.ptr(); cl_program ph = (cl_program)prog.ptr();
cl_int retval = 0; cl_int retval = 0;
...@@ -2005,11 +2005,15 @@ struct Kernel::Impl ...@@ -2005,11 +2005,15 @@ struct Kernel::Impl
images.push_back(image); images.push_back(image);
} }
void finit() void finit(cl_event e)
{ {
CV_UNUSED(e);
#if 0
printf("event::callback(%p)\n", e); fflush(stdout);
#endif
cleanupUMats(); cleanupUMats();
images.clear(); images.clear();
if(e) { clReleaseEvent(e); e = 0; } isInProgress = false;
release(); release();
} }
...@@ -2025,9 +2029,9 @@ struct Kernel::Impl ...@@ -2025,9 +2029,9 @@ struct Kernel::Impl
cv::String name; cv::String name;
#endif #endif
cl_kernel handle; cl_kernel handle;
cl_event e;
enum { MAX_ARRS = 16 }; enum { MAX_ARRS = 16 };
UMatData* u[MAX_ARRS]; UMatData* u[MAX_ARRS];
bool isInProgress;
int nu; int nu;
std::list<Image2D> images; std::list<Image2D> images;
bool haveTempDstUMats; bool haveTempDstUMats;
...@@ -2037,9 +2041,9 @@ struct Kernel::Impl ...@@ -2037,9 +2041,9 @@ struct Kernel::Impl
extern "C" { extern "C" {
static void CL_CALLBACK oclCleanupCallback(cl_event, cl_int, void *p) static void CL_CALLBACK oclCleanupCallback(cl_event e, cl_int, void *p)
{ {
((cv::ocl::Kernel::Impl*)p)->finit(); ((cv::ocl::Kernel::Impl*)p)->finit(e);
} }
} }
...@@ -2246,7 +2250,7 @@ bool Kernel::run(int dims, size_t _globalsize[], size_t _localsize[], ...@@ -2246,7 +2250,7 @@ bool Kernel::run(int dims, size_t _globalsize[], size_t _localsize[],
{ {
CV_INSTRUMENT_REGION_OPENCL_RUN(p->name.c_str()); CV_INSTRUMENT_REGION_OPENCL_RUN(p->name.c_str());
if(!p || !p->handle || p->e != 0) if(!p || !p->handle || p->isInProgress)
return false; return false;
cl_command_queue qq = getQueue(q); cl_command_queue qq = getQueue(q);
...@@ -2265,9 +2269,10 @@ bool Kernel::run(int dims, size_t _globalsize[], size_t _localsize[], ...@@ -2265,9 +2269,10 @@ bool Kernel::run(int dims, size_t _globalsize[], size_t _localsize[],
return true; return true;
if( p->haveTempDstUMats ) if( p->haveTempDstUMats )
sync = true; sync = true;
cl_event asyncEvent = 0;
cl_int retval = clEnqueueNDRangeKernel(qq, p->handle, (cl_uint)dims, cl_int retval = clEnqueueNDRangeKernel(qq, p->handle, (cl_uint)dims,
offset, globalsize, _localsize, 0, 0, offset, globalsize, _localsize, 0, 0,
sync ? 0 : &p->e); sync ? 0 : &asyncEvent);
#if CV_OPENCL_SHOW_RUN_ERRORS #if CV_OPENCL_SHOW_RUN_ERRORS
if (retval != CL_SUCCESS) if (retval != CL_SUCCESS)
{ {
...@@ -2283,18 +2288,22 @@ bool Kernel::run(int dims, size_t _globalsize[], size_t _localsize[], ...@@ -2283,18 +2288,22 @@ bool Kernel::run(int dims, size_t _globalsize[], size_t _localsize[],
else else
{ {
p->addref(); p->addref();
CV_OclDbgAssert(clSetEventCallback(p->e, CL_COMPLETE, oclCleanupCallback, p) == CL_SUCCESS); p->isInProgress = true;
CV_OclDbgAssert(clSetEventCallback(asyncEvent, CL_COMPLETE, oclCleanupCallback, p) == CL_SUCCESS);
} }
if (asyncEvent)
clReleaseEvent(asyncEvent);
return retval == CL_SUCCESS; return retval == CL_SUCCESS;
} }
bool Kernel::runTask(bool sync, const Queue& q) bool Kernel::runTask(bool sync, const Queue& q)
{ {
if(!p || !p->handle || p->e != 0) if(!p || !p->handle || p->isInProgress)
return false; return false;
cl_command_queue qq = getQueue(q); cl_command_queue qq = getQueue(q);
cl_int retval = clEnqueueTask(qq, p->handle, 0, 0, sync ? 0 : &p->e); cl_event asyncEvent = 0;
cl_int retval = clEnqueueTask(qq, p->handle, 0, 0, sync ? 0 : &asyncEvent);
if( sync || retval != CL_SUCCESS ) if( sync || retval != CL_SUCCESS )
{ {
CV_OclDbgAssert(clFinish(qq) == CL_SUCCESS); CV_OclDbgAssert(clFinish(qq) == CL_SUCCESS);
...@@ -2303,8 +2312,11 @@ bool Kernel::runTask(bool sync, const Queue& q) ...@@ -2303,8 +2312,11 @@ bool Kernel::runTask(bool sync, const Queue& q)
else else
{ {
p->addref(); p->addref();
CV_OclDbgAssert(clSetEventCallback(p->e, CL_COMPLETE, oclCleanupCallback, p) == CL_SUCCESS); p->isInProgress = true;
CV_OclDbgAssert(clSetEventCallback(asyncEvent, CL_COMPLETE, oclCleanupCallback, p) == CL_SUCCESS);
} }
if (asyncEvent)
clReleaseEvent(asyncEvent);
return retval == CL_SUCCESS; return retval == CL_SUCCESS;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment