Data parallelization means that multiple threads perform the same task on different pieces of data.
The concept of data parallelization is shown in figure 2.1.
Figure 2.1: Minimize response time using data parallelization. Here, a median filter is applied to different parts of the image simultaneously.
First, the data is split into several data pieces of approximately the same size. Then, the task is performed for each data piece individually by different threads. Finally, after the last thread has finished, the results of all threads are joined.
By default, almost all operators automatically perform a data parallelization. Note that in many cases, HALCON's automatic operator parallelization (AOP) is efficient enough so that a manual implementation of data parallelization does not lead to any further runtime enhancement. To explain the concept, in the following, an example for the manual implementation of data parallelization for a median filter is described.
The HDevelop example program hdevelop/System/Parallelization/simulate_aop.hdev shows how to implement data parallelization for a median filter (see also figure 2.1) and compares the runtime with the runtime needed for the sequential processing and for HALCON's AOP.
To use AOP, the system parameter 'parallelize_operators' must be set to 'true' (which is the default). The number of threads to be used can be set explicitely by setting the system parameter 'thread_num' to a value between 1 and the available number of cores. By default, all available cores are used for AOP.
set_system ('parallelize_operators', 'true')
median_image (Image, ImageMedianAop, 'circle', Radius, 'mirrored')
The following code shows an implementation of data parallelization by simulating AOP. As AOP should be simulated, the AOP is turned off first. Then, the image is split into pieces of approximately the same size. This is done by reducing the domains of the images to be handled by the individual threads to a respective sub-region of the domain of the input image. With the qualifier par_start (see also the HDevelop User's Guide), the individual threads that run in parallel are started. In each thread, the median is calculated for the respective image part. The results of the different threads are collected using vector variables (see also the HDevelop User's Guide).
get_system ('processor_num', Cores)
MaxNumberOfSubthreads := 20
MaxThreads := min([Cores,MaxNumberOfSubthreads])
set_system ('parallelize_operators', 'false')
for Threads := 1 to MaxThreads by 1
get_region_runs (Image, Row, ColumnBegin, ColumnEnd)
RunsPerThread := |Row| / Threads
* Split up the image and process each region by a separate thread:
for Thread := 0 to Threads - 1 by 1
IndexStart := Thread * RunsPerThread
if (Thread < Threads - 1)
IndexEnd := IndexStart + RunsPerThread - 1
else
IndexEnd := |Row| - 1
endif
gen_region_runs (Region, Row[IndexStart:IndexEnd], \
ColumnBegin[IndexStart:IndexEnd], \
ColumnEnd[IndexStart:IndexEnd])
reduce_domain (Image, Region, ImageReduced)
par_start<ThreadID.at(Thread)> : median_image (ImageReduced, \
ImageMedianThread.at(Thread), 'circle', \
Radius, 'mirrored')
endfor
convert_vector_to_tuple (ThreadID, ThreadIDs)
endfor
To wait until all involved threads are finished, the operator par_join is called. Then, the results of the individual threads are copied into one image.
par_join (ThreadIDs)
* Free references to thread IDs.
ThreadID.clear()
ThreadIDs := []
* Merge results
full_domain (ImageMedianThread.at(0), ImageMedianPar)
for ImagePart := 1 to Threads - 1 by 1
overpaint_gray (ImageMedianPar, ImageMedianThread.at(ImagePart))
endfor
The runtimes of the different cases are visualized in figure 2.2 for a machine with 4 cores. The manual parallelization has no advantage compared to the automatic operator parallelization. Due to some overhead caused by HDevelop, the runtime of the simulated AOP processing is even slightly larger than that of the AOP. But both are significantly faster than the sequential processing.
Figure 2.2: Runtimes for applying the median filter without AOP, with AOP, and with parallel programming simulating AOP on a machine with 4 cores / 8 Hardware Threads.