|
Article ID: 285
Last updated: 31 Dec, 2025
The Intel compiler's OpenMP runtime library has the ability to bind OpenMP threads to physical processing units. Depending on the system topology, application, and operating system, thread affinity can have a dramatic effect on code performance. We recommend two approaches for using the Intel OpenMP thread affinity capability. Using the KMP_AFFINITY Environment VariableThe thread affinity interface is controlled using the KMP_AFFINITY environment variable. SyntaxFor csh and tcsh:
For sh, bash, and ksh:
Using the Compiler Flag -par-affinity Compiler OptionNote: This approach does not apply when using the Intel OneAPI ifx compiler which does not support the -par-affinity flag. Starting with the Intel compiler version 11.1, thread affinity can be specified through the compiler option -par-affinity. The use of -openmp or -parallel is required in order for this option to take effect. This option overrides the environment variable when both are specified. See man ifort for more information. Note: Starting with comp-intel/2015.0.090, -openmp is deprecated and has been replaced with -qopenmp. Syntax
Possible Values for typeFor both of the recommended approaches, the only required argument is type, which indicates the type of thread affinity to use. Descriptions of all of the possible arguments (type, modifier, permute, and offset) can be found in man ifort. Recommendation: Use Intel compiler versions 11.1 and later, as some of the affinity methods described below are not supported in earlier versions. Possible values for type are:
Additional information from Intel: "The thread affinity type of KMP_AFFINITY environment variable defaults to none (KMP_AFFINITY=none). The behavior for KMP_AFFINITY=none was changed in 10.1.015 or later, and in all 11.x compilers, such that the initialization thread creates a "full mask" of all the threads on the machine, and every thread binds to this mask at startup time. It was subsequently found that this change may interfere with other platform affinity mechanism, for example, dplace() on Altix machines. To resolve this issue, a new affinity type disabled was introduced in compiler 10.1.018, and in all 11.x compilers (KMP_AFFINITY=disabled). Setting KMP_AFFINITY=disabled will prevent the runtime library from making any affinity-related system calls."
Usage example:
Note: For most OpenMP codes, type=scatter should provide the best performance, as it minimizes cache and memory bandwidth contention for all processor models. Usage example:
Usage example:
For nodes that support hyperthreading, you can use the granularity modifier to specify whether to pin OpenMP threads to physical cores using granularity=core (the default) or pin to logical cores using granularity=fine or granularity=thread for the compact and scatter types. ExamplesThe following example illustrates the thread placement of an OpenMP job with four threads on an Intel Broadwell node with two sockets with different thread affinity methods. The variable OMP_NUM_THREADS is set to 4:
The use of the verbose modifier is recommended, as it provides an output with the placement. BroadwellAs seen in the configuration diagram of a Broadwell node, each set of 14 physical cores in a socket share the same L3 cache. Four threads running on 1 node (28 physical cores and 56 logical cores due to hyperthreading) of Broadwell will get the following thread placement: |