Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29249 Discussions

SAVE attribute for automatic shaped arrays?

d_avitabile
Beginner
1,265 Views
Hello everybody. I am currently writing a SUBROUTINE in which I need two variables to be saved between callings, namely et and ey.
If I declare them this way


SUBROUTINE mysub(y)
REAL(KIND=dp), DIMENSION( : ) :: y
REAL(KIND=dp), SAVE :: et=0.0D0
REAL(KIND=dp), DIMENSION(SIZE(y,1)),SAVE :: ey=0.0D0

...
END SUBROUTINE mysub

the compiler complains: he doesn't like the SAVE attribute for the automatic-shaped array ey. I would like to obtain the same effect for ey, possibly without using allocatable array. I think this is a general feature in FORTRAN 90, and maybe has nothing to do with the IFORT compiler I'm using. Does anybody know how to overvcome the problem? Thank in advance.

Daniele
0 Kudos
4 Replies
Steven_L_Intel1
Employee
1,265 Views
Make it ALLOCATABLE rather than automatic and allocate it to the desired bounds. Or consider using a module variable. You're right that this is not a compiler issue. The whole idea of automatic arrays is that their size is determined when you enter the routine. What would it mean to save that for a different call (when the bounds might be different)?
0 Kudos
d_avitabile
Beginner
1,265 Views
Thanks for your reply. I'll try to explain what I need to do. Concentrate on the variable et. Look at the following


SUBROUTINE mysub(y)

REAL(KIND=dp), DIMENSION( : ) :: y

REAL(KIND=dp), SAVE :: et=0.0D0



et = et +1.0_dp



END SUBROUTINE mysub



The very first time I call mysub, the variable et is set to zero. During the procedure it is updated and the value et is saved for the next time I call the subroutine.

Now I want to reproduce exactly the same effect with an array, say ey. Here is my solution



SUBROUTINE mysub(y)

REAL(KIND=dp), DIMENSION( : ) :: y

REAL(KIND=dp), SAVE :: et=0.0D0

REAL(KIND=dp), DIMENSION( : ), ALLOCATABLE :: ey



et = et +1.0_dp



IF (.NOT.ALLOCATED(ey)) THEN

ALLOCATE (ey(SIZE(y,1)))

ey = 0.0_dp

ENDIF



ey = ey + 1.0_dp



END SUBROUTINE mysub



This is what I have written to obtain this effect. What I don't like in this approach is that when I return back to the main program, the variable ey cannot be DEALLOCATED. That's why I was asking for something different from an allocatable array. What do you mean by considering a module variable?







Cheers

Message Edited by d.avitabile on 10-05-2005 02:21 AM

Message Edited by d.avitabile on 10-05-2005 02:23 AM

0 Kudos
d_avitabile
Beginner
1,265 Views
Thanks for your reply. I'll try to explain what I need to do. Concentrate on the variable et. Look at the
following

SUBROUTINE mysub(y)
REAL(KIND=dp), DIMENSION( : ) :: y
REAL(KIND=dp), SAVE :: et=0.0D0

et = et +1.0_dp

END SUBROUTINE mysub

The very first time I call mysub, the variable et is set to zero. During the procedure it is updated and the
value et is saved for the next time I call the subroutine.
Now I want to reproduce exactly the same effect with an array, say ey. Here is my solution

SUBROUTINE mysub(y)
REAL(KIND=dp), DIMENSION( : ) :: y
REAL(KIND=dp), SAVE :: et=0.0D0
REAL(KIND=dp), DIMENSION( : ), ALLOCATABLE :: ey

et = et +1.0_dp

IF (.NOT.ALLOCATED(ey)) THEN
ALLOCATE (ey(SIZE(y,1)))
ey = 0.0_dp
ENDIF

ey = ey + 1.0_dp

END SUBROUTINE mysub

This is what I have written to obtain this effect. What I don't like in this approach is that when I return
back to the main program, the variable ey cannot be DEALLOCATED. That's why I was asking for something
different from an allocatable array. What do you mean by considering a module variable?

Cheers
0 Kudos
Steven_L_Intel1
Employee
1,265 Views
Well, your variable et isn't deallocated either, is it?

I was suggesting something like this:

module globals
use kinds ! defines dp
real(dp), dimension(:), allocatable :: ey
end module globals

You would then "use globals" to make ey visible and use the same code you showed to conditionally allocate it.

You can then deallocate ey where and when you want as long as you USE globals.
0 Kudos
Reply