Releasing Opencl Memory, Kernels, Devices etc

If you look further, then you will see that all those classes inherit from a template detail::Wrapper<T> and that in turn is specialized for each and every type to indeed call the corresponding clRelease... function in its destructor and as you know, a class's destructor will always call its base class destructors, so there is no need for a user-defined destructor in cl::Buffer, cl::Kernel and friends. (Well, to be correct Wrapper<T> is not specialized but uses some other specialized traits class named ReferenceHandler<T> that brings the retain and release functions.)

So since all those OpenCL objects use some kind of reference counting semantics and all those C++ wrappers wrap the corresponding clRetain/clRelease calls in their constructors and destructors, you indeed don't have to worry about properly releasing OpenCL resources when working with C++, just RAII the way it should be.

(But like already said by DarkZeros, a device is probably a bad example, since devices are usually not retained or released (and detail::Wrapper<cl_device_id>'s con/destructor will probably do nothing). With OpenCL 1.2's device fission they may, but the C++ wrapper doesn't support 1.2 anyway.)


The only things that need a release in OpenCL are the abstract constructions. Like:

  • Memory objects
  • Contexts
  • Command Queues
  • Programs
  • Kernels
  • Events

You cannot release a device, since a device cannot be "destroyed" or un-allocated. When you call getDevice you receive an ID to the device, not a new created device.

NOTE: In OCL 1.2 a device can be constructed as a partition of an upper device (subdevice). So, it should have a delete case. Maybe the CL API should take care of this specific case of the new verisons... I dunno

Tags:

C++

Opencl