sigs.k8s.io/kueue@v0.6.2/pkg/scheduler/flavorassigner/podset_reducer.go (about)

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package flavorassigner
    18  
    19  import (
    20  	"sort"
    21  
    22  	"k8s.io/utils/ptr"
    23  
    24  	kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
    25  )
    26  
    27  // PodSetReducer helper structure used to gradually walk down
    28  // from PodSets[*].Count to *PodSets[*].MinimumCount.
    29  type PodSetReducer[R any] struct {
    30  	podSets    []kueue.PodSet
    31  	fullCounts []int32
    32  	deltas     []int32
    33  	totalDelta int32
    34  	fits       func([]int32) (R, bool)
    35  }
    36  
    37  func NewPodSetReducer[R any](podSets []kueue.PodSet, fits func([]int32) (R, bool)) *PodSetReducer[R] {
    38  	psr := &PodSetReducer[R]{
    39  		podSets:    podSets,
    40  		deltas:     make([]int32, len(podSets)),
    41  		fullCounts: make([]int32, len(podSets)),
    42  		fits:       fits,
    43  	}
    44  
    45  	for i := range psr.podSets {
    46  		ps := &psr.podSets[i]
    47  		psr.fullCounts[i] = ps.Count
    48  
    49  		d := ps.Count - ptr.Deref(ps.MinCount, ps.Count)
    50  		psr.deltas[i] = d
    51  		psr.totalDelta += d
    52  	}
    53  	return psr
    54  }
    55  
    56  func fillPodSetSizesForSearchIndex(out, fullCounts, deltas []int32, upFactor int32, downFactor int32) {
    57  	// this will panic if len(out) < len(deltas)
    58  	for i, v := range deltas {
    59  		tmp := int32(int64(v) * int64(upFactor) / int64(downFactor))
    60  		out[i] = fullCounts[i] - tmp
    61  	}
    62  }
    63  
    64  // Search find the first biggest set of counts that pass fits(), it's using
    65  // binary Search so the last call to fits() might not be a successful one
    66  // Returns nil if no solution was found
    67  func (psr *PodSetReducer[R]) Search() (R, bool) {
    68  	var lastGoodIdx int
    69  	var lastR R
    70  
    71  	if psr.totalDelta == 0 {
    72  		return lastR, false
    73  	}
    74  
    75  	current := make([]int32, len(psr.podSets))
    76  	idx := sort.Search(int(psr.totalDelta)+1, func(i int) bool {
    77  		fillPodSetSizesForSearchIndex(current, psr.fullCounts, psr.deltas, int32(i), psr.totalDelta)
    78  		r, f := psr.fits(current)
    79  		if f {
    80  			lastGoodIdx = i
    81  			lastR = r
    82  		}
    83  		return f
    84  	})
    85  	return lastR, idx == lastGoodIdx
    86  }