m00nlightsh4dow
m00nlightsh4dow

Reputation: 317

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

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!

Upvotes: 13

Views: 37665

Answers (2)

Briar Campbell
Briar Campbell

Reputation: 377

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

Upvotes: 25

Rostam
Rostam

Reputation: 403

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

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

Upvotes: 7

Related Questions