Google OR-Tools v9.12
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 <memory>
23#include <string>
24
25#include "absl/functional/any_invocable.h"
26#include "absl/synchronization/blocking_counter.h"
28#include "ortools/pdlp/solvers.pb.h"
29#include "unsupported/Eigen/CXX11/ThreadPool"
30
32
33// Thread scheduling interface.
34class Scheduler {
35 public:
36 virtual ~Scheduler() = default;
37 virtual int num_threads() const = 0;
38 virtual std::string info_string() const = 0;
39
40 // Calls `do_func(i)` in parallel for `i` from `start` to `end-1`.
41 virtual void ParallelFor(int start, int end,
42 absl::AnyInvocable<void(int)> do_func) = 0;
43};
44
45// Google3 ThreadPool scheduler with barrier synchronization.
47 public:
49 : num_threads_(num_threads),
50 threadpool_(std::make_unique<ThreadPool>("pdlp", num_threads)) {
51 threadpool_->StartWorkers();
52 }
53 int num_threads() const override { return num_threads_; };
54 std::string info_string() const override { return "google_threadpool"; };
55
56 void ParallelFor(int start, int end,
57 absl::AnyInvocable<void(int)> do_func) override {
58 absl::BlockingCounter counter(end - start);
59 for (int i = start; i < end; ++i) {
60 threadpool_->Schedule([&, i]() {
61 do_func(i);
62 counter.DecrementCount();
63 });
64 }
65 counter.Wait();
66 }
67
68 private:
69 const int num_threads_;
70 std::unique_ptr<ThreadPool> threadpool_ = nullptr;
71};
72
73// Eigen ThreadPool scheduler with barrier synchronization.
75 public:
77 : num_threads_(num_threads),
78 eigen_threadpool_(std::make_unique<Eigen::ThreadPool>(num_threads)) {}
79 int num_threads() const override { return num_threads_; };
80 std::string info_string() const override { return "eigen_threadpool"; };
81
82 void ParallelFor(int start, int end,
83 absl::AnyInvocable<void(int)> do_func) override {
84 Eigen::Barrier eigen_barrier(end - start);
85 for (int i = start; i < end; ++i) {
86 eigen_threadpool_->Schedule([&, i]() {
87 do_func(i);
88 eigen_barrier.Notify();
89 });
90 }
91 eigen_barrier.Wait();
92 }
93
94 private:
95 const int num_threads_;
96 std::unique_ptr<Eigen::ThreadPool> eigen_threadpool_ = nullptr;
97};
98
99// Makes a scheduler of a given type.
100std::unique_ptr<Scheduler> MakeScheduler(SchedulerType type, int num_threads);
101
102} // namespace operations_research::pdlp
103
104#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:82
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:56
Thread scheduling interface.
Definition scheduler.h:34
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
STL namespace.