Wait for GPU calculation to complete
wait( blocks execution in MATLAB® until the GPU device identified by the
gpudev completes its calculations. This can be used before calls to
toc when timing GPU code that does not gather results back to the workspace.
When gathering results from a GPU using the
gather function, MATLAB waits until all GPU calculations are complete, so you do not need to
wait in that situation.
Wait for GPU to Complete Calculations
GPU operations in MATLAB® run asynchronously, that is they run in the background and are not necessarily complete when a subsequent line of code is run. This is relevant when timing code running on a GPU.
You can time how long a function takes to execute on the GPU using the
gputimeit function, which ensures that all operations on the GPU have finished before recording the time. As
gputimeit runs the function several times, it can be unsuitable for timing particularly long-running code. As an alternative, use the
toc functions, calling
wait before each.
Select your default GPU device.
gpu = gpuDevice; disp(gpu.Name + " GPU selected.")
NVIDIA RTX A5000 GPU selected.
Create a 1000-by-1000 matrix containing random numbers on the GPU.
A = rand(1000,"gpuArray");
Wait for the GPU device to finish creating matrix
A and start timing using
Factorize the matrix into an upper triangular matrix and a permuted lower triangular matrix using the
[L,U] = lu(A);
Wait for the calculations to finish, then output the elapsed time using
Elapsed time is 0.014811 seconds.
Introduced in R2014b