Main Content

wait

Wait for GPU calculation to complete

Description

example

wait(gpudev) blocks execution in MATLAB® until the GPU device identified by the GPUDevice object gpudev completes its calculations. This can be used before calls to tic and 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 explicitly call wait in that situation.

Examples

collapse all

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 tic and 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 tic.

wait(gpu)
tic

Factorize the matrix into an upper triangular matrix and a permuted lower triangular matrix using the lu function.

[L,U] = lu(A);

Wait for the calculations to finish, then output the elapsed time using toc.

wait(gpu)
toc
Elapsed time is 0.014811 seconds.

Input Arguments

collapse all

GPU device, specified as a GPUDevice object that represents the currently selected device. To obtain a GPUDevice object, use the gpuDevice function.

Extended Capabilities

Version History

Introduced in R2014b