* This is a simple example program that demonstrates the
* option to call several HALCON operators or procedures in parallel.* In general, owing to the overhead of thread creation,*notinall cases a speedup can be expected,* especially,if the used operators are already parallelized
* internally using the automatic operator parallelization.** Number of loops to get a more robust time measurement
Loops :=3** Initialize visualization
dev_update_off ()
dev_close_window ()
read_image (Image,'barcode/mixed/barcodes_datacodes_mixed_01')
dev_open_window_fit_image (Image,0,0,-1,-1, WindowHandle)
set_display_font (WindowHandle,16,'mono','true','false')
dev_display (Image)
disp_message (WindowHandle,'Read codes sequentially and multithreaded (multiple times)','window',12,12,'black','true')** Part A
** Show thread creation overhead
** Thread creation causes a significant overhead.* Therefore, it isnot advisable to create threads for fast tasks.*
count_seconds (T1)for L :=1 to Loops *10 by 1
par_start<Thread1>: do_nothing ()
par_start<Thread2>: do_nothing ()
par_start<Thread3>: do_nothing ()* par_join basically takes aslongas the slowest thread (plus overhead).* Use the profiler to check execution times.
par_join ([Thread1,Thread2,Thread3])
endfor
count_seconds (T2)* Time needed to create and join the three threads in milliseconds
TimeOverhead :=1000.0*(T2 - T1)/(Loops *10)** Part B
** Show perfect parallelization
** The best speedup can be achieved forlong running tasks
* that are not parallelized internally.*
count_seconds (T1)for L :=1 to Loops by 1
par_start<Thread1>: wait_seconds (1)
par_start<Thread2>: wait_seconds (1)
par_start<Thread3>: wait_seconds (1)* par_join basically takes aslongas the slowest thread (plus overhead).* Use the profiler to check execution times.
par_join ([Thread1,Thread2,Thread3])
endfor
count_seconds (T2)* This should be about 1000 milliseconds
TimeWaitSeconds :=1000.0*(T2 - T1)/ Loops
** Part C
** Read bar codes and data codes in parallel
*** Create the bar code and data code models
create_data_code_2d_model ('QR Code',[],[], QrCodeModel)
create_data_code_2d_model ('Data Matrix ECC 200','default_parameters','maximum_recognition', Ecc200Model)
create_bar_code_model ([],[], BarCodeModel)** Training
find_data_code_2d (Image, QrCodeXldsSeq, QrCodeModel,['stop_after_result_num','train'],[2,'all'], QrCodeHandlesSeq, QrCodeStringsSeq)
find_data_code_2d (Image, Ecc200XldsSeq, Ecc200Model,['stop_after_result_num','train'],[2,'all'], Ecc200HandlesSeq, Ecc200StringsSeq)
find_bar_code (Image, BarCodeRegionsSeq, BarCodeModel,['EAN-13','Code 128'], BarCodeStringsSeq)** Execute once to fill cache
find_qr_codes (Image, QrCodeXldsSeq, QrCodeModel, QrCodeHandlesSeq, QrCodeStringsSeq)
find_ecc200_codes (Image, Ecc200Xlds, Ecc200Model, Ecc200Handles, Ecc200Strings)
find_bar_codes (Image, BarCodeRegions, BarCodeModel, BarCodeStrings)** Read codes sequentially
count_seconds (T1)for L :=1 to Loops by 1
find_qr_codes (Image, QrCodeXldsSeq, QrCodeModel, QrCodeHandlesSeq, QrCodeStringsSeq)
find_ecc200_codes (Image, Ecc200Xlds, Ecc200Model, Ecc200Handles, Ecc200Strings)
find_bar_codes (Image, BarCodeRegions, BarCodeModel, BarCodeStrings)
endfor
count_seconds (T2)*
TimeSeq :=1000.0*(T2 - T1)/ Loops
** Read codes in parallel
count_seconds (T1)for L :=1 to Loops by 1
par_start<ThreadQRCode>: find_qr_codes (Image, QrCodeXlds, QrCodeModel, QrCodeHandlesSeq, QrCodeStringsSeq)
par_start<ThreadECC200>: find_ecc200_codes (Image, Ecc200Xlds, Ecc200Model, Ecc200Handles, Ecc200Strings)
par_start<ThreadBarcode>: find_bar_codes (Image, BarCodeRegions, BarCodeModel, BarCodeStrings)* par_join basically takes aslongas the slowest thread (plus overhead).
par_join ([ThreadQRCode,ThreadECC200,ThreadBarcode])
endfor
count_seconds (T2)
TimePar :=1000.0*(T2 - T1)/ Loops
** Calculate speedup
Speedup :=100*((TimeSeq / TimePar)-1)** Display result
dev_set_draw ('margin')
dev_set_line_width (4)
dev_set_color ('yellow')
dev_display (QrCodeXlds)
dev_set_color ('green')
dev_display (Ecc200Xlds)
dev_set_color ('cyan')
dev_display (BarCodeRegions)
Color :=['forest green','red']
disp_message (WindowHandle,'Multithreading speedup: '+ Speedup$'.2f'+'%','window',42,12, Color[Speedup <1],'true')*
clear_bar_code_model (BarCodeModel)
clear_data_code_2d_model (Ecc200Model)
clear_data_code_2d_model (QrCodeModel)