Skip to content

GATE CS 2006 Q81 — Memory Hierarchy: Cache, Main Memory and Secondary Storage

Explain with AI

A CPU has a 3232 KBKB direct mapped cache with 128128 byte-block size. Suppose AA is two dimensional array of size 512×512512 \times512 with elements that occupy 8−bytes8-bytes each. Consider the following two CC code segments, P1P1 and P2P2.

P1P1: 

for (i=0; i<512; i++)
{ 
    for (j=0; j<512; j++)
    { 
        x +=A[i] [j]; 
    } 
}

P2: 

for (i=0; i<512; i++)
{ 
    for (j=0; j<512; j++)
    { 
        x +=A[j] [i]; 
    } 
}

P1P1 and P2P2 are executed independently with the same initial state, namely, the array AA is not in the cache and ii, jj, xx are in registers. Let the number of cache misses experienced by P1P1 be M1M1 and that for P2P2 be M2M2.

The value of the ratio M1M2\frac{M_{1}}{M_{2}}:

Choose one option
View worked solution

Answer: B

To find the ratio M1M2\frac{M_1}{M_2}, we analyze the cache behavior for both programs P1P1 and P2P2.

Cache Parameters:

  • Cache Size = 32 KB32\text{ KB}
  • Block Size = 128 bytes128\text{ bytes}
  • Number of Cache Blocks = 32 KB128 B=256\frac{32\text{ KB}}{128\text{ B}} = 256 blocks
  • Array Element Size = 8 bytes8\text{ bytes}
  • Number of elements per cache block = 1288=16\frac{128}{8} = 16 elements
  • Size of one row of array AA = 512×8 bytes=4096 bytes512 \times 8\text{ bytes} = 4096\text{ bytes} (3232 cache blocks)

1. Analysis of P1P1 (Row-Major Access):

In P1P1, elements are accessed sequentially: A[0][0],A[0][1],A[0][2],…A[0][0], A[0][1], A[0][2], \dots

  • The first access A[0][0]A[0][0] is a miss and brings a block of 1616 elements (A[0][0]A[0][0] to A[0][15]A[0][15]) into the cache.
  • The next 1515 accesses are hits.
  • This pattern of 11 miss followed by 1515 hits repeats throughout the execution.
  • Total misses: M1=512×51216M_1 = \frac{512 \times 512}{16}

2. Analysis of P2P2 (Column-Major Access):

In P2P2, elements are accessed column-wise: A[0][0],A[1][0],A[2][0],…A[0][0], A[1][0], A[2][0], \dots

  • The stride between consecutive accesses is one row (4096 bytes4096\text{ bytes} or 3232 cache blocks).
  • Since the cache has 256256 blocks, elements A[i][j]A[i][j] and A[i+8][j]A[i+8][j] map to the same cache block index because (i×32)(mod256)=((i+8)×32)(mod256)(i \times 32) \pmod{256} = ((i+8) \times 32) \pmod{256}.
  • Due to this conflict, blocks are repeatedly evicted before their other elements can be reused. Consequently, every single access results in a cache miss.
  • Total misses: M2=512×512M_2 = 512 \times 512

Ratio:

M1M2=512×51216512×512=116\frac{M_1}{M_2} = \frac{\frac{512 \times 512}{16}}{512 \times 512} = \frac{1}{16}

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Memory Hierarchy: Cache, Main Memory and Secondary Storage questions