(Demo) NVIDIA NVML Library in Python 3


GeeXLab - Python 3 NVIDIA NVML demo



Downloads

How to run the demo?
Download and unzip GeeXLab and the demopack where you want, launch GeeXLab and drop the demo (main.xml) in GeeXLab. This demo requires OpenGL 2.1 and Python 3.8+.

 
NVIDIA NVML is NVIDIA’s Management Library. NVML allows to get various informations about the graphics hardware and the dynamic library (nvml.dll) is shipped with NVIDIA graphics drivers. Here is a more detailed description:

NVML is a C-based API for monitoring and managing various states of the NVIDIA GPU devices. It provides a direct access to the queries and commands exposed via nvidia-smi. The runtime version of NVML ships with the NVIDIA display driver, and the SDK provides the appropriate header, stub libraries and sample applications. Each new version of NVML is backwards compatible and is intended to be a platform for building 3rd party applications.

Source: NVIDIA Management Library (NVML).

NVML is also available for Python 3 via the nvidia-ml-py package.
The installation of nvidia-ml-py is simple, just type the following command in a terminal:

pip install nvidia-ml-py

Based on the example provided by NVIDIA, I coded a small GeeXLab demo that does basic GPU monitoring (clock speeds, temperature, fan speed) of your NVIDIA GPUs. The demo is available in the Python 3 demopack: geexlab-demopack-python3/nvidia/nvml/main.xml

Here are the code snippets (INIT and FRAME scripts):

INIT script:

#-------------------------------------------------------------------
# NVIDIA NVML
#-------------------------------------------------------------------
 
# Python Bindings for the NVIDIA Management Library
# https://pypi.org/project/nvidia-ml-py/
# pip install nvidia-ml-py
from pynvml import *
 
def GpuMonitoring():
 
    strResult = ''
 
    nvmlInit()
 
    strResult += '  driver version: ' + str(nvmlSystemGetDriverVersion()) + '\n'
 
    deviceCount = nvmlDeviceGetCount()
 
    for i in range(0, deviceCount):
        h = nvmlDeviceGetHandleByIndex(i)
 
        gpu_name = nvmlDeviceGetName(h)
        strResult += '\n  GPU: ' + gpu_name + '\n'
 
        gpu_cores = nvmlDeviceGetNumGpuCores(h)
        strResult += '  - CUDA cores: %d\n' % gpu_cores
 
        gpu_core_clock = nvmlDeviceGetMaxClockInfo(h, NVML_CLOCK_GRAPHICS)
        strResult += '  - core clock: ' + str(gpu_core_clock) + ' MHz\n'
        gpu_mem_clock = nvmlDeviceGetMaxClockInfo(h, NVML_CLOCK_MEM)
        strResult += '  - mem clock: ' + str(gpu_mem_clock) + ' MHz\n'
 
        gpu_temp = nvmlDeviceGetTemperature(h, NVML_TEMPERATURE_GPU)
        strResult += '  - temperature: ' + str(gpu_temp) + '°C\n'
 
        num_fans = nvmlDeviceGetNumFans(h)
        for f in range(0, num_fans):
            fan_speed = nvmlDeviceGetFanSpeed_v2(h, f)
            strResult += '  - fan speed ' + str(f) + ': ' + str(fan_speed) + '%%\n'
 
 
    nvmlShutdown()
 
    return strResult
 
 
nv_gpu_monitoring_str = ""

FRAME script:

imgui_frame_begin()
gh_imgui.set_color(IMGUI_WINDOW_BG_COLOR, 0.1, 0.1, 0.1, 0.6)
gh_imgui.set_color(IMGUI_TEXT_COLOR, 1.0, 1.0, 1.0, 1.0)
 
is_open = imgui_window_begin_pos_size_always("Control panel", winW, winH, 0, 0)
if (is_open == 1):
 
  win_hovered = gh_imgui.is_window_hovered()
 
  gh_imgui.set_color(IMGUI_TEXT_COLOR, 1.0, 1.0, 0.0, 1.0)
  gh_imgui.text("Python version: " + sys.version)
 
  gh_imgui.set_color(IMGUI_TEXT_COLOR, 1.0, 1.0, 1.0, 1.0)
  gh_imgui.spacing()
  gh_imgui.spacing()
  gh_imgui.text("NVIDIA NVMLtest")
 
  gh_imgui.spacing()
  gh_imgui.spacing()
 
  if (gh_imgui.button('GPU monitoring', 180, 30) == 1):
    nv_gpu_monitoring_str = GpuMonitoring()
 
  gh_imgui.text_wrapped(nv_gpu_monitoring_str)
 
  gh_imgui.spacing()
  gh_imgui.spacing()
 
imgui_window_end()
imgui_frame_end()

The NVML python library is shipped with GeeXLab in the python3 folder.





Leave a Comment

Your email address will not be published. Required fields are marked *