简体   繁体   English

Fortran 中的循环变量是否已保存?

[英]Are loop variables in Fortran SAVEd?

I have SAVE statement in subroutine or function. Also I haven't IMPLICIT NONE .我在子程序或 function 中有SAVE语句。我也没有IMPLICIT NONE In that case are loop variables in Fortran SAVEd?在那种情况下,Fortran 中的循环变量是否已保存? Example:例子:

FUNCTION GLOB7S(P)
      REAL LONG
      LOGICAL mess
      COMMON/iounit/konsol,mess
      COMMON/LPOLY/PLG(9,4),CTLOC,STLOC,C2TLOC,S2TLOC,C3TLOC,S3TLOC,
     $ IYR,DAY,DF,DFA,APD,APDF,APT(4),LONG
      COMMON/CSW/SW(25),ISW,SWC(25)
      DIMENSION P(100),T(14)
      SAVE
      DATA DR/1.72142E-2/,DGTR/1.74533E-2/,PSET/2./
      DATA DAYL/-1./,P32,P18,P14,P39/4*-1000./
!$OMP THREADPRIVATE(/iounit/)
!$OMP THREADPRIVATE(/LPOLY/)
!$OMP THREADPRIVATE(/CSW/)

!$OMP THREADPRIVATE(T,DR,DGTR,PSET,DAYL,P32,P18,P14,P39)
!$OMP THREADPRIVATE(CD32,CD18,CD14,CD39)
!$OMP THREADPRIVATE(T71,T72,T81,T82,TT,GLOB7S)
C       CONFIRM PARAMETER SET
      IF(P(100).EQ.0) P(100)=PSET
      IF(P(100).NE.PSET) THEN
        if(mess) WRITE(konsol,900) PSET,P(100)
  900   FORMAT(1X,'WRONG PARAMETER SET FOR GLOB7S',3F10.1)
        STOP
      ENDIF
      DO 10 J=1,14
        T(J)=0.
   10 CONTINUE
      IF(DAY.NE.DAYL.OR.P32.NE.P(32)) CD32=COS(DR*(DAY-P(32)))
      IF(DAY.NE.DAYL.OR.P18.NE.P(18)) CD18=COS(2.*DR*(DAY-P(18)))
      IF(DAY.NE.DAYL.OR.P14.NE.P(14)) CD14=COS(DR*(DAY-P(14)))
      IF(DAY.NE.DAYL.OR.P39.NE.P(39)) CD39=COS(2.*DR*(DAY-P(39)))
      DAYL=DAY
      P32=P(32)
      P18=P(18)
      P14=P(14)
      P39=P(39)
...

In this example is J SAVEd?在这个例子中是J SAVEd? I need to know this, because I'm trying to parallelize very old and very big Fortran code with OpenMP and I don't shure that I use THREADPRIVATE directive right.我需要知道这一点,因为我正在尝试使用OpenMP并行处理非常旧且非常大的 Fortran 代码,但我不确定我是否正确使用THREADPRIVATE指令。

A DO construct of the form of the question is not a scoping unit.问题形式的 DO 构造不是范围界定单元。 Further, a "loop variable" for such a construct is not a privileged entity: it's just a variable in the scope containing the loop which is used in a particular way.此外,这种构造的“循环变量”不是特权实体:它只是 scope 中的一个变量,包含以特定方式使用的循环。

j in this case is variable which exists before the DO construct, and it exists after the DO construct. j在这种情况下是存在于 DO 构造之前的变量,它存在DO 构造之后。

That j is of implicitly declared type is not relevant: a variable which is not a construct or statement entity with implicit type exists throughout the scoping unit (and possibly others) in which the reference appears (see later). j是隐式声明的类型是不相关的:在引用出现的整个作用域单元(可能还有其他)中存在一个不是具有隐式类型的构造或语句实体的变量(见下文)。 That is, the implicitly typed j here exists throughout the function, not just from the point where it appears in the DO construct.也就是说,隐式类型的j存在于整个 function 中,而不仅仅是从它出现在 DO 构造中的那一点开始。

The SAVE statement applies to the whole (non-inclusive) scoping unit. SAVE 语句适用于整个(非包含的)范围单元。 If j is a local variable (in this case, not use- or host-associated) then the SAVE saves it.如果j是局部变量(在这种情况下,不是使用或主机关联的),则 SAVE 会保存它。


As Steve Lionel points out in a comment, in general, things can be much more complicated.正如 Steve Lionel 在评论中指出的那样,一般来说,事情可能要复杂得多。 Although not relevant to this question there are exceptions to what may otherwise be seen as blanket statements above.尽管与这个问题无关,但上面可能被视为一揽子陈述的内容也有例外。

Consider the similar concepts (switching to free form code for clarity)考虑类似的概念(为清楚起见,切换到自由格式代码)

integer t(10)

data (t(j), integer :: j=1,10)/10*0/

t = [(0, integer :: j=1,10)]

forall (integer :: j=1:10) t(j) = 0

do concurrent (integer :: j=1:10)
  t(j) = 0
end do

end program

In each of these cases j is of the scope of the statement or DO CONCURRENT construct.在每种情况下, j属于语句或 DO CONCURRENT 构造的 scope。 The SAVE statement will not apply to these variables, because they are not then local variables of the scoping unit. SAVE 语句将不适用于这些变量,因为它们不是作用域单元的局部变量。

Even without the explicit integer:: j , using implicit typing, these forms do not implicitly declare a variable outside the statement or construct.即使没有显式integer:: j ,使用隐式类型,这些 forms 也不会在语句或构造之外隐式声明变量。

In other constructs, such as an ASSOCIATE construct:在其他结构中,例如 ASSOCIATE 结构:

associate (a => 1+2)
end associate

the entity has the scope of the construct, but in this case we should note that the type of any construct entity is not implicit: it is exactly that of the selector rather than being based on first letter of its name.该实体具有构造的 scope,但在这种情况下,我们应该注意任何构造实体的类型都不是隐式的:它正是选择器的类型,而不是基于其名称的首字母。

Finally, note that there is (currently) no such thing as最后,请注意(目前)没有这样的东西

do integer :: j=1,10
end do

which makes the normal DO construct pretty boring in this respect.这使得普通的 DO 构造在这方面非常无聊。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM