Bind threads to specific CPU cores using OpenMP

隐身守侯 提交于 2019-12-08 06:06:59

问题


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 ?


回答1:


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.




回答2:


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).



来源:https://stackoverflow.com/questions/14049532/bind-threads-to-specific-cpu-cores-using-openmp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!