为什么OpenCV调用UMat操作有时需要半秒才能完成?



我第一次尝试使用GPU是令人失望的,因为奇怪的计时结果。下面是在OpenCV中使用UMat查找二进制图像顶部和底部的死区的代码片段。大多数情况下,findNonZero调用的执行时间不到1毫秒,但偶尔会超过500毫秒!延迟似乎与结果的大小无关。有人能提供一个解释和解决吗?

    UMat bin; 
    // bin is loaded with a binary image of about 60 x 60;
    int top = bin.rows;
    int bottom = 0;
    for (int i = 0; i < bin.cols; i++)
    {
        UMat r = bin.col(i);
        vector<Point> pxls;
        findNonZero( r, pxls);
        cout << pxls << endl;
        if (!pxls.empty())
        {
            if (pxls.front().y < top) top = pxls.front().y;
            if (pxls.back().y > bottom) bottom = pxls.back().y;
        }
    }

这是关于我的OpenCL支持的报告:

1 GPU devices are detected.
name:              AMD KAVERI (DRM 2.50.0, 5.8.0-36-generic, LLVM 11.0.0)
available:         1
imageSupport:      0
OpenCL_C_Version:  OpenCL C 1.1 
Number of platforms                               1
  Platform Name                                   Clover
  Platform Vendor                                 Mesa
  Platform Version                                OpenCL 1.1 Mesa 20.2.6
  Platform Profile                                FULL_PROFILE
  Platform Extensions                             cl_khr_icd
  Platform Extensions function suffix             MESA
  Platform Name                                   Clover
Number of devices                                 1
  Device Name                                     AMD KAVERI (DRM 2.50.0, 5.8.0-36-generic, LLVM 11.0.0)
  Device Vendor                                   AMD
  Device Vendor ID                                0x1002
  Device Version                                  OpenCL 1.1 Mesa 20.2.6
  Driver Version                                  20.2.6
  Device OpenCL C Version                         OpenCL C 1.1 
  Device Type                                     GPU
  Device Profile                                  FULL_PROFILE
  Device Available                                Yes
  Compiler Available                              Yes
  Max compute units                               8
  Max clock frequency                             720MHz
  Max work item dimensions                        3
  Max work item sizes                             256x256x256
  Max work group size                             256
  Preferred work group size multiple              64
  Preferred / native vector sizes                 
    char                                                16 / 16      
    short                                                8 / 8       
    int                                                  4 / 4       
    long                                                 2 / 2       
    half                                                 0 / 0        (n/a)
    float                                                4 / 4       
    double                                               2 / 2        (cl_khr_fp64)
  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                                 No
    Round to infinity                             No
    IEEE754-2008 fused multiply-add               No
    Support is emulated in software               No
    Correctly-rounded divide and sqrt operations  No
  Double-precision Floating-point support         (cl_khr_fp64)
    Denormals                                     Yes
    Infinity and NANs                             Yes
    Round to nearest                              Yes
    Round to zero                                 Yes
    Round to infinity                             Yes
    IEEE754-2008 fused multiply-add               Yes
    Support is emulated in software               No
  Address bits                                    64, Little-Endian
  Global memory size                              2142642176 (1.995GiB)
  Error Correction support                        No
  Max memory allocation                           1499849523 (1.397GiB)
  Unified memory for Host and Device              No
  Minimum alignment for any data type             128 bytes
  Alignment of base address                       32768 bits (4096 bytes)
  Global Memory cache type                        None
  Image support                                   No
  Local memory type                               Local
  Local memory size                               32768 (32KiB)
  Max number of constant args                     16
  Max constant buffer size                        1499849472 (1.397GiB)
  Max size of kernel argument                     1024
  Queue properties                                
    Out-of-order execution                        No
    Profiling                                     Yes
  Profiling timer resolution                      0ns
  Execution capabilities                          
    Run OpenCL kernels                            Yes
    Run native kernels                            No
  Device Extensions                               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 cl_khr_int64_base_atomics cl_khr_int64_extended_atomics cl_khr_fp64

OpenCL代码只有在以有效的方式使用API时才有效。OpenCV代码库非常低效地使用OpenCL,并且通过其"透明API"(cv::UMat)促进了低效的使用模式。

一些效率低下的例子:

  • 内核代码的延迟编译(第一次调用opencv函数时可能会花费很多毫秒)
  • 不必要的分配和内存传输。
  • 不必要的CPU-GPU同步。

这里有更多信息

很难确切地知道是什么使你的代码效率低下,因为你不共享你初始化cv::UMat的方式。但这里有一些一般的建议。

  1. 显式地确保分配GPU内存

auto mat = cv::UMat(size, format, cv::USAGE_ALLOCATE_DEVICE_MEMORY);

  1. 在主循环之外调用OpenCV API函数,以确保它正在编译必要的OpenCL内核

  2. 做实际的工作,但使用尽可能少的OpenCV API调用。例如,在每列上分别执行findNonZero( r, pxls)是一个坏主意。即使findNonZero有一个OpenCL后端(你需要检查实现来确保),OpenCV可能会在每次调用时同步GPU。在性能方面非常糟糕。最好一次在整个缓冲区上调用它,然后按列处理结果。

  3. 明白这一切都是无望的,学会使用OpenCL和适当的GPU分析工具来真正了解正在发生的事情。好运。

最新更新