can a pointer to a 4x4 slice of a 5x5 array be CONTIGUOUS?

Tobias Burnus burnus@net-b.de
Thu Jan 3 12:12:00 GMT 2013


Dear Sylwester,

Sylwester Arabas:
> A short question on the CONTIGUOUS attribute for pointers: Do I 
> understand correctly, that declaring a pointer to a 4x4 slice within a 
> 5x5 array as CONTIGUOUS is incorrect? (as the elements pointed by the 
> pointer do not constitute a contiguous region in the memory).

It is invalid. However, due to the way loops are walked it is currently 
working. Namely, one has for

    p(:,:) = 8

the following loop (-fdump-tree-original) and the "dim[1].stride" takes 
care of the noncontiguous memory:

       while (1)
         {
           if (S.0 > D.1900) goto L.2;
           {
             integer(kind=8) D.1903;
             integer(kind=8) S.1;

             D.1903 = x->dim[1].stride * S.0 + D.1896;
             S.1 = D.1897;
             while (1)
               {
                 if (S.1 > D.1898) goto L.1;
                 (*D.1895)[S.1 + D.1903] = 8;
                 S.1 = S.1 + 1;
               }
             L.1:;
           }
           S.0 = S.0 + 1;
         }

Without contiguous, the inner loop has:

       D.1913 = x->dim[0].stride;
...
             while (1)
               {
                 if (S.3 > D.1909) goto L.3;
                 (*D.1906)[S.3 * D.1913 + D.1915] = 8;
                 S.3 = S.3 + 1;
               }

Note the additional stride, which prevents in this case the vectorization.

However, misusing CONTIGUOUS will fail if you pass this array to, e.g, 
an explicit-size/assumed-size array. Also, the compiler could in future 
fold the nested loop into a single loop, and instead of using two loops 
from 1 to 4, using a single loop from 1 to 16. In that case, the 
"stride" for dimension = 2 wouldn't be accessed and the result will be 
wrong.

Tobias



More information about the Fortran mailing list