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

Return golang errors instead of nvml.Return values #125

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ fmt:
go list -f '{{.Dir}}' $(MODULE)/pkg/... $(MODULE)/gen/... \
| xargs gofmt -s -l -w

# Apply goimports -local to the codebase
goimports:
go list -f {{.Dir}} $(MODULE)/... \
| xargs goimports -local $(MODULE) -w

golangci-lint:
golangci-lint run ./pkg/...

Expand Down
30 changes: 15 additions & 15 deletions examples/compute-processes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@ import (
)

func main() {
ret := nvml.Init()
if ret != nvml.SUCCESS {
log.Fatalf("Unable to initialize NVML: %v", nvml.ErrorString(ret))
err := nvml.Init()
if err != nil {
log.Fatalf("Unable to initialize NVML: %v", err)
}
defer func() {
ret := nvml.Shutdown()
if ret != nvml.SUCCESS {
log.Fatalf("Unable to shutdown NVML: %v", nvml.ErrorString(ret))
err := nvml.Shutdown()
if err != nil {
log.Fatalf("Unable to shutdown NVML: %v", err)
}
}()

count, ret := nvml.DeviceGetCount()
if ret != nvml.SUCCESS {
log.Fatalf("Unable to get device count: %v", nvml.ErrorString(ret))
count, err := nvml.DeviceGetCount()
if err != nil {
log.Fatalf("Unable to get device count: %v", err)
}

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

processInfos, ret := device.GetComputeRunningProcesses()
if ret != nvml.SUCCESS {
log.Fatalf("Unable to get process info for device at index %d: %v", di, nvml.ErrorString(ret))
processInfos, err := device.GetComputeRunningProcesses()
if err != nil {
log.Fatalf("Unable to get process info for device at index %d: %v", di, err)
}
fmt.Printf("Found %d processes on device %d\n", len(processInfos), di)
for pi, processInfo := range processInfos {
Expand Down
41 changes: 25 additions & 16 deletions examples/devices/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,49 @@
package main

import (
"errors"
"fmt"
"log"

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

func main() {
ret := nvml.Init()
if ret != nvml.SUCCESS {
log.Fatalf("Unable to initialize NVML: %v", nvml.ErrorString(ret))
err := nvml.Init()
if err != nil {
log.Fatalf("Unable to initialize NVML: %v", err)
}
defer func() {
ret := nvml.Shutdown()
if ret != nvml.SUCCESS {
log.Fatalf("Unable to shutdown NVML: %v", nvml.ErrorString(ret))
err := nvml.Shutdown()
if err != nil {
log.Fatalf("Unable to shutdown NVML: %v", err)
}
}()

count, ret := nvml.DeviceGetCount()
if ret != nvml.SUCCESS {
log.Fatalf("Unable to get device count: %v", nvml.ErrorString(ret))
count, err := nvml.DeviceGetCount()
if err != nil {
log.Fatalf("Unable to get device count: %v", err)
}

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

uuid, ret := device.GetUUID()
if ret != nvml.SUCCESS {
log.Fatalf("Unable to get uuid of device at index %d: %v", i, nvml.ErrorString(ret))
uuid, err := device.GetUUID()
if err != nil {
log.Fatalf("Unable to get uuid of device at index %d: %v", i, err)
}

fmt.Printf("%v\n", uuid)

current, pending, err := device.GetMigMode()
if errors.Is(err, nvml.ERROR_NOT_SUPPORTED) {
fmt.Printf("MIG is not supported for device %v\n", i)

} else if err != nil {
log.Fatalf("Error getting MIG mode for device at index %d: %v", i, err)
}
fmt.Printf("MIG mode for device %v: current=%v pending=%v\n", i, current, pending)
Comment on lines +56 to +63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this to:

		current, pending, err := device.GetMigMode()
		if err == nil {
			fmt.Printf("MIG mode for device %v: current=%v pending=%v\n", i, current, pending)
			continue
		}
		if errors.Is(err, nvml.ERROR_NOT_SUPPORTED) {
			fmt.Printf("MIG is not supported for device %v\n", i)
			continue
		}
		log.Fatalf("Error getting MIG mode for device at index %d: %v", i, err)

}
}
2 changes: 1 addition & 1 deletion gen/nvml/generateapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func generateInterfaceComment(input GeneratableInterfacePoperties) (string, erro
commentFmt := []string{
"// %s represents the interface for the %s type.",
"//",
"//go:generate moq -out mock/%s.go -pkg mock . %s:%s",
"//go:generate moq -rm -out mock/%s.go -pkg mock . %s:%s",
}

var signature strings.Builder
Expand Down
3 changes: 3 additions & 0 deletions gen/nvml/nvml.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ TRANSLATOR:
- {action: replace, from: "^nvml"}
- {action: replace, from: "_t$"}
- {transform: export}
# We explicitly unexport the SUCCESS and ERROR_ constants to allow for go-native error implementations.
- {action: replace, from: "^SUCCESS", to: "nvmlSUCCESS"}
- {action: replace, from: "^ERROR", to: "nvmlERROR"}
type:
- {action: accept, from: "^nvml"}
- {action: replace, from: "^nvml"}
Expand Down
2 changes: 1 addition & 1 deletion pkg/nvml/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package nvml
// with the list of excluded methods for the Interface type in
// gen/nvml/generateapi.go. In the future we should automate this.
//
//go:generate moq -out mock/extendedinterface.go -pkg mock . ExtendedInterface:ExtendedInterface
//go:generate moq -rm -out mock/extendedinterface.go -pkg mock . ExtendedInterface:ExtendedInterface
type ExtendedInterface interface {
LookupSymbol(string) error
}
Expand Down
62 changes: 31 additions & 31 deletions pkg/nvml/const.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading