Go package usage#
Prerequisites#
Before using the AMD SMI Go bindings make sure that:
Go 1.18+ (64-bit) is installed on your system.
The AMD SMI C library (
libamdsmi.so) is built and installed (e.g. in/usr/local/lib/), or its location is exported viaLD_LIBRARY_PATH.CGO is enabled (default when a C toolchain such as
gccis available).The AMD SR-IOV Host driver is loaded.
The example runs with administrator privileges, using
sudo. AMD SMI requires them to access the driver device.
Most examples query one or more AMD GPUs. The lifecycle example can run without a GPU processor handle. Individual examples identify their additional hardware, driver, operating-system, or build requirements for optional capabilities such as NICs, SR-IOV VFs, XGMI, and node power management.
The Go bindings are a thin idiomatic wrapper around the AMD SMI C library and live under smi-lib/go/:
smi-lib/go/
├── amdsmi/
│ └── amdsmi_interface.go # Go interface (types, constants, functions)
├── examples/
│ ├── example_utils/ # Shared helpers for optional APIs
│ ├── lifecycle/
│ │ └── amdsmi_lifecycle.go
│ ├── discovery/
│ │ └── amdsmi_discovery.go
│ ├── static_identity/
│ │ └── amdsmi_static_identity.go
│ ├── telemetry/
│ │ └── amdsmi_telemetry.go
│ ├── events/
│ │ └── amdsmi_events.go
│ ├── firmware/
│ │ └── amdsmi_firmware.go
│ ├── ras_health/
│ │ └── amdsmi_ras_health.go
│ ├── topology/
│ │ └── amdsmi_topology.go
│ ├── node_power/
│ │ └── amdsmi_node_power.go
│ ├── partitioning/
│ │ └── amdsmi_partitioning.go
│ ├── power_perf_control/
│ │ └── amdsmi_power_perf_control.go
│ ├── sriov_vf/
│ │ └── amdsmi_sriov_vf.go
│ ├── reset/
│ │ └── amdsmi_reset.go
│ └── usage.go # Full-API demo exercising exported symbols
├── go.mod # Go module definition (module: smi-lib/go)
└── README.md # Documentation
Run an example#
Open a terminal and navigate to the smi-lib/go/ directory. Run a topic example with:
sudo go run examples/<example>/amdsmi_<example>.go
The available processors, values, and optional capabilities depend on the host hardware, driver, firmware, and build configuration. Examples report unsupported optional APIs where possible and continue with their remaining operations.
Running an example requires libamdsmi.so to be discoverable at link/run time. If it is installed somewhere other than /usr/local/lib, point LD_LIBRARY_PATH at it, for example:
export LD_LIBRARY_PATH=/path/to/libamdsmi:$LD_LIBRARY_PATH
sudo go run examples/<example>/amdsmi_<example>.go
The comprehensive API demo remains sudo go run examples/usage.go.
Most functions require you to specify the processor handle for which you want to retrieve information. To obtain handles, call amdsmi.GetProcessorHandles(), which returns the list of GPU device handles on the current machine:
handles, err := amdsmi.GetProcessorHandles()
if err != nil {
log.Fatalf("GetProcessorHandles failed: %v", err)
}
fmt.Printf("Found %d processor(s)\n", len(handles))
Once you have a processor handle, you can call any API that works with it. For example:
info, err := amdsmi.GetGpuAsicInfo(handles[0])
if err != nil {
fmt.Printf("GetGpuAsicInfo failed: %v\n", err)
} else {
fmt.Printf("GPU: %s (0x%X)\n", info.MarketName, info.DeviceID)
}
Finally, call amdsmi.ShutDown() to release the SMI library resources and close the connection to the driver:
amdsmi.ShutDown()
Examples#
All examples are documented in AMD SMI examples.
AMD SMI Go example#
An application using AMD SMI must call amdsmi.Init() to initialize the AMD SMI library before all other calls. amdsmi.ShutDown() must be the last call to properly close the connection to the driver and release any resources held by AMD SMI.
A minimal Go program looks like:
package main
import (
"fmt"
"log"
"smi-lib/go/amdsmi"
)
func main() {
if err := amdsmi.Init(amdsmi.AMDSMI_INIT_AMD_GPUS); err != nil {
log.Fatalf("Init failed: %v", err)
}
defer amdsmi.ShutDown()
handles, err := amdsmi.GetProcessorHandles()
if err != nil {
log.Fatalf("GetProcessorHandles failed: %v", err)
}
for i, h := range handles {
info, err := amdsmi.GetGpuAsicInfo(h)
if err != nil {
fmt.Printf("GPU[%d] GetGpuAsicInfo failed: %v\n", i, err)
continue
}
fmt.Printf("GPU[%d] Vendor ID: 0x%04X Device ID: 0x%X Market name: %s\n",
i, info.VendorID, info.DeviceID, info.MarketName)
}
}
Error handling#
All Go wrappers return error as the last return value. Failures from the underlying C library are reported as *amdsmi.StatusError, which exposes both the numeric code (Code) and the status name (Name) mapped from amdsmi_status_t. Use errors.As to inspect the error in detail:
import "errors"
info, err := amdsmi.GetGpuAsicInfo(handle)
if err != nil {
var smiErr *amdsmi.StatusError
if errors.As(err, &smiErr) {
fmt.Printf("SMI error: %s (code %d)\n", smiErr.Name, smiErr.Code)
} else {
fmt.Printf("Unexpected error: %v\n", err)
}
return
}
For the full list of status codes and the complete Go API, refer to the Go API reference.