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?
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?
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>]
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.
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))]