Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
subsolver.cc
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include <cstdint>
17#include <functional>
18#include <limits>
19#include <memory>
20#include <string>
21#include <utility>
22#include <vector>
23
24#include "absl/flags/flag.h"
25#include "absl/log/check.h"
26#include "absl/strings/str_cat.h"
27#include "absl/strings/str_join.h"
28#include "absl/strings/string_view.h"
29#include "absl/synchronization/blocking_counter.h"
30#include "absl/synchronization/mutex.h"
31#include "absl/time/clock.h"
32#include "absl/time/time.h"
33#include "absl/types/span.h"
35#include "ortools/base/timer.h"
36#include "ortools/sat/util.h"
37#if !defined(__PORTABLE_PLATFORM__)
39#endif // __PORTABLE_PLATFORM__
40
41namespace operations_research {
42namespace sat {
43
44namespace {
45
46// Returns the next SubSolver index from which to call GenerateTask(). Note that
47// only SubSolvers for which TaskIsAvailable() is true are considered. Return -1
48// if no SubSolver can generate a new task.
49//
50// For now we use a really basic logic that tries to equilibrate the walltime or
51// deterministic time spent in each subsolver.
52int NextSubsolverToSchedule(std::vector<std::unique_ptr<SubSolver>>& subsolvers,
53 bool deterministic = true) {
54 int best = -1;
55 double best_score = std::numeric_limits<double>::infinity();
56 for (int i = 0; i < subsolvers.size(); ++i) {
57 if (subsolvers[i] == nullptr) continue;
58 if (subsolvers[i]->TaskIsAvailable()) {
59 const double score = subsolvers[i]->GetSelectionScore(deterministic);
60 if (best == -1 || score < best_score) {
61 best_score = score;
62 best = i;
63 }
64 }
65 }
66
67 if (best != -1) VLOG(1) << "Scheduling " << subsolvers[best]->name();
68 return best;
69}
70
71void ClearSubsolversThatAreDone(
72 absl::Span<const int> num_in_flight_per_subsolvers,
73 std::vector<std::unique_ptr<SubSolver>>& subsolvers) {
74 for (int i = 0; i < subsolvers.size(); ++i) {
75 if (subsolvers[i] == nullptr) continue;
76 if (num_in_flight_per_subsolvers[i] > 0) continue;
77 if (subsolvers[i]->IsDone()) {
78 // We can free the memory used by this solver for good.
79 VLOG(1) << "Deleting " << subsolvers[i]->name();
80 subsolvers[i].reset();
81 continue;
82 }
83 }
84}
85
86void SynchronizeAll(absl::Span<const std::unique_ptr<SubSolver>> subsolvers) {
87 for (const auto& subsolver : subsolvers) {
88 if (subsolver == nullptr) continue;
89 subsolver->Synchronize();
90 }
91}
92
93} // namespace
94
95void SequentialLoop(std::vector<std::unique_ptr<SubSolver>>& subsolvers) {
96 int64_t task_id = 0;
97 std::vector<int> num_in_flight_per_subsolvers(subsolvers.size(), 0);
98 while (true) {
99 SynchronizeAll(subsolvers);
100 ClearSubsolversThatAreDone(num_in_flight_per_subsolvers, subsolvers);
101 const int best = NextSubsolverToSchedule(subsolvers);
102 if (best == -1) break;
103 subsolvers[best]->NotifySelection();
104
105 WallTimer timer;
106 timer.Start();
107 subsolvers[best]->GenerateTask(task_id++)();
108 subsolvers[best]->AddTaskDuration(timer.Get());
109 }
110}
111
112#if defined(__PORTABLE_PLATFORM__)
113
114// On portable platform, we don't support multi-threading for now.
115
116void NonDeterministicLoop(std::vector<std::unique_ptr<SubSolver>>& subsolvers,
117 int num_threads, ModelSharedTimeLimit* time_limit) {
118 SequentialLoop(subsolvers);
119}
120
121void DeterministicLoop(std::vector<std::unique_ptr<SubSolver>>& subsolvers,
122 int num_threads, int batch_size, int max_num_batches) {
123 SequentialLoop(subsolvers);
124}
125
126#else // __PORTABLE_PLATFORM__
127
128void DeterministicLoop(std::vector<std::unique_ptr<SubSolver>>& subsolvers,
129 int num_threads, int batch_size, int max_num_batches) {
130 CHECK_GT(num_threads, 0);
131 CHECK_GT(batch_size, 0);
132 if (batch_size == 1) {
133 return SequentialLoop(subsolvers);
134 }
135
136 int64_t task_id = 0;
137 std::vector<int> num_in_flight_per_subsolvers(subsolvers.size(), 0);
138 std::vector<std::function<void()>> to_run;
139 std::vector<int> indices;
140 std::vector<double> timing;
141 to_run.reserve(batch_size);
142 ThreadPool pool(num_threads);
143 pool.StartWorkers();
144 for (int batch_index = 0;; ++batch_index) {
145 VLOG(2) << "Starting deterministic batch of size " << batch_size;
146 SynchronizeAll(subsolvers);
147 ClearSubsolversThatAreDone(num_in_flight_per_subsolvers, subsolvers);
148
149 // We abort the loop after the last synchronize to properly reports final
150 // status in case max_num_batches is used.
151 if (max_num_batches > 0 && batch_index >= max_num_batches) break;
152
153 // We first generate all task to run in this batch.
154 // Note that we can't start the task right away since if a task finish
155 // before we schedule everything, we will not be deterministic.
156 to_run.clear();
157 indices.clear();
158 for (int t = 0; t < batch_size; ++t) {
159 const int best = NextSubsolverToSchedule(subsolvers);
160 if (best == -1) break;
161 num_in_flight_per_subsolvers[best]++;
162 subsolvers[best]->NotifySelection();
163 to_run.push_back(subsolvers[best]->GenerateTask(task_id++));
164 indices.push_back(best);
165 }
166 if (to_run.empty()) break;
167
168 // Schedule each task.
169 timing.resize(to_run.size());
170 absl::BlockingCounter blocking_counter(static_cast<int>(to_run.size()));
171 for (int i = 0; i < to_run.size(); ++i) {
172 pool.Schedule(
173 [i, f = std::move(to_run[i]), &timing, &blocking_counter]() {
174 WallTimer timer;
175 timer.Start();
176 f();
177 timing[i] = timer.Get();
178 blocking_counter.DecrementCount();
179 });
180 }
181
182 // Wait for all tasks of this batch to be done before scheduling another
183 // batch.
184 blocking_counter.Wait();
185
186 // Update times.
187 num_in_flight_per_subsolvers.assign(subsolvers.size(), 0);
188 for (int i = 0; i < to_run.size(); ++i) {
189 subsolvers[indices[i]]->AddTaskDuration(timing[i]);
190 }
191 }
192}
193
194void NonDeterministicLoop(std::vector<std::unique_ptr<SubSolver>>& subsolvers,
195 const int num_threads,
197 CHECK_GT(num_threads, 0);
198 if (num_threads == 1) {
199 return SequentialLoop(subsolvers);
200 }
201
202 // The mutex guards num_in_flight and num_in_flight_per_subsolvers.
203 // This is used to detect when the search is done.
204 absl::Mutex mutex;
205 int num_in_flight = 0; // Guarded by `mutex`.
206 std::vector<int> num_in_flight_per_subsolvers(subsolvers.size(), 0);
207
208 // Predicate to be used with absl::Condition to detect that num_in_flight <
209 // num_threads. Must only be called while locking `mutex`.
210 const auto num_in_flight_lt_num_threads = [&num_in_flight, num_threads]() {
211 return num_in_flight < num_threads;
212 };
213
214 ThreadPool pool(num_threads);
215 pool.StartWorkers();
216
217 // The lambda below are using little space, but there is no reason
218 // to create millions of them, so we use the blocking nature of
219 // pool.Schedule() when the queue capacity is set.
220 int64_t task_id = 0;
221 while (true) {
222 // Set to true if no task is pending right now.
223 bool all_done = false;
224 {
225 // Wait if num_in_flight == num_threads.
226 const bool condition = mutex.LockWhenWithTimeout(
227 absl::Condition(&num_in_flight_lt_num_threads),
228 absl::Milliseconds(100));
229
230 // To support some "advanced" cancelation of subsolve, we still call
231 // synchronize every 0.1 seconds even if there is no worker available.
232 //
233 // TODO(user): We could also directly register callback to set stopping
234 // Boolean to false in a few places.
235 if (!condition) {
236 mutex.Unlock();
237 SynchronizeAll(subsolvers);
238 continue;
239 }
240
241 // The stopping condition is that we do not have anything else to generate
242 // once all the task are done and synchronized.
243 if (num_in_flight == 0) all_done = true;
244 mutex.Unlock();
245 }
246
247 SynchronizeAll(subsolvers);
248 int best = -1;
249 {
250 // We need to do that while holding the lock since substask below might
251 // be currently updating the time via AddTaskDuration().
252 const absl::MutexLock mutex_lock(&mutex);
253 ClearSubsolversThatAreDone(num_in_flight_per_subsolvers, subsolvers);
254 best = NextSubsolverToSchedule(subsolvers, /*deterministic=*/false);
255 if (VLOG_IS_ON(1) && time_limit->LimitReached()) {
256 std::vector<std::string> debug;
257 for (int i = 0; i < subsolvers.size(); ++i) {
258 if (subsolvers[i] != nullptr && num_in_flight_per_subsolvers[i] > 0) {
259 debug.push_back(absl::StrCat(subsolvers[i]->name(), ":",
260 num_in_flight_per_subsolvers[i]));
261 }
262 }
263 if (!debug.empty()) {
264 VLOG_EVERY_N_SEC(1, 1)
265 << "Subsolvers still running after time limit: "
266 << absl::StrJoin(debug, ",");
267 }
268 }
269 }
270 if (best == -1) {
271 if (all_done) break;
272
273 // It is hard to know when new info will allows for more task to be
274 // scheduled, so for now we just sleep for a bit. Note that in practice We
275 // will never reach here except at the end of the search because we can
276 // always schedule LNS threads.
277 absl::SleepFor(absl::Milliseconds(1));
278 continue;
279 }
280
281 // Schedule next task.
282 subsolvers[best]->NotifySelection();
283 {
284 absl::MutexLock mutex_lock(&mutex);
285 num_in_flight++;
286 num_in_flight_per_subsolvers[best]++;
287 }
288 std::function<void()> task = subsolvers[best]->GenerateTask(task_id++);
289 const std::string name = subsolvers[best]->name();
290 pool.Schedule([task = std::move(task), name, best, &subsolvers, &mutex,
291 &num_in_flight, &num_in_flight_per_subsolvers]() {
292 WallTimer timer;
293 timer.Start();
294 task();
295
296 const absl::MutexLock mutex_lock(&mutex);
297 DCHECK(subsolvers[best] != nullptr);
298 DCHECK_GT(num_in_flight_per_subsolvers[best], 0);
299 num_in_flight_per_subsolvers[best]--;
300 VLOG(1) << name << " done in " << timer.Get() << "s.";
301 subsolvers[best]->AddTaskDuration(timer.Get());
302 num_in_flight--;
303 });
304 }
305}
306
307#endif // __PORTABLE_PLATFORM__
308
309} // namespace sat
310} // namespace operations_research
double Get() const
Definition timer.h:46
void Start()
When Start() is called multiple times, only the most recent is used.
Definition timer.h:32
void Schedule(std::function< void()> closure)
Definition threadpool.cc:83
The model "singleton" shared time limit.
Definition util.h:345
time_limit
Definition solve.cc:22
void DeterministicLoop(std::vector< std::unique_ptr< SubSolver > > &subsolvers, int num_threads, int batch_size, int max_num_batches)
Definition subsolver.cc:128
void NonDeterministicLoop(std::vector< std::unique_ptr< SubSolver > > &subsolvers, const int num_threads, ModelSharedTimeLimit *time_limit)
Definition subsolver.cc:194
void SequentialLoop(std::vector< std::unique_ptr< SubSolver > > &subsolvers)
Definition subsolver.cc:95
In SWIG mode, we don't want anything besides these top-level includes.