Fortran是否在编译时解析可选参数并显示语句? [英] Does Fortran resolve optional arguments and present statements during compile-time?

查看:70
本文介绍了Fortran是否在编译时解析可选参数并显示语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索没有任何结果之后,我想问一个问题,关于包含可选参数的子例程以及编译器如何处理它们(运行时/编译时).考虑下面的示例程序.

After a search without any result I want to ask you a question regarding subroutines containing optional arguments and how they are treated by the compiler (run time/compile time). Consider the following example program.

  module CONTAINS_ABC

  contains

  subroutine ABC( a, b, c)

  implicit none

  real, intent(in)           :: a
  real, intent(in), optional :: b, c

  if( present(b) )then ! PATH_B
     write(*,*) 'Provide c'
  else if( present(c) )then "! PATH_C
     write(*,*) 'Provide b'
  end if

  ! Do all the computations with a, b and c 

  end subroutine ABC

  end module CONTAINS_ABC


  program CALL_ABC

  use CONTAINS_ABC

  real :: aa, bb, cc


  call ABC( a = aa, b=bb )

  call ABC( a = aa, c=cc )


  end program CALL_ABC

我想知道编译器如何用optimization来处理带有可选参数的子例程.编译器是否为子程序 FCN 隐式地生成两个接口,然后在主程序中的编译期间选择正确的一个?此外, present(b)/present(c)语句是在运行时还是在编译时进行评估的? 如果我理解正确,编译器可能会知道第一次调用ABC会导致路径 B ,而第二次调用 ABC 则必须导致路径 C . 我问这个问题,因为我有一个子程序,叫做百万次.

I wonder how the compiler treats subroutines with optional arguments in terms of optimization. Does the compiler produce implicitly two interfaces for the subroutine FCN and chooses then the right one during compile time in the main program? Furthermore, is the present(b)/present(c) statement evaluated at runtime or during compile time? In case I understand things right, the compiler might know that the first call to ABC leads to the path B, while the second call to ABC must lead to path C. I ask this question, since I have a subroutine that is called million-million of times.

我想避免在运行时决定沿路径B 路径C 进行决策.当然有可能两个简单地编写两个子例程,但是,这将产生许多实际执行相同操作的附加行.

I want to avoid that during runtime the decision is made wether to go along path B or path C. It would be possible of course two write simply two subroutines, however, this would produce a lot of additional lines that would actually do the same thing.

在此先感谢大家的帮助!

Thank you all in advance for your help!

推荐答案

除了语法和语义外,Fortran标准在语言实现的几乎所有方面都保持沉默.例如,人们经常认为Fortran通过引用传递参数,但是该标准仅要求程序的行为就像通过引用传递参数一样.实施者可以根据需要自由使用 pass-by-pixies ,并且可以借此产生令人信服的通过引用进行模拟.

The Fortran standards are silent on pretty much every aspect of the implementation of the language other than the syntax and semantics. For example, people often think that Fortran passes arguments by reference but the standard only requires that programs behave as if arguments are passed by reference. Implementers are free to use pass-by-pixies if they wish and if they can thereby produce a convincing simulation of pass by reference.

这只是一篇冗长的介绍,告诉您特定的编译器如何实现语言功能通常是特定于编译器的.众所周知,实现细节也会因同一编译器的版本而异.

That's just a long-winded introduction to telling you that how a particular compiler implements a language feature is, in general, compiler-specific. Implementation details are also know to vary from version to version of the same compiler.

我认为您认为在编译时会检查可选参数的存在是错误的,即使可以证明它是可能的-我只是不认为这是当前的问题的编译器.我希望编译器生成带有可选参数的子例程的单个实现.毕竟,它确实需要程序员来确保程序不会尝试处理缺少的可选参数.

I think that you are wrong to think that the presence of an optional argument will be checked at compile-time even if it is provable that it could be -- I just don't think that that is something that the current crop of compilers do. I expect the compiler to generate a single implementation of the subroutine with optional arguments. It does, after all, rest with the programmer to ensure that a procedure does not attempt to process absent optional arguments.

如果您(程序员)知道一个过程将被执行多次,并且您怀疑一个不同的实现(一个带有参数b而没有c的实现),一个 vice-versa ,然后由您来确定单独的实现是否会比带有可选参数的一种实现快.如果您担心代码重复,则始终可以使用第三个过程来实现通用代码,并从两个变体中调用它.

If you, the programmer, know that a procedure will be executed many times and if you suspect that different implementations, one with argument b and without c, one vice-versa, then it's on you to determine if separate implementations will be faster than one implementation with optional arguments. If you are concerned about code duplication you could always have yet a third procedure to implement the common code, and call it from both variants.

同样,您需要检查编译器生成的汇编程序,以查看编译器(版本)如何处理所编写代码的变化.

Equally, it is on you to inspect the assembler generated by your compiler to see just what your compiler (version) does with the variations in code that you write.

这篇关于Fortran是否在编译时解析可选参数并显示语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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