Skip to content
Draft
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
2 changes: 1 addition & 1 deletion solve_solver_parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type ParallelSolveOptions struct {
Duration time.Duration `json:"duration" usage:"maximum duration of the solver" default:"5s"`
Plateau PlateauOptions `json:"plateau" usage:"plateau options"`
ParallelRuns int `json:"parallel_runs" usage:"maximum number of parallel runs, -1 results in using all available resources" default:"-1"`
StartSolutions int `json:"start_solutions" usage:"number of solutions to generate on top of those passed in; one solution generated with sweep algorithm, the rest generated randomly" default:"-1"`
StartSolutions int `json:"start_solutions" usage:"number of solutions to generate on top of those passed in; one solution generated with sweep algorithm, the rest generated randomly. Solutions are generated in parallel, but not more than 'parallel_runs' many at a time" default:"-1"`
RunDeterministically bool `json:"run_deterministically" usage:"run the parallel solver deterministically"`
}

Expand Down
28 changes: 19 additions & 9 deletions solver_parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,35 @@ func (p *parallelSolverWrapperImpl) Solve(
interpretedParallelSolveOptions.StartSolutions = runtime.NumCPU()
}

initialSolutions := make(Solutions, interpretedParallelSolveOptions.StartSolutions)
if interpretedParallelSolveOptions.StartSolutions > 0 {
initialSolutions := make(Solutions, interpretedParallelSolveOptions.StartSolutions)
var wg sync.WaitGroup
wg.Add(interpretedParallelSolveOptions.StartSolutions)
solution, err := NewSolution(p.solver.Model())
if err != nil {
return nil, err
}
for idx := 0; idx < interpretedParallelSolveOptions.StartSolutions; idx++ {
go func(idx int, sol Solution) {
defer wg.Done()
randomSolution, err := RandomSolutionConstruction(ctx, sol)
if err != nil {
panic(err)
// We generate start solutions in parallel.
// But only `ParallelRuns` at a time.
startSolutionnRequests := make(chan int)
for i := 0; i < interpretedParallelSolveOptions.ParallelRuns; i++ {
go func(sol Solution) {
for idx := range startSolutionnRequests {
randomSolution, err := RandomSolutionConstruction(ctx, sol)
if err != nil {
panic(err)
}
initialSolutions[idx] = randomSolution
wg.Done()
}
initialSolutions[idx] = randomSolution
}(idx, solution.Copy())
}(solution.Copy())
}

for i := 0; i < interpretedParallelSolveOptions.StartSolutions; i++ {
startSolutionnRequests <- i
}
wg.Wait()
close(startSolutionnRequests)
startSolutions = append(startSolutions, initialSolutions...)
}

Expand Down
Loading