About fortran feature: SAVE

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.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 简单规则 建议使用自有格式编码(free-format). 注释使用“!”开始。但是老的程序,可能由固定格式与自由...
    Hello育种阅读 4,632评论 0 0
  • This chapter discusses some of the design decisions that ...
    狂风无迹阅读 1,025评论 0 0
  • Fortran Best Practices ====================== .. highligh...
    boliecon阅读 197评论 0 1
  • 在gdb中键入help all可以看到所有gdb-peda组件的命令。复制粘贴如下以备查: Command cla...
    飞熊先生阅读 2,619评论 0 0
  • 参考:耶鲁 x86 Assembly Guide Contents: Registers | Memory and...
    倔强swj阅读 637评论 0 0