pytorch:测试GPU是否可用

2022-10-05 07:55:49
import torch
flag= torch.cuda.is_available()if flag:print("CUDA可使用")else:print("CUDA不可用")

ngpu=1# Decide which device we want to run on
device= torch.device("cuda:0"if(torch.cuda.is_available()and ngpu>0)else"cpu")print("驱动为:",device)print("GPU型号: ",torch.cuda.get_device_name(0))

在这里插入图片描述

import 	torchimport  timeprint(torch.__version__)print(torch.cuda.is_available())# print('hello, world.')


a= torch.randn(10000,1000)
b= torch.randn(1000,2000)

t0= time.time()
c= torch.matmul(a, b)
t1= time.time()print(a.device, t1- t0, c.norm(2))

device= torch.device('cuda')
a= a.to(device)
b= b.to(device)

t0= time.time()
c= torch.matmul(a, b)
t2= time.time()print(a.device, t2- t0, c.norm(2))

t0= time.time()
c= torch.matmul(a, b)
t2= time.time()print(a.device, t2- t0, c.norm(2))

在这里插入图片描述

  • 作者:James_Bobo
  • 原文链接:https://jamesbobo.blog.csdn.net/article/details/103116903
    更新时间:2022-10-05 07:55:49