0

We have this old Fortran script that we're trying to recompile using Intel's Visual Fortran but we get calculation errors and different results than the old compiled version of the code.

We found what we believe to be the problem in the code below (which is loosely from Numerical Recipes).

The problem is that the 'it' parameter is reset for each call, it should however be stored between the function calls.

Our best guess for what might be the problem is that an older compiler might have treated 'it' as a 'save attribute' and therefore stored it between the function calls.

We may be completely wrong here and if some Fortran-guru can confirm this or have a better explenation we would love some help!

      subroutine TrapezoidalRule(Func, a, b, s, n)
*
*     This routine performs the trapezoidal rule, see Numerical Recipes
*
      implicit none
      real*8 Func, a, b, s
      Integer*4 n
      external Func
*
      real*8 del, x, sum
      Integer*4 it, tnm, j
*
      if (n .eq. 1) then
*
         s=0.5d0*(b-a)*(Func(a)+Func(b))
         it=1
*
      else
*
         tnm=it
         del=(b-a)/dble(tnm)
         x=a+0.5d0*del
         sum=0.d0
         do 11 j=1,it
*
            sum=sum+Func(x)
            x=x+del
*
11       continue
*
         s=0.5d0*(s+(b-a)*sum/dble(tnm))
         it=2*it
*
      endif
*
      return
      end
Petter
  • 321
  • 3
  • 9
  • Well, what you want to hear? Sort of, kinda... If you have reasons to believe so, then it is probably true. So, what is the question? To say it is possible? Yes, it is possible. But what do you want more? – Vladimir F Героям слава Nov 10 '17 at 15:53
  • I mean, we do not have your good and wrong results. We don't know in which way they differ and we cannot replicate the tests. We really can't do much more then to believe you. Did adding the `save` attribute help? We don't even know which compiler it was. – Vladimir F Героям слава Nov 10 '17 at 15:55
  • I'm sorry if I was unclear in my question. A bit frustrated here. The thing ist that we do not know which compiler was used as no one is still around involved in the initial work. We were hoping someone with good insight to Fortran knew how the 'save attribute' has been treated in the past as we had a hard time finding good info about this. – Petter Nov 10 '17 at 15:57
  • 1
    Yes, it was very common for FORTRAN 77 compilers to perform as if SAVE was applied to all variables. All current compilers that I know of provide an option to select this non-standard behavior. This has been discussed before, e.g., https://stackoverflow.com/questions/2582409/are-local-variables-in-fortran-77-static-or-stack-dynamic/2583248 – M. S. B. Nov 10 '17 at 18:10

1 Answers1

2

Yes, the explanation is plausible. The code accesses variable it at

   tnm=it

and this value is undefined when it is not save.

The old compile might not have used stack at all and might have used static storage for all variables. It also might have used stack, but it never got overwritten and the value happened to be at the same place. Who knows, we don't have the information to know.

There are compiler options to force the save attribute to all variables for bad codes like this (the SAVE for all was never standard!). It is just -save for Intel Fortran.

  • Thank you for confirming this might be possible, I will dig deeper into it. I'm a bit worried as there is alot of code and this might mean that this will be an occuring problem in the project. – Petter Nov 10 '17 at 16:07
  • 1
    You can try the `-save` option I referenced. You can look into the manual for more. There are also related threads at the Intel support forum. – Vladimir F Героям слава Nov 10 '17 at 16:08