OpenCV 3.4 C++ Cuda 加速比 CPU 花费更多时间

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

本文关键字:时间 CPU 加速比 C++ Cuda OpenCV      更新时间:2023-10-16

我正在使用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;
}

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

还要尝试查看比我机器上的CPU版本慢的OpenCV算法的GPU版本?以及为什么Opencv GPU代码比CPU慢?,以便在进行比较时要考虑的其他问题。对于所有功能,您获得的加速并不相同。有些得到了小的增强,有些则速度非常惊人。