UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
execution.hpp
1
42#ifndef UFO_EXECUTION_EXECUTION_HPP
43#define UFO_EXECUTION_EXECUTION_HPP
44
45// STL
46#include <cstdint>
47#include <type_traits>
48#include <utility>
49
50#if defined(UFO_PAR_STL)
51#include <execution>
52#endif
53
54// GCD (Grand Central Dispatch)
55#if defined(UFO_PAR_GCD)
56#include <dispatch/dispatch.h>
57#else
58#define __block
59#endif
60
61// oneTBB (Threading Building Blocks)
62#if defined(UFO_PAR_TBB)
63#include <oneapi/tbb.h>
64#endif
65
66// OMP (OpenMP)
67#if defined(UFO_PAR_OMP)
68#include <omp.h>
69#endif
70
71namespace ufo
72{
73namespace execution
74{
75namespace detail
76{
77enum class ExecutionMode : std::uint32_t {
78 NONE = 0u,
79 SEQ = 1u << 0,
80 UNSEQ = 1u << 1,
81 PAR = 1u << 2,
82 PAR_UNSEQ = 1u << 3,
83};
84
85enum class ExecutionBackend : std::uint32_t {
86 NONE = 0u,
87
88#if defined(UFO_PAR_STL)
89 STL = 1u << 0,
90#else
91 STL = NONE,
92#endif
93
94#if defined(UFO_PAR_GCD)
95 GCD = 1u << 1,
96#else
97 GCD = NONE,
98#endif
99
100#if defined(UFO_PAR_TBB)
101 TBB = 1u << 2,
102#else
103 TBB = NONE,
104#endif
105
106#if defined(UFO_PAR_OMP)
107 OMP = 1u << 3,
108#else
109 OMP = NONE,
110#endif
111
112 ALL = STL | GCD | TBB | OMP
113};
114
115[[nodiscard]] constexpr ExecutionMode operator|(ExecutionMode lhs,
116 ExecutionMode rhs) noexcept
117{
118 return ExecutionMode(std::to_underlying(lhs) | std::to_underlying(rhs));
119}
120
121[[nodiscard]] constexpr ExecutionMode operator&(ExecutionMode lhs,
122 ExecutionMode rhs) noexcept
123{
124 return ExecutionMode(std::to_underlying(lhs) & std::to_underlying(rhs));
125}
126
127[[nodiscard]] constexpr ExecutionBackend operator|(ExecutionBackend lhs,
128 ExecutionBackend rhs) noexcept
129{
130 return ExecutionBackend(std::to_underlying(lhs) | std::to_underlying(rhs));
131}
132
133[[nodiscard]] constexpr ExecutionBackend operator&(ExecutionBackend lhs,
134 ExecutionBackend rhs) noexcept
135{
136 return ExecutionBackend(std::to_underlying(lhs) & std::to_underlying(rhs));
137}
138} // namespace detail
139
140template <detail::ExecutionMode Policy, detail::ExecutionBackend Backend>
142 static constexpr detail::ExecutionMode const policy = Policy;
143 static constexpr detail::ExecutionBackend const backend = Backend;
144};
145
146using sequenced_policy =
148
149using unsequenced_policy =
151
152using parallel_policy =
154
157
160
163
166
169
172
175
178
181
184
187
190
193
196
199
202
205
206template <class T>
207struct is_execution_policy : std::false_type {
208};
209
210template <detail::ExecutionMode Policy, detail::ExecutionBackend Backend>
211 requires(detail::ExecutionBackend::NONE != Backend &&
212 detail::ExecutionMode::NONE != Policy)
215
216template <class T>
217constexpr inline bool is_execution_policy_v =
219
220template <class T>
221constexpr inline bool is_seq_v =
222 detail::ExecutionMode::NONE !=
223 (detail::ExecutionMode::SEQ & std::remove_cvref_t<T>::policy);
224
225template <class T>
226constexpr inline bool is_unseq_v =
227 detail::ExecutionMode::NONE !=
228 (detail::ExecutionMode::UNSEQ & std::remove_cvref_t<T>::policy);
229
230template <class T>
231constexpr inline bool is_par_v =
232 detail::ExecutionMode::NONE !=
233 (detail::ExecutionMode::PAR & std::remove_cvref_t<T>::policy);
234
235template <class T>
236constexpr inline bool is_par_unseq_v =
237 detail::ExecutionMode::NONE !=
238 (detail::ExecutionMode::PAR_UNSEQ & std::remove_cvref_t<T>::policy);
239
240template <class T>
241constexpr inline bool is_stl_v =
242 detail::ExecutionBackend::NONE !=
243 (detail::ExecutionBackend::STL & std::remove_cvref_t<T>::backend);
244
245template <class T>
246constexpr inline bool is_gcd_v =
247 detail::ExecutionBackend::NONE !=
248 (detail::ExecutionBackend::GCD & std::remove_cvref_t<T>::backend);
249
250template <class T>
251constexpr inline bool is_tbb_v =
252 detail::ExecutionBackend::NONE !=
253 (detail::ExecutionBackend::TBB & std::remove_cvref_t<T>::backend);
254
255template <class T>
256constexpr inline bool is_omp_v =
257 detail::ExecutionBackend::NONE !=
258 (detail::ExecutionBackend::OMP & std::remove_cvref_t<T>::backend);
259
260//
261// Concepts
262//
263
264template <class T>
265concept ExecutionPolicy = is_execution_policy_v<T>;
266
267template <class T>
268concept Sequenced = ExecutionPolicy<T> && is_seq_v<T>;
269
270template <class T>
271concept Unsequenced = ExecutionPolicy<T> && is_unseq_v<T>;
272
273template <class T>
274concept Parallel = ExecutionPolicy<T> && is_par_v<T>;
275
276template <class T>
277concept ParallelUnsequenced = ExecutionPolicy<T> && is_par_unseq_v<T>;
278
279template <class T>
280concept STLBackend = ExecutionPolicy<T> && is_stl_v<T>;
281
282template <class T>
283concept GCDBackend = ExecutionPolicy<T> && is_gcd_v<T>;
284
285template <class T>
286concept TBBBackend = ExecutionPolicy<T> && is_tbb_v<T>;
287
288template <class T>
289concept OMPBackend = ExecutionPolicy<T> && is_omp_v<T>;
290
291constexpr inline sequenced_policy seq{};
292constexpr inline unsequenced_policy unseq{};
293constexpr inline parallel_policy par{};
294constexpr inline parallel_unsequenced_policy par_unseq{};
295constexpr inline gcd_sequenced_policy gcd_seq{};
296constexpr inline gcd_unsequenced_policy gcd_unseq{};
297constexpr inline gcd_parallel_policy gcd_par{};
298constexpr inline gcd_parallel_unsequenced_policy gcd_par_unseq{};
299constexpr inline tbb_sequenced_policy tbb_seq{};
300constexpr inline tbb_unsequenced_policy tbb_unseq{};
301constexpr inline tbb_parallel_policy tbb_par{};
302constexpr inline tbb_parallel_unsequenced_policy tbb_par_unseq{};
303constexpr inline omp_sequenced_policy omp_seq{};
304constexpr inline omp_unsequenced_policy omp_unseq{};
305constexpr inline omp_parallel_policy omp_par{};
306constexpr inline omp_parallel_unsequenced_policy omp_par_unseq{};
307
308template <ExecutionPolicy T>
309[[nodiscard]] constexpr auto toSTL([[maybe_unused]] T&& policy)
310{
311#if defined(UFO_PAR_STL)
312 if constexpr (is_stl_v<T>) {
313 if constexpr (is_seq_v<T>) {
314 return std::execution::seq;
315 } else if constexpr (is_unseq_v<T>) {
316 return std::execution::unseq;
317 } else if constexpr (is_par_v<T>) {
318 return std::execution::par;
319 } else if constexpr (is_par_unseq_v<T>) {
320 return std::execution::par_unseq;
321 } else {
322 std::unreachable();
323 }
324 } else {
325 std::unreachable();
326 }
327#else
328 std::unreachable();
329#endif
330}
331} // namespace execution
332
333template <class T>
335} // namespace ufo
336
337#endif // UFO_EXECUTION_EXECUTION_HPP
STL namespace.
All vision-related classes and functions.
Definition cloud.hpp:49