Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Using ocl module is pretty much alike using gpu module. To use OCL module, you must call getDevice() once before call any of the ocl module functions. It is like this:

vector<ocl::Info> info;
ocl::getDevice(info);

This function will detect if there is any valid GPU devices in all OpenCL enabled platforms on your machine; particularly, the ocl module should have best compatibility with an AMD graphic card.


Most ocl module functions require input matrices of type oclMat. oclMat is similar with Mat, but it encapsulates some OpenCL device's buffer object (cl_mem) and controls buffer release, transferring, etc. To convert a Mat to an oclMat object, simply call

oclMat myOclMat(mat); // mat is a Mat object

Alternatively, you could explicitly call

oclMat myOclMat;
myOclMat.upload(mat);

oclMat then can be fed into ocl module functions once the memory data is uploaded to opencl device's memory. To access the oclMat's data, you need to download the gpu buffer data into host memory. Similar with upload operations, you could either call

mat = Mat(myOclMat);

or

myOclMat.download(mat);

Normally you do not need to worry about memory releasing. oclMat will deconstruct its encapsulated cl_mem buffer once they are not referenced any more.


To sum up,

, the usage of the OCL module is simply:

  1. globally register an OpenCL device // call ocl::getDevice()

  2. transfer from host memory to gpu memory // convert Mat to oclMat

  3. Do the calculations on OpenCL device // call ocl module functions,

  4. transfer back from gpu memory to host memory // convert oclMat to Mat

  5. Do rest calculations with Mat

There is some sample ocl module programs provided in the trunk repository. You could refer to them as a start.

BTW, if you are not familiar with OpenCL, I recommend you to read some basics about OpenCL's basics before include OpenCL into your project.