【问题标题】:OpenCV 3.4 C++ Cuda acceleration takes more time than CPUOpenCV 3.4 C++ Cuda 加速比 CPU 需要更多时间
【发布时间】:2018-09-15 05:54:18
【问题描述】:

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

【问题讨论】:

  • 您正在使用有史以来最小、最慢的 CUDA GPU 之一运行。也许更好的问题是为什么你会期望 GPU 更快?
  • @talonmies 现在一切都清楚了 :) 快速 CUDA GPU 的主要标准是什么? CUDA 内核数、帧缓冲区、时钟速度还是其他?
  • 是的 cuda 核心是一个因素,但它与时钟速度和内存速度有关,所以是的,48 个 cuda 核心并没有那么多(我有 1050 ti 和 768 个 cuda 核心,它不是最好的市场)
  • 中值过滤器在 gpu 上也不应该那么好。

标签: c++ opencv gpu hardware-acceleration


【解决方案1】:

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

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

【讨论】:

  • 你的第一句话是错误的。 GPU 被列为here 并支持 CUDA,尽管仅通过 CUDA 6.5。但是,由于 OP 的帖子清楚地表明使用 CUDA 6.5,我没有看到任何证据支持您的说法。
  • @RobertCrovella 感谢您的评论。我删除了错误的陈述。我没有看过旧版列表。
  • 第二个链接主要处理 GPU 的初始化时间问题,但 OpenCV GPU 代码有时尽管初始化时间较慢
猜你喜欢
  • 2011-11-23
  • 1970-01-01
  • 2022-06-16
  • 1970-01-01
  • 2019-04-29
  • 2022-01-19
  • 2020-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多