Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: probe all supported mem and graphics frequencies #135

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions examples/frequency-probe/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"fmt"
"log"

"github.com/NVIDIA/go-nvml/pkg/nvml"
)

func main() {
// Initialize NVML
ret := nvml.Init()
if ret != nvml.SUCCESS {
log.Fatalf("Failed to initialize NVML: %v", nvml.ErrorString(ret))
}
defer nvml.Shutdown()

// Get the first device
device, ret := nvml.DeviceGetHandleByIndex(0)
if ret != nvml.SUCCESS {
log.Fatalf("Failed to get device handle: %v", nvml.ErrorString(ret))
}

// Get device name for reference
name, ret := device.GetName()
if ret != nvml.SUCCESS {
log.Printf("Warning: Failed to get device name: %v", nvml.ErrorString(ret))
name = "Unknown GPU"
}

fmt.Printf("GPU: %s\n", name)
fmt.Println("Supported Clock Frequencies:")
fmt.Println("===========================")

// Get supported memory clocks
memCount, memClocks, ret := device.GetSupportedMemoryClocks()
if ret != nvml.SUCCESS {
log.Fatalf("Failed to get supported memory clocks: %v", nvml.ErrorString(ret))
}

fmt.Printf("Found %d supported memory clock speeds\n", memCount)

// Iterate over each memory clock
for _, memClock := range memClocks {
fmt.Printf("\nMemory Clock: %d MHz\n", memClock)
fmt.Println("Graphics Clocks (MHz):")

// Get supported graphics clocks for this memory clock
graphicsCount, graphicsClocks, ret := device.GetSupportedGraphicsClocks(int(memClock))
if ret != nvml.SUCCESS {
log.Printf("Warning: Failed to get graphics clocks for memory clock %d MHz: %v", memClock, nvml.ErrorString(ret))
continue
}

// Print each graphics clock
for _, graphicsClock := range graphicsClocks {
fmt.Printf(" %d MHz\n", graphicsClock)
}
fmt.Printf("Total graphics clocks for this memory speed: %d\n", graphicsCount)
}

// Get current clocks
currentGraphicsClock, ret := device.GetClockInfo(nvml.CLOCK_GRAPHICS)
if ret == nvml.SUCCESS {
fmt.Printf("\nCurrent Graphics Clock: %d MHz\n", currentGraphicsClock)
} else {
log.Printf("Warning: Failed to get current graphics clock: %v", nvml.ErrorString(ret))
}

currentMemClock, ret := device.GetClockInfo(nvml.CLOCK_MEM)
if ret == nvml.SUCCESS {
fmt.Printf("Current Memory Clock: %d MHz\n", currentMemClock)
} else {
log.Printf("Warning: Failed to get current memory clock: %v", nvml.ErrorString(ret))
}
}
26 changes: 18 additions & 8 deletions pkg/nvml/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,24 +557,34 @@ func (device nvmlDevice) GetMaxCustomerBoostClock(clockType ClockType) (uint32,
}

// nvml.DeviceGetSupportedMemoryClocks()
func (l *library) DeviceGetSupportedMemoryClocks(device Device) (int, uint32, Return) {
func (l *library) DeviceGetSupportedMemoryClocks(device Device) (int, []uint32, Return) {
return device.GetSupportedMemoryClocks()
}

func (device nvmlDevice) GetSupportedMemoryClocks() (int, uint32, Return) {
var count, clocksMHz uint32
ret := nvmlDeviceGetSupportedMemoryClocks(device, &count, &clocksMHz)
func (device nvmlDevice) GetSupportedMemoryClocks() (int, []uint32, Return) {
var count uint32
ret := nvmlDeviceGetSupportedMemoryClocks(device, &count, nil)

clocksMHz := make([]uint32, count)

ret = nvmlDeviceGetSupportedMemoryClocks(device, &count, &clocksMHz[0])

return int(count), clocksMHz, ret
}

// nvml.DeviceGetSupportedGraphicsClocks()
func (l *library) DeviceGetSupportedGraphicsClocks(device Device, memoryClockMHz int) (int, uint32, Return) {
func (l *library) DeviceGetSupportedGraphicsClocks(device Device, memoryClockMHz int) (int, []uint32, Return) {
return device.GetSupportedGraphicsClocks(memoryClockMHz)
}

func (device nvmlDevice) GetSupportedGraphicsClocks(memoryClockMHz int) (int, uint32, Return) {
var count, clocksMHz uint32
ret := nvmlDeviceGetSupportedGraphicsClocks(device, uint32(memoryClockMHz), &count, &clocksMHz)
func (device nvmlDevice) GetSupportedGraphicsClocks(memoryClockMHz int) (int, []uint32, Return) {
var count uint32
ret := nvmlDeviceGetSupportedGraphicsClocks(device, uint32(memoryClockMHz), &count, nil)

clocksMHz := make([]uint32, count)

ret = nvmlDeviceGetSupportedGraphicsClocks(device, uint32(memoryClockMHz), &count, &clocksMHz[0])

return int(count), clocksMHz, ret
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/nvml/zz_generated.api.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,8 @@ type Interface interface {
DeviceGetSupportedClocksEventReasons(Device) (uint64, Return)
DeviceGetSupportedClocksThrottleReasons(Device) (uint64, Return)
DeviceGetSupportedEventTypes(Device) (uint64, Return)
DeviceGetSupportedGraphicsClocks(Device, int) (int, uint32, Return)
DeviceGetSupportedMemoryClocks(Device) (int, uint32, Return)
DeviceGetSupportedGraphicsClocks(Device, int) (int, []uint32, Return)
DeviceGetSupportedMemoryClocks(Device) (int, []uint32, Return)
DeviceGetSupportedPerformanceStates(Device) ([]Pstates, Return)
DeviceGetSupportedVgpus(Device) ([]VgpuTypeId, Return)
DeviceGetTargetFanSpeed(Device, int) (int, Return)
Expand Down Expand Up @@ -827,8 +827,8 @@ type Device interface {
GetSupportedClocksEventReasons() (uint64, Return)
GetSupportedClocksThrottleReasons() (uint64, Return)
GetSupportedEventTypes() (uint64, Return)
GetSupportedGraphicsClocks(int) (int, uint32, Return)
GetSupportedMemoryClocks() (int, uint32, Return)
GetSupportedGraphicsClocks(int) (int, []uint32, Return)
GetSupportedMemoryClocks() (int, []uint32, Return)
GetSupportedPerformanceStates() ([]Pstates, Return)
GetSupportedVgpus() ([]VgpuTypeId, Return)
GetTargetFanSpeed(int) (int, Return)
Expand Down