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