Page 1 of 1

NVIDIA Management Library (NVML) and PureBasic

Posted: Wed Nov 27, 2019 10:26 am
by kvitaliy
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.
https://developer.nvidia.com/nvidia-man ... brary-nvml

Code: Select all

; NVIDIA Management Library (NVML)
;https://developer.nvidia.com/nvidia-management-library-nvml
; PureBasic x64

#NVML_CLOCK_GRAPHICS  = 0;   Graphics clock domain
#NVML_CLOCK_MEM       = 2;   Memory clock domain
Global nvml_Lib.s = GetUserDirectory(#PB_Directory_Programs) + "NVIDIA Corporation\NVSMI\nvml.dll";

Structure nvmlUtilization_t 
  GPU.l
  Mem.l
EndStructure

UtilGPU.nvmlUtilization_t

Prototype nvmlInit()
Prototype nvmlShutdown()
Prototype nvmlDeviceGetCount(pDevCount.l)
Prototype nvmlDeviceGetHandleByIndex (GPU_num.l, GPU_Handle.l)
Prototype nvmlDeviceGetTemperature (GPU_Handle.l, SensorType.l, Temperature.l)
Prototype nvmlDeviceGetUtilizationRates(GPU_Handle1.l, pUtilizationl)
Prototype nvmlDeviceGetClockInfo ( GPU_Handle.l, ClockType.l,  Clock.i)
Prototype nvmlDeviceGetFanSpeed ( GPU_Handle.l, nvmlValue.l)
Prototype nvmlDeviceGetPowerUsage(GPU_Handle.l, nvmlValue.i) 

Global nvmlInit.nvmlInit
Global nvmlShutdown.nvmlShutdown
Global nvmlDeviceGetCount.nvmlDeviceGetCount
Global nvmlDeviceGetHandleByIndex.nvmlDeviceGetHandleByIndex
Global nvmlDeviceGetTemperature.nvmlDeviceGetTemperature
Global nvmlDeviceGetUtilizationRates.nvmlDeviceGetUtilizationRates
Global nvmlDeviceGetClockInfo.nvmlDeviceGetClockInfo
Global nvmlDeviceGetFanSpeed.nvmlDeviceGetFanSpeed
Global nvmlDeviceGetPowerUsage.nvmlDeviceGetPowerUsage


;{ Windows
Enumeration
  #Window_0
EndEnumeration
;}
;{ Gadgets
Enumeration
  #Text_0
  #Text_1
  #Text_2
  #Text_3
  #Text_4
  #Text_5
  #Text_6
  #Text_7
   
  #Text_GPU_name
  #Text_Temperatur
  #Text_Usage_GPU
  #Text_Usage_Memory_GPU
  #Text_GPU_freq
  #Text_Memory_freq
  #Text_FanSpeed
  #Text_Power_GPU
   
  #Text_20
  #Text_21
  #Text_22
  #Text_23
  #Text_24
  #Text_25
  #Text_26
  #Text_27
   
EndEnumeration
;}


Procedure.i nvml_LoadDLL()
  Protected hDLL.i 

  hDLL = OpenLibrary(#PB_Any, nvml_Lib)
  If hDLL <> 0
    nvmlInit = GetFunction(hDLL, "nvmlInit")
    nvmlShutdown= GetFunction(hDLL, "nvmlShutdown")
    nvmlDeviceGetCount= GetFunction(hDLL, "nvmlDeviceGetCount")
    nvmlDeviceGetHandleByIndex= GetFunction(hDLL, "nvmlDeviceGetHandleByIndex")
    nvmlDeviceGetTemperature = GetFunction(hDLL, "nvmlDeviceGetTemperature")
    nvmlDeviceGetUtilizationRates = GetFunction(hDLL, "nvmlDeviceGetUtilizationRates")
    nvmlDeviceGetClockInfo = GetFunction(hDLL, "nvmlDeviceGetClockInfo")
    nvmlDeviceGetFanSpeed  = GetFunction(hDLL, "nvmlDeviceGetFanSpeed")
    nvmlDeviceGetPowerUsage   = GetFunction(hDLL, "nvmlDeviceGetPowerUsage")
  ProcedureReturn hDLL
  EndIf

  ProcedureReturn #False
EndProcedure


; nco2k : https://www.purebasic.fr/english/viewtopic.php?f=13&t=70048
Procedure$ GPUName()
  Protected Result$, DisplayDeviceIndex, display_device.DISPLAY_DEVICE\cb = SizeOf(DISPLAY_DEVICE)
  While EnumDisplayDevices_(0, DisplayDeviceIndex, @display_device, 0)
    If display_device\StateFlags & #DISPLAY_DEVICE_PRIMARY_DEVICE = #DISPLAY_DEVICE_PRIMARY_DEVICE
      Result$ = PeekS(@display_device\DeviceString)
      Break
    EndIf
    DisplayDeviceIndex + 1
  Wend
  ProcedureReturn Result$
EndProcedure


;}
Procedure OpenWindow_Window_0()
  If OpenWindow(#Window_0, 860, 130, 400, 400, "GPU INFO", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
    
      TextGadget(#Text_0, 5, 15, 105, 25, "GPU name  ")
      TextGadget(#Text_1, 5, 45, 205, 25, "Temperature ")
      TextGadget(#Text_2, 5, 75, 205, 25, "Usage GPU  ")
      TextGadget(#Text_3, 5, 105, 205, 25, "Usage Memory GPU")
      TextGadget(#Text_4, 5, 135, 205, 25, "GPU freq")
      TextGadget(#Text_5, 5, 165, 205, 25, "Memory freq")
      TextGadget(#Text_6, 5, 195, 205, 25, "FanSpeed")
      TextGadget(#Text_7, 5, 225, 205, 25, "Power GPU")
 
      
      TextGadget(#Text_GPU_name, 130, 15, 225, 25, "0")
      TextGadget(#Text_Temperatur, 230, 45, 125, 25, "0")
      TextGadget(#Text_Usage_GPU, 230, 75, 125, 25, "0")
      TextGadget(#Text_Usage_Memory_GPU, 230, 105, 125, 25, "0")
      TextGadget(#Text_GPU_freq, 230, 135, 125, 25, "0")
      TextGadget(#Text_Memory_freq, 230, 165, 125, 25, "0")
      TextGadget(#Text_FanSpeed, 230, 195, 125, 25, "0")
      TextGadget(#Text_Power_GPU, 230, 225, 125, 25, "0")
 
      
      TextGadget(#Text_20, 360, 15, 35, 25, "")
      TextGadget(#Text_21, 360, 45, 35, 25, " C")
      TextGadget(#Text_22, 360, 75, 35, 25, " %")
      TextGadget(#Text_23, 360, 105, 35, 25, " %")
      TextGadget(#Text_24, 360, 135, 35, 25, " MGz")
      TextGadget(#Text_25, 360, 165, 35, 25, " MGz")
      TextGadget(#Text_26, 360, 195, 35, 25, " %")
      TextGadget(#Text_27, 360, 225, 35, 25, " mW")
      AddWindowTimer(#Window_0, 1, 250)
   
  EndIf
EndProcedure

OpenWindow_Window_0()

SetGadgetText(#Text_GPU_name,GPUName())

If nvml_LoadDLL()
  nvmlInit()
  nvmlDeviceGetCount(@GPU_count)
   
   If GPU_count>0
     nvmlDeviceGetHandleByIndex(0,@GPU_Handle)
   Else
     MessageRequester("Error", "There is no Data")
     End
  EndIf
  Else
    MessageRequester("Error", "There is no Data") 
    End 
   EndIf  

;{- Event loop
Repeat
  Event = WaitWindowEvent()
  Select Event
    ; ///////////////////
    Case #PB_Event_Gadget
      EventGadget = EventGadget()
      EventType = EventType()
      If EventGadget = #Text_0
      
      EndIf
    Case #PB_Event_Timer
      If EventTimer() = 1 
        nvmlDeviceGetTemperature(GPU_Handle, 0, @Temperature)
        SetGadgetText(#Text_Temperatur, "" + Temperature)
        nvmlDeviceGetUtilizationRates(GPU_Handle,@UtilGPU)
        SetGadgetText(#Text_Usage_GPU, "" + UtilGPU\GPU)
        SetGadgetText(#Text_Usage_Memory_GPU, "" + UtilGPU\Mem)
       
        ; Get information about the frequencies of the processor and GPU memory
        
        If nvmlDeviceGetClockInfo(GPU_Handle, #NVML_CLOCK_GRAPHICS, @nvmlValue) = 0  
           SetGadgetText(#Text_GPU_freq, "" +nvmlValue)
       Else
           SetGadgetText(#Text_GPU_freq, "there is no data" )
      EndIf
      
      If nvmlDeviceGetClockInfo(GPU_Handle, #NVML_CLOCK_MEM,@nvmlValue) = 0
        SetGadgetText(#Text_Memory_freq, "" + nvmlValue)
          Else
        SetGadgetText(#Text_Memory_freq, "there is no data" )
      EndIf
    
      ;  Get NVIDIA-GPU Fan Speed Information
      If nvmlDeviceGetFanSpeed(GPU_Handle, @nvmlValue) = 0
        SetGadgetText(#Text_FanSpeed,"" + nvmlValue)
        Else
        SetGadgetText(#Text_FanSpeed, "there is no data" )
      EndIf

      
      ; Get NVIDIA-GPU Power Consumption Information
      err = nvmlDeviceGetPowerUsage (GPU_Handle, @nvmlValue)
      If  err = 0
        SetGadgetText(#Text_Power_GPU,"" + nvmlValue)
       
      Else
         SetGadgetText(#Text_Power_GPU, "Error № " + err )
      EndIf
      
      EndIf
      
    ; ////////////////////////
    Case #PB_Event_CloseWindow
      EventWindow = EventWindow()
      If EventWindow = #Window_0
        CloseWindow(#Window_0)
        nvmlShutdown()
        Break
      EndIf
  EndSelect
ForEver
;
;}
Image

Re: NVIDIA Management Library (NVML) and PureBasic

Posted: Wed Nov 27, 2019 11:14 am
by CELTIC88
interesting
i have an error

Image

Re: NVIDIA Management Library (NVML) and PureBasic

Posted: Wed Nov 27, 2019 11:33 am
by kvitaliy
CELTIC88 wrote:interesting
i have an error
PureBasic compiler x64?
check GPU_count, GPU_Handle values.

Re: NVIDIA Management Library (NVML) and PureBasic

Posted: Wed Nov 27, 2019 11:33 am
by infratec
Have you download the dll :?:

Re: NVIDIA Management Library (NVML) and PureBasic

Posted: Wed Nov 27, 2019 11:37 am
by kvitaliy
infratec wrote:Have you download the dll :?:
The runtime version of NVML ships with the NVIDIA display driver,
usually in "C:\Program Files\NVIDIA Corporation\NVSMI\nvml.dll"

Re: NVIDIA Management Library (NVML) and PureBasic

Posted: Wed Nov 27, 2019 11:43 am
by CELTIC88
kvitaliy wrote:
CELTIC88 wrote:interesting
i have an error
PureBasic compiler x64?
check GPU_count, GPU_Handle values.

"" ah it work fine in x64, thank for sharing :)

Re: NVIDIA Management Library (NVML) and PureBasic

Posted: Wed Nov 27, 2019 11:46 am
by CELTIC88
i have temperature 55 c ? it's normal?

Re: NVIDIA Management Library (NVML) and PureBasic

Posted: Wed Nov 27, 2019 12:03 pm
by kvitaliy
CELTIC88 wrote:i have temperature 55 c ? it's normal?
GeForce GTX 1080
idle temperature = 42
permissible temperature = 60-84
maximum temperature = 94

Re: NVIDIA Management Library (NVML) and PureBasic

Posted: Wed Nov 27, 2019 12:13 pm
by blueb
CELTIC88...

I'm showing 36 degrees and 15,000 mW and steady.

(see signature)

Thanks kvitaliy 8)

I'm thinking that a high temperature alarm might be a added, possibly even a temperature log.

(It would be handy when I'm flying my Flight Simulator on 3 screens)

Re: NVIDIA Management Library (NVML) and PureBasic

Posted: Thu Nov 28, 2019 8:58 am
by dige
Thx kvitaliy, nice tool!