Speeding up matrix expotentials by using GPU
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
jinlong
le 18 Août 2024
Réponse apportée : jinlong
le 20 Août 2024
Hey all:
I am trying to accelerate the speed of calculation of high dimisional matrix expotential by using GPU, but I find that the speed of calculating them on CPU is faster than GPU, and I can't find where the problem is. The code is:
dev = gpuDevice();
CPU_time = 0;
GPU_time = 0;
for i = 1:10
CPU_matrix = rand(4096, 4096);
GPU_matrix = gpuArray(complex(CPU_matrix));
tic;
Exp_CPU = expm(-1i * CPU_matrix);
CPU_time = CPU_time + toc;
tic;
Exp_GPU = expm(-1i * GPU_matrix);
GPU_time = GPU_time + toc;
end
disp("CPU time:" + string(CPU_time));
disp("GPU time:" + string(GPU_time));
I tested this code using my computer, and its CPU configuration is: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz 2.59 GHz, RAM 16 GB. Its GPU configuration is: NVIDIA GeForce GTX 1650. The final result is:
CPU time:452.1338
GPU time:915.5892
Why the speed of GPU is slower than CPU?
Thanks
0 commentaires
Réponse acceptée
Joss Knight
le 18 Août 2024
Your GTX 1650 is designed for single precision computing. In single precision it has a peak performance of about 3 teraflops, whereas its double precision performance is just 90 gigaflops, a 1/32nd of that. A back-of-the-envelope calculation would give your CPU a performance of around 130 gigaflops in double precision.
In single precision (rand(4096,4096,'single')) your code runs about 8x faster on the GPU than on the CPU. On a card designed for double precision computation such as the Titan V, it can also achieve this improvement in double precision.
5 commentaires
John D'Errico
le 19 Août 2024
This suggests the inexpensive solution may be to just use single precision, convert the matrix to single, and it will go much faster. But remember the cost of doing so. This is a tradeoff between speed and precision. Your computations will lose precision. And for some of what one does in MATLAB, we might afford to use single precision. It depends on how much you need that precision. The result is that a picture will become a little less sharp. Edges a little less crisp. Sharp transitions in a curve may now exhibit visible oscillations. Essentially, you can start to lose the fine detail in what you do. So you will need to watch, make sure the use of single precision does not push you over the edge of acceptability.
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!