对大型数组正确使用 FORTRAN INTENT() [英] Correct use of FORTRAN INTENT() for large arrays

查看:25
本文介绍了对大型数组正确使用 FORTRAN INTENT()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出使用 FORTRAN 子例程处理大型数据数组的最佳方法.我有一个适用于大型 3,4 或 5 维数组的代码.我正在使用许多子例程,并且想知道通过引用调用这些数组的最佳方法是什么,同时又要保证它们不被写入(显然,输出数组除外).

I'm trying to figure out the best way to use FORTRAN subroutines to work with big arrays of data. I have a code which works on big 3,4 or 5 dimensional arrays. I'm working with many subroutines and was wondering what is the best way to call these arrays by reference yet keeping them safe from writing (except for the output array, obviously).

我已阅读 [this related intent() question]:fortran 意图 (in,out,inout) 之间的显式区别是什么? 我应该使用 intent(out) 通过引用所有输出数组来调用,但是如果我这样做与输入数组相同,那么我可能需要一个类似 C 的 CONST 来使其具有写保护.

I have read in [this related intent() question]: What is the explicit difference between the fortran intents (in,out,inout)? that I should use intent(out) to call by reference all the output arrays, however if I do the same with input arrays, then I probably need a C-like CONST to make it write-protected.

所以底线是:1. 我应该如何根据输入和输出变量的 INTENT() 声明虚拟变量?2. 如何使子程序的引用调用变量写保护?

So the bottom line is: 1. How should I declare the dummy variables in terms of INTENT() for input and output variables? 2. How do I make called-by-reference variables write-protected for the subroutine?

推荐答案

其实C++ like const reference 和intent(in) 很像,但更重要的是sumeed shape 的区别,假设大小,显式大小数组虚拟参数.

Actually, C++ like const reference is very similar to intent(in), but more important is the distinction between assumed shape, assumed size, explicit size array dummy arguments.

对于显式大小和假定大小的数组,Fortran 规则有几个要求,(结合隐式接口的可能性)使得有必要使用指向第一个元素的指针来使用经典的传递引用.

For explicit size and assumed size arrays the Fortran rules have several requirements, that (in combination with the possibility of an implicit interface) make it necessary to use classical pass-by reference using a pointer to the first element.

但是,编译器不必将指针传递给原始数组,它可以将指针传递给临时副本.

However, the compiler does not have to pass the pointer to the original array, it can pass a pointer to a temporary copy.

对于需要显式接口的更复杂的传递机制,传递一个描述符(或指向描述符的指针).同样,描述符实际上可以是数组的临时副本.但是这些先进的传递机制,作为假定的形状数组、可分配和指针数组参数,使得它不太可能需要临时的.

For more complex passing mechanisms, which require explicit interface, a descriptor is passed (or a pointer to a descriptor). Again, the descriptor can in fact be of a temporary copy of the array. But these advanced passing mechanisms, as the assumed shape arrays, allocatable and pointer array arguments, make it less likely to need the temporary.

当使用 value 属性时,总是会创建临时的.传递了一个指向临时对象的指针(或描述符).

The temporary is always created when the value attribute is used. A pointer (or descriptor) to the temporary is passed.

如果您使用任何意图,在不需要临时性的情况下,它不应该有太大变化.这只是一个承诺,传递机制不会改变.

If you use any of the intents, it shouldn't change much where no temporary was necessary. It is just a promise and the passing mechanism doesn't change.

当需要临时文件时,如果您指定 intent(in)intent(out),编译器可以避免与临时文件之间的副本之一.

When the temporary was necessary, the compiler can avoid one of the copies to or from the temporary if you specified intent(in) or intent(out).

这篇关于对大型数组正确使用 FORTRAN INTENT()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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