Skip to content

Commit 34046ec

Browse files
committed
Merge pull request opencv#9105 from alalek:ocl_update_event_callback
2 parents f670a99 + daee982 commit 34046ec

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

modules/core/src/ocl.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,7 +1962,7 @@ KernelArg KernelArg::Constant(const Mat& m)
19621962
struct Kernel::Impl
19631963
{
19641964
Impl(const char* kname, const Program& prog) :
1965-
refcount(1), e(0), nu(0)
1965+
refcount(1), isInProgress(false), nu(0)
19661966
{
19671967
cl_program ph = (cl_program)prog.ptr();
19681968
cl_int retval = 0;
@@ -2005,11 +2005,15 @@ struct Kernel::Impl
20052005
images.push_back(image);
20062006
}
20072007

2008-
void finit()
2008+
void finit(cl_event e)
20092009
{
2010+
CV_UNUSED(e);
2011+
#if 0
2012+
printf("event::callback(%p)\n", e); fflush(stdout);
2013+
#endif
20102014
cleanupUMats();
20112015
images.clear();
2012-
if(e) { clReleaseEvent(e); e = 0; }
2016+
isInProgress = false;
20132017
release();
20142018
}
20152019

@@ -2025,9 +2029,9 @@ struct Kernel::Impl
20252029
cv::String name;
20262030
#endif
20272031
cl_kernel handle;
2028-
cl_event e;
20292032
enum { MAX_ARRS = 16 };
20302033
UMatData* u[MAX_ARRS];
2034+
bool isInProgress;
20312035
int nu;
20322036
std::list<Image2D> images;
20332037
bool haveTempDstUMats;
@@ -2037,9 +2041,9 @@ struct Kernel::Impl
20372041

20382042
extern "C" {
20392043

2040-
static void CL_CALLBACK oclCleanupCallback(cl_event, cl_int, void *p)
2044+
static void CL_CALLBACK oclCleanupCallback(cl_event e, cl_int, void *p)
20412045
{
2042-
((cv::ocl::Kernel::Impl*)p)->finit();
2046+
((cv::ocl::Kernel::Impl*)p)->finit(e);
20432047
}
20442048

20452049
}
@@ -2246,7 +2250,7 @@ bool Kernel::run(int dims, size_t _globalsize[], size_t _localsize[],
22462250
{
22472251
CV_INSTRUMENT_REGION_OPENCL_RUN(p->name.c_str());
22482252

2249-
if(!p || !p->handle || p->e != 0)
2253+
if(!p || !p->handle || p->isInProgress)
22502254
return false;
22512255

22522256
cl_command_queue qq = getQueue(q);
@@ -2265,9 +2269,10 @@ bool Kernel::run(int dims, size_t _globalsize[], size_t _localsize[],
22652269
return true;
22662270
if( p->haveTempDstUMats )
22672271
sync = true;
2272+
cl_event asyncEvent = 0;
22682273
cl_int retval = clEnqueueNDRangeKernel(qq, p->handle, (cl_uint)dims,
22692274
offset, globalsize, _localsize, 0, 0,
2270-
sync ? 0 : &p->e);
2275+
sync ? 0 : &asyncEvent);
22712276
#if CV_OPENCL_SHOW_RUN_ERRORS
22722277
if (retval != CL_SUCCESS)
22732278
{
@@ -2283,18 +2288,22 @@ bool Kernel::run(int dims, size_t _globalsize[], size_t _localsize[],
22832288
else
22842289
{
22852290
p->addref();
2286-
CV_OclDbgAssert(clSetEventCallback(p->e, CL_COMPLETE, oclCleanupCallback, p) == CL_SUCCESS);
2291+
p->isInProgress = true;
2292+
CV_OclDbgAssert(clSetEventCallback(asyncEvent, CL_COMPLETE, oclCleanupCallback, p) == CL_SUCCESS);
22872293
}
2294+
if (asyncEvent)
2295+
clReleaseEvent(asyncEvent);
22882296
return retval == CL_SUCCESS;
22892297
}
22902298

22912299
bool Kernel::runTask(bool sync, const Queue& q)
22922300
{
2293-
if(!p || !p->handle || p->e != 0)
2301+
if(!p || !p->handle || p->isInProgress)
22942302
return false;
22952303

22962304
cl_command_queue qq = getQueue(q);
2297-
cl_int retval = clEnqueueTask(qq, p->handle, 0, 0, sync ? 0 : &p->e);
2305+
cl_event asyncEvent = 0;
2306+
cl_int retval = clEnqueueTask(qq, p->handle, 0, 0, sync ? 0 : &asyncEvent);
22982307
if( sync || retval != CL_SUCCESS )
22992308
{
23002309
CV_OclDbgAssert(clFinish(qq) == CL_SUCCESS);
@@ -2303,8 +2312,11 @@ bool Kernel::runTask(bool sync, const Queue& q)
23032312
else
23042313
{
23052314
p->addref();
2306-
CV_OclDbgAssert(clSetEventCallback(p->e, CL_COMPLETE, oclCleanupCallback, p) == CL_SUCCESS);
2315+
p->isInProgress = true;
2316+
CV_OclDbgAssert(clSetEventCallback(asyncEvent, CL_COMPLETE, oclCleanupCallback, p) == CL_SUCCESS);
23072317
}
2318+
if (asyncEvent)
2319+
clReleaseEvent(asyncEvent);
23082320
return retval == CL_SUCCESS;
23092321
}
23102322

0 commit comments

Comments
 (0)