This section contains additional information that helps you to use HALCON on multi-core or multi-processor hardware.
With the help of HALCON's system parameters, which can be set and queried with the operators set_system and get_system, respectively, you can customize the behavior of the parallelization mechanisms.
You can query the number of processors (or cores) by calling
get_system('processor_num', Information)
You can switch off parts of the features of HALCON with the help of the operator set_system. To switch off the automatic parallelization mechanism, call (HDevelop notation, see the Reference Manual for more information)
set_system('parallelize_operators','false')
To switch off reentrancy, call
set_system('reentrant','false')
Of course, you can switch on both behaviors again by calling set_system with 'true' as the second parameter. Please note that when switching off reentrancy you also switch off automatic parallelization, as it requires reentrancy.
A reason for switching off the automatic parallelization mechanism could be if your multithreaded program does its own scheduling and does not want HALCON to interfere via automatic parallelization. Note that you do not need to switch off automatic parallelization when using HALCON on a single-processor or single-core computer; HALCON does so automatically if it detects only one processor or core.
When switching off the automatic parallelization, you might consider switching off the use of thread pools (see the parameter 'thread_pool' of set_system).
Please do not switch on reentrancy if this is already the case! Otherwise, this will reset the parallelization system, which includes switching on the automatic operator parallelization. This will decrease the performance in case of manual parallelization (multithreading).
With the system parameter 'parallelize_operators' you can customize the automatic parallelization mechanisms in more detail. Please see the description of set_system for more information.
Finally, you can influence the number of threads used for automatic parallelization with the parameters 'thread_num' and 'tsp_thread_num' (set_system). Reducing the number of threads is useful if you also perform a manual parallelization in your program. If you switch off automatic parallelization permanently, you should also switch off the thread pool to save resources of the operating system.
All image acquisition devices supported by HALCON can be used on multi-core or multi-processor hardware. Please note, that none of the corresponding operators is automatically parallelized. Most of the operators are reentrant, only the operators concerned with the connection to the device (open_framegrabber, info_framegrabber, and close_framegrabber) are processed exclusively in their group, i.e., they block the concurrent execution of other image acquisition operators but run in parallel with all non-exclusive operators outside of this group (see open_framegrabber). Furthermore, these operators are local, i.e., under Windows they should be called from the thread that instantiates the corresponding object (see section “A Closer Look at Reentrancy”).
By default, HALCON uses spinlocks for synchronization between threads to maximize performance. However, there are several situations where using spinlocks is not recommended:
To turn spinlocks off, call the function HSetUseSpinLock(0) before calling the first HALCON operator. Please note that calling HSetUseSpinLock after HALCON has been initialized leads to undefined behavior.
Spinlocks and the HALCON thread pool
The HALCON thread pool used for the automatic operator parallelization will always use spinlocks, even if HSetUseSpinLock has been called to turn them off. Thus, it is important to make sure the thread pool does not use more threads than CPUs are actually available to run them on. You must be especially careful if your program uses any form of real-time scheduling, as your program can deadlock otherwise.
HALCON will normally always create the thread pool during initialization. If you do not need the thread pool, you can prevent it from being created by calling the function HSetStartUpThreadPool(0) before calling the first HALCON operator.
HALCON thread pool and CPU affinity on Linux
On Linux systems, threads inherit the CPU affinity of the parent thread that created them. This means that if the HALCON thread pool is enabled, its threads will be limited by the CPU affinity mask active of the thread creating the pool at the time the thread pool is created. If the pool is turned off, HALCON's automatic operator parallelization will create threads on the fly. These threads will be limited to the CPU affinity mask of the thread calling the HALCON operator being parallelized.
Please note that HALCON looks at the CPU affinity mask of the process during initialization to determine how many of the CPUs present can actually be used for automatic operator parallelization only once at startup. Thus, if you change the CPU affinity subsequently, HALCON will not notice and may end up attempting to use more CPUs than are available to it. This can degrade performance or lead to deadlocks when using any form of real-time scheduling. Problems will also ensue if the thread used to initialize HALCON uses a different CPU affinity mask than its parent process, as HALCON only takes the CPU affinity mask of the process into account.