-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from NVIDIA/update_of_dependencies
Dependency from obsolete package was removed
- Loading branch information
Showing
8 changed files
with
438 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
`/internal` | ||
|
||
Code intended for private use only, not for external import. | ||
Note that this layout pattern is enforced by the Go compiler itself. See the Go 1.4 [`release notes`](https://golang.org/doc/go1.4#internalpackages) for more details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package nvmlprovider | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/NVIDIA/go-nvml/pkg/nvml" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var nvmlOnce *sync.Once = new(sync.Once) | ||
|
||
type MIGDeviceInfo struct { | ||
ParentUUID string | ||
GPUInstanceID int | ||
ComputeInstanceID int | ||
} | ||
|
||
// GetMIGDeviceInfoByID returns information about MIG DEVICE by ID | ||
func GetMIGDeviceInfoByID(uuid string) (*MIGDeviceInfo, error) { | ||
var err error | ||
|
||
nvmlOnce.Do(func() { | ||
ret := nvml.Init() | ||
if ret != nvml.SUCCESS { | ||
err = errors.New(nvml.ErrorString(ret)) | ||
logrus.Error("Can not init NVML library") | ||
} | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// 1. With drivers >= R470 (470.42.01+), each MIG device is assigned a GPU UUID starting | ||
// with MIG-<UUID>. | ||
|
||
device, ret := nvml.DeviceGetHandleByUUID(uuid) | ||
if ret == nvml.SUCCESS { | ||
parentDevice, ret := device.GetDeviceHandleFromMigDeviceHandle() | ||
if ret != nvml.SUCCESS { | ||
return nil, errors.New(nvml.ErrorString(ret)) | ||
} | ||
|
||
parentUUID, ret := parentDevice.GetUUID() | ||
if ret != nvml.SUCCESS { | ||
return nil, errors.New(nvml.ErrorString(ret)) | ||
} | ||
|
||
gi, ret := device.GetGpuInstanceId() | ||
if ret != nvml.SUCCESS { | ||
return nil, errors.New(nvml.ErrorString(ret)) | ||
} | ||
|
||
ci, ret := device.GetComputeInstanceId() | ||
if ret != nvml.SUCCESS { | ||
return nil, errors.New(nvml.ErrorString(ret)) | ||
} | ||
|
||
return &MIGDeviceInfo{ | ||
ParentUUID: parentUUID, | ||
GPUInstanceID: gi, | ||
ComputeInstanceID: ci, | ||
}, nil | ||
} | ||
|
||
// 2. With drivers < R470 (e.g. R450 and R460), each MIG device is enumerated by | ||
// specifying the CI and the corresponding parent GI. The format follows this | ||
// convention: MIG-<GPU-UUID>/<GPU instance ID>/<compute instance ID>. | ||
|
||
tokens := strings.SplitN(uuid, "-", 2) | ||
if len(tokens) != 2 || tokens[0] != "MIG" { | ||
return nil, fmt.Errorf("Unable to parse UUID as MIG device") | ||
} | ||
|
||
tokens = strings.SplitN(tokens[1], "/", 3) | ||
if len(tokens) != 3 || !strings.HasPrefix(tokens[0], "GPU-") { | ||
return nil, fmt.Errorf("Unable to parse UUID as MIG device") | ||
} | ||
|
||
gi, err := strconv.Atoi(tokens[1]) | ||
if err != nil { | ||
return nil, fmt.Errorf("Unable to parse UUID as MIG device") | ||
} | ||
|
||
ci, err := strconv.Atoi(tokens[2]) | ||
if err != nil { | ||
return nil, fmt.Errorf("Unable to parse UUID as MIG device") | ||
} | ||
|
||
return &MIGDeviceInfo{ | ||
ParentUUID: tokens[0], | ||
GPUInstanceID: gi, | ||
ComputeInstanceID: ci, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package nvmlprovider | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetMIGDeviceInfoByID_When_DriverVersion_Below_R470(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
uuid string | ||
expectedGPU string | ||
expectedGi int | ||
expectedCi int | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "Successfull Parsing", | ||
uuid: "MIG-GPU-b8ea3855-276c-c9cb-b366-c6fa655957c5/1/5", | ||
expectedGPU: "GPU-b8ea3855-276c-c9cb-b366-c6fa655957c5", | ||
expectedGi: 1, | ||
expectedCi: 5, | ||
}, | ||
{ | ||
name: "Fail, Missing MIG at the beginning of UUID", | ||
uuid: "GPU-b8ea3855-276c-c9cb-b366-c6fa655957c5/1/5", | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "Fail, Missing GPU at the beginning of GPU UUID", | ||
uuid: "MIG-b8ea3855-276c-c9cb-b366-c6fa655957c5/1/5", | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "Fail, GI not parsable", | ||
uuid: "MIG-GPU-b8ea3855-276c-c9cb-b366-c6fa655957c5/xx/5", | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "Fail, CI not a parsable", | ||
uuid: "MIG-GPU-b8ea3855-276c-c9cb-b366-c6fa655957c5/1/xx", | ||
expectedError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
deviceInfo, err := GetMIGDeviceInfoByID(tc.uuid) | ||
if tc.expectedError && err != nil { | ||
return | ||
} | ||
if tc.expectedError && err == nil { | ||
t.Fatalf("Expected an error, but didn't get one: uuid: %v, (gpu: %v, gi: %v, ci: %v)", | ||
tc.uuid, | ||
deviceInfo.ParentUUID, | ||
deviceInfo.GPUInstanceID, | ||
deviceInfo.ComputeInstanceID) | ||
} | ||
if !tc.expectedError && err != nil { | ||
t.Fatalf("Unexpected error: %v, uuid: %v, (gpu: %v, gi: %v, ci: %v)", | ||
err, | ||
tc.uuid, | ||
deviceInfo.ParentUUID, | ||
deviceInfo.GPUInstanceID, | ||
deviceInfo.ComputeInstanceID) | ||
} | ||
|
||
assert.Equal(t, tc.expectedGPU, deviceInfo.ParentUUID, "MIG UUID parsed incorrectly: uuid: %v, (gpu: %v, gi: %v, ci: %v)", | ||
tc.uuid, | ||
deviceInfo.ParentUUID, | ||
deviceInfo.GPUInstanceID, | ||
deviceInfo.ComputeInstanceID) | ||
assert.Equal(t, tc.expectedGi, deviceInfo.GPUInstanceID, "MIG UUID parsed incorrectly: uuid: %v, (gpu: %v, gi: %v, ci: %v)", | ||
tc.uuid, | ||
deviceInfo.ParentUUID, | ||
deviceInfo.GPUInstanceID, | ||
deviceInfo.ComputeInstanceID) | ||
assert.Equal(t, tc.expectedCi, deviceInfo.ComputeInstanceID, "MIG UUID parsed incorrectly: uuid: %v, (gpu: %v, gi: %v, ci: %v)", | ||
tc.uuid, | ||
deviceInfo.ParentUUID, | ||
deviceInfo.GPUInstanceID, | ||
deviceInfo.ComputeInstanceID) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package testutils | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
// RequireLinux checks if | ||
func RequireLinux(t *testing.T) { | ||
t.Helper() | ||
if runtime.GOOS != "linux" { | ||
t.Skipf("Test is not supported on %q", runtime.GOOS) | ||
} | ||
} |
Oops, something went wrong.