From: Lorents on
I have an old Fortran77-style program which does this kind of thing:

program test
integer, parameter :: nMax=5
double precision :: A(nMax,nMax)
double precision :: x(nMax*nMax)
equivalence (A, x)

(manipulations on A and on x)

end program test

In other words, an array and a vector are EQUIVALENCE'd and manipulated.
I was wondering if one can do this kind in F95. Of course I could skip
the EQUIVALENCE statement, allocate both A and x and then use e.g.
x = RESHAPE(A, /nMAX*nMAX/)
but this would need twice the RAM as both A and x need to be stored.
I think the best solution would be to avoid this kind of things at all,
but I'd like to know if there is a more modern way of achieving the same
result as the old EQUIVALENCE without using twice the memory.

Lorenzo
From: dpb on
Lorents wrote:
> I have an old Fortran77-style program which does this kind of thing:
>
> program test
> integer, parameter :: nMax=5
> double precision :: A(nMax,nMax)
> double precision :: x(nMax*nMax)
> equivalence (A, x)
>
> (manipulations on A and on x)
>
> end program test
>
> In other words, an array and a vector are EQUIVALENCE'd and manipulated.
> I was wondering if one can do this kind in F95.

Yes.
....

> I think the best solution would be to avoid this kind of things at all,
> but I'd like to know if there is a more modern way of achieving the same
> result as the old EQUIVALENCE without using twice the memory.

In general, I'd agree w/ the former but I'd not worry about it too much
in existing code.

As for the latter, EQUIVALENCE is still in the Standard (and will stay
as there's simply too much existent code in use that relies on it to
make any chance of it being removed nonexistent) so there's no need.

I don't know of any other way to achieve the result any more simply
other than by dummy arguments and subroutines that move the alternate
variables to a different name space but there would be many code
constructions where that would be far more convoluted than simply the
EQUIVALENCE.

All in all, maybe I'm showing my age here ( :) ), but I'd probably leave
it alone unless it were just as simple to simply remove one variable
entirely.

--
From: Gordon Sande on
On 2009-11-11 14:18:42 -0400, Lorents <lorents(a)amp.te> said:

> I have an old Fortran77-style program which does this kind of thing:
>
> program test
> integer, parameter :: nMax=5
> double precision :: A(nMax,nMax)
> double precision :: x(nMax*nMax)
> equivalence (A, x)
>
> (manipulations on A and on x)
>
> end program test
>
> In other words, an array and a vector are EQUIVALENCE'd and manipulated.
> I was wondering if one can do this kind in F95. Of course I could skip
> the EQUIVALENCE statement, allocate both A and x and then use e.g.
> x = RESHAPE(A, /nMAX*nMAX/)
> but this would need twice the RAM as both A and x need to be stored.
> I think the best solution would be to avoid this kind of things at all,
> but I'd like to know if there is a more modern way of achieving the
> same result as the old EQUIVALENCE without using twice the memory.
>
> Lorenzo

Equivalence is still alive and well and living in Fortran! Most uses
of it are to be avoided but most is not the same as all. So "It depends!".

The real question is whether this is just memory usage saving where
A and x are used separately and never mixed or is there some reason why
two alternate indexing schemes are being used for the same data? In the
first case you can just omit the equivalence as an ancient untidiness but
in the second case you will need to understand the fine details of the
program.

"Manipulations on A and x" does not answer the question so you will need to
say more.

From: glen herrmannsfeldt on
Lorents <lorents(a)amp.te> wrote:
> I have an old Fortran77-style program which does this kind of thing:

> program test
> integer, parameter :: nMax=5
> double precision :: A(nMax,nMax)
> double precision :: x(nMax*nMax)
> equivalence (A, x)

> (manipulations on A and on x)

> end program test

> In other words, an array and a vector are EQUIVALENCE'd and manipulated.
> I was wondering if one can do this kind in F95. Of course I could skip
> the EQUIVALENCE statement, allocate both A and x and then use e.g.
> x = RESHAPE(A, /nMAX*nMAX/)

As long as you don't need dynamic allocation, EQUIVALENCE should
be fine. There are some restrictions on EQUIVALENCE using Fortran 90
features that you might run into.

> but this would need twice the RAM as both A and x need to be stored.
> I think the best solution would be to avoid this kind of things at all,
> but I'd like to know if there is a more modern way of achieving the same
> result as the old EQUIVALENCE without using twice the memory.

You don't say how you use either A or x, which could make a difference
in the answer. If you use RESHAPE in an array expression, it is
possible, maybe even likely, that you get a temporary array, but
in some cases one might not be needed. Unless you are on a very
small processor, if nMAX is 5 I wouldn't worry about it.

Temporary arrays can be slow, and can surprise you with the
needed memory, but sometimes they really are needed.

-- glen
From: Arjan on
> double precision :: A(nMax,nMax)
> double precision :: x(nMax*nMax)
> equivalence (A, x)

> x = RESHAPE(A, /nMAX*nMAX/)


It wouldn't save you the RAM, but I would use

x = TRANSFER(A,x)

That's not just syntax!
With RESHAPE source and destination must be arrays of the same type,
e.g. arrays of REAL(R4KIND).
With TRANSFER only the size in BYTES must match.
Suppose you have this in your subroutine:


CHARACTER*4 :: DumStr
INTEGER*4 :: J
EQUIVALENCE(J,DumStr)

This cannot be resolved with RESHAPE (I think). With TRANSFER it would
be:

CHARACTER*4 :: DumStr
INTEGER*4 :: J

DumStr = TRANSFER(J,DumStr)


I use this often to convert to and from BIT-representations,
e.g. when reading binary files with a pathological structure.

Regards,


Arjan