How to use GUP to accelerate running code on MATLAB

In recent years, AMD's CPU has been very cost-effective, but Intel's CPU is still recommended. Because Intel has a deep accumulation in scientific computing, MATLAB uses the Intel MKL (Math Kernel Library) mathematical core computing library, and MKL has never been really tested on AMD CPUs, and the calculation speed will be significantly reduced. Therefore, currently only Intel CPUs are recommended to run MATLAB. To speed up the running speed of MATLAB code, in addition to code optimization, vectorized programming and other means, GPU can also be used to accelerate calculations.

The main difference between CPU and GPU

First of all, let's look at the main difference between CPU and GPU: CPU is the central processing unit of the computer , and GPU is the graphics processing unit of the computer.
Please add a picture description
The CPU is a very large-scale integrated circuit , which includes the ALU arithmetic logic operation unit, the Cache cache memory, and the Bus bus. It is the control and computing core of a computer. Its main function is to interpret the instructions issued by the computer and process the big data in the computer software.

GPU is the abbreviation of Image Processor , which is a kind of microprocessor dedicated to image computing work for PC or embedded devices. The work of the GPU is similar to the CPU mentioned above, but it is not exactly the same. It is designed to perform complex mathematical and geometric calculations, and this game has high requirements in this regard, so many game players also Has a deep affection for GPU.

So, CPU and GPU are two completely different things, they just sound similar in name. The CPU and GPU are originally used to process different tasks, so there is a big difference in design, and some tasks are similar to the problems that the GPU was originally used to solve, so the GPU is used for calculation. The calculation speed of the GPU depends on How many elementary school students are hired , and the CPU's computing speed depends on how powerful professors are hired . The professor's ability to handle complex tasks can crush elementary school students, but for less complicated tasks, he still can't stand the crowd. Of course, the current GPU can also do some slightly complicated work, which is equivalent to upgrading to the level of junior high school students and high school students, but it still needs the CPU to feed the data to the mouth to start working. After all, it is still managed by the CPU .

Check CUDA version and download and install

We must first know a basic knowledge, CUDA: It is a computing platform launched by the graphics card manufacturer NVIDIA. CUDA is a general-purpose parallel computing architecture introduced by NVIDIA that enables GPUs to solve complex computing problems. That is to say, the CUDA package must be used to run the MATLAB code with the GPU, and each graphics card can only install the corresponding version of the CUDA package.

How to check the CUDA version on the computer, my computer is win11, check the steps as follows: right click on the desktop ——> show more options —> NVIDIA control panel —> system information in the lower left corner —> components —> products Name
Please add a picture description
You can see that the corresponding version of my computer is 11.6. At this time, you need to install the corresponding version of cuda on your computer. You can check the link to install cuda :
https://developer.nvidia.com/cuda-toolkit-archiveHere
insert image description here
I chose 11.6 for installation. As for which version to choose, there shouldn't be much difference. Generally, it depends on whether this version requires more than the computing power of the GPU.
insert image description hereReaders can choose Linux or Windows to install according to their own computers. After the installation is complete, the environment variables are configured. According to the following steps: System --> System Information --> Advanced System Settings --> Environment Variables --> New. The environment variable configuration is shown in the figure below.
insert image description here

How to check whether CUDA is successfully installed

Check it according to the following steps: win+R --> enter cmd --> enter nvcc -V in the command line to view the version information
insert image description here

How to check the installation location of cuda: win+R --> enter cmd --> enter set cuda in the command line to view the installation location information
insert image description here

Confirm the corresponding version of MATLAB and cuda

After installation, you need to confirm that the MATLAB version corresponds to the cuda version, such as my cuda11.6, but mine does not have a corresponding version, so I chose MATLAB2018b, which does not affect the calculation effect. This is the version as of MATLAB2020b:
insert image description hereMatlab official website query link:
https://ww2.mathworks.cn/help/parallel-computing/gpu-support-by-release.html

View GPU version in MATLAB

At this point, you have installed cuda, and the MATLAB version is correct. You can open MATLAB to view the GPU. If you have not installed cuda, an error will be prompted. Enter at the command line

gpuDevice

The normal situation will appear:
insert image description here
you can see that my graphics card is RTX 3060, and the cuda version is 11.6.

Test gpu to run code on MATLAB

To run code using gpu, you first need to understand a few functions

gpuArray Arrays stored on the GPU
gather Transfer distributed array or gpuArray to local workspace
gpuDevice Query or select a GPU device
arrayfun Applies a function to each element of an array on the GPU

Speed ​​test: Enter the following code to run on the cpu and display the time

tic
G=randn(1e4);
toc

Enter the following code to run on the gpu and display the time

tic
G=randn(1e4,'gpuArray');
toc

The running results are as follows:
insert image description here
It can be seen that the computing speed on the GPU is directly increased by 94.55%.

Matlab sometimes uses GPU to accelerate why the speed is slow

For example, run the following code:

N=rand(2000,2000);
tic
[a1,b1]=eig(N);
t1=toc

tic
N=gpuArray(N);
[a2,b2]=eig(N);
a2=gather(a2);
t2=toc

The running results are as follows:
insert image description here
The main reason is that the GPU is suitable for situations where operations can be parallelized, such as finding the derivative of each element of the matrix, which can be parallelized, and using the GPU is very fast, but eig does not seem to be parallelizable.

Guess you like

Origin blog.csdn.net/weixin_48266700/article/details/129275805