2022-09-28 11:46:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, 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 device
|
|
|
|
|
|
|
|
import (
|
2023-11-15 20:38:54 +00:00
|
|
|
"github.com/NVIDIA/go-nvlib/pkg/nvml"
|
2022-09-28 11:46:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Interface provides the API to the 'device' package
|
|
|
|
type Interface interface {
|
2023-05-22 13:28:38 +00:00
|
|
|
AssertValidMigProfileFormat(profile string) error
|
2022-09-28 11:46:25 +00:00
|
|
|
GetDevices() ([]Device, error)
|
|
|
|
GetMigDevices() ([]MigDevice, error)
|
|
|
|
GetMigProfiles() ([]MigProfile, error)
|
|
|
|
NewDevice(d nvml.Device) (Device, error)
|
2023-01-24 09:37:46 +00:00
|
|
|
NewDeviceByUUID(uuid string) (Device, error)
|
2022-09-28 11:46:25 +00:00
|
|
|
NewMigDevice(d nvml.Device) (MigDevice, error)
|
2023-01-24 09:37:46 +00:00
|
|
|
NewMigDeviceByUUID(uuid string) (MigDevice, error)
|
2022-09-28 11:46:25 +00:00
|
|
|
NewMigProfile(giProfileID, ciProfileID, ciEngProfileID int, migMemorySizeMB, deviceMemorySizeBytes uint64) (MigProfile, error)
|
|
|
|
ParseMigProfile(profile string) (MigProfile, error)
|
|
|
|
VisitDevices(func(i int, d Device) error) error
|
|
|
|
VisitMigDevices(func(i int, d Device, j int, m MigDevice) error) error
|
|
|
|
VisitMigProfiles(func(p MigProfile) error) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type devicelib struct {
|
2023-01-24 09:37:46 +00:00
|
|
|
nvml nvml.Interface
|
|
|
|
skippedDevices map[string]struct{}
|
2023-05-22 13:28:38 +00:00
|
|
|
verifySymbols *bool
|
|
|
|
migProfiles []MigProfile
|
2022-09-28 11:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Interface = &devicelib{}
|
|
|
|
|
|
|
|
// New creates a new instance of the 'device' interface
|
|
|
|
func New(opts ...Option) Interface {
|
|
|
|
d := &devicelib{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(d)
|
|
|
|
}
|
|
|
|
if d.nvml == nil {
|
|
|
|
d.nvml = nvml.New()
|
|
|
|
}
|
2023-05-22 13:28:38 +00:00
|
|
|
if d.verifySymbols == nil {
|
|
|
|
verify := true
|
|
|
|
d.verifySymbols = &verify
|
|
|
|
}
|
2023-01-24 09:37:46 +00:00
|
|
|
if d.skippedDevices == nil {
|
|
|
|
WithSkippedDevices(
|
|
|
|
"DGX Display",
|
|
|
|
"NVIDIA DGX Display",
|
|
|
|
)(d)
|
|
|
|
}
|
2022-09-28 11:46:25 +00:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithNvml provides an Option to set the NVML library used by the 'device' interface
|
|
|
|
func WithNvml(nvml nvml.Interface) Option {
|
|
|
|
return func(d *devicelib) {
|
|
|
|
d.nvml = nvml
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-22 13:28:38 +00:00
|
|
|
// WithVerifySymbols provides an option to toggle whether to verify select symbols exist in dynamic libraries before calling them
|
|
|
|
func WithVerifySymbols(verify bool) Option {
|
|
|
|
return func(d *devicelib) {
|
|
|
|
d.verifySymbols = &verify
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 09:37:46 +00:00
|
|
|
// WithSkippedDevices provides an Option to set devices to be skipped by model name
|
|
|
|
func WithSkippedDevices(names ...string) Option {
|
|
|
|
return func(d *devicelib) {
|
|
|
|
if d.skippedDevices == nil {
|
|
|
|
d.skippedDevices = make(map[string]struct{})
|
|
|
|
}
|
|
|
|
for _, name := range names {
|
|
|
|
d.skippedDevices[name] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-28 11:46:25 +00:00
|
|
|
// Option defines a function for passing options to the New() call
|
|
|
|
type Option func(*devicelib)
|