How can I get an OpenMP program to use multiple cores on macOS

Viewed 462

I have an application which uses a third party library which relies on OpenMP for performance.

I have successfully built the app on macOS and have tested it and found that it runs correctly in the sense that if I set OMP_NUM_THREADS the correct number of threads are run (as reported by Activity Monitor and some diagnostic output statements.

The problem is that it seems to execute these threads on a single core and I want each thread on a separate core; my processing is CPU intensive and I will gain little to nothing from context switching on a single core.

The OpenMP documentation suggests using KMP_AFFINITY for this but explicitly says that this is not supported on OS X.

Is there any way to force the distribution of OpenMP threads over multiple cores?

PS I am aware that I could use GCD and a host of other solutions to try to solve this but I'm using a third party cross platform library so these options are not viable for me.

EDIT

After further reading I've managed to extract more info using OMP_DISPLAY_ENV and OMP_DISPLAY_AFFINITY as shown below.

OMP_NUM_THREADS=4 OMP_PLACES=cores OMP_PROC_BIND=spread  OMP_DISPLAY_AFFINITY=true OMP_DISPLAY_ENV=true  ../bin/my_app

OPENMP DISPLAY ENVIRONMENT BEGIN
   _OPENMP='201611'
  [host] OMP_AFFINITY_FORMAT='OMP: pid %P tid %i thread %n bound to OS proc set {%A}'
  [host] OMP_ALLOCATOR='omp_default_mem_alloc'
  [host] OMP_CANCELLATION='FALSE'
  [host] OMP_DEFAULT_DEVICE='0'
  [host] OMP_DISPLAY_AFFINITY='TRUE'
  [host] OMP_DISPLAY_ENV='TRUE'
  [host] OMP_DYNAMIC='FALSE'
  [host] OMP_MAX_ACTIVE_LEVELS='1'
  [host] OMP_MAX_TASK_PRIORITY='0'
  [host] OMP_NESTED: deprecated; max-active-levels-var=1
  [host] OMP_NUM_THREADS='4'
  [host] OMP_PROC_BIND='spread'
  [host] OMP_SCHEDULE='static'
  [host] OMP_STACKSIZE='8M'
  [host] OMP_TARGET_OFFLOAD=DEFAULT
  [host] OMP_THREAD_LIMIT='2147483647'
  [host] OMP_TOOL='enabled'
  [host] OMP_TOOL_LIBRARIES: value is not defined
  [host] OMP_WAIT_POLICY='PASSIVE'
OPENMP DISPLAY ENVIRONMENT END


OMP: pid 6686 tid 2795865 thread 0 bound to OS proc set {undefined}
OMP: pid 6686 tid 2795867 thread 1 bound to OS proc set {undefined}
OMP: pid 6686 tid 2795868 thread 2 bound to OS proc set {undefined}
OMP: pid 6686 tid 2795869 thread 3 bound to OS proc set {undefined}

I've experimented setting OMP_PLACES to cores, threads and sockets but the affinity always shows undefined. Could this be part of the problem? Do I need to specify places somehow?

0 Answers
Related