关于Haclon使用GPU加速的代码实例

时间:2022-02-04 14:17:21

关于Haclon使用GPU加速的代码实例

read_image(Image, 'T20170902014819_58_2_1.bmp')
*没有加加速并行处理
count_seconds(T1)
for i:= to by
rotate_image(Image, Image1, , 'constant')
endfor
count_seconds(T2)
Time1:=(T2-T1)*
stop() *以下两种加速只能选一种 *GPU加速,支持GPU加速的算子Halcon10只有56个
query_available_compute_devices(DeviceIdentifiers)
DeviceHandle:=
for i:= to |DeviceIdentifiers|- by
get_compute_device_info(DeviceIdentifiers[i], 'name', Nmae)
if(Nmae='Geoforce GTX 1070')//根据GPU名称打开GPU
open_compute_device(DeviceIdentifiers[i], DeviceHandle)
break
endif
endfor
if(DeviceHandle#)
set_compute_device_param (DeviceHandle, 'asynchronous_execution', 'false')
init_compute_device(DeviceHandle, 'rotate_image')
activate_compute_device(DeviceHandle)
endif
count_seconds(T3)
for i:= to by
rotate_image(Image, Image1, , 'constant')
endfor
count_seconds(T4)
Time2:=(T4-T3)*
if(DeviceHandle#)
deactivate_compute_device(DeviceHandle)
endif
stop() *自动加速并行处理
optimize_aop ('rotate_image', 'byte', 'no_file', ['file_mode','model','parameters'], ['nil','threshold','false'])
set_system ('parallelize_operators', 'true')//只需开一次
count_seconds(T5)
for i:= to by
rotate_image(Image, Image1, , 'constant')
endfor
count_seconds(T6)
Time3:=(T6-T5)*
set_system ('parallelize_operators', 'false')//只需最后关一次
stop() set_display_font (, , 'mono', 'true', 'false')
dev_clear_window()
Message:='CPU only Time1='+Time1+'ms, '
disp_message (, Message, 'window', , , 'red', 'false')
stop()
dev_clear_window()
Message:='GPU use Time2='+Time2+'ms, '
disp_message (, Message, 'window', , , 'red', 'false')
stop()
dev_clear_window()
Message:='optimize Time3='+Time3+'ms, '
disp_message (, Message, 'window', , , 'red', 'false')