Halcon GPU算法加速测试

Halcon GPU算法加速测试

请添加图片描述

基本流程

官方加速程序:compute_devices.hdev

1.获取显示本机显卡信息

2.打开、激活设备、准备测试数据

3.GPU 循环测试执行 (affine_trans_image)

4.GPU 循环测试执行(affine_trans_image) + 数据传入传出

5.CPU 循环测试执行(affine_trans_image)

6.显示结果

代码


* 使用CPU与GPU 选择运行
dev_update_off ()
dev_close_window ()
dev_open_window_fit_size (0, 0, 640, 480, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')

* 1.获取显示本机显卡信息 *********************************************
* 获取所有有效的 计算设备
query_available_compute_devices (DeviceIdentifier)

* 未找到GPU
if (|DeviceIdentifier| == 0) 
    return ()
endif

* 显示当前设备信息
disp_message (WindowHandle, '设备信息 显卡个数: ' + |DeviceIdentifier| + ' 个GPU', 'window', 12, 12, 'black', 'true')
for Index := 0 to |DeviceIdentifier| - 1 by 1
    get_compute_device_info (DeviceIdentifier[Index], 'name', DeviceName)
    get_compute_device_info (DeviceIdentifier[Index], 'vendor', DeviceVendor)
    Message[Index] := '显卡 序号#' + Index + ': ' + '型号:' + ' ' + DeviceName
endfor
disp_message (WindowHandle, Message, 'window', 42, 12, 'white', 'false')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()


* 运行速度测试
for DeviceIndex := 0 to |DeviceIdentifier| - 1 by 1
    dev_clear_window ()
    * 2.打开、激活设备、准备测试数据 ************************************
    disp_message (WindowHandle, '激活设备 #' + DeviceIndex + ' 基准速度测试', 'window', 12, 12, 'black', 'true')
    
    * 打开计算设备(GPU)
    open_compute_device (DeviceIdentifier[DeviceIndex], DeviceHandle)
    * 停用异步执行
    set_compute_device_param (DeviceHandle, 'asynchronous_execution', 'false')
    * 设置算法affine_trans_image使用 GPU加速
    init_compute_device (DeviceHandle, 'affine_trans_image')
    * 激活使用GPU设备计算
    activate_compute_device (DeviceHandle)
    
    * 准备数据 读取图片与设置旋转平移矩阵(测试用)
    read_image (Image, './rings_and_nuts')
    hom_mat2d_identity (HomMat2DIdentity)
    hom_mat2d_scale (HomMat2DIdentity, 0.9, 0.9, 320, 240, HomMat2DScale)
    hom_mat2d_rotate (HomMat2DScale, 0.78, 320, 240, HomMat2D)
    
    * 3.GPU 循环测试执行 (affine_trans_image) ************************************
    * 设置循环次数
    Loops := 200
    * 执行第一次,缓存
    affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
    count_seconds (Before)
    for Index := 1 to Loops by 1
        affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
    endfor
    count_seconds (After)
    TimeGPU := (After - Before) * 1000.0 / Loops
    

    * 4.GPU 循环测试执行(affine_trans_image) + 数据传入传出 ************************************
    get_grayval (Image, 0, 0, Grayval)
    affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
    count_seconds (Before)
    for Index := 1 to Loops by 1
        * 确保数据 CPU==>GPU
        set_grayval (Image, 0, 0, Grayval)
        affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
        * 获取结果 GPU==>CPU
        get_image_pointer1 (ImageAffineTrans, Pointer, Type, Width, Height)
    endfor
    count_seconds (After)
    TimeGPUinclTransfer := (After - Before) * 1000.0 / Loops
    * 
    * 5.CPU 循环测试执行(affine_trans_image)************************************
    deactivate_compute_device (DeviceHandle)
    affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
    count_seconds (Before)
    for Index := 1 to Loops by 1
        affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
    endfor
    count_seconds (After)
    TimeCPU := (After - Before) * 1000.0 / Loops
    
    * 6.显示结果************************************
    SpeedUp := TimeCPU / TimeGPU
    Message := 'affine_trans_image 运行时间:'
    Message[1] := 'GPU #' + DeviceIndex + ' (除去传输): ' + TimeGPU$'.2f' + ' ms'
    Message[2] := 'GPU #' + DeviceIndex + ' (包括传输): ' + TimeGPUinclTransfer$'.2f' + ' ms'
    Message[3] := 'CPU:              ' + TimeCPU$'.2f' + ' ms'
    Message[4] := ' '
    Message[5] := '加速倍数: ' + SpeedUp$'.1f'
    disp_message (WindowHandle, Message, 'window', 42, 12, 'white', 'false')
    if (DeviceIndex < |DeviceIdentifier| - 1)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor

请添加图片描述
请添加图片描述

参考链接

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
HALCON提供了对计算机视觉系统进行加速GPU算子。根据引用提到的,并不是所有的视觉操作都能受益于并行化(包括GPU加速)的方式,因此HALCON的智能算法会考虑到具体的算法算法的输入值和硬件条件来确定是否需要使用GPU加速。 根据引用中提到的计时器,可以通过记录双核GPU和CPU的计算效率来评估GPU加速的效果。具体的加速因子取决于使用的HALCON算子和图像大小。引用中的一个例子展示了在一个包含两个Quad-Core Intel Xeon E5345,2.33 GHz的计算机上,使用median_image算子对1280×1024的图像进行滤波操作时,根据使用的CPU核的数量的不同,加速因子可以达到1.96到6.93倍不等。 因此,HALCON提供了一些支持GPU加速的算子,但具体的加速效果取决于算法、输入值和硬件条件等因素。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [关于实现Halcon算法加速的基础知识(CPU多核并行/GPU)](https://blog.csdn.net/libaineu2004/article/details/104202063)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [Halcon-GPU加速算子](https://blog.csdn.net/weixin_41405284/article/details/107440731)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值