Skip to content

Commit 4523b2e

Browse files
committed
[no-relnote] Add function to filter nvcaps by GPU
Signed-off-by: Evan Lezar <[email protected]>
1 parent d757f6e commit 4523b2e

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

internal/nvcaps/nvcaps.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ const (
3939
// MigMinor represents the minor number of a MIG device
4040
type MigMinor int
4141

42-
// MigCap represents the path to a MIG cap file
42+
// MigCap represents the path to a MIG cap file.
43+
// These are listed in /proc/driver/nvidia-caps/mig-minors and have one of the
44+
// follown forms:
45+
// - config
46+
// - monitor
47+
// - gpu{{ .gpuIndex }}/gi{{ .gi }}/access
48+
// - gpu{{ .gpuIndex }}/gi{{ .gi }}/ci {{ .ci }}/access
4349
type MigCap string
4450

4551
// MigCaps stores a map of MIG cap file paths to MIG minors
@@ -57,6 +63,31 @@ func NewComputeInstanceCap(gpu, gi, ci int) MigCap {
5763
return MigCap(fmt.Sprintf("gpu%d/gi%d/ci%d/access", gpu, gi, ci))
5864
}
5965

66+
// FilterForGPU limits the MIG Caps to those associated with a particular GPU.
67+
func (m MigCaps) FilterForGPU(gpu int) MigCaps {
68+
if m == nil {
69+
return nil
70+
}
71+
filtered := make(MigCaps)
72+
for gi := 0; ; gi++ {
73+
giCap := NewGPUInstanceCap(gpu, gi)
74+
giMinor, exist := m[giCap]
75+
if !exist {
76+
break
77+
}
78+
filtered[giCap] = giMinor
79+
for ci := 0; ; ci++ {
80+
ciCap := NewComputeInstanceCap(gpu, gi, ci)
81+
ciMinor, exist := m[ciCap]
82+
if !exist {
83+
break
84+
}
85+
filtered[ciCap] = ciMinor
86+
}
87+
}
88+
return filtered
89+
}
90+
6091
// GetCapDevicePath returns the path to the cap device for the specified cap.
6192
// An error is returned if the cap is invalid.
6293
func (m MigCaps) GetCapDevicePath(cap MigCap) (string, error) {

0 commit comments

Comments
 (0)