PyTorch torch.cuda.synchronize Function


Pytorch torch 参考手册Pytorch torch Reference Manual

torch.cuda.synchronizeis a function in PyTorch used for CUDA synchronization. It waits for all CUDA cores to complete the current task, ensuring CPU and GPU operations are synchronized.

Function Definition

torch.cuda.synchronize(device=None)

Usage Example

Example

import torch
import time

# CUDA synchronization example
if torch.cuda.is_available():
    # Create a CUDA tensor 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 参考手册Pytorch torch Reference Manual

Other Extensions