eOn 3.2.0
Long-timescale dynamics: aKMC, NEB, parallel replica
☾
Toggle main menu visibility
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
19
namespace
eonc
{
20
21
namespace
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.
26
inline
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.
44
class
ImagePool
{
45
public
:
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
54
~ImagePool
() {
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);
82
active_
=
helpers_
;
83
++
generation_
;
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
110
private
:
111
ImagePool
() {
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.
191
template
<
typename
Work>
inline
void
forEachImage
(
long
n, Work &&work) {
192
const
std::function<void(
long
)> f = std::ref(work);
193
detail::ImagePool::instance
().
run
(n, f);
194
}
195
196
}
// namespace eonc
eonc::detail::ImagePool::active_
long active_
Definition
ForEachImage.h:179
eonc::detail::ImagePool::threads
std::size_t threads() const noexcept
Definition
ForEachImage.h:94
eonc::detail::ImagePool::ImagePool
ImagePool(const ImagePool &)=delete
eonc::detail::ImagePool::wake_
std::condition_variable wake_
Definition
ForEachImage.h:172
eonc::detail::ImagePool::failure_
std::exception_ptr failure_
Definition
ForEachImage.h:177
eonc::detail::ImagePool::instance
static ImagePool & instance()
Definition
ForEachImage.h:46
eonc::detail::ImagePool::helpers_
long helpers_
Definition
ForEachImage.h:178
eonc::detail::ImagePool::generation_
std::size_t generation_
Definition
ForEachImage.h:180
eonc::detail::ImagePool::drain
void drain()
Definition
ForEachImage.h:136
eonc::detail::ImagePool::n_
long n_
Definition
ForEachImage.h:175
eonc::detail::ImagePool::ensureThreads
void ensureThreads(std::size_t n)
Definition
ForEachImage.h:98
eonc::detail::ImagePool::loop
void loop(long index)
Definition
ForEachImage.h:148
eonc::detail::ImagePool::runMutex_
std::mutex runMutex_
Definition
ForEachImage.h:170
eonc::detail::ImagePool::run
void run(long n, const std::function< void(long)> &work)
Definition
ForEachImage.h:66
eonc::detail::ImagePool::mutex_
std::mutex mutex_
Definition
ForEachImage.h:171
eonc::detail::ImagePool::done_
std::condition_variable done_
Definition
ForEachImage.h:173
eonc::detail::ImagePool::~ImagePool
~ImagePool()
Definition
ForEachImage.h:54
eonc::detail::ImagePool::next_
std::atomic< long > next_
Definition
ForEachImage.h:176
eonc::detail::ImagePool::ImagePool
ImagePool()
Definition
ForEachImage.h:111
eonc::detail::ImagePool::threads_
std::vector< std::thread > threads_
Definition
ForEachImage.h:169
eonc::detail::ImagePool::serial
static void serial(long n, const std::function< void(long)> &work)
Definition
ForEachImage.h:122
eonc::detail::ImagePool::operator=
ImagePool & operator=(const ImagePool &)=delete
eonc::detail::ImagePool::work_
const std::function< void(long)> * work_
Definition
ForEachImage.h:174
eonc::detail::ImagePool::stop_
bool stop_
Definition
ForEachImage.h:181
eonc::detail
Definition
ForEachImage.h:21
eonc::detail::usableCores
long usableCores()
Definition
ForEachImage.h:26
eonc
RAII resource manager for the ARTn C library with global synchronization.
Definition
ARTnSaddleSearch.cpp:23
eonc::forEachImage
void forEachImage(long n, Work &&work)
Definition
ForEachImage.h:191
helpers
Definition
Create.hpp:19
client
ForEachImage.h
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf