Equivalence question

Equivalence question

rahzan's picture

Consider the situation:

------------------------
use Y
Call routineX(tweedles,nRow,nCol)
...
------------------------
module Y
real,allocatable:: tweedles(:,:)
real,allocatable:: tweedleDee(:),tweedleDum(:)

routineX(tweedles_in,nRow,nCol)
real, intent(in):: tweedles_in(nRow,nCol)
allocate(tweedles(nRow,nCol))
allocate(tweedleDee(nRow),tweedleDum(nRow))
tweedles=tweedles_in
tweedleDee=tweedles(:,1)
tweedleDum=tweedles(:,2)

--------------------
In other words I would like to be able to refer to the data arrays either by their explicit names:
tweedleDee,tweedleDum
or by their index e.g. in loop ops. using tweedles.


Question 1: is the memory used for "tweedles_in" actually allocated in the module but in a sense "wasted" because we only need it for input? does the intent(in) help in this regard?

Question 2:This would normally be the perfect place for making the 2d array EQUIVALENT with the 1d arrays. However this is not possible when they are allocatable.

Is there another way to do this and Avoid the double usage of memory for the same data which only have different names?

Thanks,
Tim

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
Steve Lionel (Intel)'s picture

What you are trying to do is not allowed in Fortran. You cannot refer to the same chunk of memory by two different names if there is any redefinition or change in definition status of the memory.

In your example, there is only one "copy" of the memory in array tweedles. In routineX, this memory is referenced as both tweedles (through host association) and as tweedles_in (argument association). tweedles gets is definition status changed by the allocate (and later assignment). Illegal.

(By the way, what do you think the assignment
tweedles=tweedles_in
does?)

You're right that you can't EQUIVALENCE to ALLOCATABLE (or POINTER) arrays. You could fudge this with the "Compaq POINTER extension", declaring two different arrays that are the pointee of the same pointer variable, and assigning LOC(tweedles) to the pointer variable. Non-standard.

Steve

Steve
rahzan's picture

First,
I'm sorry I've asked two ostensibly different questions in the same thread.

The statemet
tweedles=tweedles_in
makes a copy of one into a completely independent array. I do not know of any bad consequences of this. Otherwise tweedles (in the module is undefined). I already use this without problems.

ON the first Question: I wanted to know if intent(in) saves memory usage since the tweedles_in is only an input vehicle.

Are you saying that it is possible to simply use tweedles_in in the rest of the module (not just routineX) without copying it? if so How?
Or are you (Also) saying that what I've stated has more diabolical consequences?

As for the SECOND question:
I gathered as much about using pointers. Can anyone provide a sample?

Login to leave a comment.