Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use named array instead of array in normalizing score #80901

Merged
Merged
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: 2 additions & 4 deletions pkg/scheduler/core/generic_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,11 +804,9 @@ func PrioritizeNodes(
for j := range priorityConfigs {
result[i].Score += results[j][i].Score * priorityConfigs[j].Weight
}
}

for _, scoreList := range scoresMap {
for i := range nodes {
result[i].Score += scoreList[i]
for j := range scoresMap {
result[i].Score += scoresMap[j][i].Score
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that the original code will be faster because it has better cache locality when reading scores from the scoresMap.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need more evidence on this, before that I prefer to keep it this way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory sequential (or strided) reads are the fastest form of memory access patterns because of prefetching and cache locality.

The other thing in this nested loop that could make it slower is that it makes n*m map lookups (n=number of nodes, m = number of priority functions/score plugins), while if we make the node loop inside, the number of scoresMap lookups will be zero. So it might be even faster (and easier to read) to flip the whole thing like this:

for i := range nodes {
		result = append(result, schedulerapi.HostPriority{Host: nodes[i].Name, Score: 0})
}

for j, priorityList := range priorityConfigs {
		for i := range nodes {
			result[i].Score += priorityList[i].Score * priorityConfigs[j].Weight
		}
}

for _, scoreList := range scoresMap {
		for i := range nodes {
			result[i].Score += scoreList[i].Score
		}
}

We need more evidence on this, before that I prefer to keep it this way.
The theory says this change makes things slower, so if you don't have evidence to the contrary, then we shouldn't make the change, especially that it is not related to the purpose of the PR "using named array in normalize scores".

Copy link
Contributor Author

@draveness draveness Aug 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory sequential (or strided) reads are the fastest form of memory access patterns because of prefetching and cache locality.

I know the theory of the cache locality and here are the benchmark numbers:

package main

import "testing"

func locality(result []int, scoresMap map[int][]int, anotherScoresMap map[int][]int) {
	for _, scores := range scoresMap {
		for i := range scores {
			result[i] = result[i] + scores[i]
		}
	}

	for _, scores := range anotherScoresMap {
		for i := range scores {
			result[i] = result[i] + scores[i]
		}
	}
}

func nonlocality(result []int, scoresMap map[int][]int, anotherScoresMap map[int][]int) {
	for i := range result {
		for j := range scoresMap {
			result[i] = result[i] + scoresMap[j][i]
		}

		for j := range anotherScoresMap {
			result[i] = result[i] + anotherScoresMap[j][i]
		}
	}
}

func BenchmarkLocality(b *testing.B) {
	result := make([]int, 5000)
	scores := make(map[int][]int, 10)
	anotherScores := make(map[int][]int, 10)

	for i := 0; i < 10; i++ {
		scores[i] = make([]int, 5000)
		anotherScores[i] = make([]int, 5000)
		for j := 0; j < 5000; j++ {
			scores[i][j] = j
			anotherScores[i][j] = j
		}
	}

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		locality(result, scores, anotherScores)
	}
}

func BenchmarkNonLocality(b *testing.B) {
	result := make([]int, 5000)
	scores := make(map[int][]int, 10)
	anotherScores := make(map[int][]int, 10)

	for i := 0; i < 10; i++ {
		scores[i] = make([]int, 5000)
		anotherScores[i] = make([]int, 5000)
		for j := 0; j < 5000; j++ {
			scores[i][j] = j
			anotherScores[i][j] = j
		}
	}

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		locality(result, scores, anotherScores)
	}
}

$ go test -bench=.
goos: darwin
goarch: amd64
pkg: github.com/golang
BenchmarkLocality-8      	   30000	     44607 ns/op
BenchmarkNonLocality-8   	   30000	     44245 ns/op
PASS

According to the benchmarks, I prefer to keep it this way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange, it is either golang's compiler is so good, or new architectures have such powerful memory controllers. I would like to do the same test in C++ to see if I get different results.

Do you have a theory why they are the same?

Copy link
Member

@alculquicondor alculquicondor Aug 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is that the number of priorities (j values) is very small, and so is the number of nodes. We are only talking about ~200kb, which can fit in the caches pretty easily.

Minor note in the posted benchmark: it should be map[string][]int. but again, we are only doing lookups among 10 values.

On the other hand, I don't understand the motivation to do the change. I'm not going to hold the review.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I re ran the test and set the sizes to two orders of magnitude bigger, no difference.

The issue about cache is not only about size, but also what you move to the cache (how much of the cache line being moved from memory is actually used), so it is about effective memory bandwidth.

}
}

Expand Down
32 changes: 18 additions & 14 deletions pkg/scheduler/framework/v1alpha1/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,25 +349,29 @@ func (f *framework) RunPostFilterPlugins(
return nil
}

// RunScorePlugins runs the set of configured scoring plugins. It returns a map that
// RunScorePlugins runs the set of configured scoring plugins. It returns a list that
// stores for each scoring plugin name the corresponding NodeScoreList(s).
// It also returns *Status, which is set to non-success if any of the plugins returns
// a non-success status.
func (f *framework) RunScorePlugins(pc *PluginContext, pod *v1.Pod, nodes []*v1.Node) (PluginToNodeScoreMap, *Status) {
pluginToNodeScoreMap := make(PluginToNodeScoreMap, len(f.scorePlugins))
func (f *framework) RunScorePlugins(pc *PluginContext, pod *v1.Pod, nodes []*v1.Node) (PluginToNodeScores, *Status) {
pluginToNodeScores := make(PluginToNodeScores, len(f.scorePlugins))
for _, pl := range f.scorePlugins {
pluginToNodeScoreMap[pl.Name()] = make(NodeScoreList, len(nodes))
pluginToNodeScores[pl.Name()] = make(NodeScoreList, len(nodes))
}
ctx, cancel := context.WithCancel(context.Background())
errCh := schedutil.NewErrorChannel()
workqueue.ParallelizeUntil(ctx, 16, len(nodes), func(index int) {
for _, pl := range f.scorePlugins {
score, status := pl.Score(pc, pod, nodes[index].Name)
nodeName := nodes[index].Name
score, status := pl.Score(pc, pod, nodeName)
if !status.IsSuccess() {
errCh.SendErrorWithCancel(fmt.Errorf(status.Message()), cancel)
return
}
pluginToNodeScoreMap[pl.Name()][index] = score
pluginToNodeScores[pl.Name()][index] = NodeScore{
Name: nodeName,
Score: score,
}
}
})

Expand All @@ -377,21 +381,21 @@ func (f *framework) RunScorePlugins(pc *PluginContext, pod *v1.Pod, nodes []*v1.
return nil, NewStatus(Error, msg)
}

return pluginToNodeScoreMap, nil
return pluginToNodeScores, nil
}

// RunNormalizeScorePlugins runs the NormalizeScore function of Score plugins.
// It should be called after RunScorePlugins with the PluginToNodeScoreMap result.
// It then modifies the map with normalized scores. It returns a non-success Status
// It should be called after RunScorePlugins with the PluginToNodeScores result.
// It then modifies the list with normalized scores. It returns a non-success Status
// if any of the NormalizeScore functions returns a non-success status.
func (f *framework) RunNormalizeScorePlugins(pc *PluginContext, pod *v1.Pod, scores PluginToNodeScoreMap) *Status {
func (f *framework) RunNormalizeScorePlugins(pc *PluginContext, pod *v1.Pod, scores PluginToNodeScores) *Status {
ctx, cancel := context.WithCancel(context.Background())
errCh := schedutil.NewErrorChannel()
workqueue.ParallelizeUntil(ctx, 16, len(f.scoreWithNormalizePlugins), func(index int) {
pl := f.scoreWithNormalizePlugins[index]
nodeScoreList, ok := scores[pl.Name()]
if !ok {
err := fmt.Errorf("normalize score plugin %q has no corresponding scores in the PluginToNodeScoreMap", pl.Name())
err := fmt.Errorf("normalize score plugin %q has no corresponding scores in the PluginToNodeScores", pl.Name())
errCh.SendErrorWithCancel(err, cancel)
return
}
Expand All @@ -414,7 +418,7 @@ func (f *framework) RunNormalizeScorePlugins(pc *PluginContext, pod *v1.Pod, sco

// ApplyScoreWeights applies weights to the score results. It should be called after
// RunNormalizeScorePlugins.
func (f *framework) ApplyScoreWeights(pc *PluginContext, pod *v1.Pod, scores PluginToNodeScoreMap) *Status {
func (f *framework) ApplyScoreWeights(pc *PluginContext, pod *v1.Pod, scores PluginToNodeScores) *Status {
ctx, cancel := context.WithCancel(context.Background())
errCh := schedutil.NewErrorChannel()
workqueue.ParallelizeUntil(ctx, 16, len(f.scorePlugins), func(index int) {
Expand All @@ -423,12 +427,12 @@ func (f *framework) ApplyScoreWeights(pc *PluginContext, pod *v1.Pod, scores Plu
weight := f.pluginNameToWeightMap[pl.Name()]
nodeScoreList, ok := scores[pl.Name()]
if !ok {
err := fmt.Errorf("score plugin %q has no corresponding scores in the PluginToNodeScoreMap", pl.Name())
err := fmt.Errorf("score plugin %q has no corresponding scores in the PluginToNodeScores", pl.Name())
errCh.SendErrorWithCancel(err, cancel)
return
}
for i := range nodeScoreList {
nodeScoreList[i] = nodeScoreList[i] * weight
nodeScoreList[i].Score = nodeScoreList[i].Score * weight
}
})

Expand Down
Loading