プリンキピア

第13章計算の性能

13.4.2. Memory13.4.2 メモリ

At its most basic memory is used to store data that needs to be readily accessible. At present CPU RAM is typically of the DDR4 variety, offering 20–25 GB/s bandwidth per module. Each module has a 64-bit-wide bus. Typically pairs of memory modules are used to allow for multiple channels. CPUs have between 2 and 4 memory channels, i.e., they have between 4 0GB/s and 100 GB/s peak memory bandwidth. Often there are two banks per channel. For instance AMD’s Zen 3 Threadripper has 8 slots.
英語のヒント
最も基本的には、メモリはすぐにアクセスできる必要のあるデータを格納するのに使われる。現在、CPUのRAMはふつうDDR4の種類であり、モジュールあたり20〜25 GB/sの帯域を与える。各モジュールには64ビット幅のバスがある。ふつう、複数のチャネルを使えるようにメモリモジュールを2枚1組で使う。CPUには2〜4本のメモリチャネルがある。つまり、最大のメモリ帯域は40 GB/sから100 GB/sである。しばしば、チャネルあたり二つのバンクがある。たとえば、AMDのZen 3 Threadripperには8つのスロットがある。
While these numbers are impressive, indeed, they only tell part of the story. When we want to read a portion from memory we first need to tell the memory module where the information can be found. That is, we first need to send the address to RAM. Once this is accomplished we can choose to read just a single 64 bit record or a long sequence of records. The latter is called burst read. In a nutshell, sending an address to memory and setting up the transfer takes approximately 100 ns (details depend on the specific timing coefficients of the memory chips used), every subsequent transfer takes only 0.2 ns. In short, the first read is 500 times as expensive as subsequent ones! Note that we could perform up to 10,000,000 random reads per second. This suggests that we avoid random memory access as far as possible and use burst reads (and writes) instead.
英語のヒント
これらの数字は確かに印象的だが、話の一部しか語っていない。メモリから一部を読みたいときは、まず情報がどこにあるかをメモリモジュールに伝える必要がある。つまり、まずアドレスをRAMに送る必要がある。これが済むと、64ビットのレコードを一つだけ読むか、長いレコードの列を読むかを選べる。後者はバースト読み出しと呼ばれる。手短に言えば、メモリにアドレスを送って転送を準備するのにおよそ100 nsかかり(詳細は使うメモリチップの具体的なタイミングの係数による)、それに続く転送はそれぞれ0.2 nsしかかからない。要するに、最初の読み出しはその後の読み出しの500倍のコストがかかる!毎秒最大10,000,000回のランダムな読み出しを行えることに注意してほしい。このことは、ランダムなメモリアクセスはできるだけ避け、代わりにバースト読み出し(と書き込み)を使うべきことを示唆している。
Matters are a bit more complex when we take into account that we have multiple banks. Each bank can read memory largely independently. This means two things. On the one hand, the effective number of random reads is up to 4 times higher, provided that they are spread evenly across memory. It also means that it is still a bad idea to perform random reads since burst reads are 4 times faster, too. On the other hand, due to memory alignment to 64 bit boundaries it is a good idea to align any data structures with the same boundaries. Compilers do this pretty much automatically when the appropriate flags are set. Curious readers are encouraged to review a lecture on DRAMs such as the one by Zeshan Chishti.
英語のヒント
複数のバンクがあることを考えに入れると、事情は少し複雑になる。各バンクは、ほぼ独立にメモリを読める。これは二つのことを意味する。一方では、ランダムな読み出しがメモリ全体に均等に散らばっていれば、ランダムな読み出しの実質的な回数は最大4倍になる。それはまた、バースト読み出しも4倍速いので、ランダムな読み出しを行うのはやはりよくない考えであることも意味する。他方では、メモリが64ビットの境界にそろえられているので、どんなデータ構造も同じ境界にそろえるのがよい考えである。適切なフラグを設定すれば、コンパイラはこれをほぼ自動的に行う。好奇心のある読者には、Zeshan Chishtiによるもののような、DRAMについての講義を見直すことを勧める。
GPU memory is subject to even higher bandwidth requirements since they have many more processing elements than CPUs. By and large there are two options to address them. The first is to make the memory bus significantly wider. For instance, NVIDIA’s RTX 2080 Ti has a 352-bit-wide bus. This allows for much more information to be transferred at the same time. Second, GPUs use specific high-performance memory. Consumer-grade devices, such as NVIDIA’s RTX and Titan series typically use GDDR6 chips with over 500 GB/s aggregate bandwidth. An alternative is to use HBM (high bandwidth memory) modules. They use a very different interface and connect directly with GPUs on a dedicated silicon wafer. This makes them very expensive and their use is typically limited to high-end server chips, such as the NVIDIA Volta V100 series of accelerators. Quite unsurprisingly, GPU memory is generally much smaller than CPU memory due to the higher cost of the former. For our purposes, by and large their performance characteristics are similar, just a lot faster. We can safely ignore the details for the purpose of this book. They only matter when tuning GPU kernels for high throughput.
英語のヒント
GPUのメモリは、CPUよりずっと多くの処理要素をもつので、さらに高い帯域の要求を受ける。大まかに言って、それに対処する選択肢は二つある。一つ目は、メモリバスをかなり広くすることである。たとえば、NVIDIAのRTX 2080 Tiには352ビット幅のバスがある。これにより、ずっと多くの情報を同時に転送できる。第二に、GPUは特別な高性能のメモリを使う。NVIDIAのRTXやTitanシリーズのような一般向けの装置は、ふつう、合計帯域が500 GB/sを超えるGDDR6チップを使う。別のやり方は、HBM(高帯域メモリ)モジュールを使うことである。それらはまったく異なるインターフェースを使い、専用のシリコンウェハー上でGPUと直接つながる。このためとても高価になり、その使用はふつう、NVIDIA Volta V100シリーズのアクセラレータのような、高性能なサーバー用のチップに限られる。当然ながら、GPUのメモリは、前者のコストのほうが高いので、一般にCPUのメモリよりずっと小さい。ここでの目的には、大まかに言って、それらの性能の特徴は似ており、ただずっと速いだけである。この本の目的では、詳細は安心して無視できる。それが重要になるのは、高いスループットのためにGPUのカーネルを調整するときだけである。