When is CONTIGUOUS useful?

Dear all,

Quick question, when I am using Intel OneAPI’s Intel Advisor, I remember there was a time I saw a performance hints, saying that using F2008’s CONTIGUOUS property. So I checked it,

Intel’s example,

REAL, CONTIGUOUS, DIMENSION(:,:) :: A
REAL, POINTER, CONTIGUOUS :: MY_POINTER(:)

It is perhaps to emphasis the assumed-shape array is CONTIGUOUS so it make the compiler easier to do vectorization or some optimization.

Just curious, does anyone use CONTIGUOUS and get some performance gains?
In what scenario CONTIGUOUS can be useful?

Thanks much in advance!

2 Likes

It has its uses, although not very often, at least not for me. One case that contiguous might be useful is when you have to deal with C interoperability. Say, you call a C function from within Fortran code that allocates memory and returns a pointer to that memory block. The corresponding Fortran pointer should be something like
integer(kind=c_signed_char), dimension(:), pointer, contiguous :: data
I can recall a few cases that this didn’t work without the contiguous attribute.
Another example, actually taken from a program I wrote some time ago: Assume you have a C subroutine that renders an image using an array of RGB triplets for each pixel. Now, another subroutine, written in Fortran, has to read the data from an image file. We do not know in advance how big the image is, so we have to use an assumed-shape array to store the data; that array must be contiguous, otherwise the rendering subroutine will render garbage.

1 Like

This thread could be useful

1 Like

The programmer is her/himself responsible to make sure that is the case. Unlike in C where the compiler must take into account that things might overlap, the Fortran compiler can simply assume that to be the case. I guess the volatile attribute might help if you know that is not going to be the case?

1 Like