Is it good practice to store state within a subroutines using static variables? I have in mind something like the following:
subroutine somesub()
logical, save :: first_call = .true.
if (first_call) then
! do some stuff
first_call = .false.
end if
! ... perform standard work
end subroutine somesub
Examples of "some stuff" include:
- Allocating work arrays. This is probably best done by automatic arrays.
- Initializing physical/mathematical constants which require some sort of runtime evaluation. A good example is
pi = datan(1.0)*4d0
. - Reading in some parameters and physical contants from a namelist or text file.
According to this question, using static variables to store such state is frowned upon in instances with multithreading. Would this be an issue with openmp or mpi in a typical fortran physics simulation code?
What alternative do you recommend for dealing with physical constants and user modifiable parameters which are specific to specific subroutine?