Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenMP and CPU affinity

Tags:

c++

c

posix

openmp

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

Related: CPU Affinity

like image 867
Jakub M. Avatar asked Nov 30 '11 11:11

Jakub M.


People also ask

Should I set CPU affinity?

In certain cases, it is good to set CPU affinity. For instance, there are two threads which use cache intensively. So to prevent cache coherence problem, it is preferable to run these two threads on separate cores that don't share the common cache.

Why does CPU affinity improve performance?

The first benefit of CPU affinity is optimizing cache performance. The second benefit of CPU affinity is if multiple threads are accessing the same data, it makes sense to run them all on the same processor—helping us minimize cache misses.

Is CPU affinity inherited?

That's why the processor affinity mask is inherited: If you set it on the parent process, this covers all the child helper processes that process may launch as part of its execution. Another reason why you might want to set a process's affinity mask is to restrict its CPU usage.


1 Answers

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
like image 190
osgx Avatar answered Oct 11 '22 07:10

osgx