Showing results for 
Search instead for 
Did you mean: 

how to set omp environment from executable?

Hi there,

I am wondering whether it is possible to setup the OMP environment, namely, OMP_PLACES and OMP_PROC_BIND by the executable at the start of the program, so NOT via bashrc etc. The reason is that users may forget to or not be aware of how to do the proper bashrc settings.

I found an old thread where people used

success = SETENVQQ("OMP_PLACES=cores")

form inside the executable, which is what I want too, but I am wondering whether that has any effect at all once the program runs.

I tried this:

Program Test
  !$ use omp_lib
  implicit none
  character(len=50) :: val
  integer :: istat
  !$ write(*,*) omp_get_proc_bind()
  call GET_ENVIRONMENT_VARIABLE(name="OMP_PROC_BIND",value=val,Status=istat)
  write(*,*) "OMP_PROC_BIND: ", trim(adjustl(val))
  istat = SETENVQQ("OMP_PROC_BIND=spread")
  call GET_ENVIRONMENT_VARIABLE(name="OMP_PROC_BIND",value=val,Status=istat)
  write(*,*) "OMP_PROC_BIND: ", trim(adjustl(val))
  !$ write(*,*) omp_get_proc_bind()
End Program Test

and the ouput was that:

 OMP_PROC_BIND: spread

which, according to the omp manual, implies that "OMP_PROC_BIND" is still "false" after setting the environment variable.

Any suggestions are highly appreciated.


0 Kudos
2 Replies
Black Belt

I don't think there's a way

I don't think there's a way to do this from inside the executable. As best as I can tell, the environment variables are queried in a call made automatically by the compiler at the start of the main program, before any of your statements are executed.

Maybe you could supply your program as a script that sets the variables and then runs the executable?

Steve (aka "Doctor Fortran") -
0 Kudos

If you really want to play

If you really want to play these games, look at Low Level Affinity API. Or see Portable Hardware Locality (hwloc) library for more abstraction.

There is also PROC_BIND clause to !$OMP PARALLEL directive.

You can also build your sources with prescribed thread affinity via compiler options: par-affinity, Qpar-affinity


Note. Options above will bind threads to "places" or "cores" or will modify threads affinity. They will not modify the original environment that is passed to the process as it is initiated.


Modifying environment variables in runtime is not portable and is a bad idea.

0 Kudos