ISRO Scientist/Engineer CSE 2017Computer Organization and Architecture › Cache and Memory Hierarchy
ISRO Scientist/Engineer CSE 2017 PYQ: A CPU has a $32\, KB$ direct mapped cache with $128-byte$ block size.Suppose A is a 2-$d$ array of size…
A CPU has a $32\, KB$ direct mapped cache with $128-byte$ block size.Suppose A is a 2-$d$
array of size $512\times 512$ with elements that occupy $8\, bytes$ each.Consider the code segment
for(i=0;i<512;i++)
{
for(j=0;j<512;j++)
{
x +=A[i][j];
}
} Assuming that array is stored in order $A[0][0], A[0][1],A[0][2]\ldots,$ the number of cache misses is
- A16384Correct answer
- B512
- C2048
- D1024
Answer
(A) $16384$
Approach
In the code, the inner loop iterates j from 0 to 511, so for each i the accesses are A[i][0], A[i][1], ..., A[i][511]. Since the array is stored in row-major order, these are consecutive memory locations. A cache block is 128 bytes, and each element is 8 bytes, so one block holds 128/8 = 16 elements. Therefore, within a row, every 16 consecutive accesses hit the same block: the first access to a new block misses, and the next 15 accesses hit. Each row has 512 elements = 512/16 = 32 blocks, so there are 32 misses per row. With 512 rows, total misses = 512 × 32 = 16,384. The direct-mapped cache has 32 KB / 128 B = 256 lines, which is far smaller than the total number of blocks (16,384), but because the program scans the array sequentially and never revisits a block after its 16 elements have been read, there is no temporal reuse. Thus no block is evicted and later needed again; every block is a compulsory miss. The cache size and direct-mapped structure do not change the miss count here. Hence option A (16,384) is correct. The community answer's “16834” is a typographical transposition, but the selected option is still A.
Reviewed by ISRO IQ Editorial Team.