Franktrt
Franktrt

Reputation: 383

Set thread affinity on two cores using OpenMP

I am using a C program, compiled with gcc 4.9.2 on Windows7, using OpenMP 4.0. My computer is dual core, with four threads. I'd like to use thread affinity spread and use 2 threads put on different cores. So when I set the environment variables from DOS with:

  • set OMP_NUM_THREADS=2
  • set OMP_PROC_BIND=spread
  • set OMP_PLACES="cores"

I get, with the variable OMP_DISPLAY_ENV=true, this:

libgomp: Invalid value for environment variable OMP_PLACES

OPENMP DISPLAY ENVIRONMENT BEGIN
  _OPENMP = '201307'
  OMP_DYNAMIC = 'FALSE'
  OMP_NESTED = 'FALSE'
  OMP_NUM_THREADS = '2'
  OMP_SCHEDULE = 'DYNAMIC'
  OMP_PROC_BIND = 'SPREAD'
  OMP_PLACES = ''
  OMP_STACKSIZE = '12872703'
  OMP_WAIT_POLICY = 'PASSIVE'
  OMP_THREAD_LIMIT = '4294967295'
  OMP_MAX_ACTIVE_LEVELS = '2147483647'
  OMP_CANCELLATION = 'FALSE'
  OMP_DEFAULT_DEVICE = '0'
OPENMP DISPLAY ENVIRONMENT END

It seems that the input is not valid, so I changed it to:

  • set OMP_PLACES="cores"
  libgomp: Affinity not supported on this configuration 
  OPENMP DISPLAY ENVIRONMENT BEGIN
  _OPENMP = '201307'
  OMP_DYNAMIC = 'FALSE'
  OMP_NESTED = 'FALSE'
  OMP_NUM_THREADS = '2'
  OMP_SCHEDULE = 'DYNAMIC'
  OMP_PROC_BIND = 'SPREAD'
  OMP_PLACES = ''
  OMP_STACKSIZE = '3107827'
  OMP_WAIT_POLICY = 'PASSIVE'
  OMP_THREAD_LIMIT = '4294967295'
  OMP_MAX_ACTIVE_LEVELS = '2147483647'
  OMP_CANCELLATION = 'FALSE'
  OMP_DEFAULT_DEVICE = '0'
  OPENMP DISPLAY ENVIRONMENT END

And this is the result: affinity not supported. I get the same result even with:

set OMP_PLACES="{0},{2},{1},{3}"

Does any of you know how to solve this?

Upvotes: 3

Views: 4814

Answers (1)

Gilles
Gilles

Reputation: 9489

I tried a simple hello world code compiled with gcc 4.9.3 on Linux, with the environment variables you proposed:

~/tmp$ OMP_DISPLAY_ENV=true OMP_NUM_THREADS=2 OMP_PROC_BIND=spread OMP_PLACES=cores ./a.out 

OPENMP DISPLAY ENVIRONMENT BEGIN
  _OPENMP = '201307'
  OMP_DYNAMIC = 'FALSE'
  OMP_NESTED = 'FALSE'
  OMP_NUM_THREADS = '2'
  OMP_SCHEDULE = 'DYNAMIC'
  OMP_PROC_BIND = 'SPREAD'
  OMP_PLACES = '{0:2},{2:2}'
  OMP_STACKSIZE = '140736864318339'
  OMP_WAIT_POLICY = 'PASSIVE'
  OMP_THREAD_LIMIT = '4294967295'
  OMP_MAX_ACTIVE_LEVELS = '2147483647'
  OMP_CANCELLATION = 'FALSE'
  OMP_DEFAULT_DEVICE = '0'
OPENMP DISPLAY ENVIRONMENT END
Hello from thread 0 / 2
Hello from thread 1 / 2

So basically, it works for me, and I believe that what you tried is legit. However, the message you get Affinity not supported on this configuration let me believe that affinity might simply not be available on your Windows 7 machine. I have no such environment to test, but the OpenMP standard says that:

The determination of whether the affinity request can be fulfilled is implementation defined. If the affinity request cannot be fulfilled, then the affinity of threads in the team is implementation defined.

I guess that's were we are...

Upvotes: 2

Related Questions