Get Available GPU Devices using PyTorch

Get Available GPU Devices using PyTorch

PyTorch provides support for utilizing Graphics Processing Units (GPUs) to accelerate computations and improve training times for deep learning models. Obtaining available GPU devices can be useful for identifying and verifying the presence of multiple GPUs in the system. By knowing the available GPU devices, computational tasks can be distributed across multiple GPUs. This tutorial shows how to get available GPU devices using PyTorch.

In the following code, we iterate over the range of available GPU devices and create a list of them. Next, we print the name of each GPU device to the console. It provides useful information about the GPUs installed in your system, helping you identify and select the specific GPU(s) you want to utilize for your PyTorch computations.

import torch

devices = [torch.cuda.device(i) for i in range(torch.cuda.device_count())]

for device in devices:
    print(torch.cuda.get_device_name(device))

Here's an example of what the output could look like:

NVIDIA GeForce RTX 3070 Laptop GPU

In this example, the system has one available GPU device.

Leave a Comment

Cancel reply

Your email address will not be published.