Last Updated : 19 Nov, 2018

A CPU has a 32 KB direct mapped cache with 128 byte block size. Suppose A is a 2 dimensional array of size 512×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]……, the number of cache misses is
(A) 16384
(B) 512
(C) 2048
(D) 1024


Answer: (A)

Explanation:

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


Quiz of this Question


Share your thoughts in the comments