How to check if an NVIDIA GPU is available on my system?

Viewed 3822

Is there a simple way to check if an NVIDIA GPU is available on my system using only standard libraries? I've already seen other answers where they recommend using PyTorch or TensorFlow but that's not what I'm looking for. I'd like to know how to do this on both Windows and Linux. Thanks!

2 Answers

When you have Nvidia drivers installed, the command nvidia-smi outputs a neat table giving you information about your GPU, CUDA, and driver setup.

By checking whether or not this command is present, one can know whether or not an Nvidia GPU is present.

Do note that this code will only work if both an Nvidia GPU and appropriate drivers are installed.

This code should work on both Linux and Windows, and the only library it uses is subprocess, which is a standard library.

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!')
    

Following code shows if cuda available. cuda is in contact with gpu

print(torch.cuda.is_available())
print(torch.backends.cudnn.enabled)
Related