Fortran编译器术语:虚拟变量和属性

Cet*_*ert 7 variables attributes fortran

有人可以向我解释一下虚拟变量或属性对应的解析器/编译器中的抽象是什么吗?

      PURE SUBROUTINE F(X, Y)
        IMPLICIT NONE
        REAL, INTENT(INOUT) :: X, Y, C
C        REAL :: A, B
C        REAL, SAVE :: C = 3.14E0
        PARAMETER (C = 3.14E0, X = 32, Y = X)
        X = Y + 2 * SIN(Y)
      END
Run Code Online (Sandbox Code Playgroud)
cetin@unique:~/lab/secret/tapenade$ gfortran -x f77 -c 1.f 
1.f:6.37:

        PARAMETER (C = 3.14E0, X = 32, Y = X)                           
                                    1
Error: PARAMETER attribute conflicts with DUMMY attribute in 'x' at (1)
1.f:3.38:

        REAL, INTENT(INOUT) :: X, Y, C                                  
                                     1
Error: Symbol at (1) is not a DUMMY variable
Run Code Online (Sandbox Code Playgroud)
cetin@unique:~/lab/secret/tapenade$ ifort -c 1.f
1.f(3): error #6451: A dummy argument name is required in this context.   [C]
        REAL, INTENT(INOUT) :: X, Y, C
-------------------------------------^
1.f(6): error #6406: Conflicting attributes or multiple declaration of name.   [X]
        PARAMETER (C = 3.14E0, X = 32, Y = X)
-------------------------------^
1.f(6): error #6406: Conflicting attributes or multiple declaration of name.   [Y]
        PARAMETER (C = 3.14E0, X = 32, Y = X)
---------------------------------------^
1.f(6): error #6592: This symbol must be a defined parameter, an enumerator, or an argument of an inquiry function that evaluates to a compile-time constant.   [X]
        PARAMETER (C = 3.14E0, X = 32, Y = X)
-------------------------------------------^
compilation aborted for 1.f (code 1)
Run Code Online (Sandbox Code Playgroud)

Tim*_*omb 10

Fortran通过引用传递.伪属性对应到在传递给函数(这些变量XY你的情况).参数语句期望某些东西是静态的,但由于X是传递给函数的任何东西,它实际上没有任何意义.参数语句是一种设置常量的方法 - 它与子例程的参数没有任何关系.

当你得到错误说C不是DUMMY变量时,那就意味着它没有C在变量列表中找到将被传入/传出函数 - 你的声明只是F(X, Y):没有C看到.虽然您没有DUMMY显式使用该属性,但您拥有该INTENT(INOUT)属性,这意味着这些变量对应于子例程输入/输出.

为了获得你想要的东西,你会有一个看起来像这样的子程序:

subroutine F(X, Y)
    implicit none

    ! These are the dummy variables
    real, intent(inout) :: X, Y

    ! These are the variables in the scope of this subroutine
    real                  :: a, b
    real, parameter, save :: c = 3.14E0

    X = Y + 2*sin(Y)
end subroutine F
Run Code Online (Sandbox Code Playgroud)

我不完全知道你正在尝试做的-你宣布一个pure子程序,这意味着无副作用的子程序,但你正在使用intent(inout)你的变数,这意味着XY在执行过程中被改变.

我还要在子例程中添加一个变量,在其声明语句中初始化变量,就像REAL :: C = 3.14E0产生一个带隐式 save属性的变量一样.但是,如果您希望将其从呼叫保存到呼叫,那么您已经通过明确添加save属性来做正确的事情,以明确这就是您正在做的事情.

我不是解析器/编译器的人,但我认为要回答你的问题,该dummy属性意味着你只是得到一个指针 - 你不必分配任何空间,因为函数调用中使用的变量已经有空间分配.


Vla*_*r F 5

Tim Whitcomb 很好地解释了通话的实际问题。我将尝试更明确地解释这些术语。

虚拟参数是 Fortran 特定术语。这是其他语言所谓的形式参数X或类似的东西,即它是被调用的对象(在您的情况下),并且在调用过程时与实际参数Y相关联。

因此在:

subroutine s(i)
  integer :: i
end

call s(1)
Run Code Online (Sandbox Code Playgroud)

thei是子例程的虚拟参数s,而 expression1是传递给子例程的虚拟参数的实际参数i

属性是指定数据对象或过程的附加属性的一种形式。可以使用语句指定属性:

real c
intent(in) c
optional c
Run Code Online (Sandbox Code Playgroud)

或者可以在单个声明中指定它们:

real, intent(in), optional :: c
Run Code Online (Sandbox Code Playgroud)

这样,虚拟参数就是具有属性和c的默认实数。intent(in)optional

冲突属性是指不能同时为一个对象指定的属性。你的例子和intent(...)很好parameter用。它们是不兼容的,因为第一个隐含虚拟参数,另一个指定命名常量