OpenMP and CPU affinity
Yes, named calls will work to set thread affinity. The only problem is to fix thread number and to set right affinity in right thread (you can try using static scheduling of for
loop for known number of threads).
As I know, almost every openmp allows to set affinity via environment. The name of environment variable varies (it was not standartized some time ago).
I use http://www.spec.org/omp2001/results/omp2001.html page to find openMP implementation and the will search for specific environment variable name. Affinity is set in ~half of specOMP results. There are some additional OpenMP performance-tuning settings in results too.
E.g. For intel compiler the variable is
export KMP_AFFINITY=compact,0
For sun compiler:
export SUNW_MP_PROCBIND=TRUE
For gcc (pre-openmp 3.1)
export GOMP_CPU_AFFINITY=0-63
where 63 is maximum CPU number (when counted from 0)
And newer OpenMP Standard, version 3.1 defines environment variable OMP_PROC_BIND (see section 4.4) which is standardized way of setting affinity in OpenMP. Usage is:
export OMP_PROC_BIND=true
Setting CPU Affinity and blocking CPU usage for background task
Setting affinity for a process means that, only that process can run on a particular CPU. However, that CPU is still in use by kernel scheduler and kernel can schedule other processes to it.
One option would be to use isolcpus kernel command line option like isolcpus=0-3. Now, 0-3 cpus will not be used by kernel and setting a process affinity to 0-3 cpus will now execute only your process and nothing else.
OpenMP parallel for region thread affinity
To the best of my knowledge, OpenMP 3.1 specifications do not provide any means to re-bind threads.
In fact, the only way to have some control over thread binding is through the OMP_PROC_BIND
environment variable:
The OMP_PROC_BIND environment variable sets the value of the global bind-var ICV. The value of this environment variable must be true or false. If the environment variable is set to true, the execution environment should not move OpenMP threads between processors. If the environment variable is set to false, the execution environment may move OpenMP threads between processors. The behavior of the program is implementation defined if the value of OMP_PROC_BIND is neither true nor false.
The OpenMP 4.0 draft extends the possible values of OMP_PROC_BIND
and adds the OMP_PLACES
environment variable, which permits to select how threads are bound to resources. Still, there is no standard way of re-binding the threads.
If this behavior is for you absolutely necessary, you may think of using the hwloc library, in particular the CPU binding part.
How to make OpenMP thread or task run on a certain core
As far as I know as of OpemMP 3.0 they're all vendor specific extensions.
For example GOMP (GCC's implementation) honours the environment variable GOMP_CPU_AFFINITY
for setting thread affinity.
In their documentation they give the example:
GOMP_CPU_AFFINITY="0 3 1-2 4-15:2"
Which they state:
will bind the initial thread to CPU 0, the second to CPU 3, the third
to CPU 1, the fourth to CPU 2, the fifth to CPU 4, the sixth through
tenth to CPUs 6, 8, 10, 12, and 14 respectively and then start
assigning back from the beginning of the list. GOMP_CPU_AFFINITY=0
binds all threads to CPU 0
Related Topics
What Does the "::" Mean in "::Tolower"
Why How to Call a Non-Constexpr Function Inside a Constexpr Function
Seeking Code Stub Generator (From Header Files)
Use of 'Auto Func(Int)' Before Deduction of 'Auto' in C++14
Dangling References and Undefined Behavior
Why Can't I Return Bigger Values from Main Function
C++11 'Native_Handle' Is Not a Member of 'Std::This_Thread'
Calling an Explicit Constructor with a Braced-Init List: Ambiguous or Not
Three Forward Slashes for Block Commenting
Constexpr Not Working If the Function Is Declared Inside Class Scope
"Volatile" Qualifier and Compiler Reorderings
What Is the Cin Analougus of Scanf Formatted Input
Why Add Void to Method Parameter List