プリンキピア

第13章計算の性能

13.3.1. Parallel Computation on GPUs13.3.1 GPUでの並列計算

Let’s start by defining a reference workload to test: the run function below performs 10 matrix-matrix multiplications on the device of our choice using data allocated into two variables: x_gpu1 and x_gpu2.
英語のヒント
テストする基準の作業量を定義することから始めよう。下のrun関数は、二つの変数x_gpu1とx_gpu2に割り当てたデータを使い、選んだ装置で10回の行列と行列の積を行う。
devices = d2l.try_all_gpus()
def run(x):
    return [x.mm(x) for _ in range(50)]

x_gpu1 = torch.rand(size=(4000, 4000), device=devices[0])
x_gpu2 = torch.rand(size=(4000, 4000), device=devices[1])
Now we apply the function to the data. To ensure that caching does not play a role in the results we warm up the devices by performing a single pass on either of them prior to measuring. torch.cuda.synchronize() waits for all kernels in all streams on a CUDA device to complete. It takes in a device argument, the device for which we need to synchronize. It uses the current device, given by current_device(), if the device argument is None (default).
英語のヒント
では、この関数をデータに当てはめる。キャッシュが結果に影響しないように、測る前にどちらの装置でも1回パスを実行して装置をウォームアップする。torch.cuda.synchronize()は、CUDAの装置のすべてのストリームのすべてのカーネルが完了するのを待つ。それは、同期する必要のある装置を表すdevice引数をとる。その引数がNone(デフォルト)なら、current_device()で与えられる現在の装置を使う。
run(x_gpu1)
run(x_gpu2)  # Warm-up all devices
torch.cuda.synchronize(devices[0])
torch.cuda.synchronize(devices[1])

with d2l.Benchmark('GPU1 time'):
    run(x_gpu1)
    torch.cuda.synchronize(devices[0])

with d2l.Benchmark('GPU2 time'):
    run(x_gpu2)
    torch.cuda.synchronize(devices[1])
GPU1 time: 0.4660 sec
GPU2 time: 0.4510 sec
If we remove the synchronize statement between both tasks the system is free to parallelize computation on both devices automatically.
英語のヒント
二つのタスクのあいだのsynchronize文を取り除くと、システムは両方の装置で自由に計算を自動的に並列化できる。
with d2l.Benchmark('GPU1 & GPU2'):
    run(x_gpu1)
    run(x_gpu2)
    torch.cuda.synchronize()
GPU1 & GPU2: 0.4659 sec
In the above case the total execution time is less than the sum of its parts, since the deep learning framework automatically schedules computation on both GPU devices without the need for sophisticated code on behalf of the user.
英語のヒント
上の場合、深層学習のフレームワークがユーザーの側で凝ったコードを必要とせずに、両方のGPUの装置で計算を自動的にスケジュールするので、全体の実行時間は各部分の和より短い。