MATLAB Answers

Why do I need 'wait' commands to assess GPU performance in the Parallel Computing Toolbox 6.0 (R2012a)?

3 views (last 30 days)
The following script was used to compare performance of GPU vs CPU:
dev = gpuDevice;
wait(dev);
Narr=[1024:1024:5*1024];
for i = 1:numel(Narr)
N = Narr(i);
A=rand(N,N);
tic;
x1=fft(A);
cpuTime=toc;
Ag=gpuArray(A);
wait(dev);
tic;
x2=fft(Ag);
wait(dev);
gpuTime1=toc;
fprintf('Size = %d, speedup = %f\n',N,cpuTime/gpuTime1)
end
Why do we need to use the 'wait' function when timing GPU performance while this was not the case in MATLAB 7.13 (R2011b) and prior releases?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 26 Apr 2012
In MATLAB 7.14 (R2012a), GPU operations are asynchronous from CPU operations which means that MATLAB continues while the GPU is running. Thus, when assessing GPU performance, we need to include 'wait' commands to ensure that the GPU completed its work. Without waiting on the GPU, the tic/toc results do not make sense.
In previous releases (R2011b and earlier), MATLAB and the GPU were synchronous, so that any calls to the GPU had to complete before MATLAB proceeded to the next command.
This information is in the R2012a release notes as well:
  4 Comments
Jan
Jan on 1 Jan 2014
For users of this forum, who are not associated to a specific license, the posted link leads to the "Access to archived documentation is a benefit for licensed users of MathWorks products" page. What a pitty.

Sign in to comment.

More Answers (1)

Ben Tordoff
Ben Tordoff on 15 Jan 2014
Edited: MathWorks Support Team on 17 Feb 2021
If you have R2013b or above, an even better way to measure the time taken for a function is to use GPUTIMEIT (for GPU code that doesn't "gather" at the end) or TIMEIT (for all other MATLAB code). These helpers will run the function you give them multiple times and do all the right waiting etc. to get accurate timing results.

Tags

No tags entered yet.

Products


Release

R2012a

Community Treasure Hunt

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

Start Hunting!