Sub topic 5
Average memory access time
This chapter assumes you know what a cache hit and a miss are, and that you can convert between a hit ratio and a miss ratio. It builds on the earlier chapters on hit ratio, miss ratio and miss penalty. It does not need address splitting or mapping yet.
What AMAT measures
The CPU does not wait the same time for every memory reference. A hit costs the cache time. A miss costs more, because the block must come from a slower level. Average memory access time (AMAT) is the expected time per reference, averaged over all references in a workload.
AMAT is an average. It does not describe the worst case (every reference misses) or the best case (every reference hits). It depends on two things:
- the hardware, through the cache time and the memory time;
- the program, through the hit ratio.
The same machine therefore has a different AMAT for a program with good locality than for one with poor locality.
The two standard formulas
Let these symbols be defined:
t_cis the time to access the cache (the hit time).t_mis the time to access main memory.his the hit ratio, andm = 1 - his the miss ratio.
Hierarchical (sequential) access
The CPU looks in the cache first. On a miss, it then goes to main memory. A miss therefore pays the cache lookup and the memory access.
AMAT = h * t_c + (1 - h) * (t_c + t_m)
= t_c + (1 - h) * t_mThe second form is the one to remember: AMAT = hit time + miss ratio × miss penalty. Here the miss penalty is t_m, the extra time beyond a hit.
Simultaneous (parallel) access
Some questions say the cache and memory are accessed at the same time. The memory access starts immediately and is abandoned on a hit. A miss then costs only t_m.
AMAT = h * t_c + (1 - h) * t_mHow to choose
GATE questions signal the model in their wording.
| Wording in the question | Model to use |
|---|---|
| "first checks the cache, then memory", "hierarchical", "on a miss the memory is accessed" | `t_c + (1 - h) * t_m` |
| "simultaneously", "in parallel", "memory access starts at the same time" | `h * t_c + (1 - h) * t_m` |
| Wording gives no hint | Use hierarchical, the textbook default |
Worked example
A system has t_c = 2 ns, t_m = 100 ns and a hit ratio of 95%.
Hierarchical:
AMAT = 2 + 0.05 * 100 = 7 nsSimultaneous:
AMAT = 0.95 * 2 + 0.05 * 100 = 1.9 + 5 = 6.9 nsThe two models differ by (1 - h) * t_c, which here is 0.1 ns. The gap is small when the miss ratio is small, but a GATE answer key checks the exact number.
The 5 ns of miss cost is more than twice the 2 ns hit time, even though only 1 reference in 20 misses. A small miss ratio can therefore dominate AMAT, which is why improving the hit ratio is so valuable.
The same calculation in code
The following script computes both models. It takes the miss ratio as input so that no 1 - h rounding noise appears.
def amat(t_hit, miss_ratio, t_mem, parallel=False):
"""Average memory access time for a single cache level."""
if parallel:
return (1 - miss_ratio) * t_hit + miss_ratio * t_mem
return t_hit + miss_ratio * t_mem
print(round(amat(2, 0.05, 100), 3)) # 7.0
print(round(amat(2, 0.05, 100, parallel=True), 3)) # 6.9Miss penalty is a block transfer
On a miss, the cache loads an entire block, not one word. If the problem gives a block size and a transfer rate, compute the penalty first and then apply the formula.
Suppose a block is 8 words. Memory delivers the first word in 60 ns and each further word in 5 ns. The miss penalty is:
penalty = 60 + 7 * 5 = 95 nsWith t_c = 1 ns and a 4% miss ratio, AMAT is 1 + 0.04 * 95 = 4.8 ns.
Check which time the problem calls "memory access time". It may mean the time for one word or the time for a whole block. If a block fill is described, use the block fill time as the penalty.
Splitting the workload: instructions and data
Some designs use separate instruction and data caches. Each cache has its own miss ratio, so you compute an AMAT for each and weight them by how often each kind of reference occurs.
AMAT = f_i * AMAT_i + f_d * AMAT_dExample: 75% of references are instruction fetches and 25% are data accesses. The hit time is 1 ns and the miss penalty is 50 ns for both caches. The instruction cache misses 2% of the time and the data cache misses 8%.
AMAT_i = 1 + 0.02 * 50 = 2 ns
AMAT_d = 1 + 0.08 * 50 = 5 ns
AMAT = 0.75 * 2 + 0.25 * 5 = 2.75 nsThe same weighting appears when a question gives separate read and write times, or a separate hit ratio for reads and writes. Weight each case by its fraction of all references.
Working backwards
GATE often asks for the hit ratio needed to meet an AMAT target. Rearrange the hierarchical formula:
m = (AMAT - t_c) / t_mExample: t_c = 2 ns, t_m = 100 ns, and the target AMAT is at most 4 ns.
m <= (4 - 2) / 100 = 0.02
h >= 0.98You need a hit ratio of at least 98%. You can also compute the hit ratio from counts: h = hits / total references.
Extending to two levels
A second cache level is only a nested application of the same idea. The miss penalty of L1 is no longer the memory time. It is the average time to service the miss from L2 and below:
def amat_two_level(t_l1, m_l1, t_l2, m_l2, t_mem):
# m_l2 is the local miss ratio of L2
return t_l1 + m_l1 * (t_l2 + m_l2 * t_mem)
print(amat_two_level(1, 0.1, 10, 0.2, 100)) # 1 + 0.1 * (10 + 0.2 * 100) = 4.0Here m_l2 is a local miss ratio: the fraction of L2 lookups that miss. The chapter on multilevel caches covers local and global ratios in detail. For now, notice that the pattern hit time + miss ratio × penalty repeats at each level.
Common errors
- Using the hit ratio where the miss ratio belongs. The term multiplying the penalty is
(1 - h), noth. If your AMAT is nearly equal tot_m, check this first. - Mixing up the two models. Adding
t_con a miss when the problem says access is simultaneous gives an answer too high by(1 - h) * t_c. Omittingt_con a miss when the access is sequential gives one too low. - Mixing units. Convert everything to the same unit before you add. If times are given in cycles and the clock is in GHz, convert to ns only if the question asks for ns.
- Using the word time instead of the block time. If a miss loads a block of several words, the penalty is the whole block transfer.
- Treating AMAT as a latency bound. A single miss still takes the full penalty. AMAT only describes the long-run mean.
- Using a local ratio as a global one in multilevel questions. Check which one the question gives before multiplying.
Practice
Exercise
A cache has tc = 5 ns and main memory has tm = 80 ns. The hit ratio is 0.9. Find the AMAT for (a) hierarchical access and (b) simultaneous access.
Show answer
(a) 5 + 0.1 80 = 13 ns. (b) 0.9 5 + 0.1 * 80 = 4.5 + 8 = 12.5 ns.
Exercise
A cache has a hit time of 1 ns and a miss ratio of 4%. On a miss, a block of 8 words is loaded. Memory delivers the first word in 60 ns and each of the next words in 5 ns. Find the AMAT.
Show answer
The miss penalty is 60 + 7 5 = 95 ns. AMAT = 1 + 0.04 95 = 1 + 3.8 = 4.8 ns.
Exercise
With hierarchical access, tc = 1 ns and tm = 50 ns. What is the minimum hit ratio needed for the AMAT to be at most 2 ns?
Show answer
AMAT = 1 + m * 50 <= 2, so m <= 1/50 = 0.02. The hit ratio must be at least 0.98, or 98%.