Speeding up matrix expotentials by using GPU

조회 수: 9 (최근 30일)
jinlong
jinlong 2024년 8월 18일
답변: jinlong 2024년 8월 20일
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

채택된 답변

Joss Knight
Joss Knight 2024년 8월 18일
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
John D'Errico
John D'Errico 2024년 8월 19일
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.
Paul
Paul 2024년 8월 19일
What are you doing with ExpGPU (or ExpCPU) downstream of the expm compuation? For example, if multiplying it by a vector, or a couple of vectors, maybe expmv would be of use.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

jinlong
jinlong 2024년 8월 20일
Thanks a lot! Now I know where the problem is and maybe I will try to use single precision and some better GPUs. You all really help me a lot.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by