From da3da84a203da56f6348fa4c97fe41bd6c355d4d Mon Sep 17 00:00:00 2001 From: Wu Zhiwen Date: Fri, 25 Aug 2017 08:42:11 +0800 Subject: [PATCH] ocl: Add a function to unload a run-time cached program This function is the counterpart of "Context::getProg". With this function, users have chance to unload a program from global run-time cached programs, and save resource. --- modules/core/include/opencv2/core/ocl.hpp | 1 + modules/core/src/ocl.cpp | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/modules/core/include/opencv2/core/ocl.hpp b/modules/core/include/opencv2/core/ocl.hpp index 2fb02b9014..a7d5f19485 100644 --- a/modules/core/include/opencv2/core/ocl.hpp +++ b/modules/core/include/opencv2/core/ocl.hpp @@ -248,6 +248,7 @@ public: const Device& device(size_t idx) const; Program getProg(const ProgramSource& prog, const String& buildopt, String& errmsg); + void unloadProg(Program& prog); static Context& getDefault(bool initialize = true); void* ptr() const; diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp index 7d7c24e210..c34d7319c7 100644 --- a/modules/core/src/ocl.cpp +++ b/modules/core/src/ocl.cpp @@ -1397,6 +1397,23 @@ struct Context::Impl return prog; } + void unloadProg(Program& prog) + { + cv::AutoLock lock(program_cache_mutex); + for (CacheList::iterator i = cacheList.begin(); i != cacheList.end(); ++i) + { + phash_t::iterator it = phash.find(*i); + if (it != phash.end()) + { + if (it->second.ptr() == prog.ptr()) + { + phash.erase(*i); + cacheList.erase(i); + return; + } + } + } + } IMPLEMENT_REFCOUNTABLE(); @@ -1660,7 +1677,11 @@ Program Context::getProg(const ProgramSource& prog, return p ? p->getProg(prog, buildopts, errmsg) : Program(); } - +void Context::unloadProg(Program& prog) +{ + if (p) + p->unloadProg(prog); +} #ifdef HAVE_OPENCL_SVM bool Context::useSVM() const