Loading...
Searching...
No Matches
ForEachImage.h
Go to the documentation of this file.
1#pragma once
2// Persistent bounded thread pool for per-image work (NEB image forces and
3// projections).
4
5#include <algorithm>
6#include <atomic>
7#include <condition_variable>
8#include <cstddef>
9#include <exception>
10#include <functional>
11#include <mutex>
12#include <thread>
13#include <vector>
14
15#if defined(__linux__)
16#include <sched.h>
17#endif
18
19namespace eonc {
20
21namespace detail {
22
23// Cores this process may run on: the affinity mask under Slurm or taskset,
24// else hardware_concurrency(). A band on 4 allocated cores of a 32-core
25// node then gets 4 threads, not 32 that share 4 cores.
26inline long usableCores() {
27#if defined(__linux__)
28 cpu_set_t set;
29 CPU_ZERO(&set);
30 if (sched_getaffinity(0, sizeof(set), &set) == 0) {
31 const int n = CPU_COUNT(&set);
32 if (n > 0)
33 return n;
34 }
35#endif
36 return std::max(1L, static_cast<long>(std::thread::hardware_concurrency()));
37}
38
39// Workers start once and wait for the next band, so a band costs a
40// condition-variable round trip instead of creating and joining threads.
41// One band runs at a time. A call made while a band runs (from inside a
42// work item, or from a second thread) runs serially on its caller, so the
43// pool never waits on itself.
44class ImagePool {
45public:
46 static ImagePool &instance() {
47 static ImagePool pool;
48 return pool;
49 }
50
51 ImagePool(const ImagePool &) = delete;
52 ImagePool &operator=(const ImagePool &) = delete;
53
55 {
56 std::lock_guard<std::mutex> lock(mutex_);
57 stop_ = true;
58 }
59 wake_.notify_all();
60 for (auto &t : threads_)
61 t.join();
62 }
63
64 // Calls work(1) .. work(n), the caller taking indices too, and rethrows
65 // the first exception after every index has run.
66 void run(long n, const std::function<void(long)> &work) {
67 if (n <= 0)
68 return;
69 std::unique_lock<std::mutex> busy(runMutex_, std::try_to_lock);
70 if (!busy.owns_lock() || threads_.empty() || n == 1) {
71 serial(n, work);
72 return;
73 }
74 {
75 std::lock_guard<std::mutex> lock(mutex_);
76 work_ = &work;
77 n_ = n;
78 next_.store(1);
79 failure_ = nullptr;
80 // Only as many helpers as there are images beyond the caller's.
81 helpers_ = std::min<long>(static_cast<long>(threads_.size()), n - 1);
84 }
85 wake_.notify_all();
86 drain();
87 std::unique_lock<std::mutex> lock(mutex_);
88 done_.wait(lock, [this] { return active_ == 0; });
89 work_ = nullptr;
90 if (failure_)
91 std::rethrow_exception(failure_);
92 }
93
94 [[nodiscard]] std::size_t threads() const noexcept { return threads_.size(); }
95
96 // The constructor starts usableCores()-1 helpers. A caller that must
97 // observe more than one worker asks for them here.
98 void ensureThreads(std::size_t n) {
99 std::lock_guard<std::mutex> busy(runMutex_);
100 while (threads_.size() < n) {
101 const long index = static_cast<long>(threads_.size());
102 try {
103 threads_.emplace_back([this, index] { loop(index); });
104 } catch (...) {
105 break;
106 }
107 }
108 }
109
110private:
112 const long helpers = usableCores() - 1;
113 for (long t = 0; t < helpers; t++) {
114 try {
115 threads_.emplace_back([this, t] { loop(t); });
116 } catch (...) {
117 break; // the threads already running serve every band
118 }
119 }
120 }
121
122 static void serial(long n, const std::function<void(long)> &work) {
123 std::exception_ptr failure;
124 for (long i = 1; i <= n; i++) {
125 try {
126 work(i);
127 } catch (...) {
128 if (!failure)
129 failure = std::current_exception();
130 }
131 }
132 if (failure)
133 std::rethrow_exception(failure);
134 }
135
136 void drain() {
137 for (long i = next_.fetch_add(1); i <= n_; i = next_.fetch_add(1)) {
138 try {
139 (*work_)(i);
140 } catch (...) {
141 std::lock_guard<std::mutex> lock(mutex_);
142 if (!failure_)
143 failure_ = std::current_exception();
144 }
145 }
146 }
147
148 void loop(long index) {
149 std::size_t seen = 0;
150 for (;;) {
151 {
152 std::unique_lock<std::mutex> lock(mutex_);
153 wake_.wait(lock, [&] {
154 return stop_ || (generation_ != seen && index < helpers_);
155 });
156 if (stop_)
157 return;
158 seen = generation_;
159 }
160 drain();
161 {
162 std::lock_guard<std::mutex> lock(mutex_);
163 if (--active_ == 0)
164 done_.notify_one();
165 }
166 }
167 }
168
169 std::vector<std::thread> threads_;
170 std::mutex runMutex_;
171 std::mutex mutex_;
172 std::condition_variable wake_;
173 std::condition_variable done_;
174 const std::function<void(long)> *work_{nullptr};
175 long n_{0};
176 std::atomic<long> next_{1};
177 std::exception_ptr failure_;
178 long helpers_{0};
179 long active_{0};
180 std::size_t generation_{0};
181 bool stop_{false};
182};
183
184} // namespace detail
185
186// Calls work(1) .. work(n) on at most usableCores() threads of a pool that
187// lives for the process. Each thread takes the next index until none are
188// left, so a band with more images than cores does not oversubscribe. The
189// first exception thrown by any call is rethrown here after every index has
190// run.
191template <typename Work> inline void forEachImage(long n, Work &&work) {
192 const std::function<void(long)> f = std::ref(work);
194}
195
196} // namespace eonc
std::size_t threads() const noexcept
ImagePool(const ImagePool &)=delete
std::condition_variable wake_
std::exception_ptr failure_
static ImagePool & instance()
void ensureThreads(std::size_t n)
void loop(long index)
void run(long n, const std::function< void(long)> &work)
std::condition_variable done_
std::atomic< long > next_
std::vector< std::thread > threads_
static void serial(long n, const std::function< void(long)> &work)
ImagePool & operator=(const ImagePool &)=delete
const std::function< void(long)> * work_
long usableCores()
RAII resource manager for the ARTn C library with global synchronization.
void forEachImage(long n, Work &&work)