Print

Print


S Yuan wrote:
> For the following Common block:
>
> common A(10,3,3)
> ...
> common B(10,3),C1(15),C2(15)
>
> how can I replace it with F90 features?
>
> What I can do is 
>
> real,target :: A(10,3,3)
> real,pointer:: B(:,:),C1(:),C2(:)
> B => A(:,:,1)
>
> but then
> C1 => ?
The answer to your first question depends, in part, on things you
haven't told us.

First, I assume that the two common statements you've shown us are in
different program units. If that were not the case, using a pointer to
make B be an alias for part of A would be entirely off track.

The critical issue is whether the program ever puts a value into B, C1,
or C2 and expects to retrieve it from A or vice versa. The fact that C1
and C2 correspond to rather odd pieces of A suggests that it is quite
likely that the program _does not_, and that this was just a trick to
reduce the total amount of storage allocated. In that case, you don't
have to do much of anything other than declare A, B, C1, and C2 as
ordinary arrays in their respective program units. Unlike most FORTRAN
77 implementations, most Fortran 90 implementations naturally reuse the
storage for local variables that do not have the SAVE attribute.
[Additionally, this trick only saved 60 words of storage, an amount too
small to be worth worrying about on any modern system.]

If the program _does_ put values in one variable and take them out of
another, you pointer approach could be made to work:

call HELPER(C1,A(1,1,2),15)	! HELPER does the pointer assignment
call HELPER(C2,A(6,2,2),15)

where HELPER is defined something like

subroutine HELPER(POINTER,TARGET,LENGTH)
real,pointer:: POINTER(:)
real,target :: TARGET(LENGTH)
POINTER => TARGET
end subroutine HELPER

However, the unnecessary use of pointers tends to adversely affect code
optimization, so I would be more inclined to just put the statements

real :: A(10,3,3),B(10,3),C1(15),C2(15)
equivalence (A,B),(A(1,1,2),C1),(A(6,2,2),C2))

into a module. Equivalences also have an effect on optimization, but it
is much less severe.

Since it is a bit of a pain to figure out that C2(1) corresponds to
A(6,2,2), I might actually do the equivalences in terms of an underlying
one-dimensional array:

real :: SMAP(90),A(10,3,3),B(10,3),C1(15),C2(15)
equivalence (SMAP(1),A,B),(SMAP(31),C1),(SMAP(46),C2)


-Kurt