Loop Transformation for Cache Optimization in Original Code
Restructure loops to improve cache performance and minimize misses by operating on block units, ensuring blocks fit in cache, enhancing temporal locality, and reducing worst-case misses.
Loop Transformation for Cache Optimization in Original Code
E N D
Presentation Transcript
4.2 Blocking Original Code Transformed Code for (jj=0; jj<N; jj = jj+B) for (kk=0; kk<N; kk = kk+B) for (i=0; i<N; i++) for (j=jj; j<min(jj+B,N); j++) {r=0; for(k=kk;j<min(kk+B,N); k++) r = r + y[i][k] * z[k][[j]; x[i][j] = x[i][j] + r; }; for (i=0; i<N; i++) for (j=0; j<N; j++) {r=0; for (k=0; k<n; k++) r = r + y[i][k] * z[k][[j]; x[i][j] = r; }; A row in a block One block in a column All blocks in a column All columns of blocks • Restructure the loops to improve • Fit in the cache • Improve temporal locality • Solutions now become machine dependent
4.2 Blocking (cont.) z[k][j] y[i][k] • What is the miss behavior? • Decompose the computation to operate on BxB blocks such that three blocks fit in the cache • Reduce the overall number of worst case misses by a factor of B Compute the partial product for this block Complete computation of all columns (jj) Compute a row in the block (j and k) Complete computation of Block (0,0) (i) Complete computation of Blocks in a column (kk)