PyTorch torch.cuda.synchronize 函数
torch.cuda.synchronize 是 PyTorch 中用于 CUDA 同步的函数。等待所有 CUDA 核心完成当前任务,确保 CPU 和 GPU 操作同步。
函数定义
torch.cuda.synchronize(device=None)
使用示例
实例
import torch
import time
# CUDA 同步示例
if torch.cuda.is_available():
# 创建 CUDA 张量并执行操作
x = torch.randn(1000, 1000).cuda()
y = torch.mm(x, x)
# 同步 CUDA 操作
torch.cuda.synchronize()
print("CUDA operations synchronized")
else:
print("CUDA not available")
import time
# CUDA 同步示例
if torch.cuda.is_available():
# 创建 CUDA 张量并执行操作
x = torch.randn(1000, 1000).cuda()
y = torch.mm(x, x)
# 同步 CUDA 操作
torch.cuda.synchronize()
print("CUDA operations synchronized")
else:
print("CUDA not available")

Pytorch torch 参考手册