Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/device/nvidia/calculate_score.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/NVIDIA/go-nvlib/pkg/nvlib/device"
"github.com/NVIDIA/go-nvml/pkg/nvml"
"k8s.io/klog/v2"
)

// Device represents a GPU device as reported by NVML, including all of its
Expand Down Expand Up @@ -201,6 +202,8 @@ func calculateGPUScore(devices []*Device) ListDeviceScore {
return ds
}

// calculateGPUPairScore calculates a heuristic score representing the P2P connection
// strength between two GPUs. A higher score implies better/faster communication links.
func calculateGPUPairScore(gpu0 *Device, gpu1 *Device) int {
if gpu0 == nil || gpu1 == nil {
return 0
Expand All @@ -212,7 +215,8 @@ func calculateGPUPairScore(gpu0 *Device, gpu1 *Device) int {

if len(gpu0.Links[gpu1.Index]) != len(gpu1.Links[gpu0.Index]) {
err := fmt.Errorf("internal error in bestEffort GPU allocator: all P2PLinks between 2 GPUs should be bidirectional")
panic(err)
klog.ErrorS(err, "P2PLinks mismatch", "gpu0", gpu0.UUID, "gpu1", gpu1.UUID)
Comment thread
shellyco-code marked this conversation as resolved.
return 0
Comment thread
shellyco-code marked this conversation as resolved.
}

score := 0
Expand Down
32 changes: 32 additions & 0 deletions pkg/device/nvidia/calculate_score_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,49 @@
UUID: "gpu3",
Score: map[string]int{"gpu0": 200, "gpu1": 100, "gpu2": 200},
},
},
{
name: "asymmetric links",
args: []*Device{
{
Index: 0,
nvlibDevice: nvlibDevice{
UUID: "gpu0",
},
Links: map[int][]P2PLink{
1: {{Type: SingleNVLINKLink}},
},
},
{
Index: 1,
nvlibDevice: nvlibDevice{
UUID: "gpu1",
},
Links: map[int][]P2PLink{
0: {{Type: SingleNVLINKLink}, {Type: SingleNVLINKLink}}, // Asymmetric: 2 links here vs 1 above
},
},
},
want: ListDeviceScore{
{
UUID: "gpu0",
Score: map[string]int{"gpu1": 0},
},
{
UUID: "gpu1",
Score: map[string]int{"gpu0": 0},
},
},
},
}

Check failure on line 143 in pkg/device/nvidia/calculate_score_test.go

View workflow job for this annotation

GitHub Actions / lint

missing ',' before newline in composite literal (typecheck)

Check failure on line 143 in pkg/device/nvidia/calculate_score_test.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected newline in composite literal; possibly missing comma or }
for _, test := range tests {

Check failure on line 144 in pkg/device/nvidia/calculate_score_test.go

View workflow job for this annotation

GitHub Actions / lint

expected operand, found 'for' (typecheck)

Check failure on line 144 in pkg/device/nvidia/calculate_score_test.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected keyword for at end of statement (typecheck)
t.Run(test.name, func(t *testing.T) {
scoreList := make(ListDeviceScore, len(test.args))
for i, gpuI := range test.args {

Check failure on line 147 in pkg/device/nvidia/calculate_score_test.go

View workflow job for this annotation

GitHub Actions / lint

missing ',' in composite literal (typecheck)
score := make(map[string]int)
for j, gpuJ := range test.args {

Check failure on line 149 in pkg/device/nvidia/calculate_score_test.go

View workflow job for this annotation

GitHub Actions / lint

missing ',' in composite literal (typecheck)
if i == j {

Check failure on line 150 in pkg/device/nvidia/calculate_score_test.go

View workflow job for this annotation

GitHub Actions / lint

missing ',' in composite literal (typecheck)
continue

Check failure on line 151 in pkg/device/nvidia/calculate_score_test.go

View workflow job for this annotation

GitHub Actions / lint

expected operand, found 'continue' (typecheck)
}
score[gpuJ.UUID] = calculateGPUPairScore(gpuI, gpuJ)
}
Expand Down
Loading