16#ifndef dealii_thread_management_h
17# define dealii_thread_management_h
37# ifdef DEAL_II_WITH_TBB
39# include <tbb/task_group.h>
121 template <
typename ForwardIterator>
122 std::vector<std::pair<ForwardIterator, ForwardIterator>>
124 const ForwardIterator &end,
125 const unsigned int n_intervals);
135 std::vector<std::pair<unsigned int, unsigned int>>
137 const unsigned int end,
138 const unsigned int n_intervals);
190 template <
typename ForwardIterator>
191 std::vector<std::pair<ForwardIterator, ForwardIterator>>
193 const ForwardIterator &end,
194 const unsigned int n_intervals)
196 using IteratorPair = std::pair<ForwardIterator, ForwardIterator>;
202 if (n_intervals == 1)
203 return (std::vector<IteratorPair>(1, IteratorPair(begin, end)));
206 const unsigned int n_elements = std::distance(begin, end);
207 const unsigned int n_elements_per_interval = n_elements / n_intervals;
208 const unsigned int residual = n_elements % n_intervals;
210 std::vector<IteratorPair> return_values(n_intervals);
212 return_values[0].first =
begin;
213 for (
unsigned int i = 0; i < n_intervals; ++i)
215 if (i != n_intervals - 1)
217 return_values[i].second = return_values[i].first;
222 std::advance(return_values[i].
second,
223 static_cast<signed int>(n_elements_per_interval));
227 ++return_values[i].second;
229 return_values[i + 1].first = return_values[i].second;
232 return_values[i].second =
end;
234 return return_values;
262 template <
typename RT>
284 value = std::move(v);
290 value = std::move(v.get());
314 template <
typename RT>
327 inline reference_type
385 template <
typename RT>
387 call(
const std::function<RT()> & function,
390 ret_val.
set(function());
415 template <
typename RT>
495 start(
const std::function<RT()> &function)
498 ret_val = std::make_shared<return_value<RT>>();
541 catch (
const std::exception &exc)
580 template <
typename RT =
void>
588 Thread(
const std::function<RT()> &function)
714 template <
typename T>
733 template <
typename T>
736 static std::reference_wrapper<T>
758 template <
typename RT>
839 template <
typename FunctionObjectType>
842 ->
Thread<
decltype(function_object())>
846 using return_type =
decltype(function_object());
861 template <
typename RT,
typename... Args>
869 [dummy, fun_ptr]() -> RT {
return std_cxx17::apply(fun_ptr, dummy); });
882 template <
typename RT,
typename C,
typename... Args>
889 return new_thread(std::function<RT()>(std::bind(
900 template <
typename RT,
typename C,
typename... Args>
909 return new_thread(std::function<RT()>(std::bind(
925 template <
typename RT =
void>
935 threads.push_back(t);
948 for (
auto &t : threads)
965 template <
typename RT,
typename Function>
969 promise.set_value(function());
980 template <
typename Function>
1017 template <
typename RT =
void>
1032 Task(
const std::function<RT()> &function_object)
1036# ifdef DEAL_II_WITH_TBB
1041 std::unique_ptr<std::promise<RT>> promise =
1042 std::make_unique<std::promise<RT>>();
1044 std::make_shared<TaskData>(std::move(promise->get_future()));
1084 std::shared_ptr<std::promise<RT>>(std::move(promise))]() {
1094 promise->set_exception(std::current_exception());
1123 std::async(std::launch::async | std::launch::deferred,
1139 std::promise<RT> promise;
1140 task_data = std::make_shared<TaskData>(promise.get_future());
1150 promise.set_exception(std::current_exception());
1300 "The current object is not associated with a task that "
1301 "can be joined. It may have been detached, or you "
1302 "may have already joined it in the past.");
1407 std::lock_guard<std::mutex> lock(
mutex);
1412# ifdef DEAL_II_WITH_TBB
1490# ifdef DEAL_II_WITH_TBB
1496 friend class Task<RT>;
1528 template <
typename RT>
1612 template <
typename FunctionObjectType>
1615 ->
Task<
decltype(function_object())>
1617 using return_type =
decltype(function_object());
1619 return new_task(std::function<return_type()>(function_object));
1630 template <
typename RT,
typename... Args>
1636 [dummy, fun_ptr]() -> RT {
return std_cxx17::apply(fun_ptr, dummy); });
1647 template <
typename RT,
typename C,
typename... Args>
1654 return new_task(std::function<RT()>(std::bind(
1664 template <
typename RT,
typename C,
typename... Args>
1671 return new_task(std::function<RT()>(std::bind(
1690 template <
typename RT =
void>
1715 return tasks.size();
1728 for (
auto &t :
tasks)
static void initialize_multithreading()
static unsigned int n_threads()
Mutex & operator=(const Mutex &)
internal::return_value< RT >::reference_type return_value()
Task(const std::function< RT()> &function_object)
std::list< Thread< RT > > threads
ThreadGroup & operator+=(const Thread< RT > &t)
Thread(const std::function< RT()> &function)
internal::return_value< RT >::reference_type return_value()
std::shared_ptr< internal::ThreadDescriptor< RT > > thread_descriptor
bool operator==(const Thread &t) const
Thread(const Thread< RT > &t)
#define DEAL_II_DEPRECATED
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
#define DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
static ::ExceptionBase & ExcNoTask()
TaskGroup & operator+=(const Task< RT > &t)
TaskData(std::future< RT > &&future) noexcept
TaskData(const TaskData &)=delete
std::shared_ptr< TaskData > task_data
TaskData & operator=(const TaskData &)=delete
#define Assert(cond, exc)
std::atomic< bool > task_has_finished
internal::return_value< RT > returned_object
TaskData & operator=(TaskData &&)=delete
std::list< Task< RT > > tasks
#define DeclExceptionMsg(Exception, defaulttext)
TaskData(TaskData &&)=delete
static ::ExceptionBase & ExcInternalError()
internal::return_value< RT >::reference_type get()
tbb::task_group task_group
#define AssertThrow(cond, exc)
std::vector< std::pair< ForwardIterator, ForwardIterator > > split_range(const ForwardIterator &begin, const ForwardIterator &end, const unsigned int n_intervals)
Thread< RT > new_thread(const std::function< RT()> &function)
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 call(const std::function< RT()> &function, internal::return_value< RT > &ret_val)
void handle_std_exception(const std::exception &exc)
VectorType::value_type * end(VectorType &V)
VectorType::value_type * begin(VectorType &V)
Mutex thread_is_active_mutex
std::shared_ptr< return_value< RT > > ret_val
std::atomic< bool > thread_is_active
void start(const std::function< RT()> &function)
static void thread_entry_point(const std::function< RT()> &function, std::shared_ptr< return_value< RT > > ret_val)
static std::reference_wrapper< T > act(T &t)
reference_type get() const
void set_from(std::future< RT & > &v)
void set_from(std::future< void > &)
void set_from(std::future< RT > &v)