-
Notifications
You must be signed in to change notification settings - Fork 13
/
fgd_score.go
90 lines (76 loc) · 3.5 KB
/
fgd_score.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package plugin
import (
"context"
"fmt"
"strconv"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
resourcehelper "k8s.io/kubectl/pkg/util/resource"
"k8s.io/kubernetes/pkg/scheduler/framework"
simontype "github.com/hkust-adsl/kubernetes-scheduler-simulator/pkg/type"
gpushareutils "github.com/hkust-adsl/kubernetes-scheduler-simulator/pkg/type/open-gpu-share/utils"
"github.com/hkust-adsl/kubernetes-scheduler-simulator/pkg/utils"
)
type FGDScorePlugin struct {
handle framework.Handle
typicalPods *simontype.TargetPodList
}
var _ framework.ScorePlugin = &FGDScorePlugin{}
func NewFGDScorePlugin(_ runtime.Object, handle framework.Handle, typicalPods *simontype.TargetPodList) (framework.Plugin, error) {
plugin := &FGDScorePlugin{
handle: handle,
typicalPods: typicalPods,
}
allocateGpuIdFunc[plugin.Name()] = allocateGpuIdBasedOnFGDScore
return plugin, nil
}
func (plugin *FGDScorePlugin) Name() string {
return simontype.FGDScorePluginName
}
func (plugin *FGDScorePlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) (int64, *framework.Status) {
if podReq, _ := resourcehelper.PodRequestsAndLimits(p); len(podReq) == 0 {
return framework.MaxNodeScore, framework.NewStatus(framework.Success)
}
nodeResPtr := utils.GetNodeResourceViaHandleAndName(plugin.handle, nodeName)
if nodeResPtr == nil {
return framework.MinNodeScore, framework.NewStatus(framework.Error, fmt.Sprintf("failed to get nodeRes(%s)\n", nodeName))
}
nodeRes := *nodeResPtr
podRes := utils.GetPodResource(p)
if !utils.IsNodeAccessibleToPod(nodeRes, podRes) {
return framework.MinNodeScore, framework.NewStatus(framework.Error, fmt.Sprintf("Node (%s) %s does not match GPU type request of pod %s\n", nodeName, nodeRes.Repr(), podRes.Repr()))
}
score, _ := calculateGpuShareFragExtendScore(nodeRes, podRes, plugin.typicalPods)
return score, framework.NewStatus(framework.Success)
}
func (plugin *FGDScorePlugin) ScoreExtensions() framework.ScoreExtensions {
return nil
}
func calculateGpuShareFragExtendScore(nodeRes simontype.NodeResource, podRes simontype.PodResource, typicalPods *simontype.TargetPodList) (score int64, gpuId string) {
nodeGpuShareFragScore := utils.NodeGpuShareFragAmountScore(nodeRes, *typicalPods)
if podRes.GpuNumber == 1 && podRes.MilliGpu < gpushareutils.MILLI { // request partial GPU
score, gpuId = 0, ""
for i := 0; i < len(nodeRes.MilliGpuLeftList); i++ {
if nodeRes.MilliGpuLeftList[i] >= podRes.MilliGpu {
newNodeRes := nodeRes.Copy()
newNodeRes.MilliCpuLeft -= podRes.MilliCpu
newNodeRes.MilliGpuLeftList[i] -= podRes.MilliGpu
newNodeGpuShareFragScore := utils.NodeGpuShareFragAmountScore(newNodeRes, *typicalPods)
fragScore := int64(sigmoid((nodeGpuShareFragScore-newNodeGpuShareFragScore)/1000) * float64(framework.MaxNodeScore))
if gpuId == "" || fragScore > score {
score = fragScore
gpuId = strconv.Itoa(i)
}
}
}
return score, gpuId
} else {
newNodeRes, _ := nodeRes.Sub(podRes)
newNodeGpuShareFragScore := utils.NodeGpuShareFragAmountScore(newNodeRes, *typicalPods)
return int64(sigmoid((nodeGpuShareFragScore-newNodeGpuShareFragScore)/1000) * float64(framework.MaxNodeScore)), simontype.AllocateExclusiveGpuId(nodeRes, podRes)
}
}
func allocateGpuIdBasedOnFGDScore(nodeRes simontype.NodeResource, podRes simontype.PodResource, _ simontype.GpuPluginCfg, typicalPods *simontype.TargetPodList) (gpuId string) {
_, gpuId = calculateGpuShareFragExtendScore(nodeRes, podRes, typicalPods)
return gpuId
}