Loop Interchange and Subscripts: Matrix Multiply

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

Example 1: Typical Matrix Multiplication

for(i=0; i<N; i++)

{

    for(j=0; j<n; j++)

    {

        for(k=0; k<n; k++)

        {

            c[i][j]=c[i][j]+a[i][k]*b[k][j];

        }

    }

}

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 2: Matrix Multiplication with Stride-1

for(i = 0; i<N; i++)

{

    for(k=0; k<n; k++)

    {

        for(j=0; j<n; j++)

        {

            c[i][j]=c[i][j]+a[i][k]*b[k][j];

        }

    }

}