ttg 1.0.0
Template Task Graph (TTG): flowgraph-based programming model for high-performance distributed-memory algorithms
Loading...
Searching...
No Matches
constraint.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2#ifndef TTG_CONSTRAINT_H
3#define TTG_CONSTRAINT_H
4
5#include <functional>
6#include <atomic>
7#include <cstdint>
8#include <mutex>
9#include <map>
10
11#include "ttg/util/span.h"
12
14
15namespace ttg {
16
17 template<typename Key>
19 using key_type = Key;
20 using listener_t = std::function<void(const ttg::span<key_type>&)>;
21
24
26 : m_listeners(std::move(cb.m_listeners))
27 { }
28
30 : m_listeners(cb.m_listeners)
31 {}
32
34 m_listeners = std::move(cb.m_listeners);
35 }
37 m_listeners = cb.m_listeners;
38 }
39
40 virtual ~ConstraintBase() = default;
41
43 auto g = this->lock_guard();
44 m_listeners.insert_or_assign(tt, std::move(l));
45 }
46
47 void notify_listener(const ttg::span<key_type>& keys, ttg::TTBase* tt) {
48 auto& release = m_listeners[tt];
49 release(keys);
50 }
51
52 protected:
53
54 auto lock_guard() {
55 return std::lock_guard{m_mtx};
56 }
57
58 private:
59 std::map<ttg::TTBase*, listener_t> m_listeners;
60 std::mutex m_mtx;
61 };
62
63 template<typename Key,
64 typename Ordinal = std::size_t,
65 typename Compare = std::less<Ordinal>,
66 typename Mapper = ttg::Void>
68
69 using key_type = std::conditional_t<ttg::meta::is_void_v<Key>, ttg::Void, Key>;
70 using ordinal_type = Ordinal;
71 using keymap_t = std::function<Ordinal(const key_type&)>;
72 using compare_t = Compare;
74
75 static_assert((!Compare{}(Ordinal{}, Ordinal{})), "Comparator must provide strict ordering.");
76
77 protected:
79 std::map<ttg::TTBase*, std::vector<key_type>> m_keys;
80
81 sequence_elem_t() = default;
86
87 void add_key(const key_type& key, ttg::TTBase* tt) {
88 auto it = m_keys.find(tt);
89 if (it == m_keys.end()) {
90 m_keys.insert(std::make_pair(tt, std::vector<key_type>{key}));
91 } else {
92 it->second.push_back(key);
93 }
94 }
95 };
96
97 bool comp_equal(const Ordinal& a, const Ordinal& b) const {
98 return (!m_order(a, b) && !m_order(b, a));
99 }
100
101 bool eligible(const Ordinal& ord) const {
102 return m_order(ord, m_current) || comp_equal(ord, m_current);
103 }
104
105 bool check_key_impl(const key_type& key, Ordinal ord, ttg::TTBase *tt) {
106 if (!m_stopped) {
107 if (eligible(ord)) {
108 // key should be executed
109 if (m_auto_release) { // only needed for auto-release
110 m_active.fetch_add(1, std::memory_order_relaxed);
111 // revert the current ordinal to the lower ordinal
112 m_current = ord;
113 }
114 return true;
115 } else if (m_sequence.empty() && m_auto_release && 0 == m_active.load(std::memory_order_relaxed)) {
116 // there are no keys (active or blocked) so we execute to avoid a deadlock
117 // we don't change the current ordinal because there may be lower ordinals coming in later
118 // NOTE: there is a race condition between the check here and the increment above.
119 // This is mostly benign as it can lead to out-of-sequence released tasks.
120 // Avoiding this race would incur significant overheads.
121 m_active.fetch_add(1, std::memory_order_relaxed);
122 return true;
123 }
124 }
125 // key should be deferred
126 auto g = this->lock_guard();
127 if (!m_stopped && eligible(ord)) {
128 // someone released this ordinal while we took the lock
129 return true;
130 }
131 auto it = m_sequence.find(ord);
132 if (it == m_sequence.end()) {
133 auto [iter, success] = m_sequence.insert(std::make_pair(ord, sequence_elem_t{}));
134 assert(success);
135 it = iter;
136 }
137 it->second.add_key(key, tt);
138 return false;
139 }
140
141
143 if (m_auto_release) {
144 auto active = m_active.fetch_sub(1, std::memory_order_relaxed) - 1;
145 if (0 == active) {
146 release_next();
147 }
148 }
149 }
150
151 // used in the auto case
153 if (this->m_stopped) {
154 // don't release tasks if we're stopped
155 return;
156 }
157 // trigger the next sequence
158 sequence_elem_t elem;
159 {
160 // extract the next sequence
161 auto g = this->lock_guard();
162 auto it = this->m_sequence.begin(); // sequence is ordered by ordinal
163 if (it == this->m_sequence.end()) {
164 return; // nothing to be done
165 }
166 this->m_current = it->first;
167 elem = std::move(it->second);
168 this->m_sequence.erase(it);
169 }
170
171 for (auto& seq : elem.m_keys) {
172 // account for the newly active keys
173 this->m_active.fetch_add(seq.second.size(), std::memory_order_relaxed);
174 this->notify_listener(ttg::span<key_type>(seq.second.data(), seq.second.size()), seq.first);
175 }
176 }
177
178
179 // used in the non-auto case
180 void release_next(ordinal_type ord, bool force_check = false) {
181 if (this->m_stopped) {
182 // don't release tasks if we're stopped but remember that this was released
183 this->m_current = ord;
184 return;
185 }
186 if (!force_check && eligible(ord)) {
187 return; // already at the provided ordinal, nothing to be done
188 }
189 // trigger the next sequence(s) (m_sequence is ordered by ordinal)
190 std::vector<sequence_elem_t> seqs;
191 {
192 auto g = this->lock_guard();
193 // set current ordinal
194 this->m_current = ord;
195 for (auto it = this->m_sequence.begin(); it != this->m_sequence.end();) {
196 if (!eligible(it->first)) break;
197 // extract the next sequence
198 this->m_current = it->first;
199 seqs.push_back(std::move(it->second));
200 it = this->m_sequence.erase(it);
201 }
202 }
203 for (auto& elem : seqs) {
204 for (auto& e : elem.m_keys) {
205 // account for the newly active keys
206 this->notify_listener(ttg::span<key_type>(e.second.data(), e.second.size()), e.first);
207 }
208 }
209 }
210
211 public:
212
216 SequencedKeysConstraint(bool auto_release = false)
217 : base_t()
218 , m_auto_release(auto_release)
219 { }
220
221 template<typename Mapper_>
222 requires(std::is_invocable_v<Mapper_, Key>)
223 SequencedKeysConstraint(Mapper_&& map, bool auto_release = false)
224 : base_t()
225 , m_map(std::forward<Mapper_>(map))
226 , m_auto_release(auto_release)
227 { }
228
230
232
234
236
237 virtual ~SequencedKeysConstraint() = default;
238
239 /* Check whether the key may be executed.
240 * Returns true if the key may be executed.
241 * Otherwise, returns false and */
242 template<typename Key_ = key_type, typename Mapper_ = Mapper>
243 std::enable_if_t<!ttg::meta::is_void_v<Key_> && !ttg::meta::is_void_v<Mapper_>, bool>
244 check(const key_type& key, ttg::TTBase *tt) {
245 ordinal_type ord = m_map(key);
246 return this->check_key_impl(key, ord, tt);
247 }
248
249 template<typename Key_ = key_type, typename Mapper_ = Mapper>
250 std::enable_if_t<!ttg::meta::is_void_v<Key_> && ttg::meta::is_void_v<Mapper_>, bool>
251 check(const key_type& key, Ordinal ord, ttg::TTBase *tt) {
252 return this->check_key_impl(key, ord, tt);
253 }
254
255 template<typename Key_ = key_type, typename Mapper_ = Mapper>
256 std::enable_if_t<ttg::meta::is_void_v<Key_> && !ttg::meta::is_void_v<Mapper_>, bool>
258 return this->check_key_impl(ttg::Void{}, m_map(), tt);
259 }
260
261 template<typename Key_ = key_type, typename Mapper_ = Mapper>
262 std::enable_if_t<ttg::meta::is_void_v<Key_> && ttg::meta::is_void_v<Mapper_>, bool>
264 return this->check_key_impl(ttg::Void{}, ord, tt);
265 }
266
267 template<typename Key_ = key_type, typename Mapper_ = Mapper>
268 std::enable_if_t<!ttg::meta::is_void_v<Key_> && !ttg::meta::is_void_v<Mapper_>>
269 complete(const key_type& key, ttg::TTBase *tt) {
270 this->complete_key_impl();
271 }
272
273 template<typename Key_ = key_type, typename Mapper_ = Mapper>
274 std::enable_if_t<!ttg::meta::is_void_v<Key_> && ttg::meta::is_void_v<Mapper_>>
275 complete(const key_type& key, Ordinal ord, ttg::TTBase *tt) {
276 this->complete_key_impl();
277 }
278
279 template<typename Key_ = key_type, typename Mapper_ = Mapper>
280 std::enable_if_t<!ttg::meta::is_void_v<Key_> && ttg::meta::is_void_v<Mapper_>>
281 complete(Ordinal ord, ttg::TTBase *tt) {
282 this->complete_key_impl();
283 }
284
285 template<typename Key_ = key_type, typename Mapper_ = Mapper>
286 std::enable_if_t<!ttg::meta::is_void_v<Key_> && !ttg::meta::is_void_v<Mapper_>>
288 this->complete_key_impl();
289 }
290
296 void stop() {
297 m_stopped = true;
298 }
299
305 void start() {
306 if (m_stopped) {
307 m_stopped = false;
308 if (m_auto_release) {
309 release_next();
310 } else {
311 auto ord = m_current;
312 // release the first set of available keys if none were set explicitly
313 if (ord == std::numeric_limits<ordinal_type>::min() &&
314 this->m_sequence.begin() != this->m_sequence.end()) {
315 ord = this->m_sequence.begin()->first;
316 }
317 release_next(ord, true); // force the check for a next release even if the current ordinal hasn't changed
318 }
319 }
320 }
321
325 void release(ordinal_type ord = 0) {
326 if (m_auto_release) {
327 // last key for this ordinal, release the next
328 // the provided ordinal is ignored
329 release_next();
330 } else {
331 release_next(ord);
332 }
333 }
334
335 bool is_auto() const {
336 return m_auto_release;
337 }
338
339
340 protected:
341 std::map<ordinal_type, sequence_elem_t, compare_t> m_sequence;
342 ordinal_type m_current = std::numeric_limits<ordinal_type>::min();
343 [[no_unique_address]]
344 Mapper m_map;
345 [[no_unique_address]]
347 std::atomic<std::size_t> m_active;
348 bool m_stopped = false;
349 bool m_auto_release = false;
350 };
351
352 // deduction guides: take type of first argument to Mapper as the key type
353 template<typename Mapper, typename = std::enable_if_t<std::is_invocable_v<Mapper, std::decay_t<std::tuple_element_t<0, ttg::meta::callable_args_t<Mapper>>>>>>
356 std::decay_t<std::tuple_element_t<0, ttg::meta::callable_args_t<Mapper>>>,
357 std::decay_t<ttg::meta::callable_return_t<Mapper>>,
358 std::less<std::decay_t<ttg::meta::callable_return_t<Mapper>>>,
359 std::enable_if_t<std::is_invocable_v<Mapper, std::decay_t<std::tuple_element_t<0, ttg::meta::callable_args_t<Mapper>>>>, Mapper>
360 >;
361
362 template<typename Mapper, typename = std::enable_if_t<std::is_invocable_v<Mapper, std::decay_t<std::tuple_element_t<0, ttg::meta::callable_args_t<Mapper>>>>>>
365 std::decay_t<std::tuple_element_t<0, ttg::meta::callable_args_t<Mapper>>>,
366 std::decay_t<ttg::meta::callable_return_t<Mapper>>,
367 std::less<std::decay_t<ttg::meta::callable_return_t<Mapper>>>,
368 std::enable_if_t<std::is_invocable_v<Mapper, std::decay_t<std::tuple_element_t<0, ttg::meta::callable_args_t<Mapper>>>>, Mapper>
369 >;
370
371 template<typename Key, typename Ordinal, typename Compare, typename Mapper>
374
375 template<typename Key, typename Ordinal, typename Compare, typename Mapper>
378
393 template<template<typename...> typename Constraint, typename... Args>
394 auto make_shared_constraint(Args&&... args) {
395 return std::make_shared<decltype(Constraint(std::forward<Args>(args)...))>(std::forward<Args>(args)...);
396 }
397
402 template<typename Constraint, typename... Args>
403 auto make_shared_constraint(Args&&... args) {
404 return std::make_shared<Constraint>(std::forward<Args>(args)...);
405 }
406
407
408
409} // namespace ttg
410
411#endif // TTG_CONSTRAINT_H
A base class for all template tasks.
Definition tt.h:32
A complete version of void.
Definition void.h:12
STL namespace.
constexpr auto get(typelist< T, RestOfTs... >)
Definition typelist.h:102
top-level TTG namespace contains runtime-neutral functionality
Definition keymap.h:9
auto make_shared_constraint(Args &&... args)
Definition constraint.h:394
void notify_listener(const ttg::span< key_type > &keys, ttg::TTBase *tt)
Definition constraint.h:47
ConstraintBase(ConstraintBase &&cb)
Definition constraint.h:25
virtual ~ConstraintBase()=default
ConstraintBase & operator=(const ConstraintBase &cb)
Definition constraint.h:36
ConstraintBase(const ConstraintBase &cb)
Definition constraint.h:29
std::function< void(const ttg::span< key_type > &)> listener_t
Definition constraint.h:20
void add_listener(listener_t l, ttg::TTBase *tt)
Definition constraint.h:42
ConstraintBase & operator=(ConstraintBase &&cb)
Definition constraint.h:33
std::map< ttg::TTBase *, std::vector< key_type > > m_keys
Definition constraint.h:79
sequence_elem_t(sequence_elem_t &&)=default
sequence_elem_t(const sequence_elem_t &)=default
void add_key(const key_type &key, ttg::TTBase *tt)
Definition constraint.h:87
sequence_elem_t & operator=(const sequence_elem_t &)=default
sequence_elem_t & operator=(sequence_elem_t &&)=default
std::enable_if_t<!ttg::meta::is_void_v< Key_ > &&!ttg::meta::is_void_v< Mapper_ > > complete(const key_type &key, ttg::TTBase *tt)
Definition constraint.h:269
SequencedKeysConstraint(bool auto_release=false)
Definition constraint.h:216
std::enable_if_t<!ttg::meta::is_void_v< Key_ > &&ttg::meta::is_void_v< Mapper_ > > complete(Ordinal ord, ttg::TTBase *tt)
Definition constraint.h:281
std::enable_if_t< ttg::meta::is_void_v< Key_ > &&!ttg::meta::is_void_v< Mapper_ >, bool > check(ttg::TTBase *tt)
Definition constraint.h:257
SequencedKeysConstraint & operator=(SequencedKeysConstraint &&skc)=default
bool check_key_impl(const key_type &key, Ordinal ord, ttg::TTBase *tt)
Definition constraint.h:105
std::enable_if_t< ttg::meta::is_void_v< Key_ > &&ttg::meta::is_void_v< Mapper_ >, bool > check(ordinal_type ord, ttg::TTBase *tt)
Definition constraint.h:263
SequencedKeysConstraint & operator=(const SequencedKeysConstraint &skc)=default
ConstraintBase< Key > base_t
Definition constraint.h:73
std::enable_if_t<!ttg::meta::is_void_v< Key_ > &&ttg::meta::is_void_v< Mapper_ >, bool > check(const key_type &key, Ordinal ord, ttg::TTBase *tt)
Definition constraint.h:251
std::atomic< std::size_t > m_active
Definition constraint.h:347
bool comp_equal(const Ordinal &a, const Ordinal &b) const
Definition constraint.h:97
virtual ~SequencedKeysConstraint()=default
void release(ordinal_type ord=0)
Definition constraint.h:325
std::conditional_t< ttg::meta::is_void_v< Key >, ttg::Void, Key > key_type
Definition constraint.h:69
std::map< ordinal_type, sequence_elem_t, compare_t > m_sequence
Definition constraint.h:341
std::enable_if_t<!ttg::meta::is_void_v< Key_ > &&!ttg::meta::is_void_v< Mapper_ >, bool > check(const key_type &key, ttg::TTBase *tt)
Definition constraint.h:244
void release_next(ordinal_type ord, bool force_check=false)
Definition constraint.h:180
SequencedKeysConstraint(const SequencedKeysConstraint &skc)=default
std::enable_if_t<!ttg::meta::is_void_v< Key_ > &&ttg::meta::is_void_v< Mapper_ > > complete(const key_type &key, Ordinal ord, ttg::TTBase *tt)
Definition constraint.h:275
std::function< Ordinal(const key_type &)> keymap_t
Definition constraint.h:71
std::enable_if_t<!ttg::meta::is_void_v< Key_ > &&!ttg::meta::is_void_v< Mapper_ > > complete(ttg::TTBase *tt)
Definition constraint.h:287
SequencedKeysConstraint(Mapper_ &&map, bool auto_release=false)
Definition constraint.h:223
bool eligible(const Ordinal &ord) const
Definition constraint.h:101
SequencedKeysConstraint(SequencedKeysConstraint &&skc)=default