OpenCL Kernels not cached in OpenCL 3.2?

asked 2017-03-23 04:46:53 -0500

iko79 gravatar image

Hi,

I noticed something that's ringing my alarm bells: In the OpenCV 3.0 overview slides you state that the OpenCL "kernel is compiled only once and cached". However, when I use the suggested code like this and use Nsight for Visual Studio as a profiler, I can see that every call of a processing method/function with OpenCL support seems to recreate a new OpenCL Kernel object since every call is associated with a new Kernel ID. E.g. if I run this code here...

cv::UMat m1;
cv::UMat m2;
cv::Mat kernel = cv::getStructuringElement( cv::MORPH_ELLIPSE, cv::Size( 5, 5 ), cv::Point( 3, 3 ) );

cv::imread( "frame.tiff" ).copyTo( m1 );

for( int i = 0; i < 10; i++ )
    cv::morphologyEx( m1, m1, cv::MORPH_DILATE, kernel );

...I can see ten calls of "morph" like this:

Kernel ID   Creation Time (μs)  Lifetime (μs)   Kernel Name Program ID  Count   
1           144,977.128         2,639,412.861   morph       1           1       
2           149,341.788         2,635,090.195   morph       1           1       
3           150,546.564         2,633,925.752   morph       1           1       
4           151,932.220         2,632,580.169   morph       1           1       
5           153,527.674         2,631,024.282   morph       1           1       
6           155,105.123         2,629,485.587   morph       1           1       
7           156,673.963         2,627,955.218   morph       1           1       
8           158,268.724         2,626,399.276   morph       1           1       
9           160,592.615         2,624,114.423   morph       1           1       
10          161,995.603         2,622,750.544   morph       1           1

That said, I didn't step into the OpenCV code with the debugger, because right now I don't got time for this, and I also didn't compare to OpenCV 3.0 source code. I also am by no means an expert in OpenCL and am not entirely sure what a Kernel ID is and at what point it's issued, so I can only speculate on what's going on, but to me it seems like Kernel objects are created over and over again and I'm pretty sure this isn't good.

Are the slides outdated? Is this a bug? It certainly doesn't seem like it's meant that way.

edit retag flag offensive close merge delete

Comments

2

"kernel is compiled only once and cached" it is true in opencv 3. For your example it is true :

iter #0-Time110.832
iter #1-Time0.555454
iter #2-Time4.36974
iter #3-Time4.04652
iter #4-Time0.496462
iter #5-Time0.555143
iter #6-Time0.545518
iter #7-Time0.471313
iter #8-Time0.570357
iter #9-Time0.487769

    TickMeter ch;
    for (int i = 0; i < 10; i++)
    {
        ch.start();
        cv::morphologyEx(m1, m1, cv::MORPH_DILATE, kernel);
        ch.stop();
        cout << "iter #" << i << "-Time" << ch.getTimeMilli() << "\n";
        ch.reset();

    }

I don't know any example without this condition

LBerger gravatar imageLBerger ( 2017-03-23 04:57:53 -0500 )edit

You're right, when I look at the timing it seems to do what it's supposed to. I am still confused about 1) what a Kernel ID is, 2) why the kernel gets a new one assigned in the OpenCV implementation, and 3) whether or not this could be a potential problem.

iko79 gravatar imageiko79 ( 2017-03-29 02:30:24 -0500 )edit

I don't know opencl but may be kernel is the code name and ID is a process number

LBerger gravatar imageLBerger ( 2017-03-29 03:05:40 -0500 )edit