A CPU has a direct mapped cache with byte-block size. Suppose is two dimensional array of size with elements that occupy each. Consider the following two code segments, and .
:
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];
}
}
and are executed independently with the same initial state, namely, the array is not in the cache and , , are in registers. Let the number of cache misses experienced by be and that for be .
The value of the ratio :
View worked solution
Answer: B
To find the ratio , we analyze the cache behavior for both programs and .
Cache Parameters:
- Cache Size =
- Block Size =
- Number of Cache Blocks = blocks
- Array Element Size =
- Number of elements per cache block = elements
- Size of one row of array = ( cache blocks)
1. Analysis of (Row-Major Access):
In , elements are accessed sequentially:
- The first access is a miss and brings a block of elements ( to ) into the cache.
- The next accesses are hits.
- This pattern of miss followed by hits repeats throughout the execution.
- Total misses:
2. Analysis of (Column-Major Access):
In , elements are accessed column-wise:
- The stride between consecutive accesses is one row ( or cache blocks).
- Since the cache has blocks, elements and map to the same cache block index because .
- 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:
Ratio:
Free practice · Your answers on this page are not saved.
