In this example we will display information about an Nvidia graphics card using GPUtil
GPUtil
is a Python module for getting the GPU status from NVIDA GPUs using nvidia-smi
.
GPUtil
locates all GPUs on the computer, determines their availablity and returns a ordered list of available GPUs.
Availablity is based upon the current memory consumption and load of each GPU.
Installation
pip install gputil
Requirements
NVIDIA GPU with latest NVIDIA driver installed. GPUtil uses the program nvidia-smi
to get the GPU status of all available NVIDIA GPUs.
nvidia-smi
should be installed automatically, when you install your NVIDIA driver.
Supports both Python 2.X and 3.X.
Example
In this example we also use the tabulate package, not required but I use it to display the output in a neat way on the command line. We will talk about tabulate in another article
To install tabulate
pip install tabulate
# GPU information import GPUtil from tabulate import tabulate print("="*40, "GPU Details", "="*40) gpus = GPUtil.getGPUs() list_gpus = [] for gpu in gpus: # get the GPU id gpu_id = gpu.id # name of GPU gpu_name = gpu.name # get % percentage of GPU usage of that GPU gpu_load = f"{gpu.load*100}%" # get free memory in MB format gpu_free_memory = f"{gpu.memoryFree}MB" # get used memory gpu_used_memory = f"{gpu.memoryUsed}MB" # get total memory gpu_total_memory = f"{gpu.memoryTotal}MB" # get GPU temperature in Celsius gpu_temperature = f"{gpu.temperature} °C" list_gpus.append(( gpu_id, gpu_name, gpu_load, gpu_free_memory, gpu_used_memory, gpu_total_memory, gpu_temperature )) print(tabulate(list_gpus, headers=("id", "name", "load", "free mem", "used mem", "total mem", "temperature")))
Link
https://github.com/anderskm/gputil