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

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

问题描述

我试图找出使用FORTRAN子程序处理大数据数组的最佳方法。
我有一个适用于3,4或5维大数组的代码。
我正在处理许多子程序,并想知道通过引用调用这些数组的最佳方式是什么,但保持写入安全(显然除输出数组外)。



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

所以最后一行是:
1.我应该如何根据INTENT()来为输入和输出变量声明虚拟变量?
2.我如何为子程序写保护被引用的变量?

解决方案

实际上,类似于const引用的C ++非常类似于 intent(in),但更重要的是假设形状,假定大小,显式大小数组虚拟参数之间的区别。 b
$ b

对于显式大小和假定的大小数组,Fortran规则有几个要求,(与隐式接口的可能性相结合)使得有必要使用经典传递引用第一个元素。但是,编译器不必将指针传递给原始数组,它可以将指针传递给临时副本。



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



临时文件总是在<使用code> value 属性。传递给临时指针(或描述符)。



如果使用任何意图,在没有临时需要的地方不应该改变太多。这只是一个承诺,传递机制不会改变。



当临时文件是必需的时,编译器可以避免临时文件中的一个副本指定 intent(in) intent(out)


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).

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.

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?

解决方案

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.

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.

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.

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天全站免登陆