Ask Your Question

How to run OpenCL file (example in OpenCV library?

asked 2017-03-19 22:20:02 -0600

minhntu gravatar image

Hi guys, Anyone knows how to run OpenCL source code in OpenCV library? I see that there are many kernels in a file. I am confused how to set up arguments to run it. Thank you very much for your help.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-03-19 22:48:54 -0600

Tetragramm gravatar image

Take a look HERE for an explanation of the Transparent API.

edit flag offensive delete link more


I want to run OpenCL file (example at opencv310\sources\modules\features2d\src\opencl\ in OpenCV library. But I see in this file have many kernels. And each kernel has a different number of arguments. How can I set up arguments to run this file?

minhntu gravatar imageminhntu ( 2017-03-20 05:06:10 -0600 )edit

You don't need to. You just run normal fast with UMat as the arguments.

Tetragramm gravatar imageTetragramm ( 2017-03-20 17:52:11 -0600 )edit

it's mean when I change Mat by UMat. FAST function will be run on GPU. Do you know what in OpenCV library use for?

minhntu gravatar imageminhntu ( 2017-03-21 01:31:35 -0600 )edit

I'm not entirely sure what you're asking. Here is where the kernels are called from.

Again, you don't need to do anything but use UMat matrices and have compiled OpenCV with OpenCL support.

Tetragramm gravatar imageTetragramm ( 2017-03-21 17:40:50 -0600 )edit

It means I only need to change from

Mat img = imread("chessboard.jpg", IMREAD_UNCHANGED);
FAST(gray, keypointsCorners, thresholdCorner, true);


UMat img, gray; 
imread("chessboard.jpg", 1).copyTo(img); 
FAST(gray, keypointsCorners, thresholdCorner, true);

to have OpenCL code and this code will run on GPU? And while FAST () function is implementing it will call file inside?

minhntu gravatar imageminhntu ( 2017-03-21 20:50:56 -0600 )edit

Try to use the code tag in the editor to make it readable.

Yes, so long as your OpenCV was compiled with OpenCL support, then it will run on the gpu. Though do note that in the snippet here, you don't actually fill gray. I assume you just left that out for brevity.

Tetragramm gravatar imageTetragramm ( 2017-03-21 20:57:07 -0600 )edit

When I change Mat by UMat. My result is code ran on GPU. But it is slower than CPU. And when I change a name of "" file in path: opencv310\sources\modules\features2d\src\opencl\, Code still run normally. Whether file was not called while the code is implementing. How do we know that when changing Mat by UMat, Code will run parallel on GPU?

minhntu gravatar imageminhntu ( 2017-03-22 04:39:54 -0600 )edit

Just to make sure, you are compiling the code? Not using the installer? The installer is not (I think) compiled with OpenCL, so you will see no benefit.

Tetragramm gravatar imageTetragramm ( 2017-03-22 21:10:34 -0600 )edit

When I run code below

        cout << context.ndevices() << " GPU devices are detected." << endl;
    for (int i = 0; i < context.ndevices(); i++)
        cv::ocl::Device device = context.device(i);
        cout << "name                 : " << << endl;
        cout << "available            : " << device.available() << endl;
        cout << "imageSupport         : " << device.imageSupport() << endl;
        cout << "OpenCL_C_Version     : " << device.OpenCL_C_Version() << endl;
        cout << endl;

It showed results: 1 GPU devices are detected name: Quadro K2000 available: 1 image surport: 1

OpenCL version: OpenCL C 1.2

And while Code is implementing. It took 11026 ms for MAT and 29340ms for UMAT in the same time. Do you know why it is?

minhntu gravatar imageminhntu ( 2017-03-23 01:56:34 -0600 )edit

How many iterations? The first iteration may be much slower because of initializing the context and memory. That looks like a decent amount though.

Also, that's a really old card. What CPU do you have? If it's anything recent, it'll be faster than the GPU. Not because of the processing, but just the memory transfer. If you keep the same data on the GPU and do lots of work on it, it's much better than transferring back and forth.

Tetragramm gravatar imageTetragramm ( 2017-03-23 19:17:02 -0600 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-03-19 22:20:02 -0600

Seen: 533 times

Last updated: Mar 19 '17