プリンキピア

第13章計算の性能

13.4.4. CPUs13.4.4 CPU

Central processing units (CPUs) are the centerpiece of any computer. They consist of a number of key components: processor cores that are able to execute machine code, a bus connecting them (the specific topology differs significantly between processor models, generations, and vendors), and caches to allow for higher bandwidth and lower latency memory access than what is possible by reads from main memory. Lastly, almost all modern CPUs contain vector processing units to aid with high performance linear algebra and convolutions, as they are common in media processing and machine learning.
英語のヒント
中央処理装置(CPU)は、どのコンピュータでも中心となる部品である。それはいくつかの主な構成要素からなる。機械語のコードを実行できるプロセッサのコア、それらをつなぐバス(具体的なトポロジーは、プロセッサのモデル、世代、ベンダーによって大きく異なる)、そしてメインメモリからの読み出しで可能なものより高い帯域と低い遅延のメモリアクセスを可能にするキャッシュである。最後に、ほとんどすべての現代のCPUは、メディア処理や機械学習でよく使われる高性能な線形代数と畳み込みを助けるため、ベクトル処理ユニットを含んでいる。
Fig. 13.4.3図13.4.3
Intel Skylake consumer quad-core CPU.
英語のヒント
Intel Skylakeの一般向けクアッドコアCPU。
Fig. 13.4.3 depicts an Intel Skylake consumer-grade quad-core CPU. It has an integrated GPU, caches, and a ringbus connecting the four cores. Peripherals, such as Ethernet, WiFi, Bluetooth, SSD controller, and USB, are either part of the chipset or directly attached (PCIe) to the CPU.
英語のヒント
図13.4.3は、Intel Skylakeの一般向けクアッドコアCPUを描いている。それには、統合されたGPU、キャッシュ、四つのコアをつなぐリングバスがある。イーサネット、WiFi、Bluetooth、SSDコントローラ、USBのような周辺機器は、チップセットの一部であるか、CPUに直接(PCIeで)つながっている。

13.4.4.1. Microarchitecture13.4.4.1 マイクロアーキテクチャ

Each of the processor cores consists of a rather sophisticated set of components. While details differ between generations and vendors, the basic functionality is pretty much standard. The front-end loads instructions and tries to predict which path will be taken (e.g., for control flow). Instructions are then decoded from assembly code to microinstructions. Assembly code is often not the lowest level code that a processor executes. Instead, complex instructions may be decoded into a set of more lower level operations. These are then processed by the actual execution core. Often the latter is capable of performing many operations simultaneously. For instance, the ARM Cortex A77 core of Fig. 13.4.4 is able to perform up to 8 operations simultaneously.
英語のヒント
プロセッサの各コアは、かなり高度な構成要素の組からなる。詳細は世代とベンダーによって異なるが、基本的な機能はほぼ標準的である。フロントエンドは命令を読み込み、(たとえば制御フローについて)どの経路がとられるかを予測しようとする。次に、命令はアセンブリコードからマイクロ命令にデコードされる。アセンブリコードは、しばしばプロセッサが実行する最も低いレベルのコードではない。代わりに、複雑な命令は、もっと低いレベルの演算の組にデコードされうる。それらは次に、実際の実行コアで処理される。しばしば後者は、多くの演算を同時に行える。たとえば、図13.4.4のARM Cortex A77コアは、最大8つの演算を同時に行える。
Fig. 13.4.4図13.4.4
ARM Cortex A77 Microarchitecture.
英語のヒント
ARM Cortex A77のマイクロアーキテクチャ。
This means that efficient programs might be able to perform more than one instruction per clock cycle, provided that they can be carried out independently. Not all units are created equal. Some specialize in integer instructions whereas others are optimized for floating point performance. To increase throughput, the processor might also follow multiple code paths simultaneously in a branching instruction and then discard the results of the branches not taken. This is why branch prediction units matter (on the front-end) such that only the most promising paths are pursued.
英語のヒント
これは、効率的なプログラムは、命令を独立に実行できるなら、クロックサイクルあたり二つ以上の命令を実行できるかもしれないことを意味する。すべてのユニットが同じなのではない。整数の命令に特化したものもあれば、浮動小数点の性能に最適化されたものもある。スループットを上げるため、プロセッサは分岐命令で複数のコードの経路を同時にたどり、とられなかった分岐の結果を捨てることもある。これが、最も有望な経路だけを追うように、(フロントエンドの)分岐予測ユニットが重要である理由である。

13.4.4.2. Vectorization13.4.4.2 ベクトル化

Deep learning is extremely compute-hungry. Hence, to make CPUs suitable for machine learning, one needs to perform many operations in one clock cycle. This is achieved via vector units. They have different names: on ARM they are called NEON, on x86 they (a recent generation) are referred to as AVX2 units. A common aspect is that they are able to perform SIMD (single instruction multiple data) operations. Fig. 13.4.5 shows how 8 short integers can be added in one clock cycle on ARM.
英語のヒント
深層学習は極端に計算を食う。したがって、CPUを機械学習に向いたものにするには、1クロックサイクルで多くの演算を行う必要がある。これはベクトルユニットによって実現される。それらにはさまざまな名前がある。ARMではNEONと呼ばれ、x86では(最近の世代は)AVX2ユニットと呼ばれる。共通する点は、SIMD(単一命令複数データ)演算を行えることである。図13.4.5は、ARMで8つの短い整数を1クロックサイクルでどう足せるかを示している。
Fig. 13.4.5図13.4.5
128 bit NEON vectorization.
英語のヒント
128ビットのNEONによるベクトル化。
Depending on architecture choices, such registers are up to 512 bits long, allowing for the combination of up to 64 pairs of numbers. For instance, we might be multiplying two numbers and adding them to a third, which is also known as a fused multiply-add. Intel’s OpenVino uses these to achieve respectable throughput for deep learning on server-grade CPUs. Note, though, that this number is entirely dwarfed by what GPUs are capable of achieving. For instance, NVIDIA’s RTX 2080 Ti has 4,352 CUDA cores, each of which is capable of processing such an operation at any time.
英語のヒント
アーキテクチャの選択によっては、そうしたレジスタは最大512ビットの長さがあり、最大64組の数を組み合わせられる。たとえば、二つの数を掛けて三つ目に足すことがあり、これは融合積和演算とも呼ばれる。IntelのOpenVinoは、これらを使って、サーバー用のCPUで深層学習のまずまずのスループットを達成している。しかし、この数字は、GPUが達成できるものにはまったく及ばないことに注意してほしい。たとえば、NVIDIAのRTX 2080 Tiには4,352個のCUDAコアがあり、それぞれがいつでもそうした演算を処理できる。

13.4.4.3. Cache13.4.4.3 キャッシュ

Consider the following situation: we have a modest CPU core with 4 cores as depicted in Fig. 13.4.3 above, running at 2 GHz frequency. Moreover, let’s assume that we have an IPC (instructions per clock) count of 1 and that the units have AVX2 with 256-bit width enabled. Let’s furthermore assume that at least one of the registers used for AVX2 operations needs to be retrieved from memory. This means that the CPU consumes 4×256 bit=128 bytes of data per clock cycle. Unless we are able to transfer 2×109×128=256×109 bytes to the processor per second the processing elements are going to starve. Unfortunately the memory interface of such a chip only supports 20–40 GB/s data transfer, i.e., one order of magnitude less. The fix is to avoid loading new data from memory as far as possible and rather to cache it locally on the CPU. This is where caches come in handy. Commonly the following names or concepts are used:
英語のヒント
次の状況を考えよう。上の図13.4.3に描いたような、4コアのそこそこのCPUがあり、2 GHzの周波数で動いている。さらに、IPC(クロックあたりの命令数)が1で、ユニットでは256ビット幅のAVX2が有効になっているとしよう。さらに、AVX2の演算に使うレジスタのうち少なくとも一つは、メモリから取り出す必要があるとしよう。これは、CPUがクロックサイクルあたり4×256 bit=128 bytesのデータを消費することを意味する。毎秒2×109×128=256×109バイトをプロセッサに転送できなければ、処理要素は飢える。残念ながら、そのようなチップのメモリのインターフェースは20〜40 GB/sのデータ転送しかサポートせず、1桁少ない。直し方は、新しいデータをメモリから読み込むのをできるだけ避け、代わりにCPU上にローカルにキャッシュすることである。ここでキャッシュが役に立つ。ふつう、次の名前や概念が使われる。
  • Registers are strictly speaking not part of the cache. They help stage instructions. That said, CPU registers are memory locations that a CPU can access at clock speed without any delay penalty. CPUs have tens of registers. It is up to the compiler (or programmer) to use registers efficiently. For instance the C programming language has a register keyword.
    英語のヒント
    レジスタは、厳密に言えばキャッシュの一部ではない。それらは命令の準備を助ける。とはいえ、CPUのレジスタは、CPUが遅延のペナルティなしにクロックの速さでアクセスできるメモリの場所である。CPUには数十個のレジスタがある。レジスタを効率よく使うのは、コンパイラ(あるいはプログラマ)次第である。たとえば、Cプログラミング言語にはregisterキーワードがある。
  • L1 caches are the first line of defense against high memory bandwidth requirements. L1 caches are tiny (typical sizes might be 32–64 KB) and often split into data and instructions caches. When data is found in the L1 cache, access is very fast. If they cannot be found there, the search progresses down the cache hierarchy.
    英語のヒント
    L1キャッシュは、高いメモリ帯域の要求に対する最初の防衛線である。L1キャッシュはとても小さく(典型的な大きさは32〜64 KBくらい)、しばしばデータ用と命令用のキャッシュに分かれている。データがL1キャッシュで見つかると、アクセスはとても速い。そこで見つからなければ、探索はキャッシュの階層を下っていく。
  • L2 caches are the next stop. Depending on architecture design and processor size they might be exclusive. They might be accessible only by a given core or shared among multiple cores. L2 caches are larger (typically 256–512 KB per core) and slower than L1. Furthermore, to access something in L2 we first need to check to realize that the data is not in L1, which adds a small amount of extra latency.
    英語のヒント
    L2キャッシュは次の立ち寄り先である。アーキテクチャの設計とプロセッサの大きさによっては、排他的なこともある。特定のコアだけがアクセスできることもあれば、複数のコアで共有されることもある。L2キャッシュはL1より大きく(ふつうコアあたり256〜512 KB)、遅い。さらに、L2の何かにアクセスするには、まずデータがL1にないことを確かめる必要があり、それによりわずかな遅延が加わる。
  • L3 caches are shared among multiple cores and can be quite large. AMD’s Epyc 3 server CPUs have a whopping 256 MB of cache spread across multiple chiplets. More typical numbers are in the 4–8 MB range.
    英語のヒント
    L3キャッシュは複数のコアで共有され、かなり大きくなりうる。AMDのEpyc 3サーバーCPUには、複数のチップレットに散らばった、なんと256 MBのキャッシュがある。もっと典型的な数字は4〜8 MBの範囲である。
Predicting which memory elements will be needed next is one of the key optimization parameters in chip design. For instance, it is advisable to traverse memory in a forward direction since most caching algorithms will try to read ahead rather than backwards. Likewise, keeping memory access patterns local is a good way of improving performance.
英語のヒント
次にどのメモリ要素が必要になるかを予測することは、チップの設計での主な最適化のパラメータの一つである。たとえば、ほとんどのキャッシュのアルゴリズムは後ろ向きではなく先読みしようとするので、メモリを前向きにたどるのが望ましい。同じように、メモリアクセスのパターンを局所的に保つことは、性能を上げるよい方法である。
Adding caches is a double-edge sword. On the one hand they ensure that the processor cores do not starve of data. At the same time they increase chip size, using up area that otherwise could have been spent on increasing processing power. Moreover, cache misses can be expensive. Consider the worst case scenario, false sharing, as depicted in Fig. 13.4.6. A memory location is cached on processor 0 when a thread on processor 1 requests the data. To obtain it, processor 0 needs to stop what it is doing, write the information back to main memory and then let processor 1 read it from memory. During this operation both processors wait. Quite potentially such code runs more slowly on multiple processors when compared with an efficient single-processor implementation. This is one more reason for why there is a practical limit to cache sizes (besides their physical size).
英語のヒント
キャッシュを加えることは両刃の剣である。一方では、プロセッサのコアがデータに飢えないようにする。同時に、チップの大きさを増やし、本来なら処理能力を上げるのに使えた面積を使ってしまう。さらに、キャッシュミスは高くつくことがある。図13.4.6に描いた最悪の場合のシナリオ、偽共有を考えよう。プロセッサ1のスレッドがデータを要求したとき、あるメモリの場所がプロセッサ0にキャッシュされている。それを得るには、プロセッサ0はしていることを止め、情報をメインメモリに書き戻し、それからプロセッサ1にメモリから読ませる必要がある。この操作のあいだ、両方のプロセッサが待つ。そのようなコードは、効率的な単一プロセッサの実装と比べて、複数のプロセッサではかえって遅く動くことも十分ありうる。これが、(物理的な大きさのほかに)キャッシュの大きさに実際上の限界があるもう一つの理由である。
Image omitted because its reuse rights could not be verified.権利の確認ができないため画像を割愛。
Fig. 13.4.6図13.4.6
False sharing (image courtesy of Intel).
英語のヒント
偽共有(画像はIntelの提供)。