为什么并行不是无限的: 简单解释 Amdahl vs Gustafson


Amdahl 定律 vs Gustafson 定律 — 完整教程、推导、应用场景及 Python 绘图

Amdahl 定律 vs Gustafson 定律:完整教程、推导、应用场景及 Python 绘图
理解并行加速:通过代码讲解 Amdahl 定律和 Gustafson 定律
并行计算基础:Amdahl 定律、Gustafson 定律及加速建模
并行加速原理:Amdahl 和 Gustafson 定律完整指南
并行扩展解析:推导并比较 Amdahl 和 Gustafson 定律
Amdahl vs Gustafson:并行加速完整指南(含 Python 代码)
并行性能建模:Amdahl 定律、Gustafson 定律及实际应用
学习并行加速:数学、直觉、应用场景及 Python 可视化
并行计算:必须掌握的两条定律(Amdahl & Gustafson)
工程师的并行加速:Amdahl 定律、Gustafson 定律及 Python 实现
从理论到代码:用 Amdahl 和 Gustafson 建模并行加速
实用并行加速指南:Amdahl 定律、Gustafson 定律及可视化
为什么并行不是无限的:简单解释 Amdahl vs Gustafson
并行加速真相:Amdahl 限制 vs Gustafson 扩展
并行计算神话与现实:Amdahl 和 Gustafson 的教训

引言

并行计算在现代计算中至关重要:多核 CPU、GPU、分布式集群、云工作负载、LLM 训练以及 HPC 模拟。

为了分析程序在更多处理器下能加速多少,主要有两种数学模型:

  • Amdahl 定律 — 固定规模工作负载的性能
  • Gustafson 定律 — 可扩展规模工作负载的性能

这两条定律并不矛盾,它们回答的是 不同的问题
本教程涵盖推导、直觉、比较、实际应用场景,以及展示两条定律的 Python 绘图脚本。

1. 什么是加速比?

加速比衡量程序在 N 个处理器上运行速度提升多少:

tex_874e8d72fc08f2af61e0df97c456b5c2 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

如果程序在一个处理器上运行 10 秒,两处理器运行 5 秒,则加速比为:

tex_fea9c4fbe000a3e47d431a26d0bf7ab2 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

完美线性加速为:

tex_e9f6efb7e9d50d657c0883dc59792232 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

但实际系统存在串行瓶颈,这正是 Amdahl 定律和 Gustafson 定律描述的内容。

2. Amdahl 定律(固定工作量)

2.1 直觉

Amdahl 假设:

  • 总工作量保持 不变
  • 部分工作是串行的,无法并行化

设:

  • f = 串行比例
  • 1 - f = 可并行比例

2.2 推导

一个处理器的运行时间:

tex_cd65cc0f8fa444a1c942c1dd435729e9 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

定义:

tex_47e86b1941e5d34562897f2bc9e72129 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

因此:

tex_b339bd84646feb5fa705f2337c45e91d 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机
tex_98f8bc0d6bf46e12033e27be130a4a7d 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

N 个处理器的运行时间:

tex_b66634c6e27fc1de8ea84ac81724010c 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

加速比:

tex_9a86b3b2234c3e7746efd503e5107b9c 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

其中 f 是串行工作比例,tex_3f4589d9b675507468afd74744fcafef 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机 是可并行工作。Amdahl 公式也可以写成:

tex_4cec42f3f3a919b7d7984827a2605bdd 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

其中 tex_73e66f4b3b996f161705ee100bb45369 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机 tex_497d56a00b5e083d6488ec3072a6ca06 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

2.3 当 N → ∞ 时的极限

tex_4f525a4ae4832e1d6d4d29923205acf8 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

如果串行比例为 10%(f = 0.1):

tex_c06286eea86d0b1a0ea19ff2498067ed 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

即使处理器无限,也无法超过该值。

2.4 Amdahl 定律的实际应用场景

Amdahl 适合优化固定任务的 延迟

  • GPU 内核优化固定张量大小
  • 单次请求推理延迟降低
  • 视频编码、压缩、排序
  • 加速固定批量作业
  • 数据库查询加速

3. Gustafson 定律(可扩展工作量)

3.1 直觉

Gustafson 反过来问:

“增加处理器,我能在相同时间内解决多大的问题?”

这反映了真实 HPC 工作负载:更多 CPU → 更高分辨率 → 更大模拟。

3.2 推导

假设程序在 N 个处理器上运行 1 个时间单位。

设:

  • f = 串行比例(按规模测量)

可并行部分随处理器数量扩展,因此其运行时间保持与 N 成比例。

一个处理器的时间:

tex_e3f2599da2e43b1173c1aaf4eda87614 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

加速比:

tex_7d6285b7fe4fe4215fffc041747b6b38 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

Gustafson 公式的 “N 减” 形式:

tex_1b159a1d1361b799fb425aa75b47ca79 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

或者,如果定义并行比例 tex_e54ad141cf10b747683362c54b759563 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机 ,公式也可写为:

tex_b17b90807c408febafb1a4cb79950309 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

“N 减” 形式用 p 表示:

tex_e644c8961d33e3b3b915a21e385056e7 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

3.3 解释

随着 N 增加,加速比趋近于:

tex_7f1c2335a53dcee7685791c4b80f9c20 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

对于小串行比例,几乎呈线性增长。

3.4 Gustafson 定律的实际应用场景

Gustafson 适用于 吞吐量扩展 或可增加问题规模的工作负载:

  • 天气和气候模拟
  • 粒子模拟、CFD、有限元分析
  • LLM 训练:更多 GPU → 更长序列或更大模型
  • 大数据分析(Spark, Dask, Flink)
  • 蒙特卡洛模拟

4. Amdahl 定律 vs Gustafson 定律(比较表)

项目 Amdahl Gustafson
工作负载 固定 随 N 扩展
目标 降低延迟 增加吞吐量
加速比上限 有界: tex_caa92bab626afd401dbbe35770e10c44 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机 近似线性: tex_6993fa207348cf6876f3ae7d7f16783b 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机
悲观/乐观 悲观 乐观
应用场景 优化现有任务 扩展大规模工作量

5. 实际应用场景(综合视角)

Amdahl(延迟优化)

  • 减少单次 LLM 查询推理时间
  • 加速数据库 join 操作
  • 固定张量 GPU 内核优化
  • 视频编码(相同视频)

Gustafson(吞吐量 / 扩展)

  • LLM 训练(扩展至更多 GPU)
  • 高分辨率天气模型模拟
  • 大数据 ETL 扩展
  • 科学 HPC 工作负载

6. Python 绘图脚本(显示两条定律)

下面代码生成 Amdahl 与 Gustafson 加速比曲线图。

可以调整 f(串行比例)和处理器数量 N

脚本绘制两条曲线在同一张图上。

包括部分 tex_f9f843126d657a7415623d3414da113e 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机 的值,例如串行部分:


import numpy as np
import matplotlib.pyplot as plt

def amdahl_speedup(N, s):
return 1.0 / (s + (1 - s) / N)

def gustafson_speedup(N, s):
return s + (1 - s) * N

# Number of processors
N = np.arange(1, 65)

# Serial fractions to consider
Serial = [0.05, 0.1, 0.2, 0.3, 0.5, 0.8, 0.9, 1.0]

plt.figure(figsize=(10, 6))

for f in Serial:
plt.plot(N, amdahl_speedup(N, f), linestyle='-', label=f"Amdahl Serial={f}")
plt.plot(N, gustafson_speedup(N, f), linestyle='--', label=f"Gustafson Serial={f}")

plt.title("Amdahl's Law")
plt.xlabel("Number of Processors (N)")
plt.ylabel("Speedup")
plt.legend()
plt.grid(True)
plt.tight_layout()

plt.savefig("parallel-speedup-amdahl-vs-gustafson.png")
## plt.show()

下面是 Amdahl 与 Gustafson 曲线图示。

parallel-speedup-amdahl 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

Amdahl 定律加速曲线

parallel-speedup-amdahl-vs-gustafson 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

Amdahl vs Gustafson 加速曲线

parallel-speedup-gustafson 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

Gustafson 定律加速曲线

图示解读

  • Amdahl 曲线迅速趋于平缓——受串行部分限制。
  • Gustafson 曲线几乎线性上升——适用于可扩展工作负载。
  • 串行比例 f 越高,两种模型差距越大。

结论

Amdahl 定律展示了固定工作负载下的并行 上限,适合延迟优化。Gustafson 定律展示了随工作负载扩展的并行 潜力

  • Amdahl 定律 → 固定规模工作负载 → 收益递减
  • Gustafson 定律 → 可扩展工作负载 → 近似线性加速
  • 结合使用理解硬件极限与算法特性
  • Python 工具使可视化直观易懂

它们共同构成现代并行系统性能分析基础,从 HPC 到 LLM 训练,再到 GPU 计算。

英文:The Truth About Parallel Speedup: Amdahl’s Limits vs Gustafson’s Scaling

本文一共 1384 个汉字, 你数一下对不对.
为什么并行不是无限的: 简单解释 Amdahl vs Gustafson. (AMP 移动加速版本)
上一篇: CloudFlare宕机, 半个互联网崩了
下一篇: 在罗马的最后一晚吃了顿中餐, 好便宜

扫描二维码,分享本文到微信朋友圈
2a7943b90b234a0679cea2bf47b234da 为什么并行不是无限的: 简单解释 Amdahl vs Gustafson Python Python 学习笔记 并行计算/Parallel Computing 计算机

评论