Enhance feature: ocl::setUseOpenCL(false) releases refcount/resources

In OpenCV 4.5 or later and on all platforms…after any successful ocl::setUseOpenCL(true) and use of that OpenCL computing device, the managed OpenCL resources (buffers, queues, etc.) are not released when ocl::setUseOpenCL(false). The setUseOpenCL(false)` only sets a boolean flag. It doesn’t release resources or any refcount on them.

Goal → “OpenCV, please turn off OpenCL and release the used resources”.

I recommend the OpenCV API itself to be enhanced to somehow enable managed OpenCL resources to be released without requiring a new computing device. It is not possible to do alternatives like (pseudocode)

  • cv::ocl::OpenCLExecutionContext::getCurrentRef() = ocl::OpenCLExecutionContext() exposes a bug. It leaves the opencv TLS with empty .oclExecutionContext yet .execcontextinitialized=true.
  • ocl::OpenCLExecutionContext().bind() asserts because there is no impl
  • cv::ocl::OpenCLExecutionContext::getCurrentRef().release() fails later due to unmatched refcount decrement
  • cl_context = cv::ocl::Context::create("disabled"); cv::ocl::OpenCLExecutionContext::create(cl_context, cl_context.device(0)).bind(); asserts with no context or device

The only way to release managed OpenCL resources is to ocl::OpenCLExecutionContext.bind() to another OpenCL computing device – the old devices resources are released, and new resources are eventually allocated on the new computing device. But that is not the goal…and there might not be a 2nd OpenCL compute device to bind(). Somewhat related to Refcounts for ocl::Context::Impl not decremented when UMats out of scope · Issue #18919 · opencv/opencv · GitHub

I am confident I can update the API itself code to achieve the goal. However, I am unclear the approach to take. I’ll probably decrement the refcount for the current OpenCLExecutionContext. But…I’m not sure if I want to…

  1. create new static cv::ocl::OpenCLExecutionContext::unbind()
  2. allow binding a blank OpenCLExecutionContext
  3. allow assigning a blank to the current ref
  4. something else, etc…

Comments, ideas, alternatives?

welcome.

you should take this to github and open an issue. no discussions of this kind take place here. actual Intel developers (who primarily run OpenCV) don’t frequent this forum.

haha :grin: it was a draft github issue but then I thought…maybe the community wants to discuss before I have something clear enough for a PR. I’ll take your advice as moderator and go back. :heavy_check_mark:

1 Like

yeah I tried something similar here once. no reactions. trying to discuss that here makes no sense. this isn’t actually a community. this is unpaid first level support :wink:

1 Like

Hi
write an issue and wait for an answer and feel free to make a pr
setUseOpenCl is only to disable opencl function and does not release ressource
I think you should proposed a new name for this functionality