1

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?

Community
  • 1
  • 1
  • 3
    As stated in the linked question, the accepted answer is wrong: `intent(in)` is *not* pass by value. Generally speaking, you have (almost) no means to force call by reference in Fortran, but you don't need to because the compiler choses the optimal mode. Using `intent`, you give hints to the compiler in doing so and ease the detection of programming errors. – Alexander Vogt Oct 26 '14 at 14:30

1 Answers1

2

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

  • So if I want to modify a large 3-dim array from within a subroutine, best practice is to use intent(inout) and let the compiler handle it? If I'd like it to pass by reference anyway, how is that done? – TheWhitestOfFangs Oct 27 '14 at 11:52
  • You probably misunderstood the answer. Try to read it again. Pass by reference is an internal thing, not a Fortran term. I used this term, because you used other C language term in the question. The point was that `intent` does not influence the passing mechanism very much. It is more useful for you to avoid some types of programming mistakes. The more importantthings is if you assumed size, expicit size or assumed shape array arguments. If you don't know what that means I **strongly** recommend you to study any Fortran book to learn it. – Vladimir F Героям слава Oct 27 '14 at 12:14
  • Arrays are passed with assumed shape. I got what you mean now. – TheWhitestOfFangs Oct 27 '14 at 15:38