2022-08-11 14:11:04 +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.
|
|
|
|
*/
|
|
|
|
|
2022-09-16 10:04:59 +00:00
|
|
|
package device
|
2022-08-11 14:11:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvml"
|
|
|
|
)
|
|
|
|
|
2022-09-16 10:04:59 +00:00
|
|
|
// Interface provides the API to the 'device' package
|
2022-08-11 14:11:04 +00:00
|
|
|
type Interface interface {
|
2022-09-16 10:19:17 +00:00
|
|
|
GetDevices() ([]Device, error)
|
|
|
|
GetMigDevices() ([]MigDevice, error)
|
|
|
|
GetMigProfiles() ([]MigProfile, error)
|
|
|
|
NewDevice(d nvml.Device) (Device, error)
|
|
|
|
NewMigDevice(d nvml.Device) (MigDevice, error)
|
2022-09-16 10:04:59 +00:00
|
|
|
NewMigProfile(giProfileID, ciProfileID, ciEngProfileID int, migMemorySizeMB, deviceMemorySizeBytes uint64) (MigProfile, error)
|
|
|
|
ParseMigProfile(profile string) (MigProfile, error)
|
2022-09-16 10:19:17 +00:00
|
|
|
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
|
2022-08-11 14:11:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 10:04:59 +00:00
|
|
|
type devicelib struct {
|
2022-08-11 14:11:04 +00:00
|
|
|
nvml nvml.Interface
|
|
|
|
}
|
|
|
|
|
2022-09-16 10:04:59 +00:00
|
|
|
var _ Interface = &devicelib{}
|
2022-08-11 14:11:04 +00:00
|
|
|
|
2022-09-16 10:04:59 +00:00
|
|
|
// New creates a new instance of the 'device' interface
|
2022-08-11 14:11:04 +00:00
|
|
|
func New(opts ...Option) Interface {
|
2022-09-16 10:04:59 +00:00
|
|
|
d := &devicelib{}
|
2022-08-11 14:11:04 +00:00
|
|
|
for _, opt := range opts {
|
2022-09-16 10:04:59 +00:00
|
|
|
opt(d)
|
2022-08-11 14:11:04 +00:00
|
|
|
}
|
2022-09-16 10:04:59 +00:00
|
|
|
if d.nvml == nil {
|
|
|
|
d.nvml = nvml.New()
|
2022-08-11 14:11:04 +00:00
|
|
|
}
|
2022-09-16 10:04:59 +00:00
|
|
|
return d
|
2022-08-11 14:11:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 10:04:59 +00:00
|
|
|
// WithNvml provides an Option to set the NVML library used by the 'device' interface
|
2022-08-11 14:11:04 +00:00
|
|
|
func WithNvml(nvml nvml.Interface) Option {
|
2022-09-16 10:04:59 +00:00
|
|
|
return func(d *devicelib) {
|
|
|
|
d.nvml = nvml
|
2022-08-11 14:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Option defines a function for passing options to the New() call
|
2022-09-16 10:04:59 +00:00
|
|
|
type Option func(*devicelib)
|