6

I was trying to pass an array to a subroutine, declared in the subroutine as an assumed shape array. That was giving me some problems that I have been able to solve by passing a pointer instead.

But some user with a high reputation tells me in a comment:

Adding pointer is also a reasonable way of telling the compilers optimiser that it doesn't have to do any work today.

Could anyone offer a short explanation on this? The language is Fortran 95, though I believe this applies to other languages.

francescalus
  • 30,576
  • 16
  • 61
  • 96
Mephisto
  • 640
  • 1
  • 6
  • 12
  • I think the OP is referring to [this comment](http://stackoverflow.com/questions/41009109/subroutine-not-returning-correct-numerical-values-in-assumed-shape-array-due-to#comment69272729_41032273). – 4castle Dec 08 '16 at 23:42
  • 1
    It is true. For SIMD it is hard to guarantee that the data is sequential whether the point is indexing sequentially. It is likely more true for Fortran than most languages, as a lot of Fortran is specifically targeting vectors/arrays. But the guts of the compilers are all generally approaching an asymptote where the assembly can be near identical between fortran and c. Generally means for some things, and more difficult code requires more work. – Holmz Dec 09 '16 at 08:31
  • 1
    @4castle I did not include a link to the original comment on purpose, because it contains misleading information: (by using a pointer) "you are no longer able to pass array sections", which is wrong. Nothing is easier than assigning a pointer to an array section before passing it as argument to the subroutine. – Mephisto Dec 11 '16 at 23:04

1 Answers1

4

Yes, Fortran compilers have to assume that pointers can alias with other pointers and with target variables.

If you have pointer arrays a and b then in

  a(i) = a(i) + b(i)

the compiler must assume that these two arrays may partially overlap and it must inhibit certain optimizations, because changing the value of a can change some value of b at some unknown index.

See also the C restrict keyword and a much more thorough discussion at Is Fortran easier to optimize than C for heavy calculations? . It is not worth repeating all points about pointer aliasing raised there.

IanH's comment was intentionally perhaps bit too strong, but there is a lot of truth in it.

Community
  • 1
  • 1