Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

parameter passing - Correct use of FORTRAN INTENT() for large arrays

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...