OpenMP and CPU affinity

15,536

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
Share:
15,536
Jakub M.
Author by

Jakub M.

Updated on June 09, 2022

Comments

  • Jakub M.
    Jakub M. almost 2 years

    Will sched_setaffinity or pthread_attr_setaffinity_np work to set thread affinity under OpenMP?

    Related: CPU Affinity

  • Jakub M.
    Jakub M. over 12 years
    thanks! nothing on language-level, like pthread_attr_setaffinity_np?
  • osgx
    osgx over 12 years
    no, openmp standard has no thread-binding pragma or functions. The only way (and recommended one) is to bind threads via environment variable, compiler-specific or openmp3.1. This setting should be done early, before starting main.
  • Jakub M.
    Jakub M. over 12 years
    so, strictly speaking, your answer should start with "No" :)
  • osgx
    osgx over 12 years
    No, only my comment. OpenMP itself has no affinity setting functions; but OpenMP usually implemented on top of pthread and libpthread has affinity setting function. So does linux kernel. If you are able to run user-defined code in thread, you can bind thread to CPU.