vvvvv
vvvvv

Reputation: 31619

How do I list all currently available GPUs with pytorch?

I know I can access the current GPU using torch.cuda.current_device(), but how can I get a list of all the currently available GPUs?

Upvotes: 41

Views: 97283

Answers (4)

Shaida Muhammad
Shaida Muhammad

Reputation: 1650

Check how many GPUs are available with PyTorch

import torch

num_of_gpus = torch.cuda.device_count()
print(num_of_gpus)

In case you want to use the first GPU from it.

device = 'cuda:0' if torch.cuda.is_available() else 'cpu'

Replace 0 in the above command with another number If you want to use another GPU.

Upvotes: 11

Thornhale
Thornhale

Reputation: 2376

I know this answer is kind of late. I thought the author of the question asked what devices are actually available to Pytorch not:

  • how many are available (obtainable with device_count()) OR
  • the device manager handle (obtainable with torch.cuda.device(i)) which is what some of the other answers give.

If you want to know what the actual GPU name is (E.g.: NVIDIA 2070 GTI etc.) try the following instead:

import torch
for i in range(torch.cuda.device_count()):
   print(torch.cuda.get_device_properties(i).name)

Note the use of get_device_properties(i) function. This returns a object that looks like this:

_CudaDeviceProperties(name='NVIDIA GeForce RTX 2070', major=8, minor=6, total_memory=12044MB, multi_processor_count=28))

This object contains a property called name. You may optionally drill down directly to the name property to get the human-readable name associated with the GPU in question.

Upvotes: 37

dmtgu
dmtgu

Reputation: 41

Extending the previous replies with device properties

$ python3 -c "import torch; print([(i, torch.cuda.get_device_properties(i)) for i in range(torch.cuda.device_count())])"
[(0, _CudaDeviceProperties(name='NVIDIA GeForce RTX 3060', major=8, minor=6, total_memory=12044MB, multi_processor_count=28))]

Upvotes: 4

vvvvv
vvvvv

Reputation: 31619

You can list all the available GPUs by doing:

>>> import torch
>>> available_gpus = [torch.cuda.device(i) for i in range(torch.cuda.device_count())]
>>> available_gpus
[<torch.cuda.device object at 0x7f2585882b50>]

Upvotes: 25

Related Questions