第 143 / 187 篇

14. 多核时代的共享内存利用

说明

本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C14-SharedMemory/index.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。

14. Leveraging Shared Memory in the Multicore Era

世界已经改变。

The world is changed.

我在硅中感觉到了。

I feel it in the silica.

我在晶体管中感觉到了。

I feel it in the transistor.

我在核心中看到了。

I see it in the core.

——向凯兰崔尔致歉(《指环王:护戒同盟》)

~ With apologies to Galadriel (Lord of the Rings: Fellowship of the Ring)

到目前为止,我们对体系结构的讨论都集中在纯粹的单 CPU 世界中。但世界已经改变。如今的 CPU 有多个核心(core),即计算单元。在本章中,我们讨论多核体系结构,以及如何利用它们来加快程序的执行。

Until now, our discussion of architecture has focused on a purely single-CPU world. But the world has changed. Today’s CPUs have multiple cores, or compute units. In this chapter, we discuss multicore architectures, and how to leverage them to speed up the execution of programs.

原书注 · CPU、处理器与核心(CPUs, Processors, and Cores)

在本章的许多地方,处理器和 CPU 这两个术语是混用的。从根本上说,处理器(processor)是对外部数据执行某种计算的任何电路。按照这个定义,中央处理器(central processing unit,CPU)就是处理器的一个例子。带有多个计算核心的处理器或 CPU 称为多核处理器(multicore processor)或多核 CPU。核心(core)是一个计算单元,它包含构成经典 CPU 的许多部件:ALU、寄存器和一点缓存。尽管核心与处理器不同,但在文献中看到这两个术语混用并不罕见(特别是那些写于多核处理器还被视为新鲜事物的年代的文献)。

In many instances in this chapter, the terms processor and CPU are used interchangeably. At a fundamental level, a processor is any circuit that performs some computation on external data. Based on this definition, the central processing unit (CPU) is an example of a processor. A processor or a CPU with multiple compute cores is referred to as a multicore processor or a multicore CPU. A core is a compute unit that contains many of the components that make up the classical CPU: an ALU, registers, and a bit of cache. Although a core is different from a processor, it is not unusual to see these terms used interchangeably in the literature (especially if the literature originated at a time when multicore processors were still considered novel).

1965 年,英特尔创始人戈登·摩尔(Gordon Moore)估计,集成电路中的晶体管数量每年会翻一番。他的预测如今被称为摩尔定律(Moore’s Law),后来被修正为晶体管数量每两年翻一番。尽管电子开关已经从巴丁(Bardeen)的晶体管演变成如今现代计算机中使用的微小芯片晶体管,摩尔定律在过去 50 年里一直成立。然而,世纪之交时,处理器设计撞上了几道关键的性能壁垒:

In 1965, the founder of Intel, Gordon Moore, estimated that the number of transistors in an integrated circuit would double every year. His prediction, now known as Moore’s Law, was later revised to transistor counts doubling every two years. Despite the evolution of electronic switches from Bardeen’s transistor to the tiny chip transistors that are currently used in modern computers, Moore’s Law has held true for the past 50 years. However, the turn of the millennium saw processor design hit several critical performance walls:

  • 内存墙(memory wall):内存技术的进步跟不上时钟速度的提升,使内存成为性能的瓶颈。因此,持续加快 CPU 的执行速度,已不能再提高系统的整体性能。
  • The memory wall: Improvements in memory technology did not keep pace with improvements in clock speed, resulting in memory becoming a bottleneck to performance. As a result, continuously speeding up the execution of a CPU no longer improves its overall system performance.
  • 功耗墙(power wall):增加处理器上的晶体管数量,必然会提高处理器的温度和功耗,进而增加为系统供电和散热所需的成本。随着多核系统的普及,功耗如今已成为计算机系统设计中最主要的考虑因素。
  • The power wall: Increasing the number of transistors on a processor necessarily increases that processor’s temperature and power consumption, which in turn increases the required cost to power and cool the system. With the proliferation of multicore systems, power is now the dominant concern in computer system design.

功耗墙和内存墙迫使计算机体系结构设计者改变了设计处理器的方式。设计者不再通过增加晶体管来提高 CPU 执行单个指令流的速度,而是开始在一个 CPU 上加入多个计算核心(compute core)。计算核心是简化了的处理单元,它们包含的晶体管比传统 CPU 少,通常也更容易制造。在一个 CPU 上组合多个核心,使 CPU 能够同时执行多个独立的指令流。

The power and memory walls caused computer architects to change the way they designed processors. Instead of adding more transistors to increase the speed at which a CPU executes a single stream of instructions, architects began adding multiple compute cores to a CPU. Compute cores are simplified processing units that contain fewer transistors than traditional CPUs and are generally easier to create. Combining multiple cores on one CPU allows the CPU to execute multiple independent streams of instructions at once.

原书警示 · 核心多 != 更好(More cores != better)

人们很容易认为所有核心都是一样的,计算机的核心越多就越好。事实并非一定如此!例如,图形处理器(graphics processing unit,GPU)的核心比 CPU 核心包含的晶体管还要少,并且专门用于涉及向量的特定任务。一个典型的 GPU 可能有 5,000 个甚至更多的 GPU 核心。然而,GPU 核心能执行的操作类型有限,并不总是像 CPU 核心那样适合通用计算。用 GPU 进行计算称为众核(manycore)计算。本章集中讨论多核(multicore)计算。关于众核计算的讨论,见第 15 章。

It may be tempting to assume that all cores are equal and that the more cores a computer has, the better it is. This is not necessarily the case! For example, graphics processing unit (GPU) cores have even fewer transistors than CPU cores, and are specialized for particular tasks involving vectors. A typical GPU can have 5,000 or more GPU cores. However, GPU cores are limited in the types of operations that they can perform and are not always suitable for general-purpose computing like the CPU core. Computing with GPUs is known as manycore computing. In this chapter, we concentrate on multicore computing. See Chapter 15 for a discussion of manycore computing.

仔细看看:有多少个核心?

Taking a Closer Look: How Many Cores?

几乎所有现代计算机系统都有多个核心,包括树莓派这样的小型设备。确定系统上的核心数,对于准确测量多核程序的性能至关重要。在 Linux 和 macOS 计算机上,lscpu 命令可以给出系统体系结构的概要。在下面的例子中,我们展示了在一台示例机器上运行 lscpu 命令的输出(省略了部分输出,以突出关键特征):

Almost all modern computer systems have multiple cores, including small devices like the Raspberry Pi. Identifying the number of cores on a system is critical for accurately measuring the performance of multicore programs. On Linux and macOS computers, the lscpu command provides a summary of a system’s architecture. In the following example, we show the output of the lscpu command when run on a sample machine (some output is omitted to emphasize the key features):

译注 · macOS 上没有 lscpu

原文说 macOS 上也有 lscpu,但译者的 Mac 上并没有这个命令(which lscpu 找不到)。在 macOS 上可以用 sysctl 查询:在 Apple M1 Pro 上,sysctl hw.physicalcpu hw.logicalcpu 都是 8(没有超线程),hw.perflevel0.physicalcpu 为 6、hw.perflevel1.physicalcpu 为 2,即 6 个性能核加 2 个能效核,正是下文「性能核与能效核」旁注所说的异构设计。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ lscpu

Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 8
On-line CPU(s) list: 0-7
Thread(s) per core: 2
Core(s) per socket: 4
Socket(s): 1
Model name: Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz
CPU MHz: 1607.562
CPU max MHz: 3900.0000
CPU min MHz: 1600.0000
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 8192K
...

lscpu 命令给出了许多有用的信息,包括处理器的类型、核心速度和核心数。要计算系统上物理(实际)核心的数目,就把插槽(socket)数乘以每个插槽的核心数。上面的 lscpu 示例输出表明,该系统有一个插槽,每个插槽四个核心,即总共四个物理核心。

The lscpu command gives a lot of useful information, including the type of processors, the core speed, and the number of cores. To calculate the number of physical (or actual) cores on a system, multiply the number of sockets by the number of cores per socket. The sample lscpu output shown above shows that the system has one socket with four cores per socket, or four physical cores in total.

原书旁注 · 超线程(Hyperthreading)

乍一看,上例中的系统似乎总共有八个核心。毕竟,「CPU(s)」字段似乎就是这个意思。然而,这个字段实际表示的是超线程(hyperthreaded,即逻辑)核心的数目,而不是物理核心的数目。超线程,即同时多线程(simultaneous multithreading,SMT),能够在单个核心上高效地处理多个线程。尽管超线程可以缩短程序的整体运行时间,但在超线程核心上,性能的扩展速度不如在物理核心上。不过,如果一个任务空闲下来(例如由于控制冒险),另一个任务仍然可以利用这个核心。简而言之,引入超线程是为了提高进程吞吐量(衡量单位时间内完成的进程数),而不是进程加速比(衡量单个进程运行时间的改善程度)。在接下来的一章中,我们对性能的讨论将主要关注加速比。

At first glance, it may appear that the system in the previous example has eight cores in total. After all, this is what the “CPU(s)” field seems to imply. However, that field actually indicates the number of hyperthreaded (logical) cores, not the number of physical cores. Hyperthreading, or simultaneous multithreading (SMT), enables the efficient processing of multiple threads on a single core. Although hyperthreading can decrease the overall runtime of a program, performance on hyperthreaded cores does not scale at the same rate as on physical cores. However, if one task idles (e.g., due to a control hazard), another task can still utilize the core. In short, hyperthreading was introduced to improve process throughput (which measures the number of processes that complete in a given unit of time) rather than process speedup (which measures the amount of runtime improvement of an individual process). Much of our discussion of performance in the coming chapter will focus on speedup.

原书旁注 · 性能核与能效核(Performance Cores and Efficiency Cores)

在一些较新的体系结构上(例如英特尔第 12 代及更新的处理器),把插槽数乘以核心数和硬件线程数,得到的数字与「CPU(s)」字段中显示的不同(通常更小)。这是怎么回事?答案在于芯片制造商正在开发的新型异构体系结构。例如,从第 12 代处理器开始,英特尔引入了一种由「性能」核心(P 核,P-core)和「能效」核心(E 核,E-core)混合组成的体系结构。这种混合设计的目标,是把较小的后台任务交给较小的、省电的 E 核,让较大的、耗电的 P 核腾出来处理计算密集型任务。Arm 早先推出的 big.LITTLE 移动体系结构,其设计也出自类似的原理。在异构体系结构上,lscpu 的默认输出只显示可用的 P 核;E 核的数目通常可以用「CPU(s)」字段中显示的总核心数减去 P 核数算出。使用 --all 和 --extended 选项调用 lscpu 命令,会显示系统上 P 核和 E 核的完整对应关系,其中 E 核可以通过较低的处理器速度识别出来。

On some newer architectures (such as Intel’s 12th generation processors and newer), multiplying the number of sockets by the numbers of cores and hardware threads yields a different (usually smaller) number than what is shown in the “CPU(s)” field. What’s going on here? The answer lies in the new heterogeneous architectures being developed by chip makers. For example, starting with its 12th generation processors, Intel has introduced an architecture that consists of a mixture of “Performance” cores (P-cores) and “Efficiency” cores (E-cores). The goal of this hybrid design is to delegate smaller background tasks to the smaller power-sipping E-cores, freeing the larger power-hungry P-cores for compute-intensive tasks. A similar principle drives the design of the earlier big.LITTLE mobile architecture introduced by Arm. On heterogeneous architectures, the default output of lscpu only displays the available P-cores; the number of E-cores can typically be computed by subtracting the number of P-cores from the total cores shown in the “CPU(s)” field. Invoking the lscpu command with its --all and --extended flags will display a full mapping of the P-cores and E-cores on a system, with the E-core identifiable by their lower processor speeds.