github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/vector/compressionhelpers/utils.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package compressionhelpers
    13  
    14  import (
    15  	"math"
    16  	"runtime"
    17  	"sync"
    18  )
    19  
    20  type Action func(taskIndex uint64)
    21  
    22  func Concurrently(n uint64, action Action) {
    23  	n64 := float64(n)
    24  	workerCount := runtime.GOMAXPROCS(0)
    25  	wg := &sync.WaitGroup{}
    26  	split := uint64(math.Ceil(n64 / float64(workerCount)))
    27  	for worker := uint64(0); worker < uint64(workerCount); worker++ {
    28  		wg.Add(1)
    29  		go func(workerID uint64) {
    30  			defer wg.Done()
    31  			for i := workerID * split; i < uint64(math.Min(float64((workerID+1)*split), n64)); i++ {
    32  				action(i)
    33  			}
    34  		}(worker)
    35  	}
    36  	wg.Wait()
    37  }