1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
//
// MIT License
// Copyright (c) 2020 Jonathan R. Madsen
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//
// ---------------------------------------------------------------
// Tasking class header file
//
// Class Description:
//
// This file creates the a class for handling a group of tasks that
// can be independently joined
//
// ---------------------------------------------------------------
// Author: Jonathan Madsen (Feb 13th 2018)
// ---------------------------------------------------------------
#pragma once
#include "PTL/JoinFunction.hh"
#include "PTL/Task.hh"
#include "PTL/ThreadData.hh"
#include "PTL/ThreadPool.hh"
#include "PTL/Utility.hh"
#include <atomic>
#include <cstdint>
#include <cstdlib>
#include <deque>
#include <future>
#include <iostream>
#include <memory>
#include <vector>
#if defined(PTL_USE_TBB)
# include <tbb/tbb.h>
#endif
namespace PTL
{
namespace internal
{
std::atomic_uintmax_t&
task_group_counter();
ThreadPool*
get_default_threadpool();
intmax_t
get_task_depth();
} // namespace internal
template <typename Tp, typename Arg = Tp, intmax_t MaxDepth = 0>
class TaskGroup
{
public:
//------------------------------------------------------------------------//
template <typename Up>
using container_type = std::vector<Up>;
using tid_type = std::thread::id;
using size_type = uintmax_t;
using lock_t = Mutex;
using atomic_int = std::atomic_intmax_t;
using atomic_uint = std::atomic_uintmax_t;
using condition_t = Condition;
using ArgTp = decay_t<Arg>;
using result_type = Tp;
using task_pointer = std::shared_ptr<TaskFuture<ArgTp>>;
using task_list_t = container_type<task_pointer>;
using this_type = TaskGroup<Tp, Arg, MaxDepth>;
using promise_type = std::promise<ArgTp>;
using future_type = std::future<ArgTp>;
using packaged_task_type = std::packaged_task<ArgTp()>;
using future_list_t = container_type<future_type>;
using join_type = typename JoinFunction<Tp, Arg>::Type;
using iterator = typename future_list_t::iterator;
using reverse_iterator = typename future_list_t::reverse_iterator;
using const_iterator = typename future_list_t::const_iterator;
using const_reverse_iterator = typename future_list_t::const_reverse_iterator;
//------------------------------------------------------------------------//
template <typename... Args>
using task_type = Task<ArgTp, decay_t<Args>...>;
//------------------------------------------------------------------------//
public:
// Constructor
template <typename Func>
TaskGroup(Func&& _join, ThreadPool* _tp = internal::get_default_threadpool());
template <typename Up = Tp>
TaskGroup(ThreadPool* _tp = internal::get_default_threadpool(),
enable_if_t<std::is_void<Up>::value, int> = 0);
// Destructor
~TaskGroup();
// delete copy-construct
TaskGroup(const this_type&) = delete;
// define move-construct
TaskGroup(this_type&& rhs) = default;
// delete copy-assign
TaskGroup& operator=(const this_type& rhs) = delete;
// define move-assign
TaskGroup& operator=(this_type&& rhs) = default;
public:
template <typename Up>
std::shared_ptr<Up> operator+=(std::shared_ptr<Up>&& _task);
// wait to finish
void wait();
// increment (prefix)
intmax_t operator++() { return ++(m_tot_task_count); }
intmax_t operator++(int) { return (m_tot_task_count)++; }
intmax_t operator--() { return --(m_tot_task_count); }
intmax_t operator--(int) { return (m_tot_task_count)--; }
// size
intmax_t size() const { return m_tot_task_count.load(); }
// get the locks/conditions
lock_t& task_lock() { return m_task_lock; }
condition_t& task_cond() { return m_task_cond; }
// identifier
uintmax_t id() const { return m_id; }
// thread pool
void set_pool(ThreadPool* tp) { m_pool = tp; }
ThreadPool*& pool() { return m_pool; }
ThreadPool* pool() const { return m_pool; }
bool is_native_task_group() const { return (m_tbb_task_group) ? false : true; }
bool is_main() const { return this_tid() == m_main_tid; }
// check if any tasks are still pending
intmax_t pending() { return m_tot_task_count.load(); }
static void set_verbose(int level) { f_verbose = level; }
ScopeDestructor get_scope_destructor();
void notify();
void notify_all();
void reserve(size_t _n)
{
m_task_list.reserve(_n);
m_future_list.reserve(_n);
}
public:
template <typename Func, typename... Args>
std::shared_ptr<task_type<Args...>> wrap(Func func, Args... args)
{
return operator+=(std::make_shared<task_type<Args...>>(
is_native_task_group(), m_depth, std::move(func), std::move(args)...));
}
template <typename Func, typename... Args, typename Up = Tp>
enable_if_t<std::is_void<Up>::value, void> exec(Func func, Args... args);
template <typename Func, typename... Args, typename Up = Tp>
enable_if_t<!std::is_void<Up>::value, void> exec(Func func, Args... args);
template <typename Func, typename... Args>
void run(Func func, Args... args)
{
exec(std::move(func), std::move(args)...);
}
protected:
template <typename Up, typename Func, typename... Args>
enable_if_t<std::is_void<Up>::value, void> local_exec(Func func, Args... args);
template <typename Up, typename Func, typename... Args>
enable_if_t<!std::is_void<Up>::value, void> local_exec(Func func, Args... args);
// shorter typedefs
using itr_t = iterator;
using citr_t = const_iterator;
using ritr_t = reverse_iterator;
using critr_t = const_reverse_iterator;
public:
//------------------------------------------------------------------------//
// Get tasks with non-void return types
//
future_list_t& get_tasks() { return m_future_list; }
const future_list_t& get_tasks() const { return m_future_list; }
//------------------------------------------------------------------------//
// iterate over tasks with return type
//
itr_t begin() { return m_future_list.begin(); }
itr_t end() { return m_future_list.end(); }
citr_t begin() const { return m_future_list.begin(); }
citr_t end() const { return m_future_list.end(); }
citr_t cbegin() const { return m_future_list.begin(); }
citr_t cend() const { return m_future_list.end(); }
ritr_t rbegin() { return m_future_list.rbegin(); }
ritr_t rend() { return m_future_list.rend(); }
critr_t rbegin() const { return m_future_list.rbegin(); }
critr_t rend() const { return m_future_list.rend(); }
//------------------------------------------------------------------------//
// wait to finish
template <typename Up = Tp, enable_if_t<!std::is_void<Up>::value, int> = 0>
inline Up join(Up accum = {});
//------------------------------------------------------------------------//
// wait to finish
template <typename Up = Tp, typename Rp = Arg,
enable_if_t<std::is_void<Up>::value && std::is_void<Rp>::value, int> = 0>
inline void join();
//------------------------------------------------------------------------//
// wait to finish
template <typename Up = Tp, typename Rp = Arg,
enable_if_t<std::is_void<Up>::value && !std::is_void<Rp>::value, int> = 0>
inline void join();
//------------------------------------------------------------------------//
// clear the task result history
void clear();
protected:
//------------------------------------------------------------------------//
// get the thread id
static tid_type this_tid() { return std::this_thread::get_id(); }
//------------------------------------------------------------------------//
// get the task count
atomic_int& task_count() { return m_tot_task_count; }
const atomic_int& task_count() const { return m_tot_task_count; }
protected:
static int f_verbose;
// Private variables
uintmax_t m_id = internal::task_group_counter()++;
intmax_t m_depth = internal::get_task_depth();
tid_type m_main_tid = std::this_thread::get_id();
atomic_int m_tot_task_count{ 0 };
lock_t m_task_lock = {};
condition_t m_task_cond = {};
join_type m_join{};
ThreadPool* m_pool = internal::get_default_threadpool();
tbb_task_group_t* m_tbb_task_group = nullptr;
task_list_t m_task_list = {};
future_list_t m_future_list = {};
private:
void internal_update();
};
} // namespace PTL
#include "PTL/TaskGroup.icc"
|