# Matrix Multiplication

This is a discussion on Matrix Multiplication within the C Programming forums, part of the General Programming Boards category; I'm writting some functions that need to multiply large matrices together. This is what I have so far for it. ...

1. ## Matrix Multiplication

I'm writting some functions that need to multiply large matrices together. This is what I have so far for it. It works fine, but it runs to slow. I need to run this function a lot so it needs to run fast. What could I do to the following code to make it run faster, other than loop unrolling, cause I know how to do that I just didn't want to type it all out here.

Code:
```void mm(void)
{

int i,j,k;
unsigned long sum;

/* Multiply the two arrays and store the result in a third array  */
for ( j=0; j<512; j++)
for ( i=0; i<512; i++)
{
sum = 0;
for ( k=0; k<128; k++ )
sum += ARRAY_4A[i][k] * ARRAY_4B[k][j];
ARRAY_RES[i][j] = sum;
}

}```

2. I'm using gcc and running on linux. pentium III

I was able to reduce the operation time to about 10% of the original time by manualy unrolling the loops in steps of three for the middle loop and steps of 16 for the inner loop. I also set gcc to optimization level 3, but I haven't tried just using fun loops yet( I don't know gcc that well).

I know of another algothrim (strasson or something like that), that is suppose to be really fast, but I don't know how to implement it.

There are also a few different way to format the loop that will make have less cache misses, but you have to do another store instuction in the inner loop, depending on the machine this can make it faster or slower. I did some simple test and found that it seems to make it faster for the pentium III.

Code:
```for(i = 0; i<512; i++){

for(k = 0; k<512; k++){

r = a[i][k];

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

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

}
}```

4. do a search on the web for c programming gems or algorithms... this has already been done and reduced for graphics work...