Re: Replicating results



kalu wrote:
I have the following piece of fortran code that seems to give me
different answers every time I run it. I need to get the same result
every time because, although these differences look small, they
unravel into substantial differences. This code snippet is part of a
gradient calculation used in a large optimization routine.

I have printed the code and results below. Has anybody else run into
this problem? I am using the absoft compiler on an intel64 machine.
Please let me know if more information is needed.

-Kalu

begin CoDE:
-----------------------

real*8 :: x(n),grad(n),dh(nmax),ee(nmax,nmax),xsav(nmax)

! ... some more code goes here ...

do i = 1,n

do j = 1,n
x(j) = xsav(j) - ee(i,j)
end do

call evalf(x,tempf1)

do j = 1,n
x(j) = xsav(j) + ee(i,j)
end do

call evalf(x,tempf2)

! point of interest

grad(i) = (tempf2-tempf1)/(2.0D+00*dh(i))

! output below shows that the arguments to the right of the equal
sign are the
! same every time while the results of this calculation differ

print *,'tempf2 = ',tempf2,' tempf1 = ',tempf1,' dh(',i,') =
',dh(i),'grad(',i,') = ',grad(i)

end do
------------
End CoDE

Begin Output
-------------

tempf2 = -11.2891215185643 tempf1 = -11.2891217937382 dh( 4 )
= 8.680700000000000E-007 grad( 4 ) = 0.158497573828246

tempf2 = -11.2891215185643 tempf1 = -11.2891217937382 dh( 4 )
= 8.680700000000000E-007 grad( 4 ) = 0.158497571781917

------------
End Output


The two values of tempf1 and tempf2 may *look* the same to 14 decimal places, but they may be different beyond that. The same is true of dh(4). The question is why.

If you don't already use "implicit none" in your main program and in all subprograms, use it. It will make sure that if you do something like declare a variable called "x1" and use a variable called "xl" the compiler will tell you that xl was undeclared. If xl was undeclared and never initialized and then used in your calculations, it will have a random value and you will get random results.

Make sure that you initialize all of your variables, including all array elements. If you don't, you will get varying results.

(There are systems that initialize everything to zero. They are the exception. Some compilers have switches that may help you trap some uninitialized variables.)

Make sure that evalf declares its arguments the same as they're declared in the main program. You could be changing the single precision half of a double precision variable and leaving the garbage in the other half unchanged.

Louis
.



Relevant Pages

  • Re: Setting pointer to null!
    ... I think this debugging fetaure emerged originally ... developer forgets to initialize a variable and it accidentally ... to declare a bunch that would be "initialized" in relatively distant code. ... because you prevent the compiler from initializing them ...
    (microsoft.public.vc.language)
  • Re: Limited returns
    ... function Factory(w: E) return I'Class is ... when you declare an object of ... then whatever the specific type you initialize it to (in this case, ... A compiler that tries to in-line the call to ...
    (comp.lang.ada)
  • Re: Default value of basic variable type
    ... i remember i read a book talking about when u declare a array variable using ... and all type of numeric variable is initialize to 0, bool type is false, ... The compiler generates an error if you attempt to use the variable before you initialize it (and in many cases, even if you do, if you're not clear enough to suit the compiler:)). ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: advice on package design
    ... > for following scope. ... the compiler would have to do ... the extra verbiage required just to declare X. ... Unfortunately this is also untrue in Ada. ...
    (comp.lang.ada)
  • Re: Properties
    ... Is there any reason why this would not work and not simplify the ... Why should I have to declare any variable most of the time? ... The reason you explicitly declare fields used by a property is that the ... compiler needs to know what the code in the property does. ...
    (microsoft.public.dotnet.languages.csharp)