My understanding is the values of aval
and bval
remain defined until fmintest
exits.
The part I think is worth questioning is the implementation of fminsearch
from the toolbox
module (a generic interface for the procedure powell
). The procedure powell
makes use of several of the private module variables prefixed with tbox_
:
! should the random tbox_seed be set
logical, private :: tbox_seed = .true.
! Level of tolerance for all routines
real*8, private :: tbox_gftol = 1d-12
! Maximum number of iterations
integer, private :: tbox_itermax_min = 1000
! Boolean value for showing iterations for optimization
logical, private :: tbox_show_min = .false.
! Maximum number of iterations for brent_pow
integer, parameter, private :: tbox_tbox_itermax_pow_b = 150
! Level of tolerance for all routines
real*8, private :: tbox_gftol_root = 1d-8
! Maximum number of iterations for broydn
integer, private :: itermax_root = 200
! Boolean value for showing iterations for root finding
logical, private :: tbox_show_root = .false.
The procedure powell
only references these values so I assume this should not be a problem.
Another potential source of problems is that powell
calls brent_pow
which uses save
’d variables:
!##########################################################################
! FUNCTION brent_pow
!
! Minimizes a one dimensional function.
!##########################################################################
function brent_pow(ax, bx, cx, tol, xmin, func)
implicit none
!##### INPUT/OUTPUT VARIABLES #########################################
! left, middle and right interval points
real*8, intent(in) :: ax, bx, cx
! level of tolerance
real*8, intent(in) :: tol
! minimum value found
real*8, intent(out) :: xmin
! function value at minimum
real*8 :: brent_pow
!##### OTHER VARIABLES ################################################
real*8, parameter :: cgold = 0.3819660d0
real*8, parameter :: zeps=1.0e-3*epsilon(ax)
integer :: iter
real*8 :: a=0d0, b=0d0, d=0d0, e=0d0, etemp=0d0 ! SAVE
real*8 :: fu, fv, fw, fx, p, q, r, tol1, tol2, &
u, v, w, x, xm
This means there is a race condition on the variables a
, b
, d
, e
, and etemp
. Essentially, the toolbox
is not written in a thread-safe way.
In general, the toolbox
code is written a bit sloppily (for instance it uses single precision in literals in several places). I’m also a bit uncertain about skipping the dot in real literals.
The callback function func
is passed to internal procedures of powell
as an argument, although it could be accessed through host-association.