PyTorch torch.cuda.synchronize function
PyTorch torch reference manual
torch.cuda.synchronize is a PyTorch function for CUDA synchronization. It waits for all CUDA cores to finish their current tasks, ensuring CPU and GPU operations are synchronized.
Function definition
torch.cuda.synchronize(device=None)
Usage examples
Example
import
torch
import
time
CUDA synchronization example
if
torch.
cuda
.
is_available
(
)
:
Create CUDA tensors and perform operations
x
=
torch.
randn
(
1000
,
1000
)
.
cuda
(
)
y
=
torch.
mm
(
x
,
x
)
Synchronize CUDA operations
torch.
cuda
.
synchronize
(
)
(
"CUDA operations synchronized"
)
else
:
(
"CUDA not available"
)