Normalize scores correctly

This commit is contained in:
Preetha Appan
2018-11-08 17:01:58 -06:00
parent 66670c0f02
commit eda98fb070
3 changed files with 14 additions and 12 deletions

View File

@@ -41,7 +41,7 @@ func (d *deviceAllocator) AssignDevice(ask *structs.RequestedDevice) (out *struc
// Hold the current best offer
var offer *structs.AllocatedDeviceResource
var offerScore float64
var matchedWeights float64
// Determine the devices that are feasible based on availability and
// constraints
for id, devInst := range d.Devices {
@@ -86,6 +86,7 @@ func (d *deviceAllocator) AssignDevice(ask *structs.RequestedDevice) (out *struc
}
choiceScore += a.Weight
}
matchedWeights += choiceScore
// normalize
choiceScore /= totalWeight
@@ -124,5 +125,5 @@ func (d *deviceAllocator) AssignDevice(ask *structs.RequestedDevice) (out *struc
return nil, 0.0, fmt.Errorf("no devices match request")
}
return offer, offerScore, nil
return offer, matchedWeights, nil
}

View File

@@ -302,7 +302,6 @@ func TestDeviceAllocator_Allocate_Affinities(t *testing.T) {
},
},
ExpectedDevice: nvidia0,
ZeroScore: true,
},
{
Name: "nvidia/gpu",

View File

@@ -196,8 +196,8 @@ OUTER:
devAllocator.AddAllocs(proposed)
// Track the affinities of the devices
devicesWithAffinities := 0.0
deviceAffinityScore := 0.0
totalDeviceAffinityWeight := 0.0
sumMatchingAffinities := 0.0
// Assign the resources for each task
total := &structs.AllocatedResources{
@@ -283,7 +283,7 @@ OUTER:
// Check if we need to assign devices
for _, req := range task.Resources.Devices {
offer, score, err := devAllocator.AssignDevice(req)
offer, sumAffinities, err := devAllocator.AssignDevice(req)
if offer == nil {
iter.ctx.Metrics().ExhaustedNode(option.Node, fmt.Sprintf("devices: %s", err))
continue OUTER
@@ -295,8 +295,10 @@ OUTER:
// Add the scores
if len(req.Affinities) != 0 {
devicesWithAffinities++
deviceAffinityScore += score
for _, a := range req.Affinities {
totalDeviceAffinityWeight += math.Abs(a.Weight)
}
sumMatchingAffinities += sumAffinities
}
}
@@ -350,10 +352,10 @@ OUTER:
iter.ctx.Metrics().ScoreNode(option.Node, "binpack", normalizedFit)
// Score the device affinity
if devicesWithAffinities != 0 {
deviceAffinityScore /= float64(devicesWithAffinities)
option.Scores = append(option.Scores, deviceAffinityScore)
iter.ctx.Metrics().ScoreNode(option.Node, "devices", deviceAffinityScore)
if totalDeviceAffinityWeight != 0 {
sumMatchingAffinities /= totalDeviceAffinityWeight
option.Scores = append(option.Scores, sumMatchingAffinities)
iter.ctx.Metrics().ScoreNode(option.Node, "devices", sumMatchingAffinities)
}
return option