ffttyy
ffttyy

Reputation: 864

OpenCV 3.4 C++ Cuda acceleration takes more time than CPU

I am testing OpenCV GPU acceleration with CUDA but GPU is slower than CPU. Is it only about median filter or am I doing something wrong in my code? And Why is pure process time on GPU higher than CPU?

Output:

Device 0:  "GeForce GT 330M"  1023Mb, sm_12 (not Fermi), 
48 cores, Driver/Runtime ver.6.50/6.50
Size of the Image: 512 x 512
GPU Time Includes up&download Times: 8531/100 = 85ms
GPU Time Includes only 'apply': 8307/100 = 83ms
CPU Time: 1855/100 = 18ms

Code:

void CPUvsGPU()
{
    QElapsedTimer timer;
    Mat cSrc;
    Mat cGray;
    cuda::GpuMat gGray;
    cuda::printShortCudaDeviceInfo(cuda::getDevice());
    cSrc = imread("baboon.jpg");
    cout << "Size of the Image: " << cSrc.size << endl;

    cvtColor(cSrc, cGray, COLOR_BGR2GRAY);

    gGray.upload(cGray);

    Mat cOut(cGray.size(), CV_8U);
    cuda::GpuMat gOut(gGray.size(), CV_8U);

    Ptr <cuda::Filter> mf;
    mf = cuda::createMedianFilter(CV_8UC1,9);

    mf->apply(gGray, gOut);//don't measure first operation's time on GPU

    timer.start();
    for (int i = 0; i<100 ; i++)
    {
        gGray.upload(cGray);
        mf->apply(gGray, gOut);
        gOut.download(cOut);
    }
    cout << "GPU Time Includes up&download Times: " << timer.elapsed() << "/100 = " << timer.elapsed()/100 <<"ms" << endl;

    timer.start();
    for (int i = 0; i<100 ; i++)
        mf->apply(gGray, gOut);
    cout << "GPU Time Includes only 'apply': " << timer.elapsed() << "/100 = " << timer.elapsed()/100 <<"ms" << endl;

    timer.start();
    for (int i = 0; i<100 ; i++)
        medianBlur(cGray,cOut,9);
    cout << "CPU Time: " << timer.elapsed() << "/100 = " << timer.elapsed()/100 <<"ms" << endl;
}

Upvotes: 0

Views: 1196

Answers (1)

Soheyb Kadi
Soheyb Kadi

Reputation: 186

Try to look at this link, your GPU is listed in legacy GPUs.

Also try to look in GPU versions of OpenCV algorithms slower than CPU versions on my machine? and in Why Opencv GPU code is slower than CPU? for other problems to take into consideration when doing comparison. The acceleration you get isn't same for all functions. Some get small enhancement and some a very remarkable speed.

Upvotes: 1

Related Questions