// Copyright 2020 ConsenSys Software Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package parallel import ( "runtime" "sync" ) // Execute process in parallel the work function func Execute(nbIterations int, work func(int, int), maxCpus ...int) { nbTasks := runtime.NumCPU() if len(maxCpus) == 1 { nbTasks = maxCpus[0] } nbIterationsPerCpus := nbIterations / nbTasks // more CPUs than tasks: a CPU will work on exactly one iteration if nbIterationsPerCpus < 1 { nbIterationsPerCpus = 1 nbTasks = nbIterations } var wg sync.WaitGroup extraTasks := nbIterations - (nbTasks * nbIterationsPerCpus) extraTasksOffset := 0 for i := 0; i < nbTasks; i++ { wg.Add(1) _start := i*nbIterationsPerCpus + extraTasksOffset _end := _start + nbIterationsPerCpus if extraTasks > 0 { _end++ extraTasks-- extraTasksOffset++ } go func() { work(_start, _end) wg.Done() }() } wg.Wait() }