wait - Wait for GPU calculation to complete - MATLAB (original) (raw)
Main Content
Wait for GPU calculation to complete
Syntax
Description
wait([gpudev](#mw%5Ff2b1e484-2951-4f9d-be60-c0990994deb4))
blocks execution in MATLABĀ® until the GPU device identified by the GPUDevice
objectgpudev
completes its calculations. This can be used before calls totic 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
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
.
Factorize the matrix into an upper triangular matrix and a permuted lower triangular matrix using the lu function.
Wait for the calculations to finish, then output the elapsed time using toc
.
Elapsed time is 0.014811 seconds.
Input Arguments
GPU device, specified as a GPUDevice
object that represents the currently selected device. To obtain a GPUDevice
object, use thegpuDevice function.
Extended Capabilities
Version History
Introduced in R2014b