Fortran SAVE 语句 [英] Fortran SAVE statement

查看:147
本文介绍了Fortran SAVE 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了(英特尔的)语言参考文档中的 save 语句,但我不太了解它的作用.有人可以用简单的语言向我解释一下 save 语句包含在模块中的含义吗?

I've read about the save statement in the (Intel's) language reference document, but I cannot quite grasp what it does. Could someone explain to me in simple language what it means when the save statement is included in a module ?

推荐答案

原则上,当模块超出范围时,该模块的变量变得未定义——除非它们使用 SAVE 属性或 SAVE 声明使用声明.未定义"意味着如果您再次使用该模块,则不允许您依赖具有先前值的变量-当您重新访问该模块时它可能具有先前的值,或者它可能没有-无法保证.但是许多编译器不会对 module 变量执行此操作——这些变量可能会保留它们的值——编译器不值得努力确定一个模块是否仍在范围内,并且可能模块变量被视为全局变量——但不要依赖它!为了安全起见,请在主程序中使用保存"或使用"模块,这样它就不会超出范围.

In principal when a module goes out-of-scope, the variables of that module become undefined -- unless they are declared with the SAVE attribute, or a SAVE statement is used. "Undefined" means that you are not allowed to rely on the variable having the previous value if you again use the module -- it might have the previous value when you re-access the module, or it might not -- there is no guarantee. But many compilers don't do this for module variables -- the variables probably retain their values -- it isn't worth the effort for the compiler to figure out whether a module remains in scope or not and probably module variables are treated as global variables -- but don't rely on that! To be safe, either use "save" or "use" the module from the main program so that it never goes out of scope.

保存"在过程中也很重要,用于在子例程或函数的调用中存储状态"(由 @ire_and_curses 编写)——第一次调用"初始化、计数器等.

"save" is also important in procedures, to store "state" across invocations of the subroutine or function (as written by @ire_and_curses) -- "first invocation" initializations, counters, etc.

subroutine my_sub (y)

integer :: var
integer, save :: counter = 0
logical, save :: FirstCall = .TRUE.

counter = counter + 1

write (*, *) counter

if (FirstCall) then
   FirstCall = .FALSE.
   ....
end if

var = ....

等等

在此代码片段中,计数器"将报告子例程 x 的调用次数.虽然实际上在 Fortran >=90 中可以省略保存",因为声明中的初始化意味着保存".

In this code fragment, "counter" will report the number of invocations of subroutine x. Though actually in Fortran >=90 one can omit the "save" because the initialization in the declaration implies "save".

与模块情况相比,在现代编译器中,没有保存属性或声明时初始化,过程的局部变量在调用过程中丢失其值是正常的.因此,如果您尝试在稍后的调用中使用var",然后在该调用中重新定义它,则该值是未定义的,并且可能不是在先前调用该过程时计算的值.

In contrast to the module case, with modern compilers, without the save attribute or initialization-on-a-declaration, it is normal for local variables of procedures to lose their values across invocations. So if you attempt to use "var" on an later call before redefining it in that call, the value is undefined and probably won't be the value calculated on a previous invocation of the procedure.

这与许多 FORTRAN 77 编译器的行为不同,其中一些编译器保留了所有局部变量的值,尽管语言标准并不要求这样做.一些旧程序是根据这种非标准行为编写的——这些程序在较新的编译器上会失败.许多编译器可以选择使用非标准行为并保存"所有局部变量.

This is different from the behavior of many FORTRAN 77 compilers, some of which retained the values of all local variables, even though this wasn't required by the language standard. Some old programs were written relying on this non-standard behavior -- these programs will fail on the newer compilers. Many compilers have an option to use the non-standard behavior and "save" all local variables.

后期更新一个代码示例,该示例显示不正确使用了应该具有 save 属性但没有的局部变量:

LATER update with a code example that shows incorrect usage of a local variable that should have the save attribute but doesn't:

module subs

contains

subroutine asub (i, control)

   implicit none

   integer, intent (in) :: i
   logical, intent (in) :: control

   integer, save :: j = 0
   integer :: k

   j = j + i
   if ( control ) k = 0
   k = k + i

   write (*, *) 'i, j, k=', i, j, k

end subroutine asub

end module subs

program test_saves

   use subs
   implicit none

   call asub ( 3, .TRUE. )
   call asub ( 4, .FALSE. )

end program test_saves

子程序的局部变量 k 被故意误用 -- 在这个程序中,它在第一次调用时被初始化,因为 control 为 TRUE,但在第二次调用 control 是 FALSE,所以 k 没有被重新定义.但是没有保存属性k是未定义的,所以使用它的值是非法的.

Local variable k of the subroutine is intentionally misused -- in this program it is initialized in the first call since control is TRUE, but on the second call control is FALSE, so k is not redefined. But without the save attribute k is undefined, so the using its value is illegal.

用gfortran编译程序,发现k还是保留了它的值:

Compiling the program with gfortran, I found that k retained its value anyway:

 i, j, k=           3           3           3
 i, j, k=           4           7           7

用 ifort 和激进的优化选项编译程序,k 失去了它的价值:

Compiling the program with ifort and aggressive optimization options, k lost its value:

 i, j, k=           3           3           3
 i, j, k=           4           7           4

使用带调试选项的 ifort,在运行时检测到问题!

Using ifort with debugging options, the problems was detected at runtime!

 i, j, k=           3           3           3
forrtl: severe (193): Run-Time Check Failure. The variable 'subs_mp_asub_$K' is being used without being defined

这篇关于Fortran SAVE 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆