简体   繁体   English

FORTRAN-减少函数调用开销

[英]FORTRAN - Reduce function call overhead

I have a fortran code like this: 我有一个这样的fortran代码:

file1.f90 file1.f90

program myprog
 use func1mod
    do i=1,N
       call subroutine1
    enddo
    subroutine subroutine1
         integer*8::var1,var2,var3,...
         do j=1,N
            x=func1(var1,var2,var3,..)
            computations based on x
         enddo
    return
    end
end 

file2.f90 file2.f90

 module func1mod
 contains
     func1(var1,var2,var3,....)
         func1=some computations based on var1, var2, var3, ...
      return
     end function func1
 end module func1mod

function func1 does not modify any of its arguments. 函数func1不会修改其任何参数。 It computes a value based on the arguments and returns a value. 它根据参数计算一个值并返回一个值。 The # of arguments is large but the function is less than 30 lines of code. 参数数很大,但函数少于30行代码。 What is the best approach to reduce the function call overhead. 减少函数调用开销的最佳方法是什么。 One approach would be to inline the function. 一种方法是内联函数。 Is there any other way out? 还有其他出路吗?

The best you can do is be as explicit as possible about the semantics of the function, turn optimization up as high as possible, and let the compiler make the best decision it can about how best to implement the call. 您可以做的最好的事情是尽可能清楚地了解函数的语义,提高优化程度,让编译器就如何最好地实现调用做出最佳决策。 Make sure the dummy variables are marked intent(in) , and mark the function as pure - although if it's only 30 lines, the compiler will doubtless notice these things anyway at high optimization - and check your compiler options to see if there's anything you can do to encourage (for instance) inlining. 确保将虚拟变量标记为intent(in) ,并将函数标记为pure函数-尽管如果只有30行,编译器无疑会在高度优化的情况下注意到这些情况-并检查编译器选项,以查看是否有可以做的事情鼓励(例如)内联。

Generally the overhead of a procedure call is low. 通常,过程调用的开销很低。 If the function has 30 lines of code probably you will gain very little because the actual function will dominant over the function call. 如果该函数有30行代码,则可能会获得很少的收益,因为实际函数将优先于函数调用。 If you want to be sure, measure the runtime of the current implementation, then inline the code and measure that runtime. 如果要确定,请测量当前实现的运行时,然后内联代码并测量该运行时。

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

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