Aizen
Aizen

Reputation: 581

Bind threads to specific CPU cores using OpenMP

I know that GOMP_CPU_AFFINITY binds threads to specific cores.

But in example what they have given here, it gives:

GOMP_CPU_AFFINITY="0 3 2 1"

Here,

thread0 gets attached to---> cpu0

thread1 gets attached to---> cpu3

thread2 gets attached to---> cpu2

thread3 gets attached to---> cpu1

This is clear.

But How can I set thread0 to core0 and core2 at same time ? What will be the value of Environment variable "GOMP_CPU_AFFINITY" for it ?

Upvotes: 2

Views: 7521

Answers (2)

Hristo Iliev
Hristo Iliev

Reputation: 74395

I would guess that CPUs 0 and 2 are hyperthreads of the same physical core, as well as CPUs 1 and 3. Intel's OpenMP library allows binding each thread to both hyperthreads with setting similar to:

KMP_AFFINITY="granularity=core,compact"

Unfortunately neither GCC (via libgomp) nor Sun/Solaris Studio allows one-to-many or many-to-many thread-to-cpu binding style. Setting GOMP_CPU_AFFINITY (GCC) or SUNW_MP_PROCBIND (Sun/Solaris Studio) allows each thread to be bound to a specific CPU but not to a set of CPUs.

With OpenMP runtimes that do not support binding styles similar to the one that Intel OpenMP supports, one can use the OS scheduler calls instead in order to modify each thread's affinity mask. This creates non-portable applications but allows to realise one-to-many binding styles. On Linux the necessary scheduler calls are sched_getaffinity(2) and sched_setaffinity(2).

Upvotes: 1

Mats Petersson
Mats Petersson

Reputation: 129374

This GOMP reference may help you. To answer your specific question `GOMP_CPU_AFFINITY="0-2:2 ..." would do that - it means "run on any processor 0-2, that is divisible by 2 (which is 0 and 2)".

At least if you are asking how you can let thread0 run on EITHER of core0 or core2 - the question you actually ask has the answer "you can't", because a thread can only be run on one core at any precise moment in time, so Thread0 can not run on two cores simultaneously.

Upvotes: 4

Related Questions