Robin Vowels writes:
> > Date: Fri, 18 Oct 2002 17:09:46 -0400
> > From: Tom Clune <[log in to unmask]>
> > To: [log in to unmask]
>
> > According to the Fortran standard, how should host-accessible variables
> > be treated in a recursive routine? I would have thought that only a single
> > instance would be created, but due to a bug, I encountered a case where
> > at least one compiler appears to have created a separate instance at each level.
>
> Are you sure?
> The codes that you give contradict your description.
> The actual (long) code has "i" defined in the recursive subroutine "SubInit".
> The wrapping subroutine "ut_tree_iter" not define "i", and your comment
> says that you accidentally omitted "i" from this routine.
Apologies - my quick stripped down version was not quite complete, and
in my hasty recreation of the problem in the "real" code, I commented
out the wrong declaration of "i".
I include here a full program which demonstrates the problem by
getting different answers on different compilers. Note that I am now
also making reference to an integer "total" that is also only declared
in main, as a simple mechanism to demonstrate the differing behavior.
!---------------------------------------
Program Main
Implicit None
! These are only declared in main, but used in
! the recursive routine sub_init() as well.
Integer :: i
Integer :: total
Integer, Parameter :: N_CHILDREN = 4
Integer, Parameter :: MAX_DEPTH = 3
total = 0
Call sub_init(1, 0)
Print*, total
Contains
Recursive Subroutine sub_init(id, depth)
Implicit None
Integer, Intent(In) :: id
Integer, Intent(In) :: depth
Integer :: ch_id
! Integer "i" is not declared here, but in main instead
Do i = 1, N_CHILDREN
If (depth < MAX_DEPTH) Then
ch_id = N_CHILDREN*id + i
Call sub_init(ch_id, depth + 1)
total = total + ch_id
End If
End Do
End Subroutine sub_init
End Program Main
!---------------------------------------
Under absoft:
% ./a.out
7938
Under Compaq Fortran V5.5-2602
% ./a.out
12993
Under MIPSpro 7.3.1.1m
% ./a.out
111
Under Cray CF90 3.5.0.4
# default optimization
% ./a.out
26
# -O0 optimization gives same as absoft
7938
From these results I infer that the code is, in fact, not standard
conforming, and I should have declared the integer "i" in routine
sub_init(). In fact, once I have done so, I get the same answer
(7938) under all of the above compilers. (Well, the default
options for Cray Cf90 still give 26, but I suspect that a compiler
bug is involved here.) Apparentl, I was just "unfortunate" enough
to be developing under the one compiler that gave the intended
answer even with the illegal code.
Cheers,
- Tom
--
--
Thomas Clune, Ph.D. <[log in to unmask]>
Advanced Software Technology Group Work: 301-286-4635
Science Computing Branch 931 Fax: 301-286-1634
NASA Goddard Space Flight Center
Greenbelt, MD 20771
|