Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.cuda.synchronize function

PyTorch torch.cuda.synchronize function


PyTorch torch reference manual 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

(

)

   

print

(

"CUDA operations synchronized"

)

else

:

   

print

(

"CUDA not available"

)


PyTorch torch reference manual PyTorch torch reference manual