Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 585
vinzee Avatar asked Nov 10 '20 21:11

vinzee


People also ask

How do I use all available GPUs PyTorch?

To use data parallelism with PyTorch, you can use the DataParallel class. When using this class, you define your GPU IDs and initialize your network using a Module object with a DataParallel object. Then, when you call your object it can split your dataset into batches that are distributed across your defined GPUs.

How do I check the availability of my graphics card?

Check GPU Availability The easiest way to check if you have access to GPUs is to call torch. cuda. is_available(). If it returns True, it means the system has the Nvidia driver correctly installed.


2 Answers

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>]
like image 52
vinzee Avatar answered Oct 07 '22 04:10

vinzee


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 cuda.is_available() else 'cpu'

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

like image 24
Shaida Muhammad Avatar answered Oct 07 '22 02:10

Shaida Muhammad