I am trying to understand Fortran 77 code but stumbled on EQUIVALENCE() statement on the code.
Here is part of the code:
REAL*8 DATA1(0:N-1)
COMPLEX*16 DATA2(0:N/2-1)
EQUIVALENCE(DATA1, DATA2)
...
...
CALL FFT(DATA1, N/2, -1)
Basically FFT subroutine is a one-dimensional complex-to-complex FFT engine. There are some permutation and matrix-vector multiplication on the subroutine.
The code calls DATA2 later in this manner:
K0=DATA2(0)
K1=DCONJG(DATA2(0))
can anyone give me clue about why EQUIVALENCE() statement is used? My assumption is DATA1 which is REAL, being changed to DATA2, which is COMPLEX variable with some changed performed on FFT subroutine. But if it is so, how about the imaginary part of DATA2? Because the FFT subroutine only contains REAL variable. And why the array size of DATA1 and DATA2 is different?
I can not find any answer in this forum which satisfy my question. Thanks for your answers. It would help me a lot.
equivalence
is one of Fortran's two features for storage-association of entities. (The other iscommon
blocks on which topic I will remain silent here). Theequivalence
statement declares that the entities named in its argument list share the same storage locations. In this casedata1
anddata2
share the same memory locations.If you have a tool for inspecting memory locations and point it at
data1
you'll see something like this:Point the same tool at
data2
and you'll see something like thisbut the 'truth' is rather more like
data1(0)
is at the same location as the real component ofdata2(0)
.data1(1)
is the imaginary component ofdata2(0)
, and so forth.This is one of the applications of
equivalence
which one still occasionally comes across -- being able to switch between viewing data ascomplex
or as pairs ofreals
. However, it's not confined to this kind of type conversion, there's nothing to say you can't equivalence integers and reals, or any other types.Another use one occasionally still sees is the use of
equivalence
for remapping arrays from one rank to another. For example, givenand
the same elements can be treated as belonging to a rank-2 array or to a rank-1 array to suit the program's needs.
equivalence
is generally frowned upon these days, most of what it has been used for can be done more safely with modern Fortran. For more, you might care to look at my answer to Is the storage of COMPLEX in fortran guaranteed to be two REALs?