Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
scheduler.h
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
14#ifndef PDLP_SCHEDULER_H_
15#define PDLP_SCHEDULER_H_
16
17// Eigen defaults to using TensorFlow's scheduler, unless we add this line.
18#ifndef EIGEN_USE_CUSTOM_THREAD_POOL
19#define EIGEN_USE_CUSTOM_THREAD_POOL
20#endif
21
22#include <math.h>
23
24#include <functional>
25#include <memory>
26#include <string>
27#include <utility>
28
29#include "absl/functional/any_invocable.h"
30#include "absl/log/log.h"
31#include "absl/synchronization/blocking_counter.h"
33#include "ortools/pdlp/solvers.pb.h"
34#include "unsupported/Eigen/CXX11/ThreadPool"
35
37
38// Thread scheduling interface.
39class Scheduler {
40 public:
41 virtual ~Scheduler() = default;
42 virtual int num_threads() const = 0;
43 virtual std::string info_string() const = 0;
44
45 // Calls `do_func(i)` in parallel for `i` from `start` to `end-1`.
46 virtual void ParallelFor(int start, int end,
47 absl::AnyInvocable<void(int)> do_func) = 0;
48};
49
50// Google3 ThreadPool scheduler with barrier synchronization.
52 public:
54 : num_threads_(num_threads),
55 threadpool_(std::make_unique<ThreadPool>("pdlp", num_threads)) {
56 threadpool_->StartWorkers();
57 }
58 int num_threads() const override { return num_threads_; };
59 std::string info_string() const override { return "google_threadpool"; };
60
61 void ParallelFor(int start, int end,
62 absl::AnyInvocable<void(int)> do_func) override {
63 absl::BlockingCounter counter(end - start);
64 for (int i = start; i < end; ++i) {
65 threadpool_->Schedule([&, i]() {
66 do_func(i);
67 counter.DecrementCount();
68 });
69 }
70 counter.Wait();
71 }
72
73 private:
74 const int num_threads_;
75 std::unique_ptr<ThreadPool> threadpool_ = nullptr;
76};
77
78// Eigen ThreadPool scheduler with barrier synchronization.
80 public:
82 : num_threads_(num_threads),
83 eigen_threadpool_(std::make_unique<Eigen::ThreadPool>(num_threads)) {}
84 int num_threads() const override { return num_threads_; };
85 std::string info_string() const override { return "eigen_threadpool"; };
86
87 void ParallelFor(int start, int end,
88 absl::AnyInvocable<void(int)> do_func) override {
89 Eigen::Barrier eigen_barrier(end - start);
90 for (int i = start; i < end; ++i) {
91 eigen_threadpool_->Schedule([&, i]() {
92 do_func(i);
93 eigen_barrier.Notify();
94 });
95 }
96 eigen_barrier.Wait();
97 }
98
99 private:
100 const int num_threads_;
101 std::unique_ptr<Eigen::ThreadPool> eigen_threadpool_ = nullptr;
102};
103
104// Makes a scheduler of a given type.
105std::unique_ptr<Scheduler> MakeScheduler(SchedulerType type, int num_threads);
106
107} // namespace operations_research::pdlp
108
109#endif // PDLP_SCHEDULER_H_
void ParallelFor(int start, int end, absl::AnyInvocable< void(int)> do_func) override
Calls do_func(i) in parallel for i from start to end-1.
Definition scheduler.h:87
void ParallelFor(int start, int end, absl::AnyInvocable< void(int)> do_func) override
Calls do_func(i) in parallel for i from start to end-1.
Definition scheduler.h:61
Thread scheduling interface.
Definition scheduler.h:39
virtual int num_threads() const =0
virtual std::string info_string() const =0
virtual void ParallelFor(int start, int end, absl::AnyInvocable< void(int)> do_func)=0
Calls do_func(i) in parallel for i from start to end-1.
Validation utilities for solvers.proto.
std::unique_ptr< Scheduler > MakeScheduler(SchedulerType type, int num_threads)
Convenience factory function.
Definition scheduler.cc:23
ClosedInterval::Iterator end(ClosedInterval interval)
STL namespace.