title: Efficient Matrix multiplication in RISC-V
body: <p>I am struggling with matrix multiplication in RISC-V</p>
  <p>Input is 128*128 matrix with unsigned short integer entry</p>
  <p>I've wrote a naive one</p>
  <p>The goal is to reduce the clock cycles</p>
  <p>• Below 20,000,000 cycles (2%)
  • Below 18,000,000 cycles (2%)
  • Below 16,000,000 cycles (2%)
  • Below 14,000,000 cycles (2%)
  • Below 12,000,000 cycles (2%)
  • Below 10,000,000 cycles (1%)
  • Below 9,000,000 cycles (1%)
  • Below 8,000,000 cycles (1%)
  • Below 7,000,000 cycles (1%)
  • Below 6,000,000 cycles (1%)</p>
  <p>Mine is about 19M</p>
  <p>and bruteforce in C with O3 is 16M</p>
  <p>I know there's the a better algorithm to do matrix multiplication o(n^3)</p>
  <p>but here n is only 128, I wonder if there's other way to reduce cycles by the O(n^3) algo</p>
  <p>is there anything can I do to boost my code?</p>
  <pre><code>.global matrix_mul
  .type matrix_mul, %function
  
  .align 2
  # void matrix_mul(unsigned int A[][], unsigned int B[][], unsinged int C[][]);
  matrix_mul:
      
      # for i = 0 to 127
      #   for j = 0 to 127
      #       for k = 0 to 127
      #           C[i][j] += A[i][k] * B[k][j] % 1024
  
      # insert code here
          addi t0, zero, 128 # size
          add s11, zero, a1 # B
  
          add t1, zero, zero # i = 0 
  loop1:  # loop1 #
          add t2, zero, zero # j = 0
          # loop2 #
  loop2:   
          add t3, zero, zero # k = 0
          add t4, zero, zero # sum = 0
          # loop3 #
  loop3:
          lhu t5, 0(a0) # A[i][k]
          lhu t6, 0(s11) # B[k][j]
          mul t5, t5, t6 # A*B
          add t4, t4, t5 # C += A*B
          andi t4, t4, 1023 # mod 1024
          addi a0, a0, 2 # A[i][k+1]
          addi s11, s11, 256 # B[k+1][j]
          addi t3, t3, 1 # k++ 
          blt t3, t0, loop3 # k&lt;size , continue
          # loop3 end #
          sh t4, 0(a2) # store back to C[i][j]
          addi a2, a2, 2 # C[i][j+1]
          addi a0, a0, -256 # A go back
          addi a1, a1, 2 # B[k][j+1]
          add  s11, zero, a1 # B
          addi t2, t2, 1 # j++
          blt t2, t0, loop2 # j&lt;size , continue
          # loop2 end #
          addi a0, a0, 256 # A[i+1][k]
          addi a1, a1, -256 # B go back (because line 40 add 2*128=256)
          add s11, zero, a1 # B
          addi t1, t1, 1 # i++
          blt t1, t0, loop1 # i&lt;size , continue
          # loop1 end #
  
      # Green card here: https://www.cl.cam.ac.uk/teaching/1617/ECAD+Arch/files/docs/RISCVGreenCardv128-20151013.pdf
  
      ret
  </code></pre>