User`s guide
9 GPU Computing
9-40
transform of a filter vector, transforms back to the time domain, and stores the result in
an output matrix.
function y = fastConvolution(data,filter)
[m,n] = size(data);
% Zero-pad filter to the column length of data, and transform
filter_f = fft(filter,m);
% Create an array of zeros of the same size and class as data
y = zeros(m,n,'like',data);
% Transform each column of data
for ix = 1:n
af = fft(data(:,ix));
y(:,ix) = ifft(af .* filter_f);
end
end
Execute this function in the CPU on data of a particular size, and measure the execution
time using the MATLAB timeit function. The timeit function takes care of common
benchmarking considerations, such as accounting for startup and overhead.
a = complex(randn(4096,100),randn(4096,100)); % Data input
b = randn(16,1); % Filter input
c = fastConvolution(a,b); % Calculate output
ctime = timeit(@()fastConvolution(a,b)); % Measure CPU time
disp(['Execution time on CPU = ',num2str(ctime)]);
On a sample machine, this code displays the output:
Execution time on CPU = 0.019335
Now execute this function on the GPU. You can do this easily by changing the input data
to be gpuArrays rather than normal MATLAB arrays. The 'like' syntax used when
creating the output inside the function ensures that y will be a gpuArray if data is a
gpuArray.
ga = gpuArray(a); % Move array to GPU
gb = gpuArray(b); % Move filter to GPU
gc = fastConvolution(ga,gb); % Calculate on GPU
gtime = gputimeit(@()fastConvolution(ga,gb)); % Measure GPU time
gerr = max(max(abs(gather(gc)-c))); % Calculate error
disp(['Execution time on GPU = ',num2str(gtime)]);
disp(['Maximum absolute error = ',num2str(gerr)]);