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