Thursday, February 2, 2012

Cache Blocking in C

 /*:) cache_blk_mat_mult :) have fun */  
 // header files  
 #include<unistd.h>  
 #include<stdio.h>  
 int min(int, int);  
 //main func  
 int main(int argc, char *argv[])  
 {  
  int jj,kk,j,k,i;  
  int B = 5, N = 10,r;  
  int x[N][N],y[N][N],z[N][N];  
  for(i=0;i<N;i++)  
   for(j=0;j<N;j++)  
    {  
      printf("%d%d ",i,j);  
      x[i][j]=5;  
      y[i][j]=6;  
      z[i][j]=7;  
    }  
  for(jj=0;jj<N;jj=jj+B)  
   {  
    printf("jj = %d\n",jj);  
    for(kk = 0;kk<N;kk=kk+B)  
      {  
       printf("kk = %d\n",kk);  
       for(i=0;i<N;i++)  
        {  
         printf("i = %d\n",i);  
         for (j = jj; j < min(jj+B-1,N); j++)  
           {  
            r = 0;  
            for (k = kk; k < min(kk+B-1,N); k++)  
             {  
              printf("r = %d + y[%d][%d]*z[%d][%d]\n",r,i,k,k,j);  
              r = r + y[i][k]*z[k][j];  
             }  
            x[i][j] = x[i][j] + r;  
            printf("x[%d][%d]\n",i,j);  
            getchar();  
           }  
        }  
      }  
   }  
  return 0;   
 }  
 int min(int a, int b)  
 {  
  if(a<b)  
   return a;  
  else  
   return b;  
 }  

No comments:

Post a Comment