fortran-lang / minpack

Modernized Minpack: for solving nonlinear equations and nonlinear least squares problems
https://fortran-lang.github.io/minpack/
Other
94 stars 20 forks source link

Port features from SciPy's minpack version #23

Open certik opened 2 years ago

certik commented 2 years ago

The version is here: https://github.com/scipy/scipy/tree/a0535bee9d4da58e6d41e963e907c1b9f7684585/scipy/optimize/minpack and we need to check to see what changes they have made to the original code and port those to this version.

Towards #14.

ivan-pi commented 2 years ago

I checked the history of that folder. The only major change is they added the recursive attribute to all subroutines to make sure they are re-entrant. The relevant issue is https://github.com/scipy/scipy/pull/7999

If I understand correctly, by adding recursive the compiler must guarantee the subroutines are re-entrant. Quoting one the replies in that thread:

Many Fortran 77 compilears saved all local variables by default. gfortran still uses this misfeature, even for Fortran 90, albeit to a lesser extent and it can be controlled with compiler flags. Since the SciPy source cannot assume anything about the compiler and compiler flags passed to the Fortran compiler, we just have to assume that Fortran subroutines are never reentrant.

milancurcic commented 2 years ago

With F2018, procedures are recursive by default, nevertheless we should use recursive to ensure this on older compiler versions.

ivan-pi commented 2 years ago

Here's a list of things we could try to improve on based upon a search of the open issues containing the keyword "minpack" in the SciPy repository:

certik commented 2 years ago

@ivan-pi awesome! Let's implement those, and bring it to SciPy to use our newer improved version, which would fix a lot of their (reported) bugs.

ivan-pi commented 2 years ago

Perhaps more relevant than issues directly related to MINPACK is to look directly at the options available with other solvers in scipy.optimize.leastsq and scipy.optimize.root.

For least squares, the features missing in MINPACK compared to the other two SciPy solvers "trf" and "dogbox" are:

The C++ library Ceres (BSD-licensed) implements all of these.

xecej4 commented 2 years ago

Many Fortran 77 compilears saved all local variables by default. gfortran still uses this misfeature, even for Fortran 90, albeit to a lesser extent and it can be controlled with compiler flags.

That was stated in 2017. I do not recall Gfortran having that "misfeature", and recent versions of Gfortran do not do so. In fact, see this quote from the current Gfortran documentation:

"The default, which is -fautomatic, uses the stack for local variables smaller than the value given by -fmax-stack-var-size. Use the option -frecursive to use no static memory."

The Gfortran option "-fmax-stack-var-size=n" allows control over which arrays are put into the stack, and to avoid stack overflow. The parameter n has a default value of n = 65536 bytes. Thus, most local arrays of small or medium size are allocated on the stack without the need for any explicit request by the user.