Toybrick

OpenCV使用OpenCL/GPU的性能测试

badboy

中级会员

积分
257
楼主
发表于 2020-11-2 11:31:09    查看: 8101|回复: 6 | [复制链接]    打印 | 只看该作者
测试代码如下:
  1. import cv2
  2. import timeit

  3. print('OpenCL available:', cv2.ocl.haveOpenCL())

  4. # A simple image pipeline that runs on both Mat and Umat
  5. def img_cal(img, mode='none'):
  6.     if mode=='UMat':
  7.         img = cv2.UMat(img)
  8.     img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  9.     img = cv2.GaussianBlur(img, (7, 7), 1.5)
  10.     img = cv2.Canny(img, 0, 50)
  11.     if type(img) == cv2.UMat:
  12.         img = cv2.UMat.get(img)
  13.     return img

  14. # Timing function
  15. def run(processor, function, n_threads, N):
  16.     cv2.setNumThreads(n_threads)
  17.     t = timeit.timeit(function, globals=globals(), number=N)/N*1000
  18.     print('%s avg. with %d threads: %0.2f ms' % (processor, n, t))
  19.     return t

  20. img = cv2.imread('a.tif')
  21. N = 100
  22. threads = [1,  6]

  23. processor = {'GPU': "img_cal(img,'UMat')",
  24.              'CPU': "img_cal(img)"}
  25. results = {}
  26. for n in threads:
  27.     for pro in processor.keys():
  28.         results[pro,n] = run(processor=pro,
  29.                              function= processor[pro],
  30.                              n_threads=n, N=N)

  31. print('\nGPU speed increase over 1 CPU thread [%%]: %0.2f' % \
  32.       (results[('CPU', 1)]/results[('GPU', 1)]*100))
  33. print('CPU speed increase on 6 threads versus 1 thread [%%]: %0.2f' % \
  34.       (results[('CPU', 1)]/results[('CPU', 6)]*100))
  35. print('GPU speed increase versus 6 threads [%%]: %0.2f' % \
  36.       (results[('GPU', 1)]/results[('GPU', 6)]*100))
复制代码
测试结果为:
  1. toybrick@debian10:~/temp/pytest$ /usr/bin/python3 /home/toybrick/temp/pytest/main.py
  2. OpenCL available: True
  3. GPU avg. with 1 threads: 25.12 ms
  4. CPU avg. with 1 threads: 3.34 ms
  5. GPU avg. with 6 threads: 16.39 ms
  6. CPU avg. with 6 threads: 3.14 ms

  7. GPU speed increase over 1 CPU thread [%]: 13.31
  8. CPU speed increase on 6 threads versus 1 thread [%]: 106.54
  9. GPU speed increase versus 6 threads [%]: 153.28

复制代码
使用了OpenCL/GPU后,性能反而下降了,好奇怪?!
回复

使用道具 举报

badboy

中级会员

积分
257
沙发
 楼主| 发表于 2020-11-2 11:32:58 | 只看该作者
  1. toybrick@debian10:~$ clinfo
  2. Number of platforms                               1
  3.   Platform Name                                   ARM Platform
  4.   Platform Vendor                                 ARM
  5.   Platform Version                                OpenCL 1.2 v1.r18p0-01rel0.5630b190419266e7fe8b09ec0007fb39
  6.   Platform Profile                                FULL_PROFILE
  7.   Platform Extensions                             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_byte_addressable_store cl_khr_3d_image_writes cl_khr_fp64 cl_khr_int64_base_atomics cl_khr_int64_extended_atomics cl_khr_fp16 cl_khr_icd cl_khr_egl_image cl_khr_image2d_from_buffer cl_arm_core_id cl_arm_printf cl_arm_thread_limit_hint cl_arm_non_uniform_work_group_size cl_arm_import_memory
  8.   Platform Extensions function suffix             ARM

  9.   Platform Name                                   ARM Platform
  10. Number of devices                                 1
  11.   Device Name                                     Mali-T860
  12.   Device Vendor                                   ARM
  13.   Device Vendor ID                                0x8602000
  14.   Device Version                                  OpenCL 1.2 v1.r18p0-01rel0.5630b190419266e7fe8b09ec0007fb39
  15.   Driver Version                                  1.2
  16.   Device OpenCL C Version                         OpenCL C 1.2 v1.r18p0-01rel0.5630b190419266e7fe8b09ec0007fb39
  17.   Device Type                                     GPU
  18.   Device Profile                                  FULL_PROFILE
  19.   Device Available                                Yes
  20.   Compiler Available                              Yes
  21.   Linker Available                                Yes
  22.   Max compute units                               4
  23.   Max clock frequency                             5MHz
  24.   Device Partition                                (core)
  25.     Max number of sub-devices                     0
  26.     Supported partition types                     None
  27.     Supported affinity domains                    (n/a)
  28.   Max work item dimensions                        3
  29.   Max work item sizes                             256x256x256
  30.   Max work group size                             256
  31.   Preferred work group size multiple              4
  32.   Preferred / native vector sizes                 
  33.     char                                                16 / 16      
  34.     short                                                8 / 8      
  35.     int                                                  4 / 4      
  36.     long                                                 2 / 2      
  37.     half                                                 8 / 8        (cl_khr_fp16)
  38.     float                                                4 / 4      
  39.     double                                               2 / 2        (cl_khr_fp64)
  40.   Half-precision Floating-point support           (cl_khr_fp16)
  41.     Denormals                                     Yes
  42.     Infinity and NANs                             Yes
  43.     Round to nearest                              Yes
  44.     Round to zero                                 Yes
  45.     Round to infinity                             Yes
  46.     IEEE754-2008 fused multiply-add               Yes
  47.     Support is emulated in software               No
  48.   Single-precision Floating-point support         (core)
  49.     Denormals                                     Yes
  50.     Infinity and NANs                             Yes
  51.     Round to nearest                              Yes
  52.     Round to zero                                 Yes
  53.     Round to infinity                             Yes
  54.     IEEE754-2008 fused multiply-add               Yes
  55.     Support is emulated in software               No
  56.     Correctly-rounded divide and sqrt operations  No
  57.   Double-precision Floating-point support         (cl_khr_fp64)
  58.     Denormals                                     Yes
  59.     Infinity and NANs                             Yes
  60.     Round to nearest                              Yes
  61.     Round to zero                                 Yes
  62.     Round to infinity                             Yes
  63.     IEEE754-2008 fused multiply-add               Yes
  64.     Support is emulated in software               No
  65.   Address bits                                    64, Little-Endian
  66.   Global memory size                              4029292544 (3.753GiB)
  67.   Error Correction support                        No
  68.   Max memory allocation                           1007323136 (960.7MiB)
  69.   Unified memory for Host and Device              Yes
  70.   Minimum alignment for any data type             128 bytes
  71.   Alignment of base address                       1024 bits (128 bytes)
  72.   Global Memory cache type                        Read/Write
  73.   Global Memory cache size                        262144 (256KiB)
  74.   Global Memory cache line size                   64 bytes
  75.   Image support                                   Yes
  76.     Max number of samplers per kernel             16
  77.     Max size for 1D images from buffer            65536 pixels
  78.     Max 1D or 2D image array size                 2048 images
  79.     Base address alignment for 2D image buffers   32 bytes
  80.     Pitch alignment for 2D image buffers          16 pixels
  81.     Max 2D image size                             65536x65536 pixels
  82.     Max 3D image size                             65536x65536x65536 pixels
  83.     Max number of read image args                 128
  84.     Max number of write image args                8
  85.   Local memory type                               Global
  86.   Local memory size                               32768 (32KiB)
  87.   Max number of constant args                     8
  88.   Max constant buffer size                        65536 (64KiB)
  89.   Max size of kernel argument                     1024
  90.   Queue properties                                
  91.     Out-of-order execution                        Yes
  92.     Profiling                                     Yes
  93.   Prefer user sync for interop                    No
  94.   Profiling timer resolution                      1000ns
  95.   Execution capabilities                          
  96.     Run OpenCL kernels                            Yes
  97.     Run native kernels                            No
  98.   printf() buffer size                            1048576 (1024KiB)
  99.   Built-in kernels                                (n/a)
  100.   Device Extensions                               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_byte_addressable_store cl_khr_3d_image_writes cl_khr_fp64 cl_khr_int64_base_atomics cl_khr_int64_extended_atomics cl_khr_fp16 cl_khr_icd cl_khr_egl_image cl_khr_image2d_from_buffer cl_arm_core_id cl_arm_printf cl_arm_thread_limit_hint cl_arm_non_uniform_work_group_size cl_arm_import_memory

  101. NULL platform behavior
  102.   clGetPlatformInfo(NULL, CL_PLATFORM_NAME, ...)  ARM Platform
  103.   clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, ...)   Success [ARM]
  104.   clCreateContext(NULL, ...) [default]            Success [ARM]
  105.   clCreateContextFromType(NULL, CL_DEVICE_TYPE_DEFAULT)  Success (1)
  106.     Platform Name                                 ARM Platform
  107.     Device Name                                   Mali-T860
  108.   clCreateContextFromType(NULL, CL_DEVICE_TYPE_CPU)  No devices found in platform
  109.   clCreateContextFromType(NULL, CL_DEVICE_TYPE_GPU)  Success (1)
  110.     Platform Name                                 ARM Platform
  111.     Device Name                                   Mali-T860
  112.   clCreateContextFromType(NULL, CL_DEVICE_TYPE_ACCELERATOR)  No devices found in platform
  113.   clCreateContextFromType(NULL, CL_DEVICE_TYPE_CUSTOM)  No devices found in platform
  114.   clCreateContextFromType(NULL, CL_DEVICE_TYPE_ALL)  Success (1)
  115.     Platform Name                                 ARM Platform
  116.     Device Name                                   Mali-T860
  117. toybrick@debian10:~$
复制代码
回复

使用道具 举报

jefferyzhang

版主

积分
12990
板凳
发表于 2020-11-2 15:22:51 | 只看该作者
我不明白为什么你会觉得GPU一定要跑的比CPU快?
这里GPU主频只有200-800Mhz,CPU却有1.2g-1.8g,跑一些常规算法明显CPU是快的。
GPU只有在做矩阵运算时候才有并行运算优势。
回复

使用道具 举报

badboy

中级会员

积分
257
地板
 楼主| 发表于 2020-11-2 15:57:02 | 只看该作者
这个测试代码就是做的矩阵运算。在其他平台上,GPU比CPU快很多。
回复

使用道具 举报

badboy

中级会员

积分
257
5#
 楼主| 发表于 2020-11-2 15:58:55 | 只看该作者
与CPU想比,GPU有更多的运算单元,可以并行运算。所以虽然GPU主频低,但速度比CPU快。
回复

使用道具 举报

jefferyzhang

版主

积分
12990
6#
发表于 2020-11-2 16:00:38 | 只看该作者
你可以把GPU频率抬到最高试试。
依然没有CPU快的话他就确实不会有CPU快的了。
回复

使用道具 举报

badboy

中级会员

积分
257
7#
 楼主| 发表于 2020-11-2 18:08:57 | 只看该作者
请问,如何设置GPU频率?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

产品中心 购买渠道 开源社区 Wiki教程 资料下载 关于Toybrick


快速回复 返回顶部 返回列表