Fortran 77代码具有不兼容的调用序列 - 如何在Fortran 90/95中进行模块化?

时间:2015-08-05 18:58:44

标签: fortran fortran90 gfortran fortran77

考虑下面的“真实世界”遗留Fortran 77代码,根据标准可能是非法的,但在现实生活中使用各种编译器,如果每个子例程单独编译,则不会产生编译器或链接器警告

subroutine a
complex z(10)
call b(z)
call r1(z)
call r2(z)
end

subroutine b(z)
complex z(10)
c ... use complex arithmetic on z
end

subroutine r1(x)
real x(2,10)
c ... do something with real and imaginary parts
c ... real parts are x(1,*)
c ... imaginary parts are x(2,*)
end

subroutine r2(x)
real x(20)
c ... do something with real and imaginary parts
end

我想使用Fortran 90/95模块以这种方式重新打包代码。

的天真方法
module m
public a
private b, r1, r2
contains 

subroutine a
complex z(10)
call b(z)
call r1(z)
call r2(z) 
end subroutine a

subroutine b(z)
complex z(10)
c ... use complex arithmetic on z
end subroutine b

subroutine r1(x)
real x(2,10)
c ... do something with real and imaginary parts
c ... real parts are x(1,*)
c ... imaginary parts are x(2,*)
end subroutine r1

subroutine r2(x)
real x(20)
c ... do something with real and imaginary parts
end subroutine r2

end module m

不编译,因为(当然)编译器现在可以看到用错误的参数类型调用子例程r1和r2。

我需要一些关于如何解决这个问题的想法,只需要对现有代码进行最少的重写,而不需要在内存中重复复制数据 - 数据的实际大小太大了。

3 个答案:

答案 0 :(得分:1)

请注意以下建议有一些倒退方面。

在问题的示例代码中,数据的原始源是局部变量。这样的局部变量可以通过使用等价语句出现在存储关联上下文中,在这种上下文中,可以将COMPLEX对象视为一对REAL对象。

module m
  public a
  private b, r1, r2
contains 
  subroutine a
    complex z(10)
    real r(size(z)*2)
    equivalence (z,r)
    call b(z)     ! pass complex array
    call r1(r)    ! pass real array
    call r2(r)    ! pass real  array
  end subroutine a

  subroutine b(z)
    complex z(10)
    ! ... use complex arithmetic on z
  end subroutine b

  subroutine r1(x)
    real x(2,10)
    ! ... do something with real and imaginary parts
    ! ... real parts are x(1,*)
    ! ... imaginary parts are x(2,*)
  end subroutine r1

  subroutine r2(x)
    real x(20)
    ! ... do something with real and imaginary parts
  end subroutine r2
end module m

答案 1 :(得分:1)

c_f_pointer()可能对获取real(2,*)数组的complex(*)指针很有用,但我不确定将复杂参数(zconst(:))传递给ctor()是否真的没问题...(这里我使用了gfortran 4.4& 4.8和ifort 14.0,为了使输出更紧凑,数组维度从10变为3。)

module m
    implicit none
contains

subroutine r1 ( x )
    real x( 2, 3 )

    print *
    print *, "In r1:"
    print *, "Real part = ",x( 1, : )
    print *, "Imag part = ",x( 2, : )
endsubroutine

subroutine r2 ( x )
    real x( 6 )

    print *
    print *, "In r2:"
    print *, "all elements = ", x( : )
endsubroutine

subroutine b ( z )
    complex :: z( 3 )
    real, pointer :: rp(:,:)

    rp => ctor( z, 3 )  !! to compare z and rp                                      
    print *
    print *, "In b:"
    print *, "1st elem = ", z( 1 ), rp( :, 1 )
    print *, "3rd elem = ", z( 3 ), rp( :, 3 )
endsubroutine

function ctor( z, n ) result( ret )   !! get real(2,*) pointer to complex(*)
    use iso_c_binding
    implicit none
    integer :: n
    complex, target :: z( n )
    real, pointer :: ret(:,:)
    call c_f_pointer( c_loc(z(1)), ret, shape=[2,n] )
endfunction

endmodule

program main
    use m
    implicit none
    complex z(3)
    complex, parameter :: zconst(3) = [(7.0,-7.0),(8.0,-8.0),(9.0,-9.0)]

    z(1) = ( 1.0, -1.0 )
    z(2) = ( 2.0, -2.0 )
    z(3) = ( 3.0, -3.0 )

    call r1 ( ctor( z, 3 ) )
    call r1 ( ctor( zconst, 3 ) )

    call r2 ( ctor( z, 3 ) )
    call r2 ( ctor( zconst, 3 ) )

    call b ( z )
    call b ( zconst )
 endprogram

答案 2 :(得分:1)

(这可能更多是评论而不是答案,但您不能在评论中包含格式化代码。)

@roygvib在标准的fortran 2003中回答了这个问题。

使用非标准的" Cray Fortran指针"可以更简洁地编码相同的想法。在许多编译器中实现的语法 - 例如gfortran中的-fcray-pointer选项。非标准内在函数loc取代ctor

subroutine b ( z )
    complex :: z( 3 )

    real :: r( 2, 3 )
    pointer(zp, r)
    zp = loc(z)

    print *
    print *, "In b:"
    print *, "1st elem = ", z( 1 ), r( :, 1)
    print *, "3rd elem = ", z( 3 ), r( :, 3)
endsubroutine
相关问题