|
| 1 | +// Package lib implements the NVIDIA Management Library (NVML) interface. |
| 2 | +// See https://docs.nvidia.com/deploy/nvml-api/nvml-api-reference.html#nvml-api-reference for more details. |
| 3 | +package lib |
| 4 | + |
| 5 | +import ( |
| 6 | + "github.com/NVIDIA/go-nvlib/pkg/nvlib/device" |
| 7 | + nvinfo "github.com/NVIDIA/go-nvlib/pkg/nvlib/info" |
| 8 | + "github.com/NVIDIA/go-nvml/pkg/nvml" |
| 9 | +) |
| 10 | + |
| 11 | +type Library interface { |
| 12 | + NVML() nvml.Interface |
| 13 | + Device() device.Interface |
| 14 | + Info() nvinfo.Interface |
| 15 | + Shutdown() nvml.Return |
| 16 | +} |
| 17 | + |
| 18 | +var _ Library = &nvmlInterface{} |
| 19 | +var _ nvml.Interface = &nvmlInterface{} |
| 20 | + |
| 21 | +type nvmlInterface struct { |
| 22 | + nvml.Interface |
| 23 | + |
| 24 | + dev *devInterface |
| 25 | + info nvinfo.Interface |
| 26 | + |
| 27 | + initReturn *nvml.Return |
| 28 | + propertyExtractor nvinfo.PropertyExtractor |
| 29 | +} |
| 30 | + |
| 31 | +func (n *nvmlInterface) NVML() nvml.Interface { |
| 32 | + return n |
| 33 | +} |
| 34 | + |
| 35 | +func (n *nvmlInterface) Device() device.Interface { |
| 36 | + return n.dev |
| 37 | +} |
| 38 | + |
| 39 | +func (n *nvmlInterface) Info() nvinfo.Interface { |
| 40 | + return n.info |
| 41 | +} |
| 42 | + |
| 43 | +func (n *nvmlInterface) Shutdown() nvml.Return { |
| 44 | + return n.Interface.Shutdown() |
| 45 | +} |
| 46 | + |
| 47 | +// New creates a new NVML instance and returns nil if NVML is not supported. |
| 48 | +func New(opts ...OpOption) Library { |
| 49 | + options := &Op{} |
| 50 | + options.applyOpts(opts) |
| 51 | + |
| 52 | + nvInterface := &nvmlInterface{ |
| 53 | + Interface: options.nvmlLib, |
| 54 | + |
| 55 | + initReturn: options.initReturn, |
| 56 | + propertyExtractor: options.propertyExtractor, |
| 57 | + } |
| 58 | + |
| 59 | + devLib := device.New(nvInterface.Interface) |
| 60 | + nvInterface.dev = &devInterface{ |
| 61 | + Interface: devLib, |
| 62 | + devices: options.devicesToReturn, |
| 63 | + getRemappedRows: options.devGetRemappedRows, |
| 64 | + } |
| 65 | + |
| 66 | + infoOpts := []nvinfo.Option{ |
| 67 | + nvinfo.WithNvmlLib(nvInterface), |
| 68 | + nvinfo.WithDeviceLib(nvInterface.dev), |
| 69 | + } |
| 70 | + if nvInterface.propertyExtractor != nil { |
| 71 | + infoOpts = append(infoOpts, nvinfo.WithPropertyExtractor(nvInterface.propertyExtractor)) |
| 72 | + } |
| 73 | + nvInterface.info = nvinfo.New(infoOpts...) |
| 74 | + |
| 75 | + return nvInterface |
| 76 | +} |
| 77 | + |
| 78 | +func (n *nvmlInterface) Init() nvml.Return { |
| 79 | + if n.initReturn != nil { |
| 80 | + return *n.initReturn |
| 81 | + } |
| 82 | + return n.Interface.Init() |
| 83 | +} |
| 84 | + |
| 85 | +var _ device.Interface = &devInterface{} |
| 86 | + |
| 87 | +type devInterface struct { |
| 88 | + device.Interface |
| 89 | + devices []device.Device |
| 90 | + getRemappedRows func() (int, int, bool, bool, nvml.Return) |
| 91 | +} |
| 92 | + |
| 93 | +func (d *devInterface) GetDevices() ([]device.Device, error) { |
| 94 | + devs := d.devices |
| 95 | + |
| 96 | + var err error |
| 97 | + if len(devs) == 0 { |
| 98 | + devs, err = d.Interface.GetDevices() |
| 99 | + } |
| 100 | + |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + |
| 105 | + updated := make([]device.Device, len(devs)) |
| 106 | + for i, dev := range devs { |
| 107 | + updated[i] = &devDevInterface{ |
| 108 | + Device: dev, |
| 109 | + getRemappedRows: d.getRemappedRows, |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return updated, nil |
| 114 | +} |
| 115 | + |
| 116 | +var _ device.Device = &devDevInterface{} |
| 117 | + |
| 118 | +type devDevInterface struct { |
| 119 | + device.Device |
| 120 | + getRemappedRows func() (int, int, bool, bool, nvml.Return) |
| 121 | +} |
| 122 | + |
| 123 | +func (d *devDevInterface) GetRemappedRows() (int, int, bool, bool, nvml.Return) { |
| 124 | + // no injected remapped rows |
| 125 | + // thus just passthrough to call the underlying device.Device.GetRemappedRows() |
| 126 | + if d.getRemappedRows == nil { |
| 127 | + return d.Device.GetRemappedRows() |
| 128 | + } |
| 129 | + return d.getRemappedRows() |
| 130 | +} |
0 commit comments