Virtual machine uses host graphics card (hyper-v and WSL2)

My system version

I updated to the latest version to avoid a lot of unnecessary trouble.
Insert image description here

WSL2+Ubuntu20.04+CUDA

Enable WSL2+ to install Ubuntu20.04

This is very simple, please see: https://blog.csdn.net/RenLJ1895/article/details/122741040

Install the WSL Cuda graphics card driver on the host

The latest graphics card drivers already include the WSL Cuda driver, so the graphics card driver has been updated to the latest version and there is no need to reinstall it. In addition: WSL2’s Ubuntu system does not need to install any graphics card related drivers.

My graphics card driver version
Insert image description here

WSL2 Ubuntu install cuda and cudnn

cuda

This is not much different from Ubuntu installation. Check the command address for CUDA installation: https://developer.nvidia.com/cuda-toolkit-archive

The version I chose is 11.6.0, and then I type the following commands one by one in Ubuntu and wait for the installation to complete.

Insert image description here
update ~/.bashrcfile

#把这三行复制到文件底部
export CUDA_HOME=/usr/local/cuda
export PATH=$PATH:$CUDA_HOME/bin
export LD_LIBRARY_PATH=/usr/local/cuda-11.6/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
#更新一下bashrc文件
source ~/.bashrc
 
#更新一下可能需要的依赖
sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
 
#用以下代码检查cuda是否检查成功,注意此处的cuda版本是你在toolkit下载那里决定的,这个版本可能和nvidia-smi显示的版本号不一样,可高可低,这是因为nvidia给cuda开了两个api,这两个api只要差不是太远,基本都可以保证正常运行cuda
nvcc -V

Insert image description here
/usr/lib/wsl/lib/nvidia-smiYou can create a soft link to the nvidia-smi directory in Ubuntu /usr/local/bin, or add this directory to the environment variable for any execution nvidia-smi(the cuda version identified here is 11.7 and should be the version of the host, this is not important)
Insert image description here

hidden

Download address: https://developer.nvidia.com/rdp/cudnn-archive (login required, download the latest 11.x, which is actually 11.6)
Insert image description here
Unzip to the cuda installation directory

#以下是安装命令
tar -zxvf cudnn-自己补全版本号.tgz
sudo cp -P cuda/lib64/* /usr/local/cuda-11.6/lib64/
sudo cp cuda/include/cudnn.h /usr/local/cuda-11.6/include/
 
#为更改读取权限:
sudo chmod a+r /usr/local/cuda-11.6/include/cudnn.h
sudo chmod a+r /usr/local/cuda-11.6/lib64/*
Install TensorFlow

I chose tensorflow-gpu==2.8.1, because TensorFlow after 2.0 comes with keras, so just import tensorflow.keras. So no additional installation is required.

Use built-in test program

cat /usr/local/cuda/samples/README_CUDA_Samples.txt
There is a link to GitHub in this file: https://github.com/NVIDIA/cuda-samples.
Copy the file (if the speed is very slow, you need some means, which I won’t go into) and then you need to wait
cd /usr/local/cuda/samples
git clone https://github.com/NVIDIA/cuda-samples.git
to compile the executable file.
cd cuda-samples
sudo make
After about ten minutes, the generated executable file passed the test in cuda-samples/bin/x86_64/linux/releasethe directory , indicating that there is no problem with the environment.
bin/x86_64/linux/release

Test keras trained model

Download the Remote-WSl plug-in in vscode, and there will be an extra button on the left. Open it and create a directory to write code.
Insert image description here
The effect (you can see that the video memory is full and the training runs normally),
but there are a lot of warning messages. I searched on Google and couldn't find any reason. Forget it, as long as it can run.
Insert image description here

Reference link

1、https://blog.csdn.net/iwanvan/article/details/122119595

hyper-v+win10

Prepare iso image

You need to download the same image as the host system (mainly to ensure that the graphics card driver is exactly the same to ensure that there will be no problems directly copying the host graphics card driver file to the virtual machine)

Install win10 system using hyper-v

There is no difference in the steps

Configuring GPU-Pv using script

Download script: https://github.com/Sam-Chai/gpu-pv-ps1

Modify two positions in the figure. The first is the name of the virtual machine, and the second is the maximum video memory that the virtual machine can use. I set it up the same as the host graphics card
Insert image description here
and then right-clicked and ran PowerShell so that the output information would not be easy to see. The window flashed by

You can use Powershell ISE to run it. The opening location is as follows.
Insert image description here
Drag the gpu.ps1 file directly to this window. Click the green execute button to see the output. If there is no error, the output will be no problem.
Insert image description here

Graphics card driver

Now open the virtual machine directly. Although you can see the graphics card information in the device manager, code 43 is displayed because the driver is indeed available, but you cannot directly use the driver software to download the driver.

Shut down the virtual machine and attach the virtual machine's disk to the host. Double-click the .vhdx file to see it in Disk Management (you can also right-click on Disk Management and select Attach VHD). There may be an error, but you can ignore it because there is still No drive letter is assigned. Just right click and assign a drive letter.

I have already allocated it and mounted it as soon as I opened it, the 500G one in the picture.
Insert image description here
Then copy C:\Windows\System32\DriverStore\FileRepositorythe folder starting with nv in the host directory to the virtual machine hard disk F:\Windows\System32\HostDriverStore\FileRepository(you need to create the directory yourself)
Insert image description here
, then right-click on the red circle in disk management and select Detach VHD , the disk will be unmounted.
Insert image description here
Then when the virtual machine is turned on, you should be able to see that the graphics card is working normally
Insert image description here
. I really don’t know whether it is actually working normally. Let’s try it with Master Lu first.
Virtual machine: Insert image description here
Host:
Insert image description here
The difference is still quite big. Yes, and when running Master Lu on the virtual machine, it got stuck. I had to wait until he finished the test before he could connect to it . It was embarrassing because the virtual machine CPU only had 1 core. I asked why it was so stuck.

When we look at the game performance later, there is no need to run the model. It is more convenient to use WSL or run it directly on the host.

hyper-v install ghost backup file

At first, I didn't want to install the system. I wanted to directly back up the host system to the ghost file. Then use hyper-v to restore it to a virtual machine, but the restored virtual machine is very stuck and basically inoperable. It is estimated that many drivers are incompatible. Please share how to install ghost.

Create a virtual machine

The other steps are the same, that is, when selecting the image, you need to select the ISO image of the PE system. I chose the micro PE image here (download the micro PE, there is an option to generate an ISO file)
Insert image description here

Create a new hard drive

The size of the hard disk is just a little larger than the ghost file. For example, the size of the system file I backed up is 62G. Just choose 63G for the virtual hard disk. Choose a fixed size (because I saw better performance written below). After generating the virtual hard disk file, just
Insert image description here
hang Load this hard disk and then copy the ghost file to the virtual hard disk. The purpose of this is because the system will be restored in the virtual machine and the ghost file cannot be placed on the system disk, so a disk is created to install it. After installing the system, this disk can be uninstalled.

Then add this hard disk to the virtual machine

Add the virtual hard disk containing the ghost file just now
Insert image description here
and then boot up. Normally you should enter the pe system.

Partition and then restore the system

Insert image description here

Because you choose the second generation, the partition format must be GPT, not MBR, and the second generation boot is UEFI. After the restoration is successful, you need to repair the UEFI boot (may not be necessary)

I won’t take screenshots to demonstrate this.

Remove DVD drive

Just select the pointless application. The virtual hard disk containing the ghost file can also be removed.
Insert image description here
If you have also repaired the UEFI boot, then a file will be added to the location of the firmware. Don't worry about this. Click to
Insert image description here
boot and then you should be able to enter the system normally.

Reference link

1、https://www.bilibili.com/read/cv15539810
2、https://www.bilibili.com/video/BV11u411U7KJ

Guess you like

Origin blog.csdn.net/Qwertyuiop2016/article/details/125701347