Solution #5: Using registers for highly reused variables.

  • C[i][j] is replaced with registers and write into memory at the final step. It effectively reduce the frequency of accessing memory.
  • For example, (index is used to identify each chuck in a matrix)
    • C00 = A00*B00 + A01*B10 + A02*B20 + A03*B30
      C01 = A00*B01 + A01*B11 + A02*B21 + A03*B31
    • A and C are reused, thus can be replaced with registers. The following is the result.
    • R2 = A00; R3 = A01; R4 = A02; R5 = A03;
      R0 = R2*B00 + R3*B10 + R4*B20 + R5*B30
      R1 = R2*B01 + R3*B11 + R4*B21 + R5*B31
      C00 = R0; C01 = R1;
  • When using registers instead of memory access, block size should be small because most systems have only a limited number of registers.
  • Loop unrolling is useful technique to apply this optimization.