Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28456 Discussions

how to set omp environment from executable?

may_ka
Beginner
516 Views

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
  USE IFPORT, only: SETENVQQ
  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:

          0
 OMP_PROC_BIND: 
 OMP_PROC_BIND: spread
           0

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

Any suggestions are highly appreciated.

Thanks

0 Kudos
2 Replies
Steve_Lionel
Honored Contributor III
516 Views

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?

0 Kudos
Andriy
Beginner
516 Views

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
Reply