Amdahl’s law(阿姆达尔定律)公式推导与思考

这篇文章围绕 Amdahl’s law 展开,说明并行计算中程序整体加速比为何会受到串行部分的限制。正文先解释公式里并行比例 a 和并行加速倍数 n 的含义,并引入 Fraction enhanced 与 Speedup enhanced 两个概念,再从总执行时间、并行代码时间和串行代码时间的关系出发,对公式 S = 1 / (1 – a + a / n) 进行推导。最后文章回到公式本身,讨论当并行比例固定、核心数持续增加时系统加速比仍存在理论上限这一结论。

介绍

Amdahl’s law(阿姆达尔定律) 由计算机科学家 Gene Amdahl 在 1967 年提出,旨在用公式描述在并行计算中,多核处理器理论上能够提高多少倍速度,公式如下:

S=11a+anS=\frac{1}{1-a+\frac{a}{n}}

SS 为 speedup,代表全局加速倍速(原来总时间/ 加速后总时间),aa 为并行计算所占比例(可以并行计算代码量 / 总代码量),nn 为并行节点处理个数,可以理解为 CPU 的核心数,这里先简要介绍下,后面会详细说明并且推导。

前置说明

Fraction enhanced

Fraction enhanced 顾名思义是部分提高。例如我的程序总共有 100 行代码,其中 50 行是可以通过并行计算的,那么这 50 行代码就是 Fraction enhanced 。但是实际上 Fraction enhanced 是一个比例数值,是并行计算代码 / 总代码量

Fraction enhanced=parallel codetotal codeFraction\ enhanced=\frac{parallel\ code}{total\ code}

例如上面的例子,Fraction enhanced=50/100=0.5Fraction\ enhanced = 50/100=0.5 ,由此我们可以发现,Fraction enhanced 的值永远小于 1

Speedup enhanced

Speedup enhanced=Old execution timeNew execution timeSpeedup\ enhanced=\frac{Old\ execution\ time}{New\ execution\ time}

如上面公式所得,Speedup enhanced 等于 原有运行时间 / 并行计算加速后的时间 。例如系统原来串行计算需要 6 秒,加速后只需要 3 秒,那么 Speed enhanced=6/3=2Speed\ enhanced=6/3=2 。由此可知 Speedup enhanced 的值永远大于 1

带入 Amdahl’s law

我们分别把 Fraction enhancedSpeedup enhanced 带入 Amdahl’s lawFraction enhanced 对应公式中的 aa ,即并行计算所占比例。Speedup enhanced 对应 nn ,即并行节点处理个数。

Speedup enhanced 为什么可以代替 nn

这里大家可能有一点疑问,Speedup enhanced 明明是 未加速前时间 / 加速后的时间,为什么就可以代表并行节点处理个数。在理论上,单核处理器处理一个任务需要 100 秒,那么双核处理它应该需要 50 秒。时间上它提速了 2 倍, cpu 个数上它也提升了 2 倍,故两个可以替换。

Speedup enhanced=Old execution timeNew execution time=10050=2=New cpu coresOld cpu cores=21=2\begin{aligned} Speedup\ enhanced&=\frac{Old\ execution\ time}{New\ execution\ time} \\ &=\frac{100}{50}=2 \\ &=\frac{New\ cpu\ cores}{Old\ cpu\ cores} \\ &=\frac{2}{1}=2 \end{aligned}

带入后公示得:

S=Old total execution timeNew total execution time=11Fractionenhanced+Fractionenhanced/Speedupenhanced\begin{aligned} S&=\frac{Old\ total\ execution\ time}{New\ total\ execution\ time}\\ &=\frac{1}{{1-Fraction_{enhanced}}+Fraction_{enhanced}/Speedup_{enhanced}}\\ \end{aligned}

证明

  • SS 为我们所需要的结果,全局提速倍速
  • Old total execution timeOld\ total\ execution\ time (原来系统执行总时间)为 TT
  • New total execution timeNew\ total\ execution\ time (加速后系统执行总时间)为 TT’
  • 系统中并行代码块(指能够通过并行计算加速的代码块)原来执行时间为 tt
  • 系统中并行代码块(指能够通过并行计算加速的代码块)加速后执行时间为 tt’
  • 系统中串行代码块(指不能通过并行计算加速的代码块)执行时间为 tnt_n
  • FractionenhancedFraction_{enhanced}ff’
  • SpeedupenhancedSpeedup_{enhanced}ss’
S=TTT=tn+tT=tn+tf=tT=ttn+ts=ttfs=ttn+t÷tt=ttn+t\begin{gather*} S=\frac{T}{T’}\\ T=t_n+t\\ T’=t_n+t’\\ f’ = \frac{t}{T}=\frac{t}{t_n+t}\\ s’=\frac{t}{t’}\\ \frac{f’}{s’}=\frac{t}{t_n+t} \div \frac{t}{t’}=\frac{t’}{t_n+t} \end{gather*}

带入公式得:

S=TT=tn+ttn+t=1(tn+t)/(tn+t)=11ttn+t+ttn+t=11f+fs=11Fractionenhanced+Fractionenhanced/Speedupenhanced=11a+an\begin{aligned} S&=\frac{T}{T’}\\ &=\frac{t_n+t}{t_n+t’}\\ &=\frac{1}{(t_n+t’)/(t_n+t)}\\ &=\frac{1}{1-\frac{t}{t_n+t}+\frac{t’}{t_n+t}}\\ &=\frac{1}{1-f’+\frac{f’}{s’}}\\ &=\frac{1}{{1-Fraction_{enhanced}}+Fraction_{enhanced}/Speedup_{enhanced}}\\ &=\frac{1}{1-a+\frac{a}{n}} \end{aligned}

证明完毕!

总结

让我们回到最初的公式

S=11a+anS=\frac{1}{1-a+\frac{a}{n}}

aa 为并行计算所占比例,nn 为并行节点处理个数。当 a=0a=0 时(即只有串行没有并行),无论 nn 为多少,加速比 SS 均为 1。当 n+n \rightarrow +\infty ,当 cpu 核心数无限增多的时候,极限加速比 S=1/(1a)S=1/(1-a) 。例如若并行代码有 75%,极限加速比不能超出 4。

由此我们可知,在并行系统中一味的增加运算资源,并不能永远成倍的提升系统整体性能。

参考资料

阿姆达尔定律

Computer Organization | Amdahl’s law and its proof

理解性能提升By阿姆达尔定律(Amdahl’s law)

原创文章,作者:Smith,如若转载,请注明出处:https://www.inlighting.org/archives/amdahls-law-and-its-proof

打赏 微信扫一扫 微信扫一扫
SmithSmith
上一篇 2020年10月13日 下午1:13
下一篇 2021年6月10日 下午1:50

相关推荐

  • 分布式系统中的 Partial ordering 和 Global ordering 的理解

    这篇文章用简化的事件顺序示例,解释了分布式系统中 partial ordering 与 global ordering 的区别。正文先说明全局有序要求所有事件都按统一顺序发生,而部分有序只约束存在依赖关系的那部分事件,再通过 A、B、C 三个事件的例子说明两者在系统行为上的差异,最后结合 Lamport Timestamp 指出这类逻辑时钟系统通常只能保证 partial ordering,而不是整个系统范围内的全局有序。

    2020年10月13日
    1.7K0
  • 理解 FLP-Impossibility 论文

    这篇文章尝试用更易读的方式梳理 FLP-Impossibility 论文,解释为什么在异步消息系统中,即使最多只有一个进程故障,也不存在能够同时满足 Validity、Agreement 和 Termination 的真正共识算法。正文先定义论文里的大量术语和系统模型,包括 process、configuration、schedule、bivalent、univalent 等概念,再围绕几个关键 lemma 逐步展开证明思路,说明系统总能停留在无法保证做出决议的状态。最后文章结合 Paxos、Raft 等现实算法讨论 FLP 的实际意义,即它证明的是最坏情况的存在,而工程系统必须通过随机化、时序假设或其他约束来换取可用性。

    2021年6月10日
    5.5K8
  • TinyKV Project1 Standalone KV

    这篇文章总结了 TinyKV Project1 的实现内容,核心是在 Badger 之上构建一个支持 Column Family 的 standalone KV 存储引擎。正文先解释 TinyKV 里 `default`、`write`、`lock` 等 CF 的基本含义,以及如何通过 key 前缀在底层 KV 中模拟多列结构,随后说明 standalone storage、Reader 和批量写入的实现方式,再继续梳理 `RawGet`、`RawScan`、`RawPut`、`RawDelete` 等服务接口如何基于自定义 Reader 完成读写。文章最后还补充介绍了 Badger 参考 WiscKey 的存储设计,以及它在读写放大上的权衡。

    2023年3月2日
    2.0K0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注


评论列表(2条)

  • Genson
    Genson 2023年11月6日 下午12:02

    你好,请教一下,文章中的公式是如何正常显示的?