Open4

CUDAで遊ぶ

ksk-stksk-st

Interface 2022年8月号を眺めつつ遊んでみる.

WSL2 Ubuntu 20.04で実施.
nvidia-smiでcuda 11.6を指していたので, 11.6版が取得できるようにinstall手順を公式から探索.

$ nvidia-smi
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 510.68.02    Driver Version: 512.77       CUDA Version: 11.6     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  On   | 00000000:01:00.0  On |                  N/A |
| 27%   36C    P8    11W / 120W |    929MiB /  6144MiB |      9%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

cuda-11-6にしないと11-7が入ってしまっていたのでここだけ変更.
(nvidia-smiが使えるようにsetupしていたはずだが, そのあたりのセットアップ手順は忘れた.)
https://developer.nvidia.com/cuda-11-6-2-download-archive?target_os=Linux&target_arch=x86_64&Distribution=WSL-Ubuntu&target_version=2.0&target_type=deb_network

$ wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
$ sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
$ sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/3bf863cc.pub
$ sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/ /"
$ sudo apt-get update
$ sudo apt-get -y install cuda-11-6

cuda_hello.cuを作成

#include <iostream>

int main(){
    std::cout << "Hello CU World." << std::endl;
}

nvccでbuild

$ nvcc cuda_hello.cu
$ ./a.out
Hello CU World.
ksk-stksk-st

NVIDIAでCUDAのサンプルを提供しているらしい.
https://github.com/NVIDIA/cuda-samples/tree/v11.6

Samples/0_Introduction/asyncAPIをまずは試してみる.

 $ ./asyncAPI
[./asyncAPI] - Starting...
GPU Device 0: "Turing" with compute capability 7.5

CUDA device [NVIDIA GeForce GTX 1660 Ti]
time spent executing by the GPU: 13.75
time spent by CPU in CUDA calls: 0.11
CPU executed 172525 iterations while waiting for GPU to finish
ksk-stksk-st

vulkan使いたくて, WSL2上の使い方を探ってみた.
使えない, という内容が多い中で動かしているプロがいたので確認してみた.
https://gitlab.freedesktop.org/mesa/mesa/-/issues/6318#note_1459269

Preinstall対応

$ sudo apt install meson ninja-build python3-pip libpciaccess-dev llvm-11 libelf-dev libwayland-dev libwayland-egl-backend-dev libx11-dev libxext-dev libxfixes-dev libxcb-glx0-dev libxcb-shm0-dev libx11-xcb-dev libxcb-dri2-0-dev libxcb-dri3-dev libxcb-present-dev libxshmfence-dev libxxf86vm-dev  x11-xserver-utils  libxrandr-dev vulkan-tools
$ pip3 install mako

libdrm_intel >= 2.4.110への対応.(20.04が2.4.107をdefaultにしているので更新する.)

$ wget http://launchpadlibrarian.net/612928838/libdrm-common_2.4.110-1ubuntu1~20.04.1_all.deb
$ wget http://launchpadlibrarian.net/612928851/libdrm2_2.4.110-1ubuntu1~20.04.1_amd64.deb
$ wget http://launchpadlibrarian.net/612928849/libdrm-radeon1_2.4.110-1ubuntu1~20.04.1_amd64.deb
$ wget http://launchpadlibrarian.net/612928848/libdrm-nouveau2_2.4.110-1ubuntu1~20.04.1_amd64.deb
$ wget http://launchpadlibrarian.net/612928847/libdrm-intel1_2.4.110-1ubuntu1~20.04.1_amd64.deb
$ wget http://launchpadlibrarian.net/612928846/libdrm-amdgpu1_2.4.110-1ubuntu1~20.04.1_amd64.deb
$ wget http://launchpadlibrarian.net/612928839/libdrm-dev_2.4.110-1ubuntu1~20.04.1_amd64.deb
$ sudo apt install ./libdrm-common_2.4.110-1ubuntu1~20.04.1_all.deb
$ sudo apt install ./libdrm2_2.4.110-1ubuntu1~20.04.1_amd64.deb
$ sudo apt install ./libdrm-radeon1_2.4.110-1ubuntu1~20.04.1_amd64.deb ./libdrm-nouveau2_2.4.110-1ubuntu1~20.04.1_amd64.deb ./libdrm-intel1_2.4.110-1ubuntu1~20.04.1_amd64.deb ./libdrm-amdgpu1_2.4.110-1ubuntu1~20.04.1_amd64.deb
$ sudo apt install ./libdrm-dev_2.4.110-1ubuntu1~20.04.1_amd64.deb

wayland-protocols >= 1.24への対応.

$ wget http://launchpadlibrarian.net/583356324/wayland-protocols_1.25-1_all.deb
$ sudo apt install ./wayland-protocols_1.25-1_all.deb

mesaを取得

$ git clone https://gitlab.freedesktop.org/mesa/mesa.git
$ cd mesa
$ mkdir build

llvm-11がllvm-configそのものをインストールしてないので,
custom-llvm.iniを作っておく.
https://docs.mesa3d.org/meson.html#llvm

[binaries]
llvm-config = '/usr/bin/llvm-config-11'

実install対応

$ meson setup build -Dvulkan-drivers=microsoft-experimental --native-file custom-llvm.ini
$ cd build
$ sudo ninja install

vulkan動作確認

$ VK_ICD_FILENAMES=/usr/local/share/vulkan/icd.d/dzn_icd.x86_64.json vkcube

vkcube
GPUも使われている.

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 510.68.02    Driver Version: 512.77       CUDA Version: 11.6     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  On   | 00000000:01:00.0  On |                  N/A |
| 27%   37C    P8    12W / 120W |    925MiB /  6144MiB |     35%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A     15243      G   /vkcube                         N/A      |
+-----------------------------------------------------------------------------+
ksk-stksk-st

これ単体でそれなりのボリュームなので記事化しておく予定.