Expression Templates Library (ETL)
thread_engine.hpp
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
8 #pragma once
9 
10 namespace etl {
11 
12 #ifdef ETL_PARALLEL_SUPPORT
13 
21 template <typename Pool>
22 struct conf_thread_engine {
30  static void acquire() {
31  cpp_assert(etl::parallel_support, "thread_engine can only be used if paralle support is enabled");
32  cpp_assert(!local_context().serial, "thread_engine cannot be used in serial context");
33  cpp_assert(etl::threads > 1, "thread_engine cannot be used with less than 2");
34  cpp_assert(is_parallel_session(), "thread_engine should only be used in parallel session");
35  }
36 
42  template <typename Functor, typename... Args>
43  static void schedule(Functor&& fun, Args&&... args) {
44  get_pool().do_task(std::forward<Functor>(fun), std::forward<Args>(args)...);
45  }
46 
50  static void wait() {
51  get_pool().wait();
52  }
53 
54 private:
59  static Pool& get_pool() {
60  static Pool pool(etl::threads);
61  return pool;
62  }
63 };
64 
65 using thread_engine = conf_thread_engine<cpp::default_thread_pool<>>;
66 
67 #else
68 
75 struct thread_engine {
83  static void acquire() {
84  cpp_unreachable("thread_engine can only be used if paralle support is enabled");
85  }
86 
91  template <typename Functor, typename... Args>
92  static void schedule([[maybe_unused]] Functor&& fun, [[maybe_unused]] Args&&... args) {
93  cpp_unreachable("thread_engine can only be used if paralle support is enabled");
94  }
95 
99  static void wait() {
100  cpp_unreachable("thread_engine can only be used if paralle support is enabled");
101  }
102 };
103 
104 #endif
105 
106 } //end of namespace etl
The default thread engine when auto-parallelization is not enabled.
Definition: thread_engine.hpp:75
static void schedule([[maybe_unused]] Functor &&fun, [[maybe_unused]] Args &&... args)
Schedule a new task.
Definition: thread_engine.hpp:92
Root namespace for the ETL library.
Definition: adapter.hpp:15
context & local_context()
Return the configuration context of the current thread.
Definition: context.hpp:50
static void acquire()
Acquire the thread engine.
Definition: thread_engine.hpp:83
const size_t threads
The number of threads ETL can use in parallel mode.
Definition: config.hpp:45
constexpr bool parallel_support
Indicates if support for parallelization is integrated into the framework.
Definition: config.hpp:51
auto serial(Expr &&expr) -> serial_expr< detail::build_type< Expr >>
Create a serial expression wrapping the given expression.
Definition: wrapper_expression_builder.hpp:66
bool is_parallel_session()
Indicates if a parallel session is currently active.
Definition: parallel_session.hpp:59
static void wait()
Wait for all the scheduled threads to finish their task.
Definition: thread_engine.hpp:99