How to use GPU to accelerate calculations on MATLAB

(1) First of all, the GPU equipment confirms that Matlab currently only supports Nvidia graphics cards. Want to know if your computer has this capability, run gpuDevice in Matlab.
As long as the data format is in gpuArray format, the calculation process will automatically call the GPU for calculation.

(2) Data transfer between GPU and CPU
①Transfer CPU memory data to GPU memory: gpuArray
1) When using GPU computing, you only need to copy the CPU data to the GPU, and you can do the name of the data If it is modified, it can also be directly re-assigned.
For example: G = gpuArray(M); or, M = gpuArray(M);
Note: Sometimes the GPU is limited by the hardware architecture, and single-precision calculations are much faster than double-precision calculations. At this time, you can consider converting the accuracy while copying, for example: A = gpuArray(single(B));
2) You can also set the data directly on the GPU.
For example: A = zeros(10,'gpuArray');
or like random sequence generation: r = gpuArray.rand(1, 100)% one row, one hundred columns
Run the class function: class®; get ans = gpuArray. It can be seen that this is an array on the gpu.

Basic operations can be run normally on the GPU, which is the same as the normal matrix calculation method. You can use commands for specific operations that can be run. You can view it through "methods(gpuArray)". The specific calculations that Matlab can run on the GPU can be viewed in the appendix. The appendix is ​​the result given by Matlab.

②GPU data return
B = gather (A);
directly use the above command to return the data in the GPU to the CPU.

(3) Use skills
If there is no parallel computing, it is not recommended to use GPU. It is recommended to run the program on a single thread on the CPU. The main frequency of the CPU is still higher. The GPU mainly supports multiple threads to run at the same time.
For example: index = 0;
index = gpuArray(index);
for i = 1: 10000
tic
for j = 1: 100000
index = index + 1;
end
toc
end
disp(index) The
second line of the program above can be used on the GPU Run it on, comment it out and it will run on the CPU.

Refer to the online fan description for reference, thank you for your advice, no commercial use, and can be deleted if infringement.
Reference link:
https://blog.csdn.net/qq_35451572/article/details/79579831
https://blog.csdn.net/C_chuxin/article/details/83027485
https://www.cnblogs.com/yymn/p /8698287.html

Guess you like

Origin blog.csdn.net/yxnooo1/article/details/108400619