Loop Interchange and Subscripts: Matrix Multiply

Matrix multiplication is commonly written as shown in the following example.

DO I=1, N
 DO J=1, N  
   DO K=1, N  
     C(I,J) = C(I,J) + A(I,K)*B(K,J)
   END DO
 END DO
END DO

The use of B(K,J), is not a stride-1 reference and therefore will not normally be vectorizable. If the loops are interchanged, however, all the references will become stride-1 as in the Matrix Multiplication with Stride-1 example that follows.

Note
Interchanging is not always possible because of dependencies, which can lead to different results.

Example of Matrix Multiplication with Stride-1:

DO J=1,N
 DO K=1,N
   DO I=1,N
     C(I,J) = C(I,J) + A(I,K)*B(K,J)
   ENDDO
 ENDDO
ENDDO

For additional information, see publications on Compiler Optimizations.