Skip to content

Commit 98e003d

Browse files
committed
Replace nvml.Return with error
Signed-off-by: Evan Lezar <[email protected]>
1 parent 511a6ba commit 98e003d

29 files changed

+3686
-3631
lines changed

examples/compute-processes/main.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,31 @@ import (
2424
)
2525

2626
func main() {
27-
ret := nvml.Init()
28-
if ret != nvml.SUCCESS {
29-
log.Fatalf("Unable to initialize NVML: %v", nvml.ErrorString(ret))
27+
err := nvml.Init()
28+
if err != nil {
29+
log.Fatalf("Unable to initialize NVML: %v", err)
3030
}
3131
defer func() {
32-
ret := nvml.Shutdown()
33-
if ret != nvml.SUCCESS {
34-
log.Fatalf("Unable to shutdown NVML: %v", nvml.ErrorString(ret))
32+
err := nvml.Shutdown()
33+
if err != nil {
34+
log.Fatalf("Unable to shutdown NVML: %v", err)
3535
}
3636
}()
3737

38-
count, ret := nvml.DeviceGetCount()
39-
if ret != nvml.SUCCESS {
40-
log.Fatalf("Unable to get device count: %v", nvml.ErrorString(ret))
38+
count, err := nvml.DeviceGetCount()
39+
if err != nil {
40+
log.Fatalf("Unable to get device count: %v", err)
4141
}
4242

4343
for di := 0; di < count; di++ {
44-
device, ret := nvml.DeviceGetHandleByIndex(di)
45-
if ret != nvml.SUCCESS {
46-
log.Fatalf("Unable to get device at index %d: %v", di, nvml.ErrorString(ret))
44+
device, err := nvml.DeviceGetHandleByIndex(di)
45+
if err != nil {
46+
log.Fatalf("Unable to get device at index %d: %v", di, err)
4747
}
4848

49-
processInfos, ret := device.GetComputeRunningProcesses()
50-
if ret != nvml.SUCCESS {
51-
log.Fatalf("Unable to get process info for device at index %d: %v", di, nvml.ErrorString(ret))
49+
processInfos, err := device.GetComputeRunningProcesses()
50+
if err != nil {
51+
log.Fatalf("Unable to get process info for device at index %d: %v", di, err)
5252
}
5353
fmt.Printf("Found %d processes on device %d\n", len(processInfos), di)
5454
for pi, processInfo := range processInfos {

examples/devices/main.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,49 @@
1717
package main
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"log"
2223

2324
"github.com/NVIDIA/go-nvml/pkg/nvml"
2425
)
2526

2627
func main() {
27-
ret := nvml.Init()
28-
if ret != nvml.SUCCESS {
29-
log.Fatalf("Unable to initialize NVML: %v", nvml.ErrorString(ret))
28+
err := nvml.Init()
29+
if err != nil {
30+
log.Fatalf("Unable to initialize NVML: %v", err)
3031
}
3132
defer func() {
32-
ret := nvml.Shutdown()
33-
if ret != nvml.SUCCESS {
34-
log.Fatalf("Unable to shutdown NVML: %v", nvml.ErrorString(ret))
33+
err := nvml.Shutdown()
34+
if err != nil {
35+
log.Fatalf("Unable to shutdown NVML: %v", err)
3536
}
3637
}()
3738

38-
count, ret := nvml.DeviceGetCount()
39-
if ret != nvml.SUCCESS {
40-
log.Fatalf("Unable to get device count: %v", nvml.ErrorString(ret))
39+
count, err := nvml.DeviceGetCount()
40+
if err != nil {
41+
log.Fatalf("Unable to get device count: %v", err)
4142
}
4243

4344
for i := 0; i < count; i++ {
44-
device, ret := nvml.DeviceGetHandleByIndex(i)
45-
if ret != nvml.SUCCESS {
46-
log.Fatalf("Unable to get device at index %d: %v", i, nvml.ErrorString(ret))
45+
device, err := nvml.DeviceGetHandleByIndex(i)
46+
if err != nil {
47+
log.Fatalf("Unable to get device at index %d: %v", i, err)
4748
}
4849

49-
uuid, ret := device.GetUUID()
50-
if ret != nvml.SUCCESS {
51-
log.Fatalf("Unable to get uuid of device at index %d: %v", i, nvml.ErrorString(ret))
50+
uuid, err := device.GetUUID()
51+
if err != nil {
52+
log.Fatalf("Unable to get uuid of device at index %d: %v", i, err)
5253
}
53-
5454
fmt.Printf("%v\n", uuid)
55+
56+
current, pending, err := device.GetMigMode()
57+
if errors.Is(err, nvml.ERROR_NOT_SUPPORTED) {
58+
fmt.Printf("MIG is not supported for device %v\n", i)
59+
60+
} else if err != nil {
61+
log.Fatalf("Error getting MIG mode for device at index %d: %v", i, err)
62+
}
63+
fmt.Printf("MIG mode for device %v: current=%v pending=%v\n", i, current, pending)
5564
}
5665
}

gen/nvml/nvml.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ TRANSLATOR:
5959
- {action: replace, from: "^nvml"}
6060
- {action: replace, from: "_t$"}
6161
- {transform: export}
62+
# We explicitly unexport the SUCCESS and ERROR_ constants to allow for go-native error implementations.
63+
- {action: replace, from: "^SUCCESS", to: "nvmlSUCCESS"}
64+
- {action: replace, from: "^ERROR", to: "nvmlERROR"}
6265
type:
6366
- {action: accept, from: "^nvml"}
6467
- {action: replace, from: "^nvml"}

pkg/nvml/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ package nvml
2222
// with the list of excluded methods for the Interface type in
2323
// gen/nvml/generateapi.go. In the future we should automate this.
2424
//
25-
//go:generate moq -out mock/extendedinterface.go -pkg mock . ExtendedInterface:ExtendedInterface
25+
//go:generate moq -rm -out mock/extendedinterface.go -pkg mock . ExtendedInterface:ExtendedInterface
2626
type ExtendedInterface interface {
2727
LookupSymbol(string) error
2828
}

pkg/nvml/const.go

Lines changed: 31 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)