Ask Your Question
1

GaussianBlur and Canny execution times are much longer on T-API

asked 2015-08-07 03:22:06 -0600

Grigory Ptashko gravatar image

Hello.

I've just started to learn OpenCV 3. I'm on OS X Yosemite. Here's my clinfo in the GPU part:

  Device Name                                     GeForce GT 330M
  Device Vendor                                   NVIDIA
  Device Vendor ID                                0x1022600
  Device Version                                  OpenCL 1.0 
  Driver Version                                  10.0.31 310.90.10.05b12
  Device OpenCL C Version                         OpenCL C 1.1 
  Device Type                                     GPU
  Device Profile                                  FULL_PROFILE
  Max compute units                               6
  Max clock frequency                             1100MHz
  Max work item dimensions                        3
  Max work item sizes                             512x512x64
  Max work group size                             512
  Preferred work group size multiple              32
  Preferred / native vector sizes                 
    char                                                 1 / 1       
    short                                                1 / 1       
    int                                                  1 / 1       
    long                                                 1 / 1       
    half                                                 0 / 0        (n/a)
    float                                                1 / 1       
    double                                               0 / 0        (n/a)
  Half-precision Floating-point support           (n/a)
  Single-precision Floating-point support         (core)
    Denormals                                     No
    Infinity and NANs                             Yes
    Round to nearest                              Yes
    Round to zero                                 Yes
    Round to infinity                             Yes
    IEEE754-2008 fused multiply-add               No
    Support is emulated in software               No
    Correctly-rounded divide and sqrt operations  No
  Double-precision Floating-point support         (n/a)
  Address bits                                    32, Little-Endian
  Global memory size                              268435456 (256MiB)
  Error Correction support                        No
  Max memory allocation                           134217728 (128MiB)
  Unified memory for Host and Device              No
  Minimum alignment for any data type             128 bytes
  Alignment of base address                       1024 bits (128 bytes)
  Global Memory cache type                        None
  Image support                                   Yes
    Max number of samplers per kernel             16
    Max 2D image size                             4096x4096 pixels
    Max 3D image size                             2048x2048x2048 pixels
    Max number of read image args                 128
    Max number of write image args                8
  Local memory type                               Local
  Local memory size                               16384 (16KiB)
  Max constant buffer size                        65536 (64KiB)
  Max number of constant args                     9
  Max size of kernel argument                     4352 (4.25KiB)
  Queue properties                                
    Out-of-order execution                        No
    Profiling                                     Yes
  Profiling timer resolution                      1000ns
  Execution capabilities                          
    Run OpenCL kernels                            Yes
    Run native kernels                            No
  Device Available                                Yes
  Compiler Available                              Yes
  Device Extensions                               cl_APPLE_SetMemObjectDestructor cl_APPLE_ContextLoggingFunctions cl_APPLE_clut cl_APPLE_query_kernel_names cl_APPLE_gl_sharing cl_khr_gl_event cl_khr_byte_addressable_store cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics

I wrote a little program to test T-API and it turns out that GaussianBlur and Canny take much much longer time to execute on T-API. Here's the code. It loads image and applies these two filter without and with T-API:

double totalTime = 0;
int64 start = getTickCount();
cvtColor(image, gray, COLOR_BGR2GRAY);
double timeMs = (getTickCount() - start) / getTickFrequency() * 1000;
totalTime += timeMs;
cout << "cvtColor ms [" << timeMs<< "]" << endl;

start = getTickCount();
GaussianBlur(gray, gray, Size(7, 7), 1.5);
timeMs = (getTickCount() - start) / getTickFrequency() * 1000;
totalTime += timeMs;
cout << "GaussianBlur ms [" << timeMs<< "]" << endl;

start = getTickCount();
Canny(gray, gray, 0, 50);
timeMs = (getTickCount() - start) / getTickFrequency() * 1000;
totalTime += timeMs;
cout << "Canny ms [" << timeMs<< "]" << endl;
cout << "= Total [" <<  totalTime << "]" << endl;

// TAPI
cout << endl << "TAPI results" << endl;
totalTime = 0;
UMat uimage;
UMat ugray;
imread(argv[1], CV_LOAD_IMAGE_COLOR).copyTo(uimage);

start = getTickCount();
cvtColor(uimage, ugray, COLOR_BGR2GRAY);
timeMs = (getTickCount() - start) / getTickFrequency() * 1000;
totalTime += timeMs;
cout << "TAPI cvtColor ms [" << timeMs<< "]" << endl;

start = getTickCount();
GaussianBlur(ugray, ugray, Size(7, 7), 1.5);
timeMs = (getTickCount() - start) / getTickFrequency() * 1000;
totalTime += timeMs;
cout << "TAPI GaussianBlur ms [" << timeMs<< "]" << endl;

start = getTickCount();
Canny(ugray, ugray, 0, 50);
timeMs = (getTickCount() - start) / getTickFrequency ...
(more)
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2015-08-07 03:41:01 -0600

LBerger gravatar image

updated 2015-08-07 06:58:33 -0600

Are you sure that opencl is not used in first case? put this line before first cvtcolor : ocl::setUseOpenCL(false);

and after // Tapi ocl::setUseOpenCL(true);

With this changed my results are

cvtColor ms [347.977]
GaussianBlur ms [4.7035]
Canny ms [5.39681]
= Total [358.078]

TAPI results
TAPI cvtColor ms [3.52646]
TAPI GaussianBlur ms [6.04416]
TAPI Canny ms [4.07012]
= Total [13.6407]

without

cvtColor ms [591.737]
GaussianBlur ms [1.75547]
Canny ms [4.09992]
= Total [597.592]

TAPI results
TAPI cvtColor ms [6.09695]
TAPI GaussianBlur ms [6.23822]
TAPI Canny ms [4.68735]
= Total [17.0225]

If you debug code you can see that some opencl codes are call with Mat

I have improve your test to have more stat (using some previous post) and changed image with path relative to my computer.

int main(int argc, char **argv)
{

    if (!cv::ocl::haveOpenCL())
    {
        cout << "OpenCL is not avaiable..." << endl;
        return 0;
    }
    cv::ocl::Context context;
    if (!context.create(cv::ocl::Device::TYPE_GPU))
    {
        cout << "Failed creating the context..." << endl;
        return 0;
    }

    // In OpenCV 3.0.0 beta, only a single device is detected.
    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                 : " << device.name() << endl;
        cout << "available            : " << device.available() << endl;
        cout << "imageSupport         : " << device.imageSupport() << endl;
        cout << "OpenCL_C_Version     : " << device.OpenCL_C_Version() << endl;
        cout << endl;
    }
    Mat gray;
    cout << "getNumberOfCPUs =" << getNumberOfCPUs() << "\t getNumThreads = " << getNumThreads() << "\n";
    Mat image=imread("F:/lib/opencv/samples/data/aloeL.jpg", CV_LOAD_IMAGE_UNCHANGED);  double totalTime = 0;
    UMat uimage;
    UMat ugray;
    imread("F:/lib/opencv/samples/data/aloeL.jpg", CV_LOAD_IMAGE_UNCHANGED).copyTo(uimage);
    vector<double> tps;
    int nbTest=100;
    for (int nTest=0;nTest<nbTest;nTest++)
    {
        int64 start = getTickCount();
        ocl::setUseOpenCL(false);
        cvtColor(image, gray, COLOR_BGR2GRAY);
        double timeMs = (getTickCount() - start) / getTickFrequency() * 1000;
        tps.push_back(timeMs);
        totalTime += timeMs;
        cout << "cvtColor ms [" << timeMs<< "]" << endl;

        start = getTickCount();
        GaussianBlur(gray, gray, Size(7, 7), 1.5);
        timeMs = (getTickCount() - start) / getTickFrequency() * 1000;
        tps.push_back(timeMs);
        totalTime += timeMs;
        cout << "GaussianBlur ms [" << timeMs<< "]" << endl;

        start = getTickCount();
        Canny(gray, gray, 0, 50);
        timeMs = (getTickCount() - start) / getTickFrequency() * 1000;
        tps.push_back(timeMs);
        totalTime += timeMs;
        cout << "Canny ms [" << timeMs<< "]" << endl;
        cout << "= Total [" <<  totalTime << "]" << endl;

        // TAPI
        ocl::setUseOpenCL(true);
        cout << endl << "TAPI results" << endl;
        totalTime = 0;

        start = getTickCount();
        cvtColor(uimage, ugray, COLOR_BGR2GRAY);
        timeMs = (getTickCount() - start) / getTickFrequency() * 1000;
        tps.push_back(timeMs);
        totalTime += timeMs;
        cout << "TAPI cvtColor ms [" << timeMs<< "]" << endl;

        start = getTickCount();
        GaussianBlur(ugray, ugray, Size(7, 7), 1.5);
        timeMs = (getTickCount() - start) / getTickFrequency() * 1000;
        tps.push_back(timeMs);
        totalTime += timeMs;
        cout << "TAPI GaussianBlur ms [" << timeMs<< "]" << endl;

        start = getTickCount();
        Canny(ugray, ugray, 0, 50);
        timeMs = (getTickCount() - start) / getTickFrequency() * 1000;
        tps.push_back(timeMs);
        totalTime += timeMs;
        cout << "TAPI Canny ms [" << timeMs<< "]" << endl;
        cout << "= Total [" <<  totalTime << "]" << endl;  
    }
    vector<double> mean,var;
    mean.resize(6);
    var.resize(6);
    for (int j = 0; j < 6; j++)
    {
        mean[j] = 0.;
        var[j] = 0.;
    }

    for (int i=0;i<nbTest;i++)
    {
        for (int j=0;j<6;j++)
            mean[j] += tps[6*i+j];
    }
    for (int j=0;j<6;j++)
        mean[j ...
(more)
edit flag offensive delete link more

Comments

I did exactly what you said and the results are much more confusing...

cvtColor ms [0.242088]
GaussianBlur ms [0.580397]
Canny ms [1.18715]
= Total [2.00964]

TAPI results
TAPI cvtColor ms [14.6621]
TAPI GaussianBlur ms [14.7547]
TAPI Canny ms [181.67]
= Total [211.087]

In my understanding methods using Mat must work slower than with UMat. Even with forced turned off use of OpenCL in the first case. But it's totally the other way. I'm really confused..

Grigory Ptashko gravatar imageGrigory Ptashko ( 2015-08-07 04:04:00 -0600 )edit

What is image size (uimage and image)?

LBerger gravatar imageLBerger ( 2015-08-07 05:21:52 -0600 )edit

The file size is 15K. It is actually the lena.jpg that comes with the opencv samples.

The size of matrices from the debugger:

image   cv::Mat     
dims    int 2   2
rows    int 225 225
cols    int 200 200

uimage  cv::UMat        
dims    int 2   2
rows    int 225 225
cols    int 200 200
Grigory Ptashko gravatar imageGrigory Ptashko ( 2015-08-07 05:56:53 -0600 )edit

Here are my results of your test:

1 GPU devices are detected.
name                 : GeForce GT 330M
available            : 1
imageSupport         : 1
OpenCL_C_Version     : OpenCL C 1.1 

getNumberOfCPUs [4] getNumThreads [512]

...

Without opencl/ with opencl  for cvtColor(0),Blur(1),Canny(2)
test 0 = 0.193865(+/-0.113415) /0.393258(+/-0.792693)
test 1 = 0.391398(+/-0.222591) /1.38872(+/-1.74931)
test 2 = 0.908895(+/-0.257745) /2.11392(+/-4.36291)

And what are yours?

Grigory Ptashko gravatar imageGrigory Ptashko ( 2015-08-07 08:06:30 -0600 )edit

Are you using Aloel image?

  1 GPU devices are detected.
    name                 : GeForce GTX 970
    available            : 1
    imageSupport         : 1
    OpenCL_C_Version     : OpenCL C 1.2


getNumberOfCPUs =12      getNumThreads = 12
Without opencl/ with opencl  for cvtColor(0),Blur(1),Canny(2)
test 0 = 0.631577(+/-0.149334) /0.351649(+/-0.954821)
test 1 = 5.2511(+/-2.46966) /3.9625(+/-1.04206)
test 2 = 18.3767(+/-4.73426) /2.74781(+/-1.45296)
LBerger gravatar imageLBerger ( 2015-08-07 08:20:21 -0600 )edit

Ok, now I used the aloeL image. And the results changed dramatically in favor of OpenCL. Here are the results:

Without opencl/ with opencl  for cvtColor(0),Blur(1),Canny(2)
test 0 = 2.43725(+/-0.370315) /0.204953(+/-0.219866)
test 1 = 6.33891(+/-1.01956) /2.03366(+/-14.2318)
test 2 = 26.6822(+/-2.07312) /2.30163(+/-8.92984)

So what's the verdict? Small image processing has large overhead for OpenCL? Correct me if I'm wrong..

Grigory Ptashko gravatar imageGrigory Ptashko ( 2015-08-07 08:51:14 -0600 )edit

Yes because time transfer between UC and GPU is not free. If you want to win this lost time you have to ask to GPU large computing.... In your result you have large variance (number between parenthesis) in OpenCL case. Is it an error in my program or a proble with your card?

(Sorry for my bad english)

LBerger gravatar imageLBerger ( 2015-08-07 09:06:13 -0600 )edit

Laurent, thank you for such helpful comments. I will mark your comment as the answer. Regarding your question. I actually do not know. I'm just in the beginning of the way, I don't know if it is the card or smth else. I will investigate it further. BTW, I'm going to test this whole stuff on NVIDIA Jetson Tk1 with Tegra chip. I can share the results with you if you want..

Grigory Ptashko gravatar imageGrigory Ptashko ( 2015-08-07 09:12:02 -0600 )edit

Yes sure I'm interesting. I think It would be interseting to share results of a test program using opencv with opencl on a website.

May be there is already web site..

LBerger gravatar imageLBerger ( 2015-08-07 09:24:43 -0600 )edit
0

answered 2017-05-25 09:20:42 -0600

Elliot_ gravatar image

While investigating the same thing, I was looking in the code of cv::GaussianBlur(...), and it looks as though it only uses opencl when the kernel size is either 3x3 or 5x5. Can this be right?

image description

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-08-07 03:22:06 -0600

Seen: 1,196 times

Last updated: Aug 07 '15