将大小作为参数传递 VS 在 Fortran 过程中假设形状 [英] Passing size as argument VS assuming shape in Fortran procedures

查看:17
本文介绍了将大小作为参数传递 VS 在 Fortran 过程中假设形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试决定这两个选项中的哪一个是最好的:

I'm trying to decide which one of these two options would be the best:

subroutine sqtrace( Msize, Matrix, Value )
  integer, intent(in)  :: Msize
  real*8,  intent(in)  :: Matrix(Msize, Msize)
  real*8,  intent(out) :: Value

  [instructions...]

end subroutine sqtrace

VS

subroutine sqtrace( Matrix, Value )
  real*8,  intent(in)  :: Matrix(:,:)
  real*8,  intent(out) :: Value

  if ( size(Matrix,1) /= size(Matrix,2) ) then
    [error case instructions]
  end if

  [instructions...]

end subroutine sqtrace

我知道当您编译时出现警告,第一种情况应该在编译时自动检查对 sqtrace 的调用是否符合指示的大小.但是,例如,当给定的参数可分配时,我不知道编译器是否可以执行这些检查(如果这种分配取决于在运行时确定的其他事情,则更是如此).第二个需要一个显式接口并有更多代码(检查),但似乎会捕获更多错误.

I understand that when you compile with warnings, the first case should automatically check at compile time if calls to sqtrace comply with the size indicated. However, I don't know if the compiler can perform those checks when the given arguments are allocatable, for example (more so if such allocation depends on other things that are determined at runtime). The second one requires an explicit interface and has more code (the checks), but would seem to catch more errors.

使用每种方法的优点/缺点是什么,在哪些情况下应该使用一种方法?

Which are the advantages/disadvantages of using each and in which cases should one go with one over the other?

推荐答案

首先,一些术语.考虑声明为

First, some terminology. Consider the dummy arguments declared as

real :: a(n)                ! An explicit shape array
real :: b(:)                ! An assumed shape array
real, allocatable :: c(:)   ! A deferred shape array
real :: d(*)                ! An assumed size array

(延迟形状数组也可能是指针而不是可分配的).

(a deferred shape array may also be a pointer rather than an allocatable).

我不会回答在给定情况下哪个更好,而是会简单地详细说明一些重要的特征,将选择的一些特征留给程序员.粗略地说,许多人将显式形状数组视为Fortran 77",并将形状数组视为Fortran 90+".

I won't answer in terms of which is better in a given situation, but will simply detail some of the important characeristics leaving choice, where there is one, to the programmer. Crudely, many view explicit shape arrays as "Fortran 77" and assumed shape arrays as "Fortran 90+".

形状:

  • 显式形状数组的形状遵循其声明;
  • 假定形状数组虚拟参数的形状是实际参数的形状;
  • 延迟形状虚拟参数的形状可能未定义,在过程中定义,或实际参数的形状.

连续性:

  • 一个显式的形状数组只是连续的;
  • 假设的形状数组虚拟参数的连续性与相关的实际参数的连续性相关;
  • 延迟形状虚拟参数可能是实际参数的参数,或者取决于过程的执行.

对实参的限制:

  • 与显式形状数组关联的实际参数必须至少具有与虚拟参数一样多的元素;
  • 与假定形状数组关联的实际参数本身不能被假定大小;
  • 与假定或延迟形状数组关联的实际参数必须与虚拟参数具有相同的等级.

调用范围内的接口:

  • 如果虚拟参数是假定或延迟的形状,则引用范围必须具有可访问的过程的显式接口.

考虑real a(6).这可能是对傻瓜的实际争论

Consider real a(6). This may be an actual argument to the dummies

real b(3)
real c(2,3)
real d(:)   ! With an explicit interface available

a(1::2) 可能与 b 相关联,但由于 b 是连续的 copy-in/copy-out涉及.当与 d 关联时,不需要涉及复制输入/复制输出,但可能会涉及.

a(1::2) may be associated with b but as b is contiguous copy-in/copy-out will be involved. Copy-in/copy-out needn't be involved when associated with d, but it may be.

还有很多其他方面,但希望这是一个初步的高级介绍.

There are plenty of other aspects, but hopefully this is an initial high-level introduction.

这篇关于将大小作为参数传递 VS 在 Fortran 过程中假设形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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