简体   繁体   English

将数组大小作为参数传递给 fortran 中的子例程

[英]Passing size of array as argument to a subroutine in fortran

I was wondering about the overhead of querying size of array in fortran. Old fortran (<f95) way was to pass the size of array to the arguments of subroutine:我想知道在 fortran 中查询数组大小的开销。旧的 fortran (<f95) 方法是将数组的大小传递给子例程的 arguments:

subroutine asub(nelem,ar)
integer,intent(in)::nelem
real*8,intent(in)::ar(:)

! do stuff with nelem such as allocate other arrays

end subroutine asub

Since the size function of f95, it can be done this way:由于f95的大小为function,所以可以这样做:

subroutine asub(ar)
real*8,intent(in)::ar(:)

! do stuff with size(ar) such as allocate other arrays

end subroutine asub

Is method 2 bad performance-wise if asub is called million times?如果 asub 被调用一百万次,方法 2 是否性能不佳?

I am asking because I am working on a relatively big code where some array sizes are global variables (not even passed as subroutine arguments), which is really bad in my opinion.我问是因为我正在处理一个相对较大的代码,其中一些数组大小是全局变量(甚至不作为子例程参数传递),我认为这真的很糟糕。 Method 1 would require a lot of work in order to propagate the array sizes to the whole code while method 2 is clearly faster to achieve in my case.方法 1 需要大量工作才能将数组大小传播到整个代码,而方法 2 在我的例子中显然更快。

Thanks !谢谢 !


nelem is a number that you need to read from memory, size(ar) is also a number that you need to read from memory. And you need to inquire the value just once. nelem是需要从memory读取的数, size(ar)也是需要从memory读取的数,查询一次即可。 And then probably do a lot of computation over nelem elements.然后可能对nelem元素进行大量计算。 The overhead inquiring the size of the value will be completely negligible.查询值大小的开销将完全可以忽略不计。

OK, size(ar) is a function call, but the compiler can just insert reading the right value from the array descriptor).好的, size(ar)是一个 function 调用,但编译器可以插入从数组描述符中读取正确的值)。 And even if it remains a function call, still it will be called just once.即使它仍然是 function 调用,它仍然只会被调用一次。

Differences, if any, will be elsewhere, mainly as described in the Q/A linked linked by francescalus Passing arrays to subroutines in Fortran: Assumed shape vs explicit shape .差异(如果有的话)将在其他地方,主要如 francescalus将 arrays 中的子程序传递给 Fortran 中链接的 Q/A 中所述:假定形状与显式形状 Depending on what the compiler can assume about the array being contiguous in memory it will be able to optimize it better or worse (eg SIMD vectorization).根据编译器可以假设数组在 memory 中是连续的,它将能够更好或更差地优化它(例如 SIMD 矢量化)。

As always, where performance matters, you should test and measure.与往常一样,在性能很重要的地方,您应该进行测试和测量。 Remember to enable all relevant compiler optimizations.请记住启用所有相关的编译器优化。

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

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