EE6156 · PART 2 习题小程序
← 学习手册
已掌握 0 / 49

EE6156 Part 2 · Tutorial 7–12

先自己算,再核对,最后看完整推导

三种用法:Tutorial 精讲收录 Tutorial 7–12 全部 48 题和老师课上补充的 1 题,题目有英文原题和中文,每题可以先填关键数值让页面自动核对,再展开提示或完整解答;课堂练习是讲义里 Exercise 7.1–12.17,题目和解答都是中英双语(讲义学生版的答案页是空白的);随机刷题每次随机出一道同类型的新题,适合考前热手。右上角可以切换"中英 / EN / 中"。

数值答案允许 ±0.5% 或 ±0.01 的误差;百分数直接填数字(例如 37.5% 填 37.5)。勾选"已掌握"会保存在这个浏览器里。

筛选
TUTORIAL 7

缓存加速比与存储停顿

对应 Lesson 7。核心公式:AMAT = HR × HT + MR × (HT + MP);实际 CPI = 理想 CPI + 每条指令的停顿;每条指令访存 = 1 + load/store 比例。

T7.1缓存带来的加速比AMAT
EN

Suppose a cache is 10 times faster than main memory and has a hit rate of 90%. How much speedup is achieved by using the cache?

中缓存比主存快 10 倍,命中率 90%。使用缓存能获得多少加速比?
提示

设缓存访问时间 = 1,主存 = 10。按课程约定,一次缺失要先查缓存再去主存:缺失时间 = 1 + 10。

完整解答
无缓存:每次访问 = 10有缓存:AMAT = 0.9 × 1 + 0.1 × (1 + 10) = 0.9 + 1.1 = 2.0加速比 = 10 / 2.0 = 5
另一种口径:如果认为缺失时只花主存时间(缓存和主存并行查找),AMAT = 0.9 × 1 + 0.1 × 10 = 1.9,加速比 = 1 / (0.1 + 0.9/10) ≈ 5.26。讲义明确规定缺失时间 = 缓存访问时间 + 缺失代价,所以答 5。
T7.2要快多少倍才能加速 5 倍反推
EN

Determine how many times faster a cache memory must be compared to the main memory in order to achieve an overall speedup of 5. Assume that the cache hit rate is 0.85.

中命中率 0.85。缓存要比主存快多少倍,整体才能获得 5 倍加速?
提示

设缓存 = 1,主存 = k。写出 AMAT,令 k / AMAT = 5 解方程。

完整解答
AMAT = 0.85 × 1 + 0.15 × (1 + k) = 1 + 0.15k加速比 = k / (1 + 0.15k) = 5k = 5 + 0.75k → 0.25k = 5 → k = 20验算:AMAT = 1 + 3 = 4,20 / 4 = 5 ✓

缓存要比主存快 20 倍。注意上限:k → ∞ 时加速比趋于 1/0.15 ≈ 6.67,所以 5 倍是可以达到的。若按"缺失只花 k"的口径,1/(0.15 + 0.85/k) = 5 得 k = 17。

T7.3缺失让机器慢多少实际 CPI
EN

(a) Assume a machine has an ideal CPI of 2.0 when all instruction and data accesses hit in the cache. Loads and stores account for 40% of all instructions. Every instruction fetch and every data access has a cache miss rate of 2%. The miss penalty is 25 clock cycles. How much slower is the machine compared with the ideal all-hit machine?

(b) What would happen if there were no cache at all, assuming every memory access incurs the same 25-cycle penalty?

(c) If the ideal CPI is reduced to 1.0, but we want to maintain the same performance ratio as in part (a), what miss penalty is required, assuming the miss rate remains unchanged?

中理想 CPI 2.0(全部命中)。load/store 占 40%。取指和取数的缺失率都是 2%,缺失代价 25 周期。
  1. 比全命中的理想机器慢多少?
  2. 如果完全没有缓存,每次访存都要 25 周期,会怎样?
  3. 理想 CPI 降到 1.0,缺失率不变,要保持 (a) 的性能比,缺失代价应为多少?
提示

每条指令访存 1.4 次。(c) 中"性能比相同"指实际 CPI / 理想 CPI 仍为 1.35。

完整解答

(a)

每条指令访存 = 1 + 0.4 = 1.4停顿 = 1.4 × 0.02 × 25 = 0.7实际 CPI = 2.0 + 0.7 = 2.7比值 = 2.7 / 2.0 = 1.35,即慢 35%

(b)

停顿 = 1.4 × 25 = 35CPI = 2.0 + 35 = 37比理想慢 37 / 2 = 18.5 倍;比有缓存时慢 37 / 2.7 ≈ 13.7 倍

(c)

要求实际 CPI = 1.0 × 1.35 = 1.35 → 停顿 = 0.351.4 × 0.02 × MP = 0.35 → MP = 0.35 / 0.028 = 12.5 周期

理想 CPI 减半后,缺失代价也必须减半才能保持同样的相对损失:CPU 越快,缓存越要紧。

T7.4循环访存计时数访存次数
EN

Consider the following program fragment:

sum = 0; i = 0; while (sum < 15 && i < 8) { sum += a[i]; i++; }

The contents of the array a are: a[0] = 3, a[1] = 4, a[2] = 2, a[3] = 6, a[4] = 5, a[5] = 1, a[6] = 7, a[7] = 2. Determine:

  • the final value of sum;
  • the number of memory accesses;
  • the total access time without using a cache;
  • the total access time with a cache; and
  • the performance improvement factor due to using the cache.
中sum = 0; i = 0; while (sum < 15 && i < 8) { sum += a[i]; i++; },数组 a = [3, 4, 2, 6, 5, 1, 7, 2]。求最终 sum、访存次数、无缓存总时间、有缓存总时间、性能提升倍数。(按老师课上的解答:缓存访问 2 ms,主存访问 20 ms;sum、i 在寄存器;数组在同一块,首次访问缺失,之后全命中。)
提示

先模拟循环:每轮结束检查 sum < 15。sum 一旦等于 15 循环就停。

完整解答
轮读 a[i]sum条件 sum < 15 且 i < 8
1a[0] = 33真,继续
2a[1] = 47真
3a[2] = 29真
4a[3] = 61515 < 15 为假,退出
最终 sum = 15,访问 a[0]–a[3] 共 4 次无缓存:4 × 20 = 80 ms有缓存:a[0] 缺失 = 2 + 20 = 22 ms;a[1]–a[3] 命中 3 × 2 = 6 ms → 28 ms提升 = 80 / 28 ≈ 2.86 倍
注意:循环条件只用寄存器里的 sum 和 i,不产生访存。学生版题目没有给时间,2 ms / 20 ms 来自老师课上的解答;如果按讲义例子的 1 ms / 10 ms 算,是 40 ms 和 14 ms,倍数一样是 2.86。
T7.5分离缓存的有效 CPIns → 周期
EN

A 500 MHz processor has a main memory access time of 60 ns. Assume that the processor has an ideal Cycles per Instruction (CPI) of 2 when using a perfect cache (i.e., all instruction and data accesses hit in the L1 cache). Suppose that 25% of the program instructions are load or store instructions. If the instruction cache miss rate is 2% and the data cache miss rate is 5%, compute the effective CPI of the processor.

中500 MHz 处理器,主存访问 60 ns,理想 CPI 2,load/store 占 25%,指令缓存缺失率 2%,数据缓存缺失率 5%。求有效 CPI。
提示

500 MHz → 时钟周期 2 ns,先把 60 ns 换成周期数。

完整解答
时钟周期 = 1 / 500 MHz = 2 ns → MP = 60 / 2 = 30 周期指令停顿 = 1 × 0.02 × 30 = 0.6数据停顿 = 0.25 × 0.05 × 30 = 0.375有效 CPI = 2 + 0.6 + 0.375 = 2.975
T7.6去掉缓存省还是亏执行时间
EN

Suppose a cache memory is 5 times faster than the main memory. The cache hit time is 10 ns, while a cache miss requires 100 ns. A program takes 1 hour to execute, and 20% of the execution time is spent on memory and cache accesses. The cache system achieves an average miss rate of 0.1. Determine how much execution time would be saved or lost if the same program were executed without cache memory.

中缓存比主存快 5 倍。命中 10 ns,一次缺失 100 ns,缺失率 0.1。程序运行 1 小时,其中 20% 的时间花在访存(缓存)上。如果不用缓存,执行时间会省多少还是多多少?
提示

老师的策略:只有 20% 那 12 分钟受访存影响,48 分钟不变。先算有缓存和无缓存的 AMAT,再把 12 分钟按两者之比放大。主存 = 5 × 10 = 50 ns。

完整解答(老师课上的版本)
主存访问 = 5 × 10 ns = 50 ns缺失时间 MT = HT + MP = 10 + 50 = 60 ns有缓存 AMAT = 0.9 × 10 + 0.1 × 60 = 9 + 6 = 15 ns无缓存:每次都去主存,AMAT = 50 ns1 小时 = 访存 12 分钟 + 其他 48 分钟无缓存访存时间 = 12 × 50 / 15 = 40 分钟无缓存总时间 = 40 + 48 = 88 分钟

不用缓存会多花 28 分钟(88 分钟对 60 分钟)。老师特别说:没见过的题先想策略,这题的策略就是"只放大受访存影响的那部分时间"。

另一种读法:如果直接用题目给的"缺失 100 ns",AMAT = 0.9 × 10 + 0.1 × 100 = 19 ns,访存时间放大到 12 × 50/19 ≈ 31.6 分钟,多花约 19.6 分钟。老师用的是 MT = HT + MP = 60 ns,考试按老师的口径写 28 分钟。
T7.6+老师补充题:每条指令 1.2 次取指课上新增
EN

(Extra question shown in the lecturer's Tutorial 7 solutions.) Assume each instruction requires on average 1.2 instruction fetches, and the processor has an ideal CPI of 1.5. The instruction-cache miss rate is 2% with a miss penalty of 50 cycles. On average there are 0.3 data accesses per instruction, the data-cache miss rate is 5% and the data miss penalty is 80 cycles. Find the memory stall cycles per instruction and the actual CPI.

中平均每条指令取指 1.2 次,理想 CPI 1.5。指令缓存缺失率 2%,指令缺失代价 50 周期;平均每条指令访问数据 0.3 次,数据缓存缺失率 5%,数据缺失代价 80 周期。求每条指令的存储停顿周期和实际 CPI。
提示

和平常唯一的区别:每条指令取指不是 1 次,而是 1.2 次。指令和数据的缺失率、代价不同,分开算再相加。

完整解答
指令停顿 = 1.2 × 0.02 × 50 = 1.2数据停顿 = 0.3 × 0.05 × 80 = 1.2存储停顿 = 1.2 + 1.2 = 2.4 周期/条实际 CPI = 1.5 + 2.4 = 3.9

这题不在学生版 Tutorial 7 题单里,是老师第 8 周讲解答时多放的一题。老师的话:题目看着复杂,套的还是同一个式子。

T7.7由实际 CPI 反推缺失率解方程
EN

A processor has an ideal CPI of 1.5 when all instruction fetches and data accesses hit in the cache. The actual CPI is measured to be 2.7. Assume that:

  • 40% of the instructions are load/store instructions.
  • The instruction-cache miss penalty is 30 clock cycles.
  • The data-cache miss penalty is 60 clock cycles.
  • The data-cache miss rate is 3.3 times the instruction-cache miss rate.

Find the instruction-cache miss rate, the data-cache miss rate, and the total memory stall cycles per instruction.

中理想 CPI 1.5,实测 CPI 2.7。load/store 占 40%,指令缓存缺失代价 30 周期,数据缓存缺失代价 60 周期,数据缺失率是指令缺失率的 3.3 倍。求指令缺失率、数据缺失率、每条指令的总停顿。
提示

停顿 = 2.7 − 1.5。设指令缺失率为 m,写出停顿关于 m 的式子。

完整解答
总停顿 = 2.7 − 1.5 = 1.2 周期/条1.2 = m × 30 + 0.4 × (3.3m) × 60 = 30m + 79.2m = 109.2mm = 1.2 / 109.2 = 0.010989 → 指令缺失率 ≈ 1.10%数据缺失率 = 3.3 × 1.0989% ≈ 3.63%检验:指令停顿 0.330 + 数据停顿 0.870 = 1.2 ✓
T7.8统一缺失率的 CPI实际 CPI
EN

A processor has an ideal CPI of 1.8. The instruction mix consists of 60% arithmetic/logic instructions, 25% load/store instructions and 15% branch instructions. Assume:

  • Every instruction requires one instruction fetch.
  • Only load/store instructions access data memory.
  • The combined miss rate (for both instruction fetches and data accesses) is 3%.
  • The miss penalty is 40 clock cycles.

Determine the average number of memory references per instruction, the memory stall cycles per instruction, the actual CPI, and the percentage of execution time spent on memory stalls.

中理想 CPI 1.8。指令构成:60% 算术逻辑、25% load/store、15% 分支。每条指令取指一次,只有 load/store 访问数据。综合缺失率 3%,缺失代价 40 周期。求每条指令访存次数、每条指令停顿、实际 CPI、停顿占执行时间的百分比。
完整解答
访存次数 = 1 + 0.25 = 1.25(分支和算术不访问数据存储器)停顿 = 1.25 × 0.03 × 40 = 1.5实际 CPI = 1.8 + 1.5 = 3.3停顿占比 = 1.5 / 3.3 = 45.45%
TUTORIAL 8

AMAT、地址划分与映射手算

对应 Lesson 8。块数 = 容量 ÷ 块大小;组数 = 块数 ÷ 相联度;Offset = log₂块大小,Index = log₂组数,Tag = 地址位 − 两者。比较器个数 = 相联度,每个比较器宽 Tag 位。

T8.1缺失时间与 AMATAMAT
EN

A cache has hit time = 2 cycles, miss rate = 4% and miss penalty = 80 cycles. Determine the miss time and the Average Memory Access Time (AMAT).

中命中时间 2 周期,缺失率 4%,缺失代价 80 周期。求缺失时间和 AMAT。
完整解答
缺失时间 = HT + MP = 2 + 80 = 82 周期AMAT = HT + MR × MP = 2 + 0.04 × 80 = 5.2 周期验算:0.96 × 2 + 0.04 × 82 = 1.92 + 3.28 = 5.2 ✓
T8.2两种缓存的完整参数地址划分
EN

A computer has 4 GB of byte-addressable main memory and uses a 32-bit address bus. For each of the following cache organisations, determine:

  1. The number of cache blocks/lines
  2. The number of sets
  3. The number of address bits used for the block offset, index, and tag
  4. The total number of bits required to store all tag fields
  5. The number of XOR gates required for tag comparison
  6. Assume that 1 word = 4 bytes.

(i) A direct-mapped cache with a total data size of 512 KB, using 8-word blocks.
(ii) A 2-way set-associative cache with a total data size of 256 KB, using 2-word blocks.

中4 GB 按字节编址主存,32 位地址,1 字 = 4 字节。对下面两种缓存,分别求块数、组数、Offset/Index/Tag 位数、全部标签的总位数、标签比较需要的 XOR 门数。
  1. 直接映射,数据容量 512 KB,块 8 字。
  2. 2 路组相联,数据容量 256 KB,块 2 字。
提示

块大小先换成字节:8 字 = 32 B,2 字 = 8 B。XOR 门数 = 比较器个数 × 每个比较器的位数。

完整解答
(i) 直接映射 512 KB, 32 B 块(ii) 2 路 256 KB, 8 B 块
块数2¹⁹ / 2⁵ = 2¹⁴ = 163842¹⁸ / 2³ = 2¹⁵ = 32768
组数16384(每组 1 块)32768 / 2 = 16384
Offsetlog₂32 = 5log₂8 = 3
Indexlog₂16384 = 1414
Tag32 − 14 − 5 = 1332 − 14 − 3 = 15
标签总位数16384 × 13 = 212,992 位(26 KB)32768 × 15 = 491,520 位(60 KB)
XOR 门1 个 13 位比较器 = 132 个 15 位比较器 = 30

(f) 只是给出 1 字 = 4 字节的假设。每个比较器除了 XOR 还需要一个把所有 XOR 输出合并的或非(NOR)门树来判断"全为 0",题目只问 XOR 数。

T8.3有效 CPI实际 CPI
EN

Consider a program in which 20% of the instructions are load or store instructions. The processor has: main memory access time = 50 ns; processor clock rate = 500 MHz; base CPI with a perfect cache = 2.0; instruction-cache miss rate = 3%; data-cache miss rate = 10%. Assume that every instruction requires one instruction fetch, and only load/store instructions require a data-cache access. Also assume that the miss penalty is equal to the main memory access time. Find the effective CPI of the machine.

中20% 的指令是 load/store。主存访问 50 ns,时钟 500 MHz,理想 CPI 2.0,指令缓存缺失率 3%,数据缓存缺失率 10%,缺失代价等于主存访问时间。求有效 CPI。
完整解答
时钟 2 ns → MP = 50 / 2 = 25 周期指令停顿 = 0.03 × 25 = 0.75数据停顿 = 0.2 × 0.10 × 25 = 0.5有效 CPI = 2.0 + 0.75 + 0.5 = 3.25
T8.4正推与反推地址位地址划分
EN

Assume a computer with a 32-bit byte-addressable address space.

  1. For a 1 MB, 8-way set-associative cache with 128-byte blocks, determine the number of bits used for the tag, index, and block offset.
  2. Consider a direct-mapped cache with 32 cache blocks and a 21-bit tag. Determine the block size and the total cache capacity.
  3. A cache has a total data capacity of 256 bytes, a 28-bit tag, and 16-byte blocks. Determine the number of index bits and the degree of associativity.
中32 位按字节编址。
  1. 1 MB、8 路组相联、128 字节块:Tag、Index、Offset 各几位?
  2. 直接映射,32 块,Tag 21 位:块大小和总容量?
  3. 容量 256 字节,Tag 28 位,块 16 字节:Index 几位?相联度是多少?
完整解答
(a) 块数 = 2²⁰ / 2⁷ = 2¹³;组数 = 2¹³ / 8 = 2¹⁰  Offset 7,Index 10,Tag = 32 − 10 − 7 = 15(b) 32 块 → Index 5;Offset = 32 − 21 − 5 = 6 → 块 = 2⁶ = 64 B  容量 = 32 × 64 = 2 KB(c) Offset = log₂16 = 4;Index = 32 − 28 − 4 = 0 → 只有 1 组  块数 = 256 / 16 = 16,全在一组 → 16 路,即全相联
T8.54 路组相联全套参数地址划分
EN

Assume a computer with a 32-bit byte-addressable address space. A cache has capacity = 256 KB, block size = 64 bytes, 4-way set associativity. Determine:

  1. The number of cache blocks.
  2. The number of sets.
  3. The number of offset bits.
  4. The number of index bits.
  5. The number of tag bits.
  6. The total number of bits required to store all tags.
  7. The number of comparators required for tag comparison.
中32 位地址,缓存 256 KB,块 64 字节,4 路组相联。求块数、组数、Offset、Index、Tag、标签总位数、比较器个数。
完整解答
块数 = 2¹⁸ / 2⁶ = 4096;组数 = 4096 / 4 = 1024Offset = 6,Index = 10,Tag = 32 − 10 − 6 = 16标签总位数 = 4096 × 16 = 65,536 位(8 KB)比较器 = 4 个(每路一个),每个 16 位,共 64 个 XOR
T8.6提高命中率还是降低代价AMAT 比较
EN

A processor uses a cache memory with cache hit time = 2 ns, cache miss penalty = 80 ns and cache hit rate = 95%.

(a) Determine 1. the cache miss rate, 2. the cache miss time, 3. the AMAT. Use both of the following formulas and verify that they give the same answer: AMAT = HR × HT + MR × MT and AMAT = HT + MR × MP.

(b) A cache designer improves the cache and increases the hit rate from 95% to 98%. Assuming the hit time and miss penalty remain unchanged, determine the new AMAT.

(c) Another design reduces the miss penalty from 80 ns to 40 ns while keeping the original hit rate of 95%. Determine the new AMAT.

(d) Compare the results obtained in Parts (a), (b), and (c). Which improvement gives the larger reduction in AMAT?

(e) A second cache design has hit time = 1 ns, miss penalty = 120 ns, hit rate = 94%. Determine its AMAT. Which cache design would you choose based on AMAT? Explain your answer.

中命中时间 2 ns,缺失代价 80 ns,命中率 95%。
  1. 求缺失率、缺失时间、AMAT(用两个公式各算一次)。
  2. 命中率提高到 98%,新 AMAT?
  3. 缺失代价降到 40 ns(命中率 95%),新 AMAT?
  4. 哪个改进降得多?
  5. 另一设计:命中 1 ns、代价 120 ns、命中率 94%,AMAT?选哪个?
完整解答
(a) MR = 5%,MT = 2 + 80 = 82 ns  HR×HT + MR×MT = 0.95×2 + 0.05×82 = 1.9 + 4.1 = 6.0 ns  HT + MR×MP = 2 + 0.05×80 = 6.0 ns ✓(b) 2 + 0.02 × 80 = 3.6 ns(降 2.4 ns)(c) 2 + 0.05 × 40 = 4.0 ns(降 2.0 ns)(e) 1 + 0.06 × 120 = 8.2 ns

(d) 本题中提高命中率更有效:比较缺失项 MR × MP,(b) 为 0.02 × 80 = 1.6,(c) 为 0.05 × 40 = 2.0。(e) 第二种设计命中更快,但缺失率和代价都更高,缺失项 7.2 ns 远大于命中时间节省的 1 ns,AMAT 8.2 ns 比原设计的 6.0 ns 差,应选原设计(若可以做 (b) 的改进则更好)。

T8.7比较器与位比较数硬件代价
EN

Consider a cache with tag size = 6 bits and number of cache lines = 16. Determine the number of comparators required for a direct-mapped cache, the total number of parallel bit-comparisons required for a direct-mapped cache, the number of comparators required for a fully-associative cache, and the total number of parallel bit-comparisons required for a fully-associative cache.

中Tag 6 位,16 行。分别求直接映射和全相联需要的比较器个数和并行位比较总数。
完整解答
直接映射:Index 直接选中 1 行 → 1 个比较器 × 6 位 = 6 次位比较全相联:所有行同时比 → 16 个比较器 × 6 位 = 96 次位比较
T8.8三种组织逐次手算LRU · 3C
EN

A computer system has main memory size 256 bytes, cache size 8 cache lines, block size 4 bytes per block, Least Recently Used (LRU) replacement (where applicable), and the cache is initially empty. The following sequence of memory references is generated: 20, 37, 54, 20, 70, 38. Repeat the following for each cache organization: (a) fully-associative; (b) direct-mapped; (c) 2-way set-associative.

(i) Determine the number of bits required for the memory address, tag, index (or set index) and offset, where applicable.

(ii) For each memory reference: determine the corresponding memory block; determine the cache set (or cache line) to which the block maps; state whether the access is a hit or a miss; if it is a miss, classify it as either a compulsory (cold) miss or a conflict miss; show the contents of the cache after the access, including the LRU information for each line (where applicable).

(iv) Using the same sequence, determine the total number of cache hits, cache misses, compulsory (cold) misses, conflict misses, and the cache hit rate.

(v) Compare the results obtained for the three cache organizations and comment on the effect of cache associativity on cache performance.

中主存 256 字节,缓存 8 行,块 4 字节,LRU,初始为空。访问序列:20, 37, 54, 20, 70, 38。分别对 (a) 全相联、(b) 直接映射、(c) 2 路组相联:求各字段位数;逐次写出块号、组/行、命中或缺失(缺失要分 compulsory / conflict)及缓存内容;统计命中、缺失、强制、冲突次数和命中率;最后比较三者。
提示

块号 = 地址 ÷ 4:20→5,37→9,54→13,70→17,38→9。直接映射行号 = 块号 mod 8,2 路组号 = 块号 mod 4。可以在学习手册的缓存模拟器里选这三个预设逐步对照。

完整解答

(i) 字段位数(地址 log₂256 = 8 位,Offset 2 位)

组织TagIndexOffset
全相联602
直接映射(8 行)332
2 路(4 组)422

(a) 全相联

#地址Tag Index Offset块组/行Tag结果被替换访问后缓存(括号内为 TSLR)
120000101 0020–23–000101MISS compulsory20–23(0)
237001001 0136–39–001001MISS compulsory20–23(1), 36–39(0)
354001101 1052–55–001101MISS compulsory20–23(2), 36–39(1), 52–55(0)
420000101 0020–23–000101HIT20–23(0), 36–39(2), 52–55(1)
570010001 1068–71–010001MISS compulsory20–23(1), 36–39(3), 52–55(2), 68–71(0)
638001001 1036–39–001001HIT20–23(2), 36–39(0), 52–55(3), 68–71(1)

命中 2、缺失 4(全部强制)、冲突 0,命中率 2/6 = 33.3%。

(b) 直接映射

#地址Tag Index Offset块组/行Tag结果被替换访问后缓存
120000 101 0020–235000MISS compulsoryL5: 20–23
237001 001 0136–391001MISS compulsoryL1: 36–39
L5: 20–23
354001 101 1052–555001MISS compulsory20–23L1: 36–39
L5: 52–55
420000 101 0020–235000MISS conflict52–55L1: 36–39
L5: 20–23
570010 001 1068–711010MISS compulsory36–39L1: 68–71
L5: 20–23
638001 001 1036–391001MISS conflict68–71L1: 36–39
L5: 20–23

块 5 与块 13 都映射到行 101,块 9 与块 17 都映射到行 001,互相踢出。命中 0、缺失 6(强制 4、冲突 2),命中率 0%。最终行 001 = 36–39(tag 001),行 101 = 20–23(tag 000),其余行无效。

(c) 2 路组相联

#地址Tag Index Offset块组/行Tag结果被替换访问后缓存(括号内为 TSLR)
1200001 01 0020–2310001MISS compulsoryS1: 20–23(0)
2370010 01 0136–3910010MISS compulsoryS1: 20–23(1), 36–39(0)
3540011 01 1052–5510011MISS compulsory20–23S1: 52–55(0), 36–39(1)
4200001 01 0020–2310001MISS conflict36–39S1: 52–55(1), 20–23(0)
5700100 01 1068–7110100MISS compulsory52–55S1: 68–71(0), 20–23(1)
6380010 01 1036–3910010MISS conflict20–23S1: 68–71(1), 36–39(0)

四个块号 5、9、13、17 除以 4 余数都是 1,全部落进组 1,而组 1 只有 2 路,三个以上的块轮流被 LRU 踢出。命中 0、缺失 6(强制 4、冲突 2),命中率 0%。

(v) 比较

全相联命中率最高(33.3%),直接映射和这里的 2 路都是 0%。一般来说提高相联度能减少冲突缺失,但前提是争同一组的块数不超过路数;本题四个块全挤在 2 路组相联的同一组里,2 路帮不上忙。相联度越高,比较器和 LRU 硬件越多。

分类口径:上表按定义把每个块的第一次访问都记为 compulsory,即使它引起了替换(例如 (b) 第 3、5 步)。讲义个别例题把"首次访问且发生替换"标成 conflict 或 capacity,如果按那种写法,(b)(c) 会变成强制 2、冲突 4。建议按定义答,必要时加一句 "block replaced"。
TUTORIAL 9

缓存 II:写策略、多级缓存与虚拟内存

对应 Lesson 9。CPU 时间 = IC × (CPI × 时钟周期),比较缓存时别只看 AMAT;多级缓存:AMAT = HT₁ + MR₁ × (HT₂ + MR₂ × MP₂);物理地址 = PPN 拼接页内偏移。

T9.1十六进制地址的直接映射手算轨迹
EN

Suppose a computer has a 12-bit byte-addressable address space and a direct-mapped cache with 16 cache blocks. Each cache block can store 16 bytes of data. A program generates the following sequence of memory accesses, given in hexadecimal: 3FE, 4FE, 4FD, 4F2, 111, 111, 2F1, 4FE, 2F7, 448, 2F9, 2A2.

  1. How many bits are used for the cache tag field?
  2. Assuming the cache is initially empty, determine the final contents of the cache after the program runs. Draw a diagram of the cache showing all fields, but fill in only the tag fields.
  3. Suppose a cache hit takes 5 ns, a cache miss takes 100 ns, and a memory access without a cache takes 95 ns. How much time is saved by using this cache for the given program, compared with an equivalent machine without a cache?
  4. Give one example of spatial locality and one example of temporal locality exhibited by this program.
中12 位按字节编址,直接映射缓存 16 块,每块 16 字节。访问序列(十六进制):3FE, 4FE, 4FD, 4F2, 111, 111, 2F1, 4FE, 2F7, 448, 2F9, 2A2。
  1. Tag 几位?
  2. 初始为空,求最终缓存内容(只填 Tag)。
  3. 命中 5 ns、缺失 100 ns、无缓存每次 95 ns,用缓存省多少时间?
  4. 各举一个空间局部性和时间局部性的例子。
提示

Offset = log₂16 = 4,Index = log₂16 = 4,Tag = 12 − 8 = 4。正好每个十六进制位是一个字段:第 1 位是 Tag,第 2 位是行号,第 3 位是偏移。

完整解答

(a) Offset 4 + Index 4,Tag = 12 − 4 − 4 = 4 位。以 3FE 为例:Tag = 3,行 = F,偏移 = E。

#地址Tag Index Offset块组/行Tag结果被替换访问后缓存
13FE0011 1111 11101008–1023150011MISS compulsoryL15: 1008–1023
24FE0100 1111 11101264–1279150100MISS compulsory1008–1023L15: 1264–1279
34FD0100 1111 11011264–1279150100HITL15: 1264–1279
44F20100 1111 00101264–1279150100HITL15: 1264–1279
51110001 0001 0001272–28710001MISS compulsoryL1: 272–287
L15: 1264–1279
61110001 0001 0001272–28710001HITL1: 272–287
L15: 1264–1279
72F10010 1111 0001752–767150010MISS compulsory1264–1279L1: 272–287
L15: 752–767
84FE0100 1111 11101264–1279150100MISS conflict752–767L1: 272–287
L15: 1264–1279
92F70010 1111 0111752–767150010MISS conflict1264–1279L1: 272–287
L15: 752–767
104480100 0100 10001088–110340100MISS compulsoryL1: 272–287
L4: 1088–1103
L15: 752–767
112F90010 1111 1001752–767150010HITL1: 272–287
L4: 1088–1103
L15: 752–767
122A20010 1010 0010672–687100010MISS compulsoryL1: 272–287
L4: 1088–1103
L10: 672–687
L15: 752–767

(b) 最终只有 4 行有效:行 1:Tag 1(块 110–11F);行 4:Tag 4(440–44F);行 A:Tag 2(2A0–2AF);行 F:Tag 2(2F0–2FF)。其余 12 行有效位为 0。画图时每行画出 Valid | Tag | Data 三栏,只填 Tag。

(c) 命中 4 次(4FD、4F2、第二次 111、2F9),缺失 8 次  有缓存 = 4 × 5 + 8 × 100 = 20 + 800 = 820 ns  无缓存 = 12 × 95 = 1140 ns  节省 = 1140 − 820 = 320 ns

(d) 空间局部性:4FE 之后访问 4FD、4F2,都在同一块 4F0–4FF 里,第一次缺失把整块搬进来,后两次就命中(2F1、2F7、2F9 同理)。时间局部性:111 连续被访问两次;4FE 在第 2 次和第 8 次都被访问。

T9.2直接映射 vs 2 路:AMAT 和 CPU 时间陷阱题
EN

Assume that a CPU has a base CPI of 2.0 with a perfect cache and a clock cycle time of 2 ns. The program has an average of 1.3 memory references per instruction. Two cache organizations are considered:

  1. Direct-mapped 64-KB cache: miss rate = 1.4%, clock cycle time = 2 ns.
  2. 2-way set-associative 64-KB cache: miss rate = 1.0%, clock cycle time increases by 10% due to the selection multiplexer.

Assume cache hit time = 1 clock cycle and cache miss penalty = 70 ns for both organizations. Calculate (a) the AMAT for each cache organization; (b) the CPU time for each cache organization in terms of instruction count, IC; (c) which cache organization gives better overall CPU performance? Explain briefly.

中理想 CPI 2.0,时钟 2 ns,每条指令平均 1.3 次访存。两种 64 KB 缓存:
  1. 直接映射:缺失率 1.4%,时钟 2 ns。
  2. 2 路组相联:缺失率 1.0%,因选择 MUX 时钟变长 10%。
命中时间 1 个周期,缺失代价都是 70 ns。求 (a) 两者 AMAT;(b) 以 IC 表示的 CPU 时间;(c) 哪个整体更好?
提示

2 路的时钟 = 2.2 ns,命中时间也是一个时钟 = 2.2 ns。CPU 时间 = IC × (理想 CPI × 时钟 + 访存次数 × 缺失率 × 缺失代价 ns)。

完整解答
(a) AMAT_DM = 2 + 0.014 × 70 = 2 + 0.98 = 2.98 ns  AMAT_2way = 2.2 + 0.010 × 70 = 2.2 + 0.7 = 2.90 ns(b) CPU_DM = IC × (2.0 × 2 + 1.3 × 0.014 × 70) = IC × (4.0 + 1.274) = 5.274 × IC ns  CPU_2way = IC × (2.0 × 2.2 + 1.3 × 0.010 × 70) = IC × (4.4 + 0.91) = 5.31 × IC ns

(c) 直接映射更好。2 路的 AMAT 更低,但它把时钟周期拉长了 10%,而时钟影响的是每条指令的所有周期(理想 CPI 那 2 个周期每条都变慢了 0.4 ns),访存上省下的 0.364 ns 抵不过。教训:AMAT 只衡量访存,比较整体性能要算 CPU 时间。

如果把 2 路的缺失代价换算成整数周期(70/2.2 = 31.8 → 32 周期 = 70.4 ns),CPU 时间是 5.315 × IC,结论不变。

T9.3两级数据缓存多级 AMAT老师说 L2 公式不考
EN

A processor has two levels of data cache: L1 — block size 8 bytes, hit time 1 cycle, local miss rate 5%; L2 — block size 32 bytes, hit time 19 cycles, local miss rate 2%. Assume that it takes 50 clock cycles to request and complete a 32-byte transfer between main memory and the L2 cache.

(a) Find the AMAT for the L2 cache. (b) Find the AMAT for the L1 cache. (c) Assume the ideal CPI is 2.0 and 25% of the instructions are loads or stores. Find 1. the average memory stall cycles per instruction; 2. the overall CPI.

中两级数据缓存:L1 块 8 B、命中 1 周期、局部缺失率 5%;L2 块 32 B、命中 19 周期、局部缺失率 2%。主存与 L2 之间传一个 32 字节块要 50 周期。
  1. L2 的 AMAT;
  2. L1 的 AMAT;
  3. 理想 CPI 2.0,load/store 占 25%:每条指令平均停顿和总 CPI。
完整解答
(a) AMAT_L2 = HT₂ + MR₂ × MP₂ = 19 + 0.02 × 50 = 20 周期(b) L1 的缺失代价就是 AMAT_L2  AMAT_L1 = 1 + 0.05 × 20 = 2 周期(c) 只有 load/store 访问这两级数据缓存,每条指令 0.25 次  停顿 = 0.25 × (AMAT_L1 − HT₁) = 0.25 × 0.05 × 20 = 0.25 周期  CPI = 2.0 + 0.25 = 2.25
注意:理想 CPI 已经包含命中时间(全部命中的情况),停顿只算缺失那部分,所以用 AMAT − HT₁ = 1 而不是 AMAT = 2。如果写成 0.25 × 2 = 0.5、CPI = 2.5,就把命中时间重复算了。题目说的是数据缓存,指令取指不计入。
T9.4写直达 vs 写回逐步追踪写策略
EN

Consider a cache that initially contains memory block 100–103 with cache contents A, B, C, D. The CPU performs: 1. Store X to address 101; 2. Store Y to address 102; 3. Store Z to address 101.

  1. Using a write-through cache, show the contents of the cache and main memory after each store operation.
  2. Using a write-back cache, show the contents of the cache and main memory after each store operation.
  3. For the write-back cache, indicate the value of the dirty bit after each operation.
  4. Assume the cache block is later evicted. Show the final contents of main memory.
  5. State two advantages of write-back caches over write-through caches.
  6. State one advantage of write-through caches over write-back caches.
中缓存里有块 100–103 = A, B, C, D(主存相同)。依次执行:Store X → 101;Store Y → 102;Store Z → 101。
  1. 写直达:每步后缓存和主存的内容;
  2. 写回:每步后缓存和主存的内容;
  3. 写回每步后的脏位;
  4. 块被替换后主存最终内容;
  5. 写回相对写直达的两个优点;
  6. 写直达相对写回的一个优点。
完整解答
步骤写直达:缓存写直达:主存写回:缓存写回:主存脏位
初始A B C DA B C DA B C DA B C D0
Store X → 101A X C DA X C DA X C DA B C D1
Store Y → 102A X Y DA X Y DA X Y DA B C D1
Store Z → 101A Z Y DA Z Y DA Z Y DA B C D1
块被替换主存已是最新,直接丢弃:A Z Y D脏位为 1,整块写回:主存变为 A Z Y D

(d) 两种策略最终主存都是 A Z Y D,但写直达写了 3 次主存,写回只在替换时写 1 次。

(e) 写回的优点:① 对同一块的多次写合并成一次写回,内存流量和总线带宽占用小(这里 3 次变 1 次,X 根本没写到主存);② 写操作以缓存速度完成,不用每次等主存,性能更高、功耗更低。

(f) 写直达的优点:主存永远是最新的,一致性简单(例如 DMA 等 I/O 直接读主存不会读到旧值);不需要脏位,替换块时也不用先写回,控制更简单。

T9.5三种组织的位数与存储开销地址划分
EN

A computer uses a 32-bit address space and a 64 KB cache with 64-byte blocks. Answer for each organization: 1. direct-mapped; 2. 4-way set-associative; 3. fully associative.

(a) Determine the number of cache blocks, cache sets, tag bits, index bits and offset bits. (b) How many tag comparators are required for each organization during a cache lookup? (c) If each cache block requires 1 valid bit and tag storage only (ignore data storage), calculate the total number of bits required to store all tags and valid bits for each organization. (d) Which organization is expected to have 1. the lowest conflict miss rate, 2. the lowest hardware complexity, 3. the largest tag storage overhead? Explain briefly.

中32 位地址,64 KB 缓存,块 64 字节。对直接映射、4 路组相联、全相联分别求:块数、组数、Tag/Index/Offset 位数、比较器个数、每块 1 个有效位加标签时的总存储位数;并说明哪种冲突缺失最少、硬件最简单、标签开销最大。
完整解答
直接映射4 路组相联全相联
块数102410241024
组数10242561
Offset / Index / Tag6 / 10 / 166 / 8 / 186 / 0 / 26
比较器141024
(Tag + 有效位) × 102417 × 1024 = 17,40819 × 1024 = 19,45627 × 1024 = 27,648

(d) ① 冲突缺失最少:全相联,块可以放任意位置,根本没有冲突缺失。② 硬件最简单:直接映射,1 个比较器,不需要替换逻辑。③ 标签开销最大:全相联,没有 Index 位,每个标签 26 位最长。

T9.6块大小与空间局部性块大小
EN

A program accesses the byte addresses 8, 12, 16, 20, 24, 28, 32, 36. Assume the cache is initially empty.

(a) Suppose the cache contains only one cache block and uses a block size of 8 bytes. For each access, indicate hit or miss, and determine the overall miss rate. (b) Repeat (a) with a block size of 16 bytes. (c) Repeat (a) with a block size of 32 bytes. (d) Compare the miss rates in (a)–(c). Explain how increasing block size exploits spatial locality. (e) Although larger blocks may reduce compulsory misses, explain why excessively large blocks can increase the overall miss rate in a fixed-size cache. (f) A designer is considering increasing the block size from 64 B to 256 B while keeping the cache size fixed. State one advantage and two disadvantages of the larger block size.

中访问字节地址 8, 12, 16, 20, 24, 28, 32, 36,缓存只有一个块,初始为空。分别用 8 B、16 B、32 B 的块求每次命中/缺失和缺失率;比较并解释;说明为什么固定容量下块过大反而会提高缺失率;块从 64 B 增大到 256 B(容量不变)的一个优点和两个缺点。
完整解答
地址812162024283236缺失率
8 B 块M
8–15
HM
16–23
HM
24–31
HM
32–39
H4/8 = 50%
16 B 块M
0–15
HM
16–31
HHHM
32–47
H3/8 = 37.5%
32 B 块M
0–31
HHHHHM
32–63
H2/8 = 25%

(d) 块越大,一次缺失搬进来的相邻字节越多;顺序访问马上用到它们,于是缺失率从 50% 降到 25%。这就是利用空间局部性。

(e) 容量固定时,块越大块数越少。程序同时活跃的不同区域放不下,块之间互相踢出,冲突和容量缺失增加;而且大块里很多字节可能根本不会被用到,却占着空间(也削弱了时间局部性)。所以缺失率随块大小先降后升。

(f) 64 B → 256 B。优点:更好地利用空间局部性,减少强制缺失(顺带减少标签存储)。缺点:① 缺失代价增大,每次缺失要传 4 倍的数据;② 块数减为 1/4,冲突/容量缺失增加,还可能搬进大量用不到的数据,浪费带宽。

T9.7虚拟地址翻译页表虚拟内存不考
EN

A computer system uses 32-bit virtual addresses, 30-bit physical addresses and a page size of 4 KB. Page table entries: VPN 0x12345 → PPN 0x1A234 (valid 1); VPN 0x12346 → PPN 0x0B567 (valid 1); VPN 0xABCDE → PPN 0x1F890 (valid 1); VPN 0x22222 → — (valid 0).

(a) Determine the number of bits used for 1. the page offset, 2. the VPN, 3. the PPN. (b) Translate the virtual addresses 0x12345678, 0x12346ABC and 0xABCDEFFF into physical addresses. Show all working. (c) What happens when the CPU attempts to access virtual address 0x22222000? Explain the role of the valid bit. (d) Explain why the page offset remains unchanged during address translation. (e) State two advantages of virtual memory. (f) A page fault requires 5 ms to service, whereas a TLB miss requires only 50 ns. Calculate how many times more expensive a page fault is than a TLB miss.

中32 位虚拟地址、30 位物理地址、4 KB 页。页表:VPN 0x12345 → PPN 0x1A234(有效);0x12346 → 0x0B567(有效);0xABCDE → 0x1F890(有效);0x22222 → —(无效)。
  1. 页内偏移、VPN、PPN 各几位?
  2. 翻译 0x12345678、0x12346ABC、0xABCDEFFF。
  3. 访问 0x22222000 会怎样?有效位起什么作用?
  4. 为什么偏移翻译后不变?
  5. 虚拟内存的两个优点。
  6. 页缺失 5 ms、TLB 缺失 50 ns,前者是后者的多少倍?
完整解答
(a) 偏移 = log₂4096 = 12;VPN = 32 − 12 = 20;PPN = 30 − 12 = 18(b) 0x12345678 → VPN 0x12345,偏移 0x678 → PPN 0x1A234 → 0x1A234678  0x12346ABC → VPN 0x12346,偏移 0xABC → PPN 0x0B567 → 0x0B567ABC  0xABCDEFFF → VPN 0xABCDE,偏移 0xFFF → PPN 0x1F890 → 0x1F890FFF(f) 5 ms / 50 ns = 5×10⁻³ / 5×10⁻⁸ = 100,000 倍

4 KB 页的偏移正好是 12 位 = 3 个十六进制位,所以直接把最后 3 位抄下来,前面换成 PPN 即可。

(c) VPN 0x22222 的有效位 = 0,页不在物理内存里,发生页缺失(page fault):硬件触发异常,操作系统从磁盘把这一页调入某个物理页框,填写 PPN、把有效位置 1,然后重新执行这条访存指令。有效位标记"这个页表项的翻译是否可用",防止使用无效的 PPN。

(d) 页和物理页框大小相同且按页对齐,翻译只决定"哪一页",字节在页内的位置不变,所以低 12 位原样保留。

(e) 任选两个:能运行比物理内存还大的程序;每个进程有独立地址空间,相互隔离、受保护,又能有控制地共享;程序可以装到内存任意位置(重定位方便);避免物理内存碎片问题。

T9.8判断缺失类型3C
EN

A computer system uses a cache. The following cache misses are observed: 1. the program accesses memory block A for the first time and incurs a miss; 2. memory blocks B and C repeatedly replace each other in the cache because they map to the same cache location; 3. a program repeatedly accesses a working set of 2 MB, but the cache size is only 64 KB. For each scenario, (a) state whether the miss is compulsory, conflict or capacity; (b) suggest one method to reduce the miss rate.

中判断缺失类型并各给一个降低缺失率的方法:1) 第一次访问块 A 缺失;2) 块 B、C 映射到同一位置反复互相替换;3) 程序反复访问 2 MB 的工作集,缓存只有 64 KB。
完整解答
情形类型降低方法
1 首次访问 A强制缺失预取;适当增大块(一次带进相邻数据)
2 B、C 互踢冲突缺失提高相联度(如 2 路、4 路);或加大缓存
3 2 MB 工作集 vs 64 KB容量缺失加大缓存或增加一级更大的缓存(L2/L3);也可以改程序提高局部性(分块)
TUTORIAL 10

流水线时间、加速比与 DLX 指令

对应 Lesson 10。任务流水:T = ΣTᵢ + (n − 1)·maxTᵢ;时钟流水:T = (k + n − 1)·T_clk;大量指令时加速比 = 单周期时间 / 流水时钟周期;完全平衡时上限 = 级数。

T10.1m + n − 1 与最少任务数推导
EN

(i) Consider a task that can be divided into m equal-duration subtasks, each requiring one clock cycle. The task is executed using an m-stage pipeline, where each stage performs one subtask. Derive an expression for the total number of clock cycles required for the pipeline to complete n tasks. Hence, show that Tpipeline = m + n − 1 clock cycles.

(ii) Figure 1 shows a sequential machine and its pipelined counterpart. Each widget must pass through three processing steps A, B and C, each requiring one clock cycle. The sequential machine completes the three steps one after another; the pipelined machine has three stages (A, B, C), each requiring one clock cycle. Determine the minimum number of widgets that must be processed for the pipelined machine to achieve a speedup of at least 2.5 over the sequential machine. Justify your answer.

中(i) 任务分成 m 个各需 1 周期的子任务,用 m 级流水线完成 n 个任务,证明总周期数为 m + n − 1。
(ii) 顺序机器依次做 A、B、C 三步(各 1 周期);流水机器有 A、B、C 三级。至少处理多少个工件,流水机器才能比顺序机器快 2.5 倍以上?
完整解答

(i) 第 1 个任务要走完 m 级,第 m 个周期结束时完成(填充流水线)。之后流水线已满,每个周期都有一个任务从最后一级出来,剩下 n − 1 个任务再用 n − 1 个周期。总计 m + (n − 1) = m + n − 1 个周期。

(ii) 顺序:3n 周期;流水:3 + n − 1 = n + 2 周期  3n / (n + 2) ≥ 2.5 → 3n ≥ 2.5n + 5 → n ≥ 10  n = 10:30 / 12 = 2.5 ✓;n = 9:27 / 11 ≈ 2.45 ✗

至少 10 个工件。n 越大加速比越接近 3(级数),但永远到不了 3,因为有 2 个周期的填充开销。

T10.2停顿与级数对加速比的影响CPI × 时钟
EN

A processor can be implemented either as a single-stage (non-pipelined) processor with a clock cycle time of 5 ns, or as a 5-stage pipelined processor with a clock cycle time of 1 ns. Both execute the same instruction set, and the program is long enough that pipeline filling and draining effects may be ignored.

(a) Assuming no pipeline stalls, determine the speedup of the 5-stage pipelined processor relative to the single-stage processor. (b) Suppose the pipelined processor incurs a one-cycle stall for 40% of all instructions: i. determine its average CPI; ii. determine its speedup relative to the single-stage processor. (c) Now consider an alternative 4-stage pipelined processor with a clock cycle time of 1.1 ns and no stalls: i. determine the execution time per instruction for both pipelined processors; ii. determine which implementation delivers better performance and explain why.

中单级(非流水)处理器时钟 5 ns;5 级流水线时钟 1 ns。指令足够多,忽略填充排空。(a) 无停顿时的加速比;(b) 40% 的指令停顿 1 周期时的平均 CPI 和加速比;(c) 另有 4 级流水线时钟 1.1 ns、无停顿:两者每条指令执行时间,哪个更好?
完整解答
(a) 非流水每条 5 ns,流水每条 1 ns → 加速比 = 5(b) CPI = 1 + 0.4 × 1 = 1.4 → 每条 1.4 ns → 加速比 = 5 / 1.4 = 3.57(c) 5 级:1 × 1 ns = 1.0 ns;4 级:1 × 1.1 ns = 1.1 ns

(c) 5 级更好,快 10%。两者 CPI 都是 1,执行时间只由时钟周期决定,5 级把工作切得更细,时钟更短。

T10.3改 ALU 延迟对加速比的影响瓶颈
EN

Component delays: instruction fetch 200 ps, register read 100 ps, ALU operation 200 ps, data memory access 200 ps, register write 100 ps. The processor can be single-cycle or a 5-stage pipeline (one stage per component), with no stalls or hazards; the pipelined clock is set by the slowest stage, and the LW instruction (the longest path) sets the single-cycle clock.

(a) Suppose the ALU delay is reduced by 25%: i. the new ALU delay; ii. the LW execution time in the single-cycle processor; iii. the pipelined clock cycle time; iv. the speedup achieved by pipelining, compared with the original speedup. (b) Repeat with the ALU delay increased by 25%.

中各部件延迟:取指 200 ps,读寄存器 100,ALU 200,访存 200,写寄存器 100。单周期处理器的时钟由 LW(最长路径,五部件都用)决定;流水时钟由最慢一级决定,无停顿。(a) ALU 延迟减少 25%;(b) ALU 延迟增加 25%。分别求新 ALU 延迟、LW 单周期时间、流水时钟、加速比,并与原加速比比较。
完整解答
ALULW 单周期流水时钟加速比
原设计200800 ps200 ps4
(a) −25%150750 ps200 ps3.75
(b) +25%250850 ps250 ps3.4

(a) ALU 不是瓶颈,缩短它对流水线没用(时钟仍是 200 ps,由取指和访存决定),但单周期机器变快了,所以相对加速比从 4 降到 3.75。(b) ALU 变成唯一的瓶颈,流水时钟升到 250 ps(流水线慢了 25%),单周期只慢了 6.25%,加速比降到 3.4。结论:流水线的性能由最慢一级决定,优化要针对瓶颈级,各级越均衡越好。

T10.4图像处理流水线ΣT + (n−1)·max
EN

A processor performs a simplified image-processing task. Each image block passes through four stages: load image block from memory 10 ns; apply filter operation 20 ns; compute feature value 15 ns; store result to memory 5 ns. A pipelined implementation is used, and each stage can process only one image block at a time.

(a) Determine the pipeline cycle time. (b) How long does it take to complete the first image block? (c) Determine the total time required to complete 8 image blocks. (d) If the blocks were processed sequentially, determine the total time for 8 blocks. (e) Determine the speedup achieved by pipelining. (f) Explain why the speedup is less than the number of stages.

中四级:读块 10 ns、滤波 20 ns、算特征 15 ns、写回 5 ns,每级一次只处理一个块。求 (a) 流水周期;(b) 第一个块完成时间;(c) 8 个块总时间;(d) 顺序处理 8 块的时间;(e) 加速比;(f) 为什么加速比小于级数?
完整解答
(a) 周期 = 最慢一级 = 20 ns(b) 第一块 = 10 + 20 + 15 + 5 = 50 ns(c) 50 + (8 − 1) × 20 = 190 ns(d) 8 × 50 = 400 ns(e) 400 / 190 ≈ 2.11

(f) ① 各级不均衡:最慢一级 20 ns,平均只有 12.5 ns,其他级大部分时间在等滤波级;② 填充流水线的开销(第一块要 50 ns),块数只有 8,这部分占比不小。即使 n 很大,加速比也只能趋近 50/20 = 2.5,而不是 4。

另一种口径:如果把它当作时钟流水线(每级都拉长到 20 ns),第一块 = 4 × 20 = 80 ns,8 块 = (4 + 7) × 20 = 220 ns,加速比 400/220 ≈ 1.82。题目没有提时钟和流水线寄存器,按讲义洗衣房公式答 190 ns;如果老师按处理器的口径讲,可以把两种都写出来并说明假设。
T10.5三级批处理流水线吞吐
EN

A simple pipelined system consists of three stages: X read input 10 s; Y process input 25 s; Z write output 15 s. Assume that a task must pass through X, Y and Z in sequence; once a stage finishes a task it can immediately begin the next; different stages can work on different tasks simultaneously; there are no resource conflicts or dependencies; the pipeline cycle time is determined by the slowest stage; pipeline register overhead is negligible; a batch job contains 1000 independent tasks.

(The tutorial slide does not list the quantities to compute; the solution works out the standard set.)

中X 读输入 10 s → Y 处理 25 s → Z 写输出 15 s。一级做完马上接下一个任务,各级可同时处理不同任务,无冲突,周期由最慢一级决定,忽略寄存器开销,一批 1000 个独立任务。(原题没有列出要求的量,下面算全套常考指标。)
完整解答
流水周期 = max(10, 25, 15) = 25 s,瓶颈是 Y单个任务延迟 = 10 + 25 + 15 = 50 s总时间 = 50 + 999 × 25 = 25,025 s(约 6.95 小时)顺序 = 1000 × 50 = 50,000 s加速比 = 50,000 / 25,025 ≈ 2.0稳态吞吐 = 1 个 / 25 s = 144 个 / 小时

三级流水理想加速比是 3,这里只有 2,原因是 Y 太慢(25 s,而平均 16.7 s)。要提速就得把 Y 拆开或加速 Y,见 T10.6。

T10.6怎么分配优化预算平衡各级
EN

A simple pipelined system consists of four stages: A read input 22 s; B decode input 38 s; C process input 34 s; D write output 30 s. A batch job contains 1000 independent tasks. The system operates as an ideal pipeline, and the cycle time is determined by the slowest stage. An engineer is allowed to reduce the total execution time across all stages by 24 seconds; the reduction may be distributed across one or more stages. Determine:

i. the original pipeline cycle time and the total time for all 1000 tasks; ii. how the 20-second reduction should be distributed among the stages to maximize throughput; iii. the new cycle time and total time after optimization; iv. the percentage improvement in throughput.

中四级:A 22 s、B 38 s、C 34 s、D 30 s,1000 个任务,理想流水,周期由最慢一级决定。工程师可以把各级时间总共减少 24 s(可以分给多级)。求 (i) 原周期和总时间;(ii) 怎么分配才能吞吐最大;(iii) 新周期和总时间;(iv) 吞吐提升百分比。(原题第 ii 问写成 20 s,下面两种都算。)
提示

吞吐只看最慢一级。把预算全花在"削峰"上:从最高的一级开始往下削,直到几级一样高("注水法"的反向)。削 A 没有任何用处。

完整解答
(i) 周期 = 38 s;总时间 = 124 + 999 × 38 = 38,086 s(ii) 设目标周期为 L,需要的预算 = Σ max(0, Tᵢ − L)  L = 26:(38 − 26) + (34 − 26) + (30 − 26) = 12 + 8 + 4 = 24 ✓  → B 减 12 s,C 减 8 s,D 减 4 s,A 不动:22 / 26 / 26 / 26(iii) 周期 = 26 s;总时间 = 100 + 999 × 26 = 26,074 s(iv) 吞吐 1/38 → 1/26,提升 38/26 − 1 = 46.2%

为什么这样最优:吞吐 = 1/最慢级。如果只削 B,削到 34 以下 C 就成了新瓶颈;把 B、C、D 一起削到同一高度,24 s 预算换来的最大级时间最低。A 本来就比别人快,削它完全浪费。

若预算是 20 s:102 − 3L = 20 → L = 27.33 s  B −10.67、C −6.67、D −2.67 → 22 / 27.33 / 27.33 / 27.33  总时间 = 104 + 999 × 27.33 ≈ 27,410 s;吞吐提升 38/27.33 − 1 ≈ 39.0%
T10.7延迟 vs 吞吐概念
EN

Consider a 5-stage DLX pipeline IF → ID → EX → MEM → WB with stage delays IF 220 ps, ID 180 ps, EX 250 ps, MEM 200 ps, WB 150 ps.

(a) Determine the pipeline clock period. (b) Determine the latency of a single instruction in the pipelined processor. (c) Determine the throughput after the pipeline is filled. (d) A student claims: "Since the pipelined instruction latency is larger than the single-cycle instruction latency, pipelining makes the processor slower." Do you agree? Explain using latency and throughput. (e) Determine the theoretical maximum speedup if all stages could be perfectly balanced.

中5 级 DLX:IF 220、ID 180、EX 250、MEM 200、WB 150 ps。(a) 时钟周期;(b) 单条指令延迟;(c) 填满后的吞吐;(d) 有同学说"流水线单条延迟比单周期更长,所以流水线更慢",对吗?(e) 各级完全平衡时的理论最大加速比。
完整解答
(a) 时钟 = max = 250 ps(b) 延迟 = 5 × 250 = 1250 ps(单周期是 1000 ps)(c) 吞吐 = 1 / 250 ps = 4 × 10⁹ 条/秒(e) 总和 1000 ps,平衡后每级 200 ps → 1000 / 200 = 5

(d) 不对。延迟描述的是一条指令从进到出要多久,流水线确实让它从 1000 ps 变成 1250 ps(每级都被拉长到 250 ps)。但程序有成千上万条指令,决定程序运行时间的是吞吐量:单周期每 1000 ps 完成 1 条(10⁹ 条/秒),流水线每 250 ps 完成 1 条(4 × 10⁹ 条/秒),大程序快 4 倍。

T10.8DLX 格式、寻址与执行指令集
EN

Registers: R1 = 100, R2 = 20, R3 = 5, R4 = 200. Memory: [120] = 50, [208] = 80. Instructions: LW R5, 20(R1), ADD R6, R2, R3, ADDI R7, R2, #10, BEQZ R3, LOOP, J EXIT.

(a) For each instruction, identify 1. the format (R-type, I-type or J-type) and 2. the addressing mode. (b) Determine the final value produced by each instruction (each executes independently). (c) For LW R5, 20(R1), identify the meaning of the opcode, RS1, RD and the immediate field. (d) Explain why ADD R8, R2, 20(R1) is illegal in DLX and rewrite it using valid DLX instructions.

中R1 = 100,R2 = 20,R3 = 5,R4 = 200;Mem[120] = 50,Mem[208] = 80。指令:LW R5, 20(R1)、ADD R6, R2, R3、ADDI R7, R2, #10、BEQZ R3, LOOP、J EXIT。(a) 每条的格式和寻址模式;(b) 各自独立执行的结果;(c) LW 的 Opcode、RS1、RD、立即数分别是什么;(d) 为什么 ADD R8, R2, 20(R1) 非法?改写。
完整解答
指令格式寻址模式结果
LW R5, 20(R1)I 型偏移(基址)寻址EA = 100 + 20 = 120 → R5 ← Mem[120] = 50
ADD R6, R2, R3R 型寄存器寻址R6 ← 20 + 5 = 25
ADDI R7, R2, #10I 型立即数寻址R7 ← 20 + 10 = 30
BEQZ R3, LOOPI 型PC 相对(立即数作位移);条件看寄存器R3 = 5 ≠ 0 → 不跳转,PC ← PC + 4
J EXITJ 型PC 相对(26 位偏移加到 PC)无条件跳到 EXIT,不读写寄存器

(c) Opcode = LW(取字);RS1 = R1,基址寄存器;RD = R5,接收数据的目的寄存器(在 I 型编码里放在 Rt 字段);立即数 = 20,地址偏移。

(d) DLX 是 load-store 架构,ALU 指令的操作数只能是寄存器(或立即数),不能直接用内存操作数。改写为:

LW R9, 20(R1)  ; R9 ← Mem[120] = 50ADD R8, R2, R9  ; R8 ← 20 + 50 = 70
TUTORIAL 11

五级 DLX 流水线填表

对应 Lesson 11。IF:IR ← Mem[PC],NPC ← PC + 4;ID:A、B、Imm、b;EX:ALUOut;MEM:LMD 或写存储器或更新 PC;WB:写 Rd/Rt。学习手册的"DLX 数据通路步进器"里有本 Tutorial 的预设,可以逐拍对照。

T11.1ADD 的五级填表
EN

Consider the DLX instruction ADD R7, R3, R4 with R3 = 25, R4 = 17 and PC = 200. For each pipeline stage (IF, ID, EX, MEM, WB): 1. describe the operation performed; 2. determine the key outputs generated; 3. state whether data memory is accessed. Use a table with columns Stage, Operation Performed, Key Output.

中ADD R7, R3, R4,R3 = 25,R4 = 17,PC = 200。对每一级写出所做操作、关键输出、是否访问数据存储器。
完整解答
级操作关键输出数据存储器
IFIR ← Mem[200];NPC ← PC + 4IR = ADD R7,R3,R4;NPC = 204否(读的是指令存储器)
ID译码;A ← Regs[R3],B ← Regs[R4];目的号 b ← Rd = R7A = 25,B = 17,b = R7否
EXALUOut ← A + B(MUX 选 B)ALUOut = 42否
MEM不访存,ALUOut 直接传到 M/WALUOut = 42否
WBRegs[R7] ← ALUOutR7 = 42否
T11.2负立即数与符号扩展补码
EN

Refer to the 5-stage DLX datapath and consider ADDI R5, R2, #-12 with R2 = 40. Determine: 1. the 16-bit two's complement representation of −12; 2. the 32-bit sign-extended value; 3. the value of A after the ID stage; 4. the value of Imm after sign extension; 5. the value of ALUOut after the EX stage; 6. the final value written into R5 during WB. Show all working and intermediate results.

中ADDI R5, R2, #-12,R2 = 40。求 −12 的 16 位补码、32 位符号扩展值、ID 后的 A、符号扩展后的 Imm、EX 后的 ALUOut、WB 写入 R5 的值。
完整解答
+12 = 0000 0000 0000 1100取反 = 1111 1111 1111 0011加 1 = 1111 1111 1111 0100 = 0xFFF4(16 位 −12)符号扩展:符号位为 1,高 16 位补 1 = 1111 1111 1111 1111 1111 1111 1111 0100 = 0xFFFFFFF4A = Regs[R2] = 40;Imm = −12ALUOut = 40 + (−12) = 28(二进制相加后丢掉第 33 位进位)WB:R5 ← 28
T11.3LW 与 SW 的有效地址load/store
EN

Register and memory contents: R2 = 500, R3 = 88, Mem[520] = 123. Consider LW R1,20(R2) and SW 20(R2),R3. For each instruction determine: 1. the effective address; 2. ALUOut; 3. whether memory is read or written; 4. the value of LMD (if any); 5. the final value stored in memory or register.

中R2 = 500,R3 = 88,Mem[520] = 123。对 LW R1, 20(R2) 和 SW 20(R2), R3 分别写出有效地址、ALUOut、读还是写存储器、LMD、最终存入存储器或寄存器的值。
完整解答
LW R1, 20(R2)SW 20(R2), R3
有效地址500 + 20 = 520500 + 20 = 520
ALUOut520520
存储器读写
LMD123无(store 不用 LMD)
最终结果R1 ← 123(WB)Mem[520] ← R3 = 88(MEM),不写回寄存器

SW 的 B 来自 R3(要存的数据),经 D/A、A/M 一路传到 MEM 级。如果两条按顺序执行,SW 会把 123 覆盖成 88。

T11.4LW 的四个流水线寄存器级间缓冲
EN

Refer to the 5-stage DLX datapath and consider LW R8,40(R4). The pipeline uses the inter-stage buffers F/D, D/A, A/M and M/W. Assume R4 = 1000 and Memory[1040] = 250. For each pipeline buffer, identify the information that must be stored after the corresponding stage, and complete the table (Buffer, Information Stored).

中LW R8, 40(R4),R4 = 1000,Mem[1040] = 250。写出每个级间缓冲 F/D、D/A、A/M、M/W 在对应级结束后存了什么。
完整解答
缓冲存的内容
F/DIR = LW R8, 40(R4);NPC = PC + 4
D/AA = Regs[R4] = 1000;Imm = 40(符号扩展后);b = R8(目的寄存器号);OP = LW;B = R8 的旧值(读了但不用);NPC
A/MALUOut = 1000 + 40 = 1040(有效地址);b = R8;OP = LW
M/WLMD = Mem[1040] = 250;b = R8;OP = LW(ALUOut 也一并传下)

WB 级:WB MUX 选 LMD,R8 ← 250。讲义的缓冲清单里 D/A 还列了 Cond 和 NPC,A/M 有 NPC、cond,这些对 LW 没有意义,写上也不扣分。

T11.5理想流水时序图时序图
EN

Consider the instruction sequence LW R1,0(R2); ADD R3,R4,R5; ADDI R6,R7,#10; SW 20(R8),R9; BEQZ R10,LOOP. The 5 DLX stages are IF → ID → EX → MEM → WB; assume ideal pipelining with no hazards or stalls. Draw the pipeline timing diagram showing the stage occupied by each instruction during every clock cycle (rows: cycle 1, 2, …; columns: I1–I5).

中指令 I1–I5:LW R1,0(R2)、ADD R3,R4,R5、ADDI R6,R7,#10、SW 20(R8),R9、BEQZ R10,LOOP。理想流水、无冒险,画出每个周期每条指令所在的级。
完整解答
周期I1 LWI2 ADDI3 ADDII4 SWI5 BEQZ
1IF
2IDIF
3EXIDIF
4MEMEXIDIF
5WBMEMEXIDIF
6WBMEMEXID
7WBMEMEX
8WBMEM
9WB

共 5 + 5 − 1 = 9 个周期。第 5 周期五级全满。SW 和 BEQZ 的 WB 级什么也不写,但仍占一个周期(固定五级结构)。这五条指令之间没有寄存器依赖,所以理想流水成立。

T11.6不均衡的五级加速比
EN

Consider a processor with five pipeline stages IF → ID → EX → MEM → WB with delays IF 250 ns, ID 350 ns, EX 150 ns, MEM 300 ns, WB 200 ns. The unpipelined design executes one instruction at a time; the pipelined design uses the same five stages; hazards, stalls and branch penalties are ignored; pipeline register overhead is negligible unless stated otherwise. Determine: a) the average instruction execution time of the unpipelined processor; b) the average instruction execution time of the pipelined processor; c) the speedup achieved by pipelining; d) the theoretical maximum speedup of this 5-stage pipeline if overhead is ignored and all stages are perfectly balanced. Show all calculations.

中IF 250、ID 350、EX 150、MEM 300、WB 200 ns。非流水一次执行一条,流水用同样五级,忽略冒险和寄存器开销。求 (a) 非流水平均每条时间;(b) 流水平均每条时间;(c) 加速比;(d) 各级完全平衡时的理论最大加速比。
完整解答
(a) 250 + 350 + 150 + 300 + 200 = 1250 ns(b) 稳态每个周期完成一条,周期 = 最慢级 = 350 ns(c) 1250 / 350 ≈ 3.57(d) 平衡后每级 250 ns → 1250 / 250 = 5(= 级数)
T11.7又一个 LW 的缓冲内容级间缓冲
EN

Consider LW R1, 20(R2) with R2 = 100 and Memory[120] = 55. For each pipeline register (F/D, D/A, A/M, M/W), determine the contents stored at the end of the cycle.

中LW R1, 20(R2),R2 = 100,Mem[120] = 55。写出每个流水线寄存器在该周期结束时的内容。
完整解答
寄存器内容
F/DIR = LW R1, 20(R2);NPC = PC + 4
D/AA = 100;Imm = 20;b = R1;OP = LW(B = R1 旧值,不用)
A/MALUOut = 120;b = R1;OP = LW
M/WLMD = 55;b = R1;OP = LW

WB:R1 ← 55。

T11.8CPI × 时钟加速比
EN

Assume: unpipelined CPI = 4.6, clock cycle = 0.8 ns; pipelined CPI = 1.2, clock cycle = 1.0 ns. Determine 1. the average instruction execution time (unpipelined); 2. the average instruction execution time (pipelined); 3. the pipeline speedup.

中非流水:CPI 4.6,时钟 0.8 ns。流水:CPI 1.2,时钟 1.0 ns。求两者平均每条时间和加速比。
完整解答
非流水 = 4.6 × 0.8 = 3.68 ns流水 = 1.2 × 1.0 = 1.2 ns加速比 = 3.68 / 1.2 ≈ 3.07

流水线的时钟反而更长(1.0 vs 0.8 ns,流水线寄存器和时钟偏移带来的开销),但 CPI 从 4.6 降到 1.2,整体仍快 3 倍。

TUTORIAL 12

流水线冒险与分支代价

对应 Lesson 12。CPI = 1 + 平均停顿;分支附加 CPI = 分支频率 × 平均分支代价;转发后 ALU→ALU 无停顿,load-use 仍停 1 拍;讲义口径:EX 判定分支代价 3,ID 判定代价 1。

T12.1R4000 三种分支方案分支 CPI
EN

For an R4000-style pipeline, the branch target address is known after 3 pipeline stages and the branch condition is evaluated 1 cycle later; assume no stalls due to register dependencies in the branch comparison. Branch penalties (unconditional / conditional not taken / conditional taken): flush pipeline 2 / 3 / 3; predict taken 2 / 3 / 2; predict not taken 2 / 0 / 3. SPEC branch frequencies: unconditional 4%, conditional not taken 6%, conditional taken 10%. Find the effective addition to CPI due to branches for each branch handling scheme.

中R4000 式流水线,代价表(周期):冲刷流水线 2 / 3 / 3;预测跳转 2 / 3 / 2;预测不跳转 2 / 0 / 3(依次为无条件分支、条件不跳、条件跳)。频率:无条件 4%,条件不跳 6%,条件跳 10%。求每种方案由分支带来的附加 CPI。
完整解答
冲刷:0.04×2 + 0.06×3 + 0.10×3 = 0.08 + 0.18 + 0.30 = 0.56预测跳转:0.04×2 + 0.06×3 + 0.10×2 = 0.08 + 0.18 + 0.20 = 0.46预测不跳转:0.04×2 + 0.06×0 + 0.10×3 = 0.08 + 0 + 0.30 = 0.38

附加 CPI = Σ(每类分支频率 × 该类代价)。预测不跳转最好:不跳的 6% 完全没有代价。如果理想 CPI = 1,三者的 CPI 分别是 1.56、1.46、1.38。

T12.2PowerPC 601 六级流水分支 CPI
EN

The PowerPC 601 has a 6-stage pipeline: Fetch → Dispatch → Decode → Execute → Cache Access → Write. A branch or jump is detected in Fetch; the target address for a jump or conditional branch is available at the end of Decode; the conditional branch outcome is decided in Execute. Benchmark profile: conditional branches 20% of all instructions; jumps or calls 5%; 60% of conditional branches are taken.

a) Find the expected CPI if the CPU stalls until the branch outcome is known. b) Suppose two-slot delayed branching is added for unconditional branches. If 60% of the delay slots are filled with useful instructions, what is the new CPI? Assume conditional branches use the "Predict Not Taken" strategy.

中六级:Fetch → Dispatch → Decode → Execute → Cache → Write。分支/跳转在 Fetch 识别;跳转和条件分支的目标在 Decode 结束时可用;条件分支结果在 Execute 决定。条件分支占 20%,跳转/调用占 5%,条件分支 60% 跳转。(a) CPU 停到分支结果已知时的 CPI;(b) 给无条件分支加两个延迟槽,60% 的槽能填有用指令,条件分支用"预测不跳转",新 CPI?
提示

某个信息在第 k 级结束时可用,下一条正确指令最早在第 k + 1 个周期取指,代价 = k − 1。Decode 是第 3 级,Execute 是第 4 级。

完整解答
跳转:目标在 Decode(第 3 级)结束可用 → 第 4 周期取目标 → 代价 2条件分支:结果在 Execute(第 4 级)结束 → 第 5 周期取指 → 代价 3(a) CPI = 1 + 0.05 × 2 + 0.20 × 3 = 1 + 0.1 + 0.6 = 1.7
(b) 跳转:2 个延迟槽,每个槽 60% 有用 → 平均浪费 2 × 0.4 = 0.8 周期  条件分支(预测不跳转):不跳 0 代价;跳(60%)代价 3 → 平均 1.8  CPI = 1 + 0.05 × 0.8 + 0.20 × 1.8 = 1 + 0.04 + 0.36 = 1.40
题意说明:"预测不跳转"那句放在全题末尾。按 (a) 的字面意思(停到结果已知),条件分支不论跳不跳都等 3 拍,得 1.7;(b) 才引入预测不跳转。如果你的老师把预测不跳转也用于 (a):1 + 0.05×2 + 0.2×0.6×3 = 1.46。
T12.3填延迟槽代码调度
EN

Assume delayed branching is implemented: the instruction immediately after the branch is always executed before the branch takes effect. Reorder the following code to make better use of the branch delay slot. Assume R3 is initialized to 8 before the loop starts; the code may need to be slightly modified to preserve correctness.

Loop: LW R2, 100(R3) / SW 200(R3), R2 / SUBI R3, R3, #4 / BNEZ R3, Loop / NOP ; branch delay slot

中延迟分支:分支后面紧跟的一条总会执行。R3 初值 8。重排下面的循环以利用延迟槽(可以稍作修改以保证正确):
Loop: LW R2, 100(R3)   SW 200(R3), R2   SUBI R3, R3, #4   BNEZ R3, Loop   NOP       ; 延迟槽
提示

SUBI 不能挪进槽,因为 BNEZ 要用它的结果。看看 SW 能不能挪到分支后面,挪过去之后 R3 已经被减了 4。

完整解答
Loop: LW R2, 100(R3)   SUBI R3, R3, #4   BNEZ R3, Loop   SW 204(R3), R2 ; 延迟槽(from before,偏移 200 → 204)

原循环每轮把 Mem[R3+100] 复制到 Mem[R3+200]。SW 被挪到 SUBI 之后执行,此时 R3 已经少了 4,所以偏移要加 4 才能写到原来的地址:(R3 − 4) + 204 = R3 + 200。

轮LW 读R3 变为BNEZ槽里 SW 写
1Mem[108]4跳回 LoopMem[4 + 204] = Mem[208] ✓
2Mem[104]0不跳Mem[0 + 204] = Mem[204] ✓

和原代码效果相同,每轮从 5 条(含 NOP)减到 4 条,延迟槽做了有用的工作。SW 不影响分支条件,所以 from-before 安全。

T12.4单端口存储器的结构冒险结构冒险
EN

A pipelined processor uses a single-port memory shared by instruction fetches and data accesses. 40% of instructions are loads/stores. Assume ideal CPI = 1 and each load/store causes 1 structural stall cycle. a) Compute the average stall cycles per instruction. b) Determine the effective CPI. c) Determine the speedup relative to a non-pipelined processor with CPI = 5.

中取指和取数共用一个单端口存储器,40% 的指令是 load/store,理想 CPI = 1,每条 load/store 造成 1 周期结构停顿。求平均停顿、有效 CPI、相对 CPI = 5 的非流水处理器的加速比。
完整解答
停顿 = 0.4 × 1 = 0.4CPI = 1 + 0.4 = 1.4加速比 = 5 / 1.4 ≈ 3.57(假设时钟相同)

改用分离的指令/数据存储器(或双端口存储器)就能消除这 0.4 的停顿,加速比回到 5。

T12.5判断冒险类型RAW/WAR/WAW
EN

For each instruction pair, identify the hazard type (RAW, WAR, WAW, or None): 1. ADD R1,R2,R3 / SUB R4,R1,R5; 2. ADD R1,R2,R3 / LW R2,0(R5); 3. LW R1,0(R2) / ADD R1,R3,R4; 4. ADD R1,R2,R3 / OR R4,R5,R6. For each dependency, state whether it can occur in a basic DLX pipeline.

中判断每对指令的冒险类型,并说明在基本 DLX 流水线里会不会发生:
  1. ADD R1,R2,R3 / SUB R4,R1,R5
  2. ADD R1,R2,R3 / LW R2,0(R5)
  3. LW R1,0(R2) / ADD R1,R3,R4
  4. ADD R1,R2,R3 / OR R4,R5,R6
完整解答
#类型原因DLX 会发生吗
1RAW(R1)ADD 写 R1,SUB 读 R1会;靠转发解决,0 停顿
2WAR(R2)ADD 读 R2,LW 写 R2不会:读在 ID,写在 WB,读永远先发生
3WAW(R1)两条都写 R1不会:都在 WB 写,按程序顺序写回
4None寄存器没有交集—
T12.6依赖链与转发时序图
EN

Consider LW R1,0(R2); ADD R3,R1,R4; SUB R5,R3,R6; AND R7,R5,R8 on a DLX pipeline with forwarding. a) Identify all RAW hazards. b) Determine which hazards can be eliminated by forwarding. c) Determine the number of stall cycles required. d) Draw the pipeline timing diagram.

中LW R1,0(R2)、ADD R3,R1,R4、SUB R5,R3,R6、AND R7,R5,R8,DLX 有转发。(a) 找出所有 RAW;(b) 哪些能被转发消除;(c) 需要几个停顿周期;(d) 画时序图。
完整解答

(a) 三个 RAW:LW → ADD(R1)、ADD → SUB(R3)、SUB → AND(R5)。

(b) ADD → SUB、SUB → AND 是 ALU 到 ALU,从 EX/MEM 转发即可,无停顿。LW → ADD 是 load-use:数据在 LW 的 MEM 结束才出来,ADD 的 EX 已经在同一周期开始,转发来不及。

(c) 需要 1 个停顿周期,硬件互锁在 ADD 的 ID 之后插入一个气泡。

指令123456789
LW R1,0(R2)IFIDEXMEMWB
ADD R3,R1,R4IFIDstallEXMEMWB
SUB R5,R3,R6IFstallIDEXMEMWB
AND R7,R5,R8stallIFIDEXMEMWB

LW 的结果在第 4 周期末出来,第 5 周期转发给 ADD 的 EX(MEM/WB → EX);ADD 的结果第 5 周期末出来,第 6 周期转发给 SUB;SUB 第 6 周期末出来,第 7 周期转发给 AND。共 4 + 4 + 1 = 9 个周期。

T12.7数据冒险 + 控制冒险时序图
EN

Consider LW R1,0(R2); ADD R3,R1,R4; BEQZ R3,Target; OR R5,R6,R7. Assume forwarding is available, the branch is resolved in the EX stage, and there is no branch prediction. a) Draw the pipeline timing diagram. b) Identify all data hazards. c) Identify all control hazards. d) Determine the total number of stall cycles.

中LW R1,0(R2)、ADD R3,R1,R4、BEQZ R3,Target、OR R5,R6,R7。有转发;分支在 EX 级判定;没有分支预测。(a) 画时序图;(b) 所有数据冒险;(c) 所有控制冒险;(d) 总停顿周期。
完整解答

(b) 数据冒险:LW → ADD(R1,load-use,停 1 拍);ADD → BEQZ(R3,分支在 EX 比较,ADD 的结果从 EX/MEM 转发过来,0 停顿)。

(c) 控制冒险:BEQZ 在 EX 才知道跳不跳,没有预测就只能等。按讲义口径(原始 DLX,EX 判定)分支代价 3 周期,下一条(OR 或 Target 处的指令)要等 BEQZ 的结果出来后才取指。

指令123456789101112
LW R1,0(R2)IFIDEXMEMWB
ADD R3,R1,R4IFIDstallEXMEMWB
BEQZ R3,TargetIFstallIDEXMEMWB
OR R5,R6,R7stallbubblebubblebubbleIFIDEXMEMWB

(d) 理想 4 + 4 = 8 个周期,实际 12 个,共 4 个停顿周期:1 个 load-use + 3 个分支。

2 还是 3:严格按"EX 结束知道结果、下一拍就取指",分支代价是 2,总停顿 3、共 11 周期。讲义把原始 DLX 的 EX 判定记为 3 周期代价(PC 在 MEM 级才更新),这里按讲义答 4。答题时写出你的假设。
T12.8预测不跳转 vs 全停顿分支 CPI
EN

A benchmark contains 15% branch instructions, of which 60% are taken and 40% not taken. The processor uses the Predict Not Taken scheme, with a branch penalty of 3 cycles when the prediction is incorrect and ideal CPI = 1. a) Compute the average branch penalty. b) Determine the additional CPI caused by branches. c) Determine the effective CPI. d) Compare the result with a processor that always stalls 3 cycles for every branch.

中15% 是分支,其中 60% 跳转、40% 不跳。预测不跳转,猜错代价 3 周期,理想 CPI = 1。求 (a) 平均分支代价;(b) 分支附加 CPI;(c) 有效 CPI;(d) 和每个分支都停 3 拍的处理器比较。
完整解答
(a) 只有实际跳转(猜错)才付代价:0.6 × 3 + 0.4 × 0 = 1.8 周期/分支(b) 0.15 × 1.8 = 0.27(c) CPI = 1 + 0.27 = 1.27(d) 全停顿:1 + 0.15 × 3 = 1.45;1.45 / 1.27 ≈ 1.14

预测不跳转比全停顿快约 14%:40% 不跳的分支省掉了全部代价。不过分支多数会跳(60%),所以预测不跳转猜错的比例较高,这也是硬件转向动态分支预测的原因。