Optimizing vectorized array operations

There are lots of really good answers here! So I won’t add to those.

Let me just add a final note which makes array-syntax sub-optimal, and in this suggestion, it can kill performance.

When you do

real, allocatable :: a(:,:,:)
...

a = b

what the compiler will do (because of the standard) is to check if a has the correct shape, and if not, deallocate it, then re-allocate it, then copy. If it has the correct shape, it will just copy. However, the checks inserts lots of unnecessary code…

While this may be intended by the programmer, it can hurt performance when one is doing that call frequently, and you intentionally don’t want to check shape. So I would encourage users to adopt this standard:

a(:,:,:) = b

whenever you really don’t want to deallocate. The slices tells the compiler that you want to overwrite values in the array, and thus the compiler will not put in checks for shape, and re-alloc statements.
The point is that you’ll be removing lots of branches, and gain free performance.
Once gotten use to it will also be more obvious when you want what.

2 Likes