国际空间研究组织 | ISRO CS 2017 |问题 38
CPU 有一个 32 KB 的直接映射缓存,块大小为 128 字节。假设 A 是一个大小为 512×512 的二维数组,其中每个元素占用 8 个字节。考虑代码段
for (i =0; i < 512; i++) {
for (j =0; j < 512; j++) {
x += A[i][j];
}
}
假设数组按A[0][0]、A[0][1]、A[0][2]……的顺序存储,则缓存未命中数为
(一) 16384
(乙) 512
(三) 2048
(四) 1024答案:(一)
解释:
Block size = 128 Byte
Number of elements in 1 block = 128/8 = 16
Block 0: A[0][0] to A[0][15]
Block 1: A[0][16] to A[0][31] and so on
For i=0: A[0][0] is not present in the cache, so there will be a miss but for the next 15 elements (j=1 to j=15), there will be no miss.
j runs from 0 to 512 and there will be a miss after every 16 elements. So total number of misses for i=0 is (512/16) = 32.
The outer loop of i runs from 0 to 512 so the total number of misses will be 512 * 16 = 16834
这个问题的测验