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

相关推荐

  • TinyKV Project4 Transactions

    这篇文章总结了 TinyKV Project4 中事务模块的实现思路,核心围绕 Percolator 如何基于多版本数据、锁和两阶段提交在 KV 存储上实现事务。正文先解释 primary key、startTs、commitTs、Lock/Write/Data 三类列族及其关系,再按顺序梳理 prewrite、commit、读取、回滚和锁状态检查的完整流程,最后结合 transaction.go 中的主要方法说明 TinyKV 是如何处理写冲突、锁超时、批量回滚和故障恢复的。

    2023年3月2日
    14.7K3
  • Talent Plan TinyKV 白皮书

    这篇文章记录了作者参加 PingCAP Talent Plan TinyKV 学习营、完成 TinyKV 项目后的整体复盘,并汇总了配套白皮书、经验和踩坑总结。正文先说明比赛结果、性能评分异常的猜测以及白皮书各章节入口,随后从硬件准备、测试方式和心态管理谈起,分析 TinyKV 在文档完整性、测试覆盖度和事务实现理解上的几个难点,并给出各个 Project 的难度排序与推荐完成顺序。文章最后补充了参考资料、批量跑测试脚本,以及对整个 TinyKV 学习过程的个人反思。

    2022年1月9日
    10.1K49
  • 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

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