OpenCV 3.4 C ++ Cuda加速比CPU占用更多时间

时间:2018-04-05 11:59:27

标签: c++ opencv gpu hardware-acceleration

我正在使用CUDA测试OpenCV GPU加速,但GPU比CPU慢。它只是关于中值滤波器还是我在代码中做错了什么?为什么GPU上的纯处理时间高于CPU?

输出:

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

代码:

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;
}

1 个答案:

答案 0 :(得分:1)

尝试查看此link,您的GPU列在旧版GPU中。

在进行比较时,还要尝试查看GPU versions of OpenCV algorithms slower than CPU versions on my machine?Why Opencv GPU code is slower than CPU?中的其他问题。您获得的加速度对于所有功能都不相同。有些得到了很小的提升,有些得到了非常显着的提升。