Skip to content

Commit d4b331f

Browse files
committed
Add Index type to nvcaps
Signed-off-by: Evan Lezar <[email protected]>
1 parent f3b730c commit d4b331f

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

internal/nvcaps/nvcaps.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ const (
3636
nvcapsDevicePath = "/dev/nvidia-caps"
3737
)
3838

39+
// An Index represents a gpu, ci, or gi index.
40+
// We use uint32 as this typically maps to a device minor number.
41+
type Index uint32
42+
3943
// MigMinor represents the minor number of a MIG device
40-
type MigMinor uint32
44+
type MigMinor Index
4145

4246
// MigCap represents the path to a MIG cap file.
4347
// These are listed in /proc/driver/nvidia-caps/mig-minors and have one of the
@@ -53,30 +57,30 @@ type MigCaps map[MigCap]MigMinor
5357

5458
// NewGPUInstanceCap creates a MigCap for the specified MIG GPU instance.
5559
// A GPU instance is uniquely defined by the GPU minor number and GI instance ID.
56-
func NewGPUInstanceCap(gpu, gi int) MigCap {
60+
func NewGPUInstanceCap[T uint32 | int | Index](gpu, gi T) MigCap {
5761
return MigCap(fmt.Sprintf("gpu%d/gi%d/access", gpu, gi))
5862
}
5963

6064
// NewComputeInstanceCap creates a MigCap for the specified MIG Compute instance.
6165
// A GPU instance is uniquely defined by the GPU minor number, GI instance ID, and CI instance ID.
62-
func NewComputeInstanceCap(gpu, gi, ci int) MigCap {
66+
func NewComputeInstanceCap[T uint32 | int | Index](gpu, gi, ci T) MigCap {
6367
return MigCap(fmt.Sprintf("gpu%d/gi%d/ci%d/access", gpu, gi, ci))
6468
}
6569

6670
// FilterForGPU limits the MIG Caps to those associated with a particular GPU.
67-
func (m MigCaps) FilterForGPU(gpu int) MigCaps {
71+
func (m MigCaps) FilterForGPU(gpu Index) MigCaps {
6872
if m == nil {
6973
return nil
7074
}
7175
filtered := make(MigCaps)
72-
for gi := 0; ; gi++ {
76+
for gi := Index(0); ; gi++ {
7377
giCap := NewGPUInstanceCap(gpu, gi)
7478
giMinor, exist := m[giCap]
7579
if !exist {
7680
break
7781
}
7882
filtered[giCap] = giMinor
79-
for ci := 0; ; ci++ {
83+
for ci := Index(0); ; ci++ {
8084
ciCap := NewComputeInstanceCap(gpu, gi, ci)
8185
ciMinor, exist := m[ciCap]
8286
if !exist {

0 commit comments

Comments
 (0)