Skip to content

Commit da8dbf6

Browse files
committed
ocl: async cl_buffer cleanup queue (for event callback)
1 parent 34046ec commit da8dbf6

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

modules/core/include/opencv2/core/mat.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,9 @@ struct CV_EXPORTS UMatData
497497
{
498498
enum { COPY_ON_MAP=1, HOST_COPY_OBSOLETE=2,
499499
DEVICE_COPY_OBSOLETE=4, TEMP_UMAT=8, TEMP_COPIED_UMAT=24,
500-
USER_ALLOCATED=32, DEVICE_MEM_MAPPED=64};
500+
USER_ALLOCATED=32, DEVICE_MEM_MAPPED=64,
501+
ASYNC_CLEANUP=128
502+
};
501503
UMatData(const MatAllocator* allocator);
502504
~UMatData();
503505

modules/core/src/ocl.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "precomp.hpp"
4343
#include <list>
4444
#include <map>
45+
#include <deque>
4546
#include <string>
4647
#include <sstream>
4748
#include <iostream> // std::cerr
@@ -1983,7 +1984,10 @@ struct Kernel::Impl
19831984
if( u[i] )
19841985
{
19851986
if( CV_XADD(&u[i]->urefcount, -1) == 1 )
1987+
{
1988+
u[i]->flags |= UMatData::ASYNC_CLEANUP;
19861989
u[i]->currAllocator->deallocate(u[i]);
1990+
}
19871991
u[i] = 0;
19881992
}
19891993
nu = 0;
@@ -3157,6 +3161,10 @@ class OpenCLAllocator : public MatAllocator
31573161

31583162
matStdAllocator = Mat::getDefaultAllocator();
31593163
}
3164+
~OpenCLAllocator()
3165+
{
3166+
flushCleanupQueue();
3167+
}
31603168

31613169
UMatData* defaultAllocate(int dims, const int* sizes, int type, void* data, size_t* step,
31623170
int flags, UMatUsageFlags usageFlags) const
@@ -3193,6 +3201,7 @@ class OpenCLAllocator : public MatAllocator
31933201
}
31943202

31953203
Context& ctx = Context::getDefault();
3204+
flushCleanupQueue();
31963205

31973206
int createFlags = 0, flags0 = 0;
31983207
getBestFlags(ctx, flags, usageFlags, createFlags, flags0);
@@ -3247,6 +3256,8 @@ class OpenCLAllocator : public MatAllocator
32473256
if(!u)
32483257
return false;
32493258

3259+
flushCleanupQueue();
3260+
32503261
UMatDataAutoLock lock(u);
32513262

32523263
if(u->handle == 0)
@@ -3381,6 +3392,15 @@ class OpenCLAllocator : public MatAllocator
33813392

33823393
CV_Assert(u->handle != 0);
33833394
CV_Assert(u->mapcount == 0);
3395+
3396+
if (u->flags & UMatData::ASYNC_CLEANUP)
3397+
addToCleanupQueue(u);
3398+
else
3399+
deallocate_(u);
3400+
}
3401+
3402+
void deallocate_(UMatData* u) const
3403+
{
33843404
if(u->tempUMat())
33853405
{
33863406
CV_Assert(u->origdata);
@@ -4184,6 +4204,33 @@ class OpenCLAllocator : public MatAllocator
41844204
}
41854205

41864206
MatAllocator* matStdAllocator;
4207+
4208+
mutable cv::Mutex cleanupQueueMutex;
4209+
mutable std::deque<UMatData*> cleanupQueue;
4210+
4211+
void flushCleanupQueue() const
4212+
{
4213+
if (!cleanupQueue.empty())
4214+
{
4215+
std::deque<UMatData*> q;
4216+
{
4217+
cv::AutoLock lock(cleanupQueueMutex);
4218+
q.swap(cleanupQueue);
4219+
}
4220+
for (std::deque<UMatData*>::const_iterator i = q.begin(); i != q.end(); ++i)
4221+
{
4222+
deallocate_(*i);
4223+
}
4224+
}
4225+
}
4226+
void addToCleanupQueue(UMatData* u) const
4227+
{
4228+
//TODO: Validation check: CV_Assert(!u->tempUMat());
4229+
{
4230+
cv::AutoLock lock(cleanupQueueMutex);
4231+
cleanupQueue.push_back(u);
4232+
}
4233+
}
41874234
};
41884235

41894236
MatAllocator* getOpenCLAllocator()

0 commit comments

Comments
 (0)