15#ifndef dealii_thread_management_h
16#define dealii_thread_management_h
26#ifdef DEAL_II_WITH_TASKFLOW
27# include <taskflow/taskflow.hpp>
40#ifdef DEAL_II_HAVE_CXX20
45#ifdef DEAL_II_WITH_TBB
46# include <tbb/task_group.h>
79 template <
typename ForwardIterator>
80 std::vector<std::pair<ForwardIterator, ForwardIterator>>
82 const ForwardIterator &end,
83 const unsigned int n_intervals);
93 std::vector<std::pair<unsigned int, unsigned int>>
95 const unsigned int end,
96 const unsigned int n_intervals);
148 template <
typename ForwardIterator>
149 std::vector<std::pair<ForwardIterator, ForwardIterator>>
151 const ForwardIterator &end,
152 const unsigned int n_intervals)
154 using IteratorPair = std::pair<ForwardIterator, ForwardIterator>;
160 if (n_intervals == 1)
161 return (std::vector<IteratorPair>(1, IteratorPair(begin, end)));
164 const unsigned int n_elements = std::distance(begin, end);
165 const unsigned int n_elements_per_interval = n_elements / n_intervals;
166 const unsigned int residual = n_elements % n_intervals;
168 std::vector<IteratorPair> return_values(n_intervals);
170 return_values[0].first =
begin;
171 for (
unsigned int i = 0; i < n_intervals; ++i)
173 if (i != n_intervals - 1)
175 return_values[i].second = return_values[i].first;
180 std::advance(return_values[i].
second,
181 static_cast<signed int>(n_elements_per_interval));
185 ++return_values[i].second;
187 return_values[i + 1].first = return_values[i].second;
190 return_values[i].second =
end;
192 return return_values;
220 template <
typename RT>
241 "You cannot read the return value of a thread or task "
242 "if that value has not been set. This happens, for example, if "
243 "a task or thread threw an exception."));
250 value = std::move(v);
265 value = std::move(v.get());
290 template <
typename RT>
302 , value_is_initialized(false)
305 inline reference_type
309 value_is_initialized,
311 "You cannot read the return value of a thread or task "
312 "if that value has not been set. This happens, for example, if "
313 "a task or thread threw an exception."));
336 value_is_initialized =
true;
392 template <
typename T>
411 template <
typename T>
414 static std::reference_wrapper<T>
429 template <
typename RT,
typename Function>
431 (std::invocable<Function> &&
432 std::convertible_to<std::invoke_result_t<Function>, RT>))
435 promise.set_value(function());
448 template <
typename Function>
451 std::promise<
void> &promise)
487 template <
typename RT =
void>
502 Task(
const std::function<RT()> &function_object)
506#ifdef DEAL_II_WITH_TASKFLOW
509#elif defined(DEAL_II_WITH_TBB)
514 std::unique_ptr<std::promise<RT>> promise =
515 std::make_unique<std::promise<RT>>();
517 std::make_shared<TaskData>(std::move(promise->get_future()));
557 std::shared_ptr<std::promise<RT>>(std::move(promise))]() {
567 promise->set_exception(std::current_exception());
596 std::async(std::launch::async | std::launch::deferred,
612 std::promise<RT> promise;
613 task_data = std::make_shared<TaskData>(promise.get_future());
623 promise.set_exception(std::current_exception());
835 "The current object is not associated with a task that "
836 "can be joined. It may have been detached, or you "
837 "may have already joined it in the past.");
858#ifdef DEAL_II_WITH_TBB
859 ,
task_group(std::make_unique<tbb::task_group>())
945 std::lock_guard<std::mutex> lock(
mutex);
950#ifdef DEAL_II_WITH_TASKFLOW
962 return (
future.wait_for(std::chrono::seconds(0)) ==
963 std::future_status::ready);
985#elif defined(DEAL_II_WITH_TBB)
1109#ifdef DEAL_II_WITH_TBB
1119 friend class Task<RT>;
1151 template <
typename RT>
1237 template <
typename FunctionObjectType>
1239 inline auto
new_task(FunctionObjectType function_object)
1240 ->
Task<decltype(function_object())>
1242 using return_type =
decltype(function_object());
1244 return new_task(std::function<return_type()>(function_object));
1255 template <
typename RT,
typename... Args>
1261 [dummy, fun_ptr]() -> RT {
return std::apply(fun_ptr, dummy); });
1307 typename FunctionObject,
1309 typename = std::enable_if_t<std::is_invocable_v<FunctionObject, Args...>>,
1310 typename = std::enable_if_t<std::is_function_v<FunctionObject> ==
false>,
1312 std::enable_if_t<std::is_member_pointer_v<FunctionObject> ==
false>,
1313 typename = std::enable_if_t<std::is_pointer_v<FunctionObject> ==
false>>
1314 inline Task<std::invoke_result_t<FunctionObject, Args...>>
1317 using RT = std::invoke_result_t<FunctionObject, Args...>;
1318 auto dummy = std::make_tuple(std::forward<Args>(args)...);
1319 return new_task([dummy, fun]() -> RT {
return std::apply(fun, dummy); });
1330 template <
typename RT,
typename C,
typename... Args>
1334 std_cxx20::type_identity_t<Args>... args)
1337 return new_task(std::function<RT()>(std::bind(
1347 template <
typename RT,
typename C,
typename... Args>
1351 std_cxx20::type_identity_t<Args>... args)
1354 return new_task(std::function<RT()>(std::bind(
1371 template <
typename RT =
void>
1396 return tasks.size();
1416 std::vector<RT> results;
1417 results.reserve(
size());
1418 for (
auto &t :
tasks)
1419 results.emplace_back(std::move(t.return_value()));
1433 for (
const auto &t :
tasks)
static void initialize_multithreading()
static unsigned int n_threads()
static tf::Executor & get_taskflow_executor()
TaskGroup & operator+=(const Task< RT > &t)
std::vector< RT > return_values()
std::list< Task< RT > > tasks
TaskData(std::future< RT > &&future) noexcept
TaskData(const TaskData &)=delete
TaskData & operator=(const TaskData &)=delete
std::atomic< bool > task_has_finished
internal::return_value< RT > returned_object
std::unique_ptr< tbb::task_group > task_group
TaskData & operator=(TaskData &&)=delete
TaskData(TaskData &&)=delete
internal::return_value< RT >::reference_type get()
Task(Task &&other) noexcept=default
std::shared_ptr< TaskData > task_data
internal::return_value< RT >::reference_type return_value()
Task & operator=(Task &&other) noexcept=default
Task(const Task &other)=default
Task(const std::function< RT()> &function_object)
Task & operator=(const Task &other)=default
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_CXX20_REQUIRES(condition)
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcNoTask()
#define Assert(cond, exc)
#define DeclExceptionMsg(Exception, defaulttext)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
std::vector< std::pair< ForwardIterator, ForwardIterator > > split_range(const ForwardIterator &begin, const ForwardIterator &end, const unsigned int n_intervals)
Task< RT > new_task(const std::function< RT()> &function)
std::vector< std::pair< unsigned int, unsigned int > > split_interval(const unsigned int begin, const unsigned int end, const unsigned int n_intervals)
void handle_unknown_exception()
void evaluate_and_set_promise(Function &function, std::promise< RT > &promise)
void handle_std_exception(const std::exception &exc)
VectorType::value_type * end(VectorType &V)
VectorType::value_type * begin(VectorType &V)
typename type_identity< T >::type type_identity_t
static std::reference_wrapper< T > act(T &t)
reference_type get() const
bool value_is_initialized
void set_from(std::future< RT & > &v)
void set_from(std::future< void > &)
bool value_is_initialized
void set_from(std::future< RT > &v)