fortran SAVE声明 [英] fortran SAVE statement

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

问题描述

我已经读过它在语言参考(英特尔)中的入口,但我无法完全理解它的功能。有人可能以外行的名义向我解释,它包含在模块中的含义是什么? 解决方案

模块超出范围时,该模块的变量将变得未定义 - 除非它们使用SAVE属性声明或使用SAVE语句。 未定义意味着如果您再次使用该模块,则不允许您依赖具有先前值的变量 - 当您重新访问该模块时,它可能具有之前的值,或者可能不会 - 不能保证。但许多编译器不会为 module 变量执行此操作 - 变量可能保留其值 - 编译器无法确定模块是否保留在作用域内,可能模块变量被视为全局变量 - 但不要依赖它!为了安全起见,可以使用保存或使用来自主程序的模块,以便它永远不会超出范围。

保存也很重要程序,在子程序或函数的调用之间存储状态(如@ire_and_curses所写) - 第一次调用的初始化,计数器等。

 子程序my_sub(y)

整数:: var $ b $整数,保存::计数器= 0
逻辑,保存:: FirstCall = .TRUE。 (*,*)计数器

if(FirstCall)then
FirstCall = .FALSE。
....
结束如果

var = ....



在这段代码片段中,counter将报告子程序x的调用次数。虽然实际上在Fortran> = 90中,但是可以省略save,因为声明中的初始化意味着save。

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



这与许多FORTRAN 77编译器的行为不同,其中一些编译器保留了所有局部变量的值,即使语言标准没有要求这些值。一些旧程序是依靠这种非标准行为编写的 - 这些程序在较新的编译器上将失败。许多编译器可以选择使用非标准行为并保存所有局部变量。



后续编辑:更新代码示例,显示不正确使用应该具有save属性但不包含的局部变量:

  module subs 

包含

子程序asub(i,control)

隐含无

整数,意图(in):: i
逻辑,意图(in)::控制

整数,保存:: j = 0
整数:: k

j = j + i
if (控制)k = 0
k = k + i

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

end子程序asub

结束模块子程序

程序test_saves

使用子程序
隐含无

调用子程序(3,.TRUE。)
调用asub(4,.FALSE。)

结束程序test_saves

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



使用gfortran编译程序,我发现 $ k $ / $>仍然保留其价值:

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

使用ifort和主动优化选项编译程序,k丢失了它的值:
$ b $ pre $ i,j,k = 3 3 3
i,j,k = 4 7 4

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

  i,j,k = 3 3 3 
forrtl:严重(193):运行时检查失败。正在使用变量'subs_mp_asub_ $ K'而不被定义


I've read it's entry in the language reference (Intel's), but I cannot quite grasp what it does. Could someone in layman's terms explain it to me, what it means when it is included in a 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.

"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 = ....

etc.

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".

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.

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.

LATER EDIT: 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

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.

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

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

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天全站免登陆