编程语言

About fortran feature: SAVE

2023-12-02  本文已影响0人  loughsjtu

When two different subroutines declares a SAVE variable with the same name, the two variables are actually using different memory address. They will not interrupt each other due to value change.

The intel compiler option "auto" places local variables (scalars and arrays of all types), except those declared as SAVE, on the run-time stack. It is as if the variables were declared with the AUTOMATIC attribute. so, the SAVE feature will not be destroyed by the compiler option. https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-0/auto.html

Global variables including SAVE variables are by default shared between threads, unless declared as threadprivate. Please think carefully whether you really need SAVE, SAVE and parallel programming are very rarely good bedfellows. There are one or two good uses (see e.g.Fortran OpenMP with subroutines and functions), but if there is an alternative way to do (e.g. passing through the argument list) this will almost certainly cause you less pain in the long run.

Remark: For intel Fortran, the default option should be auto-scalar:

Example:

program save

    implicit none

    ! Variables

print *, '1st call sub1'

call sub1()

print *, '1st call sub2'

call sub2()

print *, '2nd call sub1'

call sub1()

pause

stop

end program save

subroutine sub1()

integer IPERTW,a

SAVE              IPERTW

DATA              IPERTW /1/

print *,ipertw

ipertw=3

print *,ipertw

return

end

subroutine sub2()

integer IPERTW

SAVE              IPERTW

DATA              IPERTW /2/

print *,ipertw

ipertw=4

print *,ipertw

return

end

The output will be:

D:\workdir\temp\save\ifort>save.exe

1st call sub1

1

3

1st call sub2

2

4

2nd call sub1

3

3

Fortran Pause - Enter command or to continue.

上一篇下一篇

猜你喜欢

热点阅读