본문 바로가기
Virtualization/[상용] hyper-v

[WSL2] WSL2에서 GPU 가속화 사용하기

by blackcon 2022. 7. 1.
728x90

A. 환경 정보

  • Host: Windows 10 (version: 21H2, build: 19044.1766)
  • Guest: Ubuntu 20.04 (WSL version2)
  • GPU: NVIDIA GeForce GTX 1050

B. 환경 구축하기

1. WSL 버전 확인

  • cmd.exe에서 아래 명령어 입력하기
  • wsl --list -v
  • 실행결과
  • C:\ >wsl --list -v NAME STATE VERSION * Ubuntu-20.04 Running 2
  • WSL 버전이 1일 경우, 해당 사이트를 참고하여 Update 를 해주세요.

2. WSL 에 적합한 Windows vGPU Driver 설치하기

  • 참고
    • 가상 GPU 를 사용하려면 특정의 드라이버가 필요합니다.
    • 따라서 시스템 드라이버가 최신인 경우라도 반드시 설치하여야 합니다.
  • 그래픽 카드의 제조사를 확인한 후 설치 필요
  • 각 환경에 맞는 드라이버 다운로드 및 설치 완료

3. WSL2 환경인 Ubuntu에 NVIDIA CUDA 설치하기

  • 참고
    • Ubuntu 저장소에서 직접 CUDA 툴킷 패키지를 설치하기 위해 Linux NVIDIA Graphics Driver를 설치할 경우, WSL에서는 작동되지 않습니다.
  • CUDA toolkit 설치 (require version >= 10)
    sudo apt-key del 7fa2af80  
    wget [https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86\_64/cuda-wsl-ubuntu.pin](https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin) sudo apt-key adv --fetch-keys [https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86\_64/3bf863cc.pub](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/](https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/) /”  
    sudo apt update sudo apt -y install cuda

4. Sample Code 컴파일

  • 샘플 코드는 NVIDIA에서 제공하고 있으며, 해당 링크에서 다운받을 수 있다.
  • 테스트 폴더 생성
    • mkdir vgpu
  • 샘플 코드 clone
    • git clone https://github.com/nvidia/cuda-samples
  • 컴파일 할 Sample Code 디렉터리로 이동
    • blackcon@DESKTOP-QQQ9POJ:~/vgpu$ cd cuda-samples/Samples/1_Utilities/deviceQuery
  • 샘플 디렉터리 파일 목록
    blackcon@DESKTOP-QQQ9POJ:~/vgpu/cuda-samples/Samples/1_Utilities/deviceQuery$ ls   
    Makefile README.md deviceQuery_vs2017.sln deviceQuery_vs2019.sln deviceQuery_vs2022.sln NsightEclipse.xml deviceQuery.cpp deviceQuery_vs2017.vcxproj deviceQuery_vs2019.vcxproj deviceQuery_vs2022.vcxproj
  • make를 사용하여 컴파일 실행
blackcon@DESKTOP-QQQ9POJ:~/vgpu/cuda-samples/Samples/1_Utilities/deviceQuery$ make 
/usr/local/cuda/bin/nvcc -ccbin g++ -I../../../Common -m64 --threads 0 --std=c++11 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_86,code=compute_86 -o deviceQuery.o -c deviceQuery.cpp 
nvcc warning : The 'compute_35', 'compute_37', 'sm_35', and 'sm_37' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning). 
/usr/local/cuda/bin/nvcc -ccbin g++ -m64 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_86,code=compute_86 -o deviceQuery deviceQuery.o nvcc warning : The 'compute_35', 'compute_37', 'sm_35', and 'sm_37' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning). 
mkdir -p ../../../bin/x86_64/linux/release cp deviceQuery ../../../bin/x86_64/linux/release

5. Sample Code 실행 결과

마치며

  • 해당 포스팅을 통하여 Windows의 WSL2 환경에서 GPU를 이용한 개발을 할 수 있도록 셋팅을 하였습니다.
  • 이 기술은 머신러닝에 사용될 수 있을 것이며, Windows 11 이후 버전에서는 WSL2를 GUI로 출력시킬 수 있다고 하니 해당 기능에 활용 될 수 있을 듯 합니다.
  • 저는 이 설치를 통하여 WSL2에서 gpu에 접근할 경우, 어떤 flow로 작업되며 최종적으로는 HOST에 어떠한 영향이 일어나는지 분석을 해보고자 합니다.
  • 가능하다면 향 후 포스팅에서 소개드릴 수 있도록 해볼게요.

Reference

728x90