seams-core v2.14.2
libyodaLib, the C++ engine of d-SEAMS
Loading...
Searching...
No Matches
team_run.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <atomic>
5#include <cstddef>
6#include <exception>
7#include <initializer_list>
8#include <mutex>
9#include <stdexcept>
10#include <type_traits>
11#include <utility>
12#include <vector>
13
14#include "thread_team.hpp"
15
16#if defined(SEAMS_HAS_LINKCELL) && defined(SEAMS_HAS_LINKCELL_TEAM)
17extern "C" int lc_team_run(std::size_t nstages, const std::size_t *chunks,
18 void (*fn)(void *, std::size_t, std::size_t, std::size_t),
19 void *ctx);
20#endif
21
22// Stages of chunks in one parallel region. A run claims each chunk once
23// and every chunk of a stage returns before the next stage starts.
24//
25// While other processes keep half of the caller's cpus busy or more, the
26// region runs on linkcell's pool, the caller one of the workers: a chunk
27// is claimed from a counter and a stage closes when its last claimed
28// chunk returns, so neither a stage boundary nor the return waits for a
29// worker the scheduler has not run. An OpenMP join or barrier waits for
30// every thread of the team: beside an eight-thread spinner on eight cpus
31// one join of an empty loop took 3 to 11 ms, and a q6 call of 4 ms took
32// 8 to 19 ms in the processes whose team met one. On a machine the caller
33// has to itself, the region is one OpenMP team with a loop per stage,
34// whose spinning workers start each stage at once; Rayon workers that
35// have gone to sleep do not, and the q6 call took 3.1 ms on them instead
36// of 2.3.
37namespace seams::team {
38
39// Workers a run started now may use. The `worker` index a run passes is
40// below it, so it sizes per-worker scratch. Linkcell's team is the pool
41// run() selects with lc_set_threads(poolFor(0)), so it is never wider
42// than poolFor(0); asking lc_team_width would start that pool on a
43// machine where the run stays on OpenMP.
44inline int width() {
45#ifdef SEAMS_HAS_OPENMP
46 if (omp_in_parallel()) {
47 return 1;
48 }
49#endif
50#ifdef SEAMS_HAS_LINKCELL
51 return std::max(teamWidth(), poolFor(0));
52#else
53 return teamWidth();
54#endif
55}
56
57// Chunks per stage for `width` workers: one per OpenMP thread on a
58// machine the caller has to itself, so each thread keeps one share as the
59// original loops did, and two per worker while other processes crowd the
60// cpus, so a worker the scheduler parks holds up a smaller share. The
61// OpenMP team can be narrower than `width`, which also covers linkcell's
62// pool: eight chunks on a team narrowed to seven gave one thread two, and
63// the 110592-molecule cutoff list took 2.8 ms instead of 1.5.
64inline int parts(int width) {
65 if (width <= 1) {
66 return 1;
67 }
68 return othersHoldHalf() ? 2 * width : std::max(1, std::min(width, teamWidth()));
69}
70
71namespace detail {
72
73template <class F> struct Call {
74 F *fn;
75 std::atomic<bool> failed{false};
76 std::exception_ptr failure;
77 std::mutex lock;
78
79 void one(std::size_t stage, std::size_t chunk, std::size_t worker) noexcept {
80 if (failed.load(std::memory_order_relaxed)) {
81 return;
82 }
83 try {
84 (*fn)(stage, chunk, static_cast<int>(worker));
85 } catch (...) {
86 const std::lock_guard<std::mutex> hold(lock);
87 if (!failure) {
88 failure = std::current_exception();
89 }
90 failed.store(true, std::memory_order_relaxed);
91 }
92 }
93
94 static void trampoline(void *ctx, std::size_t stage, std::size_t chunk,
95 std::size_t worker) noexcept {
96 static_cast<Call *>(ctx)->one(stage, chunk, worker);
97 }
98};
99
100template <class C> void ompRun(const std::vector<std::size_t> &sizes, C &call) {
101 std::size_t most = 0;
102 for (const std::size_t n : sizes) {
103 most = std::max(most, n);
104 }
105#ifdef SEAMS_HAS_OPENMP
106 const int threads =
107 omp_in_parallel() ? 1 : static_cast<int>(std::min<std::size_t>(
108 static_cast<std::size_t>(omp_get_max_threads()), most));
109 if (threads > 1) {
110#pragma omp parallel num_threads(threads)
111 {
112 const std::size_t worker = static_cast<std::size_t>(omp_get_thread_num());
113 for (std::size_t s = 0; s < sizes.size(); ++s) {
114 const long long count = static_cast<long long>(sizes[s]);
115 // Each thread takes one contiguous run of chunks, as the loops
116 // this replaced did.
117#pragma omp for schedule(static)
118 for (long long c = 0; c < count; ++c) {
119 call.one(s, static_cast<std::size_t>(c), worker);
120 }
121 }
122 }
123 return;
124 }
125#endif
126 for (std::size_t s = 0; s < sizes.size(); ++s) {
127 for (std::size_t c = 0; c < sizes[s]; ++c) {
128 call.one(s, c, 0);
129 }
130 }
131}
132
133} // namespace detail
134
135// Calls fn(stage, chunk, worker) once for every chunk of every stage.
136// Every chunk of a stage returns before any chunk of the next starts.
137// The first exception is rethrown once the region ends; chunks not yet
138// started when it was thrown are skipped.
139template <class F> void run(const std::vector<std::size_t> &sizes, F &&fn) {
140 if (sizes.empty()) {
141 return;
142 }
144 call.fn = &fn;
145#if defined(SEAMS_HAS_LINKCELL) && defined(SEAMS_HAS_LINKCELL_TEAM)
146 bool nested = false;
147#ifdef SEAMS_HAS_OPENMP
148 nested = omp_in_parallel() != 0;
149#endif
150 if (!nested && othersHoldHalf()) {
152 if (lc_team_run(sizes.size(), sizes.data(), &decltype(call)::trampoline, &call) != 0) {
153 throw std::runtime_error("linkcell team run failed");
154 }
155 } else {
156 detail::ompRun(sizes, call);
157 }
158#else
159 detail::ompRun(sizes, call);
160#endif
161 if (call.failure) {
162 std::rethrow_exception(call.failure);
163 }
164}
165
166template <class F> void run(std::initializer_list<std::size_t> chunks, F &&fn) {
167 run(std::vector<std::size_t>(chunks), std::forward<F>(fn));
168}
169
170// Chunks of `grain` items covering 0..n.
171inline std::size_t chunksOf(std::size_t n, std::size_t grain) {
172 return grain == 0 ? 0 : (n + grain - 1) / grain;
173}
174
175// One stage over 0..n in chunks of about `grain`: fn(begin, end,
176// worker). The chunk count is rounded up to a multiple of the workers
177// the run starts, so each takes the same number of equal chunks.
178template <class F> void forRange(std::size_t n, std::size_t grain, F &&fn) {
179 if (n == 0) {
180 return;
181 }
182 std::size_t count = chunksOf(n, std::max<std::size_t>(1, grain));
183 const int most = width();
184 const auto workers = static_cast<std::size_t>(
185 othersHoldHalf() ? most : std::max(1, std::min(most, teamWidth())));
186 if (count > 1 && workers > 1) {
187 count = std::min(n, (count + workers - 1) / workers * workers);
188 }
189 const std::size_t step = chunksOf(n, count);
190 run({chunksOf(n, step)}, [&](std::size_t, std::size_t chunk, int worker) {
191 const std::size_t begin = chunk * step;
192 fn(begin, std::min(n, begin + step), worker);
193 });
194}
195
196} // namespace seams::team
void ompRun(const std::vector< std::size_t > &sizes, C &call)
Definition team_run.hpp:100
void run(const std::vector< std::size_t > &sizes, F &&fn)
Definition team_run.hpp:139
int width()
Definition team_run.hpp:44
int parts(int width)
Definition team_run.hpp:64
void forRange(std::size_t n, std::size_t grain, F &&fn)
Definition team_run.hpp:178
std::size_t chunksOf(std::size_t n, std::size_t grain)
Definition team_run.hpp:171
int teamWidth()
int poolFor(int n)
bool othersHoldHalf()
std::exception_ptr failure
Definition team_run.hpp:76
static void trampoline(void *ctx, std::size_t stage, std::size_t chunk, std::size_t worker) noexcept
Definition team_run.hpp:94
std::atomic< bool > failed
Definition team_run.hpp:75
void one(std::size_t stage, std::size_t chunk, std::size_t worker) noexcept
Definition team_run.hpp:79
void lc_set_threads(int n)