Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7957 Discussions

Intel thread affinity environment variable for openMP?

ltkeene
Beginner
375 Views
Hello.

I'm using the Intel compiler / openMP to parallelize some work as follows:

#pragma omp parallel num_threads(4)
{
DoWork();
}


I'd like to experiment with Intel's thread affinity feature for OpenMP but I'munsure of exactlyhow to use the environment variable (for example, where do i place the environment statement statement in my code?). Could someone please give a little example showing proper usage and where to place the statement? Thank you in advance.

-L
0 Kudos
4 Replies
TimP
Honored Contributor III
375 Views
Normally, you would set the environment variable in your shell before launching the program, e.g.
export KMP_AFFINITY=compact
for Core 2 Quad: that would put consecutive threads on same cache. For Core i7, it would put consecutive threads on the same core, using both logicals, if HT is enabled.
or
KMP_AFFINITY=physical
##(for Core I7: use 1 logical per core)
Of course, you could build it into your program by a putenv() function call prior to your first omp parallel .

The verbose version, which shows what the Intel OpenMP library sees on your system, and what it does with the options you set, is obtained by appending ,0,verbose e.g. on Windows CMD shell,
SET KMP_AFFINITY=physical,0,verbose

The affinity setting is most effective with default static scheduling, using the same scheduling from the first time the array is touched. For schedule(guided), it will work well with the initial phase.

If you were using KMP_AFFINITY to promote peaceful coexistence of multiple jobs, you would have to specify a separate set of cores for each job (not so simple to explain).
0 Kudos
ltkeene
Beginner
375 Views
Quoting - tim18
Normally, you would set the environment variable in your shell before launching the program, e.g.
export KMP_AFFINITY=compact
for Core 2 Quad: that would put consecutive threads on same cache. For Core i7, it would put consecutive threads on the same core, using both logicals, if HT is enabled.
or
KMP_AFFINITY=physical
##(for Core I7: use 1 logical per core)
Of course, you could build it into your program by a putenv() function call prior to your first omp parallel .

The verbose version, which shows what the Intel OpenMP library sees on your system, and what it does with the options you set, is obtained by appending ,0,verbose e.g. on Windows CMD shell,
SET KMP_AFFINITY=physical,0,verbose

The affinity setting is most effective with default static scheduling, using the same scheduling from the first time the array is touched. For schedule(guided), it will work well with the initial phase.

If you were using KMP_AFFINITY to promote peaceful coexistence of multiple jobs, you would have to specify a separate set of cores for each job (not so simple to explain).


I'm sorry, do you mean (in WinXP) go to:

Control Panel -> System -> Advanced -> Environment Variables -> New...

and enter "KMP_AFFINITY" in the "Variable name" field and "compact" in the "Variable value" field?

-L
0 Kudos
TimP
Honored Contributor III
375 Views
Quoting - ltkeene


I'm sorry, do you mean (in WinXP) go to:

Control Panel -> System -> Advanced -> Environment Variables -> New...

and enter "KMP_AFFINITY" in the "Variable name" field and "compact" in the "Variable value" field?

You could do that, if you want always to boot up with that setting. Check in a CMD window to see that it has been set as expected after reboot.
0 Kudos
Om_S_Intel
Employee
375 Views

I have created an article to explain it with an example. The article is available at http://software.intel.com/en-us/articles/intel-thread-affinity-environment-variable-for-openmp/.
0 Kudos
Reply