% MATLAB script that defines a random matrix and does FFT
%
% The first FFT is without a GPU
% The second is with the GPU
%
% MATLAB knows to use the GPU the second time because it
%   is passed a type gpuArray as an argument to FFT
% We do the FFT a bunch of times to make using the GPU worth it,
%   or else it spends more time offloading to the GPU 
%   than performning the calculation
%
% This example is meant to provide a general understanding 
%   of MATLAB GPU usage
% Meaningful performance measurements depend on many factors 
%   beyond the scope of this example


% Define a matrix
A1 = rand(16384,16384);

% Just use the compute node, no GPU
tic;
% Do 1000 FFT's
for i = 1:1000
  B2 = fft(A1);
end
time1 = toc;
fprintf('%s\n',"Time to run FFT on the node:")
disp(time1);

% Use GPU 
tic;
A2 = gpuArray(A1);
% Do 1000 FFT's
for i = 1:1000
  % MALAB knows to use GPU FFT because A2 is defined by gpuArray
  B2 = fft(A2);
end
time2 = toc;
fprintf('%s\n',"Time to run FFT on the GPU:")
disp(time2);

% Will be greater than 1 if GPU is faster
speedup = time1/time2
