Run the AMD SMI lifecycle example#
The lifecycle example is a small starting point for an AMD SMI application. It initializes AMD SMI, displays the library version, converts status codes to readable text, and closes the library before exiting. It does not modify device state or require a GPU processor handle.
py/examples/lifecycle/amdsmi_lifecycle.py is the Python implementation.
Prerequisites
Review the shared prerequisites for AMD SMI Python examples before running this script. This example has no GPU-specific requirements.
Run the example
From the smi-lib repository root, run:
sudo python3 py/examples/lifecycle/amdsmi_lifecycle.py
The library version and status descriptions depend on the installed AMD SMI release. The example always attempts to shut down the library before exiting.
Example flow
The program follows this lifecycle:
from amdsmi import *
try:
init_flags = AmdSmiInitFlags.INIT_ALL_PROCESSORS
amdsmi_init(init_flags)
lib_data = amdsmi_get_lib_version()
status = AmdSmiRetCode.SUCCESS
amdsmi_status_code_to_string(status)
status = AmdSmiRetCode.STATUS_NOT_INIT
amdsmi_status_code_to_string(status)
except AmdSmiException as e:
print(e)
finally:
try:
amdsmi_shut_down()
except AmdSmiException as e:
print(e)
Initialize AMD SMI
amdsmi_init() must be called before APIs that access the AMD SMI library.
The example explicitly passes the all-processors init flag, which is also the
default when no argument is supplied.
The example uses AmdSmiInitFlags.INIT_ALL_PROCESSORS. AmdSmiInitFlags
exposes the following values:
Flag |
Intended processor discovery scope |
|---|---|
|
All processors |
|
AMD CPUs |
|
AMD GPUs |
|
Non-AMD CPUs |
|
Non-AMD GPUs |
|
AMD APUs, combining AMD CPU and GPU flags |
|
AMD NICs |
Keep INIT_ALL_PROCESSORS in this example unless the implementation gains
support for selecting a discovery scope.
The current native implementation does not use the supplied initialization flags, and GPU discovery is the currently supported processor discovery path.
Read the library version
amdsmi_get_lib_version() returns the version of the AMD SMI library:
lib_data = amdsmi_get_lib_version()
print("lib_data['major'] is: {}".format(lib_data['major']))
print("lib_data['minor'] is: {}".format(lib_data['minor']))
print("lib_data['release'] is: {}".format(lib_data['release']))
This is the AMD SMI library version, not the GPU driver or firmware version.
Decode a status code
The Python interface raises AmdSmiException subclasses when an API call
fails. amdsmi_status_code_to_string() converts a known AmdSmiRetCode value
into the corresponding human-readable description.
The example displays descriptions for SUCCESS and STATUS_NOT_INIT without
causing an error.
status = AmdSmiRetCode.SUCCESS
print("{} is: {}".format(
status.name, amdsmi_status_code_to_string(status)))
status = AmdSmiRetCode.STATUS_NOT_INIT
print("{} is: {}".format(
status.name, amdsmi_status_code_to_string(status)))
To decode a status from an AmdSmiLibraryException, use its err_code value:
except AmdSmiLibraryException as e:
status = AmdSmiRetCode(e.err_code)
print("{}: {}".format(
status.name, amdsmi_status_code_to_string(status)))
Shut down AMD SMI
amdsmi_shut_down() releases AMD SMI resources and closes its driver
connection. For each successful amdsmi_init() call, make a matching shutdown
call.
The script places shutdown in a finally block so that cleanup is attempted
whether the preceding operations succeed or raise an exception.
finally:
try:
amdsmi_shut_down()
except AmdSmiException as e:
print(e)
The lifecycle example is a small starting point for an AMD SMI application. It initializes AMD SMI, displays the library version, converts status codes to readable text, and closes the library before exiting. It does not modify device state or require a GPU processor handle.
go/examples/lifecycle/amdsmi_lifecycle.go is the Go implementation.
Prerequisites
Review the shared prerequisites for AMD SMI Go examples before running this program. This example has no GPU-specific requirements.
Run the example
From smi-lib/go/, run:
sudo go run examples/lifecycle/amdsmi_lifecycle.go
The library version and status descriptions depend on the installed AMD SMI release. The example always attempts to shut down the library before exiting.
Example flow
The program follows this lifecycle:
initFlags := amdsmi.AMDSMI_INIT_ALL_PROCESSORS
if err := amdsmi.Init(initFlags); err != nil {
return err
}
defer func() {
if shutdownErr := amdsmi.ShutDown(); shutdownErr != nil {
fmt.Println(shutdownErr)
}
}()
libData, err := amdsmi.GetLibVersion()
if err != nil {
return err
}
if err := printStatusDescription(
amdsmi.AMDSMI_STATUS_SUCCESS,
"SUCCESS",
); err != nil {
return err
}
if err := printStatusDescription(
amdsmi.AMDSMI_STATUS_NOT_INIT,
"STATUS_NOT_INIT",
); err != nil {
return err
}
Initialize AMD SMI
amdsmi.Init() must be called before APIs that access the AMD SMI library.
The example explicitly passes the all-processors init flag, which is also the
default when no argument is supplied.
The example uses amdsmi.AMDSMI_INIT_ALL_PROCESSORS. amdsmi.InitFlags
exposes the following values:
Flag |
Intended processor discovery scope |
|---|---|
|
All processors |
|
AMD CPUs |
|
AMD GPUs |
|
Non-AMD CPUs |
|
Non-AMD GPUs |
|
AMD APUs, combining AMD CPU and GPU flags |
|
AMD NICs |
Keep AMDSMI_INIT_ALL_PROCESSORS in this example unless the implementation
gains support for selecting a discovery scope.
The current native implementation does not use the supplied initialization flags, and GPU discovery is the currently supported processor discovery path.
Read the library version
amdsmi.GetLibVersion() returns the version of the AMD SMI library:
libData, err := amdsmi.GetLibVersion()
if err != nil {
return err
}
fmt.Printf(" lib_data['major'] is: %d\n", libData.Major)
fmt.Printf(" lib_data['minor'] is: %d\n", libData.Minor)
fmt.Printf(" lib_data['release'] is: %d\n", libData.Release)
This is the AMD SMI library version, not the GPU driver or firmware version.
Decode a status code
The Go interface returns an error when an API call fails.
amdsmi.StatusCodeToString() converts a known amdsmi.Status value into the
corresponding human-readable description.
The example displays descriptions for AMDSMI_STATUS_SUCCESS and
AMDSMI_STATUS_NOT_INIT without causing an error.
if err := printStatusDescription(
amdsmi.AMDSMI_STATUS_SUCCESS,
"SUCCESS",
); err != nil {
return err
}
if err := printStatusDescription(
amdsmi.AMDSMI_STATUS_NOT_INIT,
"STATUS_NOT_INIT",
); err != nil {
return err
}
printStatusDescription() calls amdsmi.StatusCodeToString() and prints the
result:
func printStatusDescription(status amdsmi.Status, name string) error {
description, err := amdsmi.StatusCodeToString(status)
if err != nil {
return err
}
fmt.Printf(" %s is: %s\n", name, description)
return nil
}
To decode a status from an amdsmi.StatusError, use its Code field:
var statusErr *amdsmi.StatusError
if errors.As(err, &statusErr) {
description, decodeErr := amdsmi.StatusCodeToString(statusErr.Code)
if decodeErr != nil {
fmt.Fprintln(os.Stderr, err)
} else {
fmt.Fprintln(os.Stderr, description)
}
}
Shut down AMD SMI
amdsmi.ShutDown() releases AMD SMI resources and closes its driver
connection. For each successful amdsmi.Init() call, make a matching shutdown
call.
The program places shutdown in a defer so that cleanup runs when run()
returns, including on error.
defer func() {
if shutdownErr := amdsmi.ShutDown(); shutdownErr != nil {
fmt.Println(shutdownErr)
return
}
fmt.Println("Shutdown complete")
}()