使用名为MARS 4.5的模拟器我试图提高此代码的缓存性能。这是汇编程序的子部分,使用Sieve of Eratosthenes算法计算素数。
由于某种原因,sw(存储字)的缓存命中率为25%,其中程序的其余部分在其当前状态下平均为约50%。我已经尝试重新排列一些东西,但我无法弄清楚是什么导致了这个瓶颈。需要做些什么来提高缓存命中率?
inner: add $t2, $s2, 0 # save the bottom of stack address to $t2
mul $t3, $t1, 4 # calculate the number of bytes to jump over
sub $t2, $t2, $t3 # subtract them from bottom of stack address
add $t2, $t2, 8 # add 2 words - we started counting at 2!
sw $s0, ($t2) # store 1's -> it's not a prime number!
add $t1, $t1, $t0 # do this for every multiple of $t0
bgt $t1, $t9, outer # every multiple done? go back to outer loop
j inner # some multiples left? go back to inner loop
答案 0 :(得分:1)
我能够通过修改程序来存储字节而不是单词来解决这个问题。这增加了缓存中的存储块数量,从而提高了命中率。
inner: add $t2, $s2, 0 # save the bottom of stack address to $t2
addi $t3, $t1, 1 # add one byte
sub $t2, $t2, $t3 # subtract them from bottom of stack address
add $t2, $t2, 2 # add 2 bytes - we started counting at 2!
sb $s0, ($t2) # store 1's -> it's not a prime number!
add $t1, $t1, $t0 # do this for every multiple of $t0
bgt $t1, $t9, outer # every multiple done? go back to outer loop
j inner # some multiples left? go back to inner loop