Memory Hierarchy
When each row of a 2D array is 4,096 bytes (a power of two), walking down a column can cause many misses even though the cache has spare capacity. Which countermeasure is effective?
- APadding each row so its size is not a power of two
- BChanging the element type to a different type of the same size
- CRelocating the whole array so it starts on a large power-of-two address boundary
Show answer and explanation
Answer: A. Padding each row so its size is not a power of two
With rows exactly 4096 bytes long, elements down a column are spaced by multiples of 4096, so the index bits within the low 12 address bits are identical and the elements crowd into a few sets (all into the same set in an L1 cache with 4 KB or less per way). When more rows than the associativity map to one set, conflict misses occur even with capacity to spare. Padding each row by about one line shifts the set for each element and spreads them out. Aligning to a power of two leaves the problem intact, and a same-sized type changes nothing.
- A. Correct. Shifting the row size spreads elements across different sets.
- B. A type of the same size leaves the addresses unchanged.
- C. Aligning to a power of two leaves the concentration in one set.