有没有一种简单的方法,仅使用标准库就能够检查我的系统上是否有NVIDIA GPU? 我已经看到其他回答中推荐使用PyTorch或TensorFlow,但那不是我想要的。我想知道如何在Windows和Linux上进行此操作。谢谢!
如果你已经安装了Nvidia显卡驱动程序,那么命令nvidia-smi
将输出一个整洁的表格,向你提供有关GPU、CUDA和驱动程序设置的信息。
通过检查是否存在此命令,可以知道是否存在Nvidia GPU。
请注意,只有在安装了Nvidia GPU和适当的驱动程序时,此代码才能正常工作。
此代码应该适用于Linux和Windows,并且它所使用的唯一库是subprocess,这是一个标准库。
import subprocess
try:
subprocess.check_output('nvidia-smi')
print('Nvidia GPU detected!')
except Exception: # this command not being found can raise quite a few different errors depending on the configuration
print('No Nvidia GPU in system!')
except Exception
而不是裸的except
!https://dev59.com/yVQI5IYBdhLWcg3w8wSR - ti7以下代码展示了如何检查是否可用 CUDA。CUDA 与 GPU 相关。
print(torch.cuda.is_available())
print(torch.backends.cudnn.enabled)
pip install torch==1.12+cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html
- 蔡宗容torch.cuda.is_available()
和torch.backends.cudnn.enabled
之间的区别,有人能给出解释吗? - 蔡宗容
platform
和ctypes
,你可以自己编写。这似乎是很多其他人已经可能完成并维护的工作,例如在 Windows 上的wmi
。 - JonSG