# setting device on GPU if available, else CPU device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print('Using device:', device) print()
#Additional Info when using cuda if device.type == 'cuda': print(torch.cuda.get_device_name(0)) print('Memory Usage:') print('Allocated:', round(torch.cuda.memory_allocated(0)/1024**3,1), 'GB') print('Cached: ', round(torch.cuda.memory_reserved(0)/1024**3,1), 'GB')
device = torch.device('cuda') # 用GPU来运行 a = a.to(device) b = b.to(device)
# 初次调用GPU,需要数据传送,因此比较慢 t0 = time.time() c = torch.matmul(a, b) t2 = time.time() print(a.device, t2 - t0, c.norm(2))
# 这才是GPU处理数据的真实运行时间,当数据量越大,GPU的优势越明显 t0 = time.time() c = torch.matmul(a, b) t2 = time.time() print(a.device, t2 - t0, c.norm(2))
Q&A
补充
安装失败: InvalidArchiveError
1 2 3 4
WARNING conda.gateways.disk.delete:unlink_or_rename_to_trash(139): Could not remove or rename D:\anaconda3\pkgs\pytorch-1.11.0-py3.8_cuda11.3_cudnn8_0\Lib\site-packages\torch\lib\torch_cpu.lib. Please remove this file manually (you may need to reboot to free file handles) WARNING conda.gateways.disk.delete:unlink_or_rename_to_trash(139): Could not remove or rename D:\anaconda3\pkgs\pytorch-1.11.0-py3.8_cuda11.3_cudnn8_0\Lib\site-packages\torch\lib\torch_cpu.lib. Please remove this file manually (you may need to reboot to free file handles)
InvalidArchiveError('Error with archive D:\\anaconda3\\pkgs\\pytorch-1.11.0-py3.8_cuda11.3_cudnn8_0.tar.bz2. You probably need to delete and re-download or re-create this file. Message from libarchive was:\n\nCould not unlink')