Halcon算法加速的基础知识(多核并行GPU)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Halcon算法加速的基础知识(多核并⾏GPU)
⼀、提⾼Halcon的运算速度,有以下⼏种⽅法:
1、Multithreading(多线程)
2、Automatic Parallelization(⾃动操作并⾏化)
3、Compute devices,利⽤GPU提速,如果显卡性能好,⾄少可以提⾼5~10倍的运算速度
⼆、多线程
1、官⽅⾃带的例程get_operator_info.hdev,可以查看⽀持多线程的算⼦;
1 * Determine the multithreading information
2 get_multithreading_operators (TypeExclusive, TypeMutual, TypeReentrant, TypeIndependent)
3 * ⾃定义函数展开之后,有get_operator_info算⼦
4 * get names of all operators of the library
5 get_operator_name ('', OperatorNames)
6 get_operator_info (OperatorNames[Index], 'parallelization', Information)
2、官⽅的⼿册
C:\Program Files\MVTec\HALCON-19.11-Progress\doc\pdf\manuals\programmers_guide.pdf
Chapter 2 Parallel Programming and HALCON
C:\Program Files\MVTec\HALCON-19.11-Progress\doc\pdf\reference\reference_hdevelop.pdf
Chapter 25 System --- 25.6 Multithreading
三、多核并⾏
看看官⽅的说明,关于HALCON-多核性能:
1、算⼦⾃动并⾏化(AOP)
Automatic Operator Parallelization (AOP)
多核和多处理器的计算机显著提升了计算机视觉系统的速度。
⼋年多以来,HALCON提供了通过⼯业验证的算⼦并⾏化,能很好地⽀持这种速度的提升。
当然,并不是全部的视觉操作都能受益于并⾏化这种⽅式。
因此,HALCON的智能算法可以确定是否需要⽤并⾏化⽅式——会考虑到具体的算法,算法的输⼊值和硬件条件。
1来⾃官⽅例程query_system_parameters.hdev
2 * Parallelization
3 get_system ('processor_num', ProcessorNum)
4 get_system ('thread_pool', ThreadPool)
5 get_system ('thread_num', ThreadNum)
6 *Automatic Operator Parallelization,默认值是true
7 get_system ('parallelize_operators', AOP)
8 *这个修饰符⽤于把函数定义为可重⼊函数,默认值是true;所谓可重⼊函数就是允许被递归调⽤的函数
9 get_system ('reentrant', Reentrant)
10 *故意关掉测试性能
11 *set_system('parallelize_operators','false')
并⾏HALCON在多核计算机上会⾃动将数据,⽐如图像数据分配给多个线程,每⼀个线程对应⼀个内核。
⽤户甚⾄不需要改动已有的HALCON程序来就能使⽤⾃动划分功能,从⽽⽴即获得显著的速度提升。
2、并⾏编程
HALCON⽀持并⾏编程,如多线程的程序。
它不仅仅是线程安全的⽽且可多次调⽤。
因此多个线程可在同⼀时刻同时调⽤HALCON算⼦。
利⽤这种特性,⽤户可以将⼀个机器视觉应⽤软件分解成多个独⽴的部分,让它们在不同的处理器上并⾏运⾏。
在⼀个四核的计算机上运⾏算⼦,HALCON会⾃动将图像分为四部分,由四个线程并⾏处理。
在⼀个包含两个Quad-Core Intel Xeon E5345,2.33 GHz在内的计算机上使⽤median_image算⼦(13×13的模板)对1280×1024的图像进⾏滤波操作时,根据使⽤CPU核的数量的不同,加速因⼦分别为*1/1.96/2.90/3.79/4.51/5.48/6.34/6.93。
注意:可以达到的最⾼加速因⼦与所⽤的HALCON算⼦和图像⼤⼩有关。
3、AOP默认是激活的
(1)Halcon⼀⽅⾯提供⾃动操作员并⾏化(AOP)和⼿动并⾏化的⼿段。
另⼀⽅⾯,对应⽤程序部分进⾏编程。
⾃动操作员并⾏化(AOP)将输⼊数据(例如图像)分割成多个部分并进⾏处理数据部分独⽴且并⾏。
这也称为数据并⾏化。
默认情况下,AOP是激活的,即这种类型的并⾏化是⾃动完成的,在许多情况下您不会⾄少对于单个操作员⽽⾔,必须关⼼进⼀步的数据并⾏化。
有关AOP的详细信息,请参见programmers_guide.pdf,第2.1节。
(2)Halcon还提供了optimize_aop算⼦,⽤于优化aop,提⾼性能。
默认情况下(即不使⽤optimize_aop算⼦),Halcon使⽤AOP的最⼤可⽤线程数,最多使⽤处理器数量。
但是,根据传递给运算符的数据⼤⼩和参数集,最⼤线程数上的并⾏化可能会过度且效率低下。
optimize_aop根据线程号优化AOP,并针对HALCON运算符的并⾏处理检查给定的硬件。
这样,它将检查每个运算符,可以通过在tuple元组,channel通道或domain level域级别(不考虑the partial level部分级别)上的⾃动并⾏化来加快操作速度。
每个检查的运算符都将执⾏⼏次(依次和并⾏),并带有⼀组不断变化的输⼊参数值/图像。
后者有助于评估操作员的输⼊参数特征(例如,输⼊图像的⼤⼩)与其并⾏处理效率之间的依赖性。
根据操作员参数的设置,这可能要花费⼏个⼩时。
对于正确的优化,⾄关重要的是不要在计算机上同时运⾏任何其他计算密集型应⽤程序,因为这会严重影响硬件检查的时间测量,从⽽导致错误的结果。
详情参见官⽅例程optimize_aop.hdev
4、查看⽀持AOP的算⼦
⾃动并⾏化⽅法,为了实现运算符的⾃动并⾏化,HALCON利⽤数据并⾏性,即操作员的输⼊数据可以彼此独⽴地进⾏处理。
数据并⾏性可以在四个位置找到。
官⽅⾃带的例程get_operator_info.hdev,可以查看;
(1)tuple level (2)channel level (3)domain level (4)internal data level
1 * Determine the parallelization method of all parallelized operators
2 get_parallel_method_operators (SplitTuple, SplitChannel, SplitDomain, SplitPartial, None)
3 AutoParallel := [SplitTuple,SplitChannel,SplitDomain,SplitPartial]
4 AutoParallel := uniq(sort(AutoParallel))
5 * ⾃定义函数展开之后,有get_operator_info算⼦
6 * get names of all operators of the library
7 get_operator_name ('', OperatorNames)
8 get_operator_info (OperatorNames[Index], 'parallel_method', Information)
5、如果程序员不想使⽤AOP,⽽是⾃⼰实现并⾏化,那较为复杂,需要使⽤多线程技术,把图像进⾏拆分处理,最后再合并。
因此需要更多专业知识,详情参见官⽅例程simulate_aop.hdev和官⽅说明书parallel_programming.pdf。
*set_system('parallelize_operators','false')
6、官⽅的⼿册
C:\Program Files\MVTec\HALCON-19.11-Progress\doc\pdf\manuals\programmers_guide.pdf
Chapter 2 Parallel Programming and HALCON
C:\Program Files\MVTec\HALCON-19.11-Progress\doc\pdf\reference\reference_hdevelop.pdf
Chapter 25 System --- 25.8 Parallelization
C:\Program Files\MVTec\HALCON-19.11-Progress\doc\pdf\manuals\parallel_programming.pdf
四、GPU
1、Halcon中使⽤GPU提速,效果明显。
Windows开始菜单--运⾏--输⼊dxdiag--显⽰,可以看到⾃⼰电脑的显卡型号。
官⽅⾃带的例程compute_devices.hdev,实现提速的优良效果,必须先关闭设备:dev_update_off();
1来⾃官⽅例程compute_devices.hdev
2 * This example shows how to use compute devices with HALCON.
3 *
4 dev_update_off ()
5 dev_close_window ()
6 dev_open_window_fit_size (0, 0, 640, 480, -1, -1, WindowHandle)
7 set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
8 *
9 * Get list of all available compute devices.
10 query_available_compute_devices (DeviceIdentifier)
11 *
12 * End example if no device could be found.
13if (|DeviceIdentifier| == 0)
14return ()
15 endif
16 *
17 * Display basic information on detected devices.
18 disp_message (WindowHandle, 'Found ' + |DeviceIdentifier| + ' Compute Device(s):', 'window', 12, 12, 'black', 'true')
19for Index := 0 to |DeviceIdentifier| - 1 by 1
20 get_compute_device_info (DeviceIdentifier[Index], 'name', DeviceName)
21 get_compute_device_info (DeviceIdentifier[Index], 'vendor', DeviceVendor)
22 Message[Index] := 'Device #' + Index + ': ' + DeviceVendor + '' + DeviceName
23 endfor
24 disp_message (WindowHandle, Message, 'window', 42, 12, 'white', 'false')
25 disp_continue_message (WindowHandle, 'black', 'true')
26 stop ()
2、操作GPU设备有关的算⼦:
query_available_compute_devices
get_compute_device_info
open_compute_device
init_compute_device
activate_compute_device
deactivate_compute_device
3、官⽅⾃带的例程get_operator_info.hdev,可以查看⽀持GPU加速(OpenCL)的算⼦;
1 * Determine all operators that support OpenCL
2 get_opencl_operators (OpenCLSupport)
3 * ⾃定义函数展开之后,有get_operator_info算⼦
4 get_operator_name ('', OperatorNames)
5 get_operator_info (OperatorNames[Index], 'compute_device', Information)
这⾥举例Halcon 19.11版本可以加速的算⼦有82个:
['abs_diff_image', 'abs_image', 'acos_image', 'add_image', 'affine_trans_image', 'affine_trans_image_size', 'area_center_gray', 'asin_image',
'atan2_image', 'atan_image', 'binocular_disparity_ms', 'binocular_distance_ms', 'binomial_filter', 'cfa_to_rgb', 'change_radial_distortion_image',
'convert_image_type', 'convol_image', 'cos_image', 'crop_domain', 'crop_part', 'crop_rectangle1', 'depth_from_focus', 'derivate_gauss',
'deviation_image', 'div_image', 'edges_image', 'edges_sub_pix', 'exp_image', 'find_ncc_model', 'find_ncc_models', 'gamma_image', 'gauss_filter', 'gauss_image', 'gray_closing_rect', 'gray_closing_shape', 'gray_dilation_rect', 'gray_dilation_shape', 'gray_erosion_rect', 'gray_erosion_shape',
'gray_histo', 'gray_opening_rect', 'gray_opening_shape', 'gray_projections', 'gray_range_rect', 'highpass_image', 'image_to_world_plane',
'invert_image', 'linear_trans_color', 'lines_gauss', 'log_image', 'lut_trans', 'map_image', 'max_image', 'mean_image', 'median_image', 'median_rect', 'min_image', 'mirror_image', 'mult_image', 'points_harris', 'polar_trans_image', 'polar_trans_image_ext', 'polar_trans_image_inv', 'pow_image',
'principal_comp', 'projective_trans_image', 'projective_trans_image_size', 'rgb1_to_gray', 'rgb3_to_gray', 'rotate_image', 'scale_image', 'sin_image', 'sobel_amp', 'sobel_dir', 'sqrt_image', 'sub_image', 'tan_image', 'texture_laws', 'trans_from_rgb', 'trans_to_rgb', 'zoom_image_factor',
'zoom_image_size']
4、官⽅⼿册
C:\Program Files\MVTec\HALCON-19.11-Progress\doc\pdf\reference\reference_hdevelop.pdf
Chapter 25 System --- 25.1 Compute Devices
五、举例测试
1 *参考官⽅例程optimize_aop.hdev;query_aop_info.hdev;simulate_aop.hdev;
2 *举例edges_sub_pix算⼦性能测试
3 dev_update_off ()//实现提速的优良效果,必须先关闭设备
4 dev_close_window ()
5 dev_open_window_fit_size (0, 0, 640, 480, -1, -1, WindowHandle)
6 set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
7 get_system ('processor_num', NumCPUs)
8 get_system ('parallelize_operators', AOP)
9
10 *读取图⽚
11 read_image(Image, 'D:/hellowprld/2/1-.jpg')
12 *彩⾊转灰度图
13 count_channels (Image, Channels)
14if (Channels == 3 or Channels == 4)
15 rgb1_to_gray (Image, ImageGray)
16 endif
17
18 alpha:=5
19 low:=10
20 high:=20
21
22 *测试1:去掉AOP,即没有加速并⾏处理
23 set_system ('parallelize_operators', 'false')
24 get_system ('parallelize_operators', AOP)
25 count_seconds(T0)
26 edges_sub_pix (ImageGray, Edges1, 'canny', alpha, low, high)
27 count_seconds(T1)
28 Time0:=(T1-T0)*1000
29 stop()
30
31 *测试2:AOP⾃动加速并⾏处理
32 *Halcon的默认值是开启AOP的,即parallelize_operators值为true
33 set_system ('parallelize_operators', 'true')
34 count_seconds(T1)
35 edges_sub_pix (ImageGray, Edges1, 'canny', alpha, low, high)
36 count_seconds(T2)
37 Time1:=(T2-T1)*1000
38 stop()
39
40 *测试3:GPU加速,⽀持GPU加速的算⼦Halcon19.11有82个
41 *GPU加速是先从CPU中将数据拷贝到GPU上处理,处理完成后再将数据从GPU拷贝到CPU上。
从CPU到GPU再从GPU到CPU是要花费时间的。
42 *GPU加速⼀定会⽐正常的AOP运算速度快吗?不⼀定!结果取决于显卡的好坏.
43 query_available_compute_devices(DeviceIdentifiers)
44 DeviceHandle:=0
45for i:=0 to |DeviceIdentifiers|-1 by 1
46 get_compute_device_info(DeviceIdentifiers[i], 'name', Nmae)
47if (Nmae == 'GeForce GT 630')//根据GPU名称打开GPU
48 open_compute_device(DeviceIdentifiers[i], DeviceHandle)
49break
50 endif
51 endfor
52
53if(DeviceHandle#0)
54 set_compute_device_param (DeviceHandle, 'asynchronous_execution', 'false')
55 init_compute_device(DeviceHandle, 'edges_sub_pix')
56 activate_compute_device(DeviceHandle)
57 endif
58
59 *获得显卡的信息
60 get_compute_device_param (DeviceHandle, 'buffer_cache_capacity', GenParamValue0)//默认值是显卡缓存的1/3
61 get_compute_device_param (DeviceHandle, 'buffer_cache_used', GenParamValue1)
62 get_compute_device_param (DeviceHandle, 'image_cache_capacity', GenParamValue2)
63 get_compute_device_param (DeviceHandle, 'image_cache_used', GenParamValue3)
64
65 *GenParamValue0 := GenParamValue0 / 3
66 *set_compute_device_param (DeviceHandle, 'buffer_cache_capacity', GenParamValue0)
67 *get_compute_device_param (DeviceHandle, 'buffer_cache_capacity', GenParamValue4)
68
69 count_seconds(T3)
70 *如果显卡缓存不够,会报错,error #4104 : Out of compute device memory
71 edges_sub_pix (ImageGray, Edges1, 'canny', alpha, low, high)
72 count_seconds(T4)
73 Time2:=(T4-T3)*1000
74
75if(DeviceHandle#0)
76 deactivate_compute_device(DeviceHandle)
77 endif
78 stop()
79
80 *测试4:AOP⼿动优化
81 set_system ('parallelize_operators', 'true')
82 get_system ('parallelize_operators', AOP)
83
84 *4.1-优化线程数⽬⽅法'threshold'
85 optimize_aop ('edges_sub_pix', 'byte', 'no_file', ['file_mode','model','parameters'], ['nil','threshold','false'])
86
87 count_seconds(T5)
88 edges_sub_pix (ImageGray, Edges1, 'canny', alpha, low, high)
89 count_seconds(T6)
90 Time3:=(T6-T5)*1000
91
92 *4.2-优化线程数⽬⽅法'linear'
93 optimize_aop ('edges_sub_pix', 'byte', 'no_file', ['file_mode','model','parameters'], ['nil','linear','false'])
94
95 count_seconds(T7)
96 edges_sub_pix (ImageGray, Edges1, 'canny', alpha, low, high)
97 count_seconds(T8)
98 Time4:=(T8-T7)*1000
99 stop()
100
101 *4.3-优化线程数⽬⽅法'mlp'
102 optimize_aop ('edges_sub_pix', 'byte', 'no_file', ['file_mode','model','parameters'], ['nil','mlp','false'])
103
104 count_seconds(T9)
105 edges_sub_pix (ImageGray, Edges1, 'canny', alpha, low, high)
106 count_seconds(T10)
107 Time5:=(T10-T9)*1000
108 stop()
109
110 dev_clear_window()
111 Message := 'edges_sub_pix runtimes:'
112 Message[1] := 'CPU only Time0 without AOP='+Time0+'ms,'
113 Message[2] := 'CPU only Time1 with AOP='+Time1+'ms,'
114 Message[3] := 'GPU use Time2='+Time2+'ms,'
115 Message[4] := 'optimize Time3 threshold='+Time3+'ms'
116 Message[5] := 'optimize Time4 linear='+Time4+'ms'
117 Message[6] := 'optimize Time5 mlp='+Time5+'ms'
118 disp_message (WindowHandle, Message, 'window', 12, 12, 'red', 'false')
119 stop()
edges_sub_pix算⼦性能测试结果:
rotate_image算⼦性能测试结果:
得出的结论是:
1、GPU加速是先从CPU中将数据拷贝到GPU上处理,处理完成后再将数据从GPU拷贝到CPU上。
从CPU到GPU再从GPU到CPU是要花费时间的。
2、GPU加速⼀定会⽐正常的AOP运算速度快吗?不⼀定!结果取决于显卡的好坏.
3、GPU加速,如果显卡缓存不够,会报错,error #4104 : Out of compute device memory。