GPU Support
先确认Docker Desktop的Backend使用的是WSL2,并且Windows、Nvidia驱动的版本足够,随后管理员权限终端执行wsl --update
更新wsl。完成后,终端执行
docker run --rm -it --gpus=all nvcr.io/nvidia/k8s/cuda-sample:nbody nbody -gpu -benchmark
如果GPU可用,则输出类似于
Run "nbody -benchmark [-numbodies=<numBodies>]" to measure performance.
-fullscreen (run n-body simulation in fullscreen mode)
-fp64 (use double precision floating point values for simulation)
-hostmem (stores simulation data in host memory)
-benchmark (run benchmark to measure performance)
-numbodies=<N> (number of bodies (>= 1) to run in simulation)
-device=<d> (where d=0,1,2.... for the CUDA device to use)
-numdevices=<i> (where i=(number of CUDA devices > 0) to use for simulation)
-compare (compares simulation results running once on the default GPU and once on the CPU)
-cpu (run n-body simulation on the CPU)
-tipsy=<file.bin> (load a tipsy model file for simulation)
> NOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.
> Windowed mode
> Simulation data stored in video memory
> Single precision floating point simulation
> 1 Devices used for simulation
MapSMtoCores for SM 7.5 is undefined. Default to use 64 Cores/SM
GPU Device 0: "GeForce RTX 2060 with Max-Q Design" with compute capability 7.5
> Compute 7.5 CUDA device: [GeForce RTX 2060 with Max-Q Design]
30720 bodies, total time for 10 iterations: 69.280 ms
= 136.219 billion interactions per second
= 2724.379 single-precision GFLOP/s at 20 flops per interaction
Use docker in WSL
Docker Desktop的settings-resources-WSL Integration
勾选"Enable intergration with my default WSL distro"以及所需的发行版,点击"Refresh",随用Windows Terminal新打开WSL发行版的终端即可。输入
docker --version
> Docker version 20.10.21, build baeda1f
有版本号说明docker集成正常
Use docker with GPU in WSL
完成以上步骤后,在WSL里同样可以用docker run --gpus=all
的方式在WSL的docker里启用GPU,例如:
# 注意此命令是在WSL而非Windows里执行的
docker run -it --name "nimble-build" --restart always --user "${USER}" --workdir "${PWD}" --env LANG=zh_CN.UTF-8 --env TERM=xterm-256color --gpus=all --volume="$HOME":"$HOME" --detach "nimble-image" /bin/bash
测试nvidia-smi
在docker里是否工作正常:
docker exec -it "nimble-build" nvidia-smi
Tue Dec 13 11:58:05 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 515.65.01 Driver Version: 516.94 CUDA Version: 11.7 |
|-------------------------------+----------------------+----------------------+
| 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 |
| 0% 37C P8 9W / 130W | 932MiB / 8192MiB | 15% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
容器实际上运行在Windows的Docker上(而非WSL系统),因而在Windows终端可以查看到该容器:
# windows powershell内执行
docker container list
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
597d09d2c09c nimble-image "/bin/bash" 9 hours ago Up 3 hours nimble-build
并且也可以用VS Code远程到此容器进行开发。
题外话:修改WSL发行版的密码
WSL发行版的root是没有设置密码的,因而如果忘了当前用户的密码,可以在Windows终端内执行
ubuntu2004.exe config --default-user root # 视WSL发行版的不同,有可能是ubuntu.exe或其它可执行文件,ubuntu2004对应WSL Ubuntu 20.04
切换WSL发行版登录用户为root。以root登录后,用
passwd ${用户}
修改对应用户的密码即可。完成后再用
ubuntu2004.exe config --default-user ${用户}
切换默认用户回去即可。
参考
Docker Desktop WSL 2 backend on Windows | Docker Documentation