Clear Filters
Clear Filters

FFT is suddenly slow down for repeated operations.

14 views (last 30 days)
Hello. I am conducting a simulation with a 3-dimensional matrix using Matlab. While performing FFT about one dimension of the 3-dimensional matrix using GPU for repeated operations, I noticed that the computation speed suddenly slows down. Below is a brief summary of the problematic code.
clear all; close all; clc;
k = 501;
A = zeros(k,k,k,'gpuArray');
B = zeros(k,k,k,'gpuArray');
for n = 1:k
B(:,:,:) = fftshift(fft(ifftshift(A,1),[],1),1);
disp(num2str([n, toc]))
The result of running the code is as follows:
236 7.52e-05
237 7.03e-05
238 7.11e-05
239 7.34e-05
240 7.29e-05
241 7.02e-05
242 0.0235287
243 0.0472623
244 0.0312132
245 0.0444045
246 0.0305904
247 0.0463637
248 0.0309244
As the result, from the 242nd iteration, the FFT operation significantly slows down.
I am curious about the cause of this issue.
The GPU used is 4090.
I would appreciate it if you could let me know what the problem might be.

Accepted Answer

Edric Ellis
Edric Ellis on 16 May 2024
This is due to the asynchronous nature of gpuArray operations. Where possible, operations on the GPU are queued up and run in the background. At some point, we run out of queue, and the operations have to actually run - that's the point at which you see the change in time.
The best way to time operations on the GPU is to use gputimeit which takes into account this asynchronous behaviour. The other option is to call wait on the gpuDevice before calling toc, like this:
d = gpuDevice();
t = tic();
% do stuff on the GPU
time = toc(t)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!