Geant4-11
|
#include <ThreadPool.hh>
Public Types | |
typedef std::function< intmax_t(intmax_t)> | affinity_func_t |
using | atomic_bool_type = std::shared_ptr< std::atomic_bool > |
using | atomic_int_type = std::shared_ptr< std::atomic_uintmax_t > |
typedef std::vector< bool > | bool_list_t |
using | condition_t = std::shared_ptr< Condition > |
typedef std::function< void()> | initialize_func_t |
using | lock_t = std::shared_ptr< Mutex > |
using | pool_state_type = std::shared_ptr< std::atomic_short > |
using | size_type = size_t |
using | task_count_type = std::shared_ptr< std::atomic_uintmax_t > |
using | task_pointer = std::shared_ptr< task_type > |
using | task_queue_t = VUserTaskQueue |
using | task_type = VTask |
using | thread_data_t = std::vector< std::shared_ptr< ThreadData > > |
typedef std::map< ThreadId, uintmax_t > | thread_id_map_t |
typedef std::map< uintmax_t, ThreadId > | thread_index_map_t |
typedef std::deque< ThreadId > | thread_list_t |
using | thread_vec_t = std::vector< Thread > |
template<typename KeyT , typename MappedT , typename HashT = KeyT> | |
using | uomap = std::unordered_map< KeyT, MappedT, std::hash< HashT > > |
Public Member Functions | |
size_type | add_task (task_pointer &&task, int bin=-1) |
template<typename ListT > | |
size_type | add_tasks (ListT &) |
size_type | destroy_threadpool () |
template<typename FuncT > | |
void | execute_on_all_threads (FuncT &&_func) |
template<typename FuncT > | |
void | execute_on_specific_threads (const std::set< std::thread::id > &_tid, FuncT &&_func) |
int | get_active_threads_count () const |
task_queue_t * | get_queue () const |
tbb_task_arena_t * | get_task_arena () |
Thread * | get_thread (size_type _n) const |
Thread * | get_thread (std::thread::id id) const |
task_queue_t *& | get_valid_queue (task_queue_t *&) const |
int | get_verbose () const |
size_type | initialize_threadpool (size_type) |
bool | is_alive () |
bool | is_initialized () const |
bool | is_main () const |
bool | is_tbb_threadpool () const |
void | notify () |
void | notify (size_type) |
void | notify_all () |
ThreadPool & | operator= (const ThreadPool &)=delete |
ThreadPool & | operator= (ThreadPool &&)=default |
void | reset_initialization () |
void | resize (size_type _n) |
void | set_affinity (affinity_func_t f) |
void | set_affinity (intmax_t i, Thread &) |
void | set_initialization (initialize_func_t f) |
void | set_verbose (int n) |
size_type | size () const |
const pool_state_type & | state () const |
size_type | stop_thread () |
ThreadPool (const size_type &pool_size, VUserTaskQueue *task_queue=nullptr, bool _use_affinity=GetEnv< bool >("PTL_CPU_AFFINITY", false), affinity_func_t=[](intmax_t) { static std::atomic< intmax_t > assigned;intmax_t _assign=assigned++;return _assign % Thread::hardware_concurrency();}) | |
ThreadPool (const ThreadPool &)=delete | |
ThreadPool (ThreadPool &&)=default | |
bool | using_affinity () const |
virtual | ~ThreadPool () |
Static Public Member Functions | |
static uintmax_t | add_thread_id () |
static uintmax_t | get_this_thread_id () |
static const thread_id_map_t & | get_thread_ids () |
static void | set_use_tbb (bool val) |
static tbb_global_control_t *& | tbb_global_control () |
static bool | using_tbb () |
Protected Member Functions | |
void | execute_thread (VUserTaskQueue *) |
int | insert (task_pointer &&, int=-1) |
void | record_entry () |
void | record_exit () |
int | run_on_this (task_pointer &&) |
Static Protected Member Functions | |
static void | start_thread (ThreadPool *, thread_data_t *, intmax_t=-1) |
Private Attributes | |
affinity_func_t | m_affinity_func |
atomic_bool_type | m_alive_flag = std::make_shared<std::atomic_bool>(false) |
bool | m_delete_task_queue = false |
initialize_func_t | m_init_func = []() {} |
bool_list_t | m_is_joined {} |
bool_list_t | m_is_stopped {} |
thread_list_t | m_main_threads {} |
ThreadId | m_main_tid = ThisThread::get_id() |
size_type | m_pool_size = 0 |
pool_state_type | m_pool_state = std::make_shared<std::atomic_short>(0) |
thread_list_t | m_stop_threads {} |
condition_t | m_task_cond = std::make_shared<Condition>() |
lock_t | m_task_lock = std::make_shared<Mutex>() |
task_queue_t * | m_task_queue = nullptr |
tbb_task_arena_t * | m_tbb_task_arena = nullptr |
tbb_task_group_t * | m_tbb_task_group = nullptr |
bool | m_tbb_tp = false |
atomic_int_type | m_thread_active = std::make_shared<std::atomic_uintmax_t>() |
atomic_int_type | m_thread_awake = std::make_shared<std::atomic_uintmax_t>() |
thread_data_t | m_thread_data {} |
thread_vec_t | m_threads {} |
bool | m_use_affinity = false |
int | m_verbose = GetEnv<int>("PTL_VERBOSE", 0) |
Static Private Attributes | |
static PTL_DLL thread_id_map_t | f_thread_ids |
static PTL_DLL bool | f_use_tbb = false |
Definition at line 68 of file ThreadPool.hh.
typedef std::function<intmax_t(intmax_t)> PTL::ThreadPool::affinity_func_t |
Definition at line 95 of file ThreadPool.hh.
using PTL::ThreadPool::atomic_bool_type = std::shared_ptr<std::atomic_bool> |
Definition at line 79 of file ThreadPool.hh.
using PTL::ThreadPool::atomic_int_type = std::shared_ptr<std::atomic_uintmax_t> |
Definition at line 77 of file ThreadPool.hh.
typedef std::vector<bool> PTL::ThreadPool::bool_list_t |
Definition at line 88 of file ThreadPool.hh.
using PTL::ThreadPool::condition_t = std::shared_ptr<Condition> |
Definition at line 83 of file ThreadPool.hh.
typedef std::function<void()> PTL::ThreadPool::initialize_func_t |
Definition at line 94 of file ThreadPool.hh.
using PTL::ThreadPool::lock_t = std::shared_ptr<Mutex> |
Definition at line 82 of file ThreadPool.hh.
using PTL::ThreadPool::pool_state_type = std::shared_ptr<std::atomic_short> |
Definition at line 78 of file ThreadPool.hh.
using PTL::ThreadPool::size_type = size_t |
Definition at line 75 of file ThreadPool.hh.
using PTL::ThreadPool::task_count_type = std::shared_ptr<std::atomic_uintmax_t> |
Definition at line 76 of file ThreadPool.hh.
using PTL::ThreadPool::task_pointer = std::shared_ptr<task_type> |
Definition at line 84 of file ThreadPool.hh.
Definition at line 85 of file ThreadPool.hh.
using PTL::ThreadPool::task_type = VTask |
Definition at line 81 of file ThreadPool.hh.
using PTL::ThreadPool::thread_data_t = std::vector<std::shared_ptr<ThreadData> > |
Definition at line 92 of file ThreadPool.hh.
typedef std::map<ThreadId, uintmax_t> PTL::ThreadPool::thread_id_map_t |
Definition at line 89 of file ThreadPool.hh.
typedef std::map<uintmax_t, ThreadId> PTL::ThreadPool::thread_index_map_t |
Definition at line 90 of file ThreadPool.hh.
typedef std::deque<ThreadId> PTL::ThreadPool::thread_list_t |
Definition at line 87 of file ThreadPool.hh.
using PTL::ThreadPool::thread_vec_t = std::vector<Thread> |
Definition at line 91 of file ThreadPool.hh.
using PTL::ThreadPool::uomap = std::unordered_map<KeyT, MappedT, std::hash<HashT> > |
Definition at line 72 of file ThreadPool.hh.
ThreadPool::ThreadPool | ( | const size_type & | pool_size, |
VUserTaskQueue * | task_queue = nullptr , |
||
bool | _use_affinity = GetEnv<bool>("PTL_CPU_AFFINITY", false) , |
||
affinity_func_t | _affinity_func = [](intmax_t) { static std::atomic<intmax_t> assigned; intmax_t _assign = assigned++; return _assign % Thread::hardware_concurrency(); } |
||
) |
Definition at line 140 of file ThreadPool.cc.
References get_this_thread_id(), initialize_threadpool(), m_pool_size, m_task_queue, m_verbose, and anonymous_namespace{ThreadPool.cc}::thread_data().
|
virtual |
Definition at line 162 of file ThreadPool.cc.
References m_alive_flag, m_pool_state, m_task_cond, m_task_lock, m_threads, and PTL::thread_pool::state::STOPPED.
|
delete |
|
default |
|
inline |
Definition at line 380 of file ThreadPool.hh.
References insert(), m_alive_flag, m_tbb_tp, and run_on_this().
Referenced by PTL::TaskManager::async(), and PTL::TaskManager::exec().
|
inline |
Definition at line 391 of file ThreadPool.hh.
References get_valid_queue(), PTL::VUserTaskQueue::InsertTask(), m_alive_flag, m_task_queue, and notify().
|
inlinestatic |
Definition at line 191 of file ThreadPool.hh.
References f_thread_ids, and PTL::Threading::SetThreadId().
Referenced by execute_on_all_threads(), and execute_on_specific_threads().
ThreadPool::size_type ThreadPool::destroy_threadpool | ( | ) |
Definition at line 364 of file ThreadPool.cc.
References PTL::tbb::task_arena::execute(), f_thread_ids, get_verbose(), m_alive_flag, m_delete_task_queue, m_is_joined, m_main_threads, m_pool_state, m_task_cond, m_task_lock, m_task_queue, m_tbb_task_arena, m_tbb_task_group, m_tbb_tp, m_thread_active, m_thread_data, m_threads, PTL::thread_pool::state::STOPPED, tbb_global_control(), and PTL::tbb::task_group::wait().
Referenced by PTL::TaskManager::finalize(), PTL::TaskRunManager::Terminate(), and G4TaskRunManager::~G4TaskRunManager().
|
inline |
Definition at line 423 of file ThreadPool.hh.
References PTL::tbb::global_control::active_value(), add_thread_id(), PTL::VUserTaskQueue::ExecuteOnAllThreads(), get_queue(), get_task_arena(), get_verbose(), PTL::Threading::GetNumberOfCores(), is_main(), m_tbb_task_group, m_tbb_tp, PTL::tbb::global_control::max_allowed_parallelism, G4INCL::Math::min(), PTL::tbb::task_group::run(), size(), tbb_global_control(), and PTL::tbb::task_group::wait().
Referenced by G4TaskRunManager::CreateAndStartWorkers(), G4TaskRunManager::RequestWorkersProcessCommandsStack(), G4TaskRunManager::TerminateWorkers(), and G4TaskRunManager::WaitForEndEventLoopWorkers().
|
inline |
Definition at line 541 of file ThreadPool.hh.
References add_thread_id(), PTL::VUserTaskQueue::ExecuteOnSpecificThreads(), get_queue(), get_task_arena(), get_verbose(), PTL::Threading::GetNumberOfCores(), m_tbb_task_group, m_tbb_tp, PTL::tbb::task_group::run(), size(), and PTL::tbb::task_group::wait().
|
protected |
Definition at line 546 of file ThreadPool.cc.
References PTL::ThreadData::current_queue, PTL::VUserTaskQueue::empty(), PTL::VUserTaskQueue::GetTask(), m_init_func, m_is_stopped, m_pool_size, m_pool_state, m_stop_threads, m_task_cond, m_task_lock, m_thread_awake, PTL::thread_pool::state::PARTIAL, PTL::thread_pool::state::STOPPED, anonymous_namespace{ThreadPool.cc}::thread_data(), PTL::VUserTaskQueue::true_size(), PTL::ThreadData::update(), and PTL::ThreadData::within_task.
|
inline |
Definition at line 173 of file ThreadPool.hh.
References m_thread_awake.
|
inline |
Definition at line 127 of file ThreadPool.hh.
References m_task_queue.
Referenced by execute_on_all_threads(), execute_on_specific_threads(), and PTL::ThreadData::update().
|
inline |
Definition at line 321 of file ThreadPool.hh.
References PTL::tbb::global_control::active_value(), PTL::tbb::task_arena::initialize(), m_tbb_task_arena, PTL::tbb::global_control::max_allowed_parallelism, size(), and tbb_global_control().
Referenced by execute_on_all_threads(), execute_on_specific_threads(), and run_on_this().
|
static |
Definition at line 122 of file ThreadPool.cc.
References f_thread_ids.
Referenced by PTL::UserTaskQueue::GetThreadBin(), initialize_threadpool(), G4TaskRunManagerKernel::InitializeWorker(), ThreadPool(), and PTL::UserTaskQueue::UserTaskQueue().
Thread * PTL::ThreadPool::get_thread | ( | std::thread::id | id | ) | const |
|
static |
Definition at line 114 of file ThreadPool.cc.
References f_thread_ids.
ThreadPool::task_queue_t *& ThreadPool::get_valid_queue | ( | task_queue_t *& | _queue | ) | const |
Definition at line 536 of file ThreadPool.cc.
References m_pool_size.
Referenced by add_tasks(), and insert().
|
inline |
Definition at line 182 of file ThreadPool.hh.
References m_verbose.
Referenced by destroy_threadpool(), execute_on_all_threads(), and execute_on_specific_threads().
ThreadPool::size_type ThreadPool::initialize_threadpool | ( | size_type | proposed_size | ) |
Definition at line 213 of file ThreadPool.cc.
References f_use_tbb, get_this_thread_id(), m_alive_flag, m_delete_task_queue, m_is_joined, m_main_threads, m_pool_size, m_pool_state, m_task_lock, m_task_queue, m_tbb_task_group, m_tbb_tp, m_thread_data, m_threads, m_use_affinity, m_verbose, PTL::tbb::global_control::max_allowed_parallelism, set_affinity(), start_thread(), PTL::thread_pool::state::STARTED, stop_thread(), and tbb_global_control().
Referenced by resize(), and ThreadPool().
|
inlineprotected |
Definition at line 369 of file ThreadPool.hh.
References get_valid_queue(), PTL::ThreadData::GetInstance(), PTL::VUserTaskQueue::InsertTask(), m_task_queue, and notify().
Referenced by add_task().
|
inline |
bool ThreadPool::is_initialized | ( | ) | const |
Definition at line 183 of file ThreadPool.cc.
References m_pool_state, and PTL::thread_pool::state::NONINIT.
|
inline |
Definition at line 183 of file ThreadPool.hh.
References m_main_tid.
Referenced by execute_on_all_threads().
|
inline |
Definition at line 130 of file ThreadPool.hh.
References m_tbb_tp.
Referenced by G4TaskRunManager::InitializeThreadPool().
|
inline |
Definition at line 271 of file ThreadPool.hh.
References m_pool_size, m_task_cond, m_task_lock, and m_thread_awake.
Referenced by add_tasks(), and insert().
|
inline |
Definition at line 290 of file ThreadPool.hh.
References m_pool_size, m_task_cond, m_task_lock, m_thread_awake, and size().
|
inline |
Definition at line 282 of file ThreadPool.hh.
References m_task_cond, and m_task_lock.
|
delete |
|
default |
|
inlineprotected |
Definition at line 215 of file ThreadPool.hh.
References m_thread_active.
|
inlineprotected |
Definition at line 221 of file ThreadPool.hh.
References m_thread_active.
|
inline |
Definition at line 153 of file ThreadPool.hh.
References m_init_func.
|
inline |
Definition at line 342 of file ThreadPool.hh.
References initialize_threadpool(), m_pool_size, m_task_queue, and PTL::VUserTaskQueue::resize().
Referenced by PTL::TaskRunManager::Initialize(), and G4TaskRunManager::SetNumberOfThreads().
|
inlineprotected |
Definition at line 351 of file ThreadPool.hh.
References get_task_arena(), m_tbb_task_group, m_tbb_tp, and PTL::tbb::task_group::run().
Referenced by add_task().
|
inline |
Definition at line 178 of file ThreadPool.hh.
References m_affinity_func.
Referenced by initialize_threadpool().
void ThreadPool::set_affinity | ( | intmax_t | i, |
Thread & | _thread | ||
) |
Definition at line 191 of file ThreadPool.cc.
References m_affinity_func, m_verbose, and PTL::Threading::SetPinAffinity().
|
inline |
|
static |
Definition at line 102 of file ThreadPool.cc.
References f_use_tbb.
Referenced by G4TaskRunManager::G4TaskRunManager(), and PTL::TaskRunManager::TaskRunManager().
|
inline |
|
inline |
Definition at line 163 of file ThreadPool.hh.
References m_pool_size.
Referenced by G4TaskRunManager::ComputeNumberOfTasks(), execute_on_all_threads(), execute_on_specific_threads(), get_task_arena(), PTL::TaskRunManager::GetNumberActiveThreads(), PTL::TaskRunManager::GetNumberOfThreads(), PTL::TaskRunManager::Initialize(), G4TaskRunManagerKernel::InitializeWorker(), notify(), and PTL::TaskManager::size().
|
staticprotected |
Definition at line 72 of file ThreadPool.cc.
References f_thread_ids, PTL::Threading::SetThreadId(), anonymous_namespace{ThreadPool.cc}::thread_data(), and G4InuclParticleNames::tp.
Referenced by initialize_threadpool().
|
inline |
ThreadPool::size_type ThreadPool::stop_thread | ( | ) |
Definition at line 495 of file ThreadPool.cc.
References m_alive_flag, m_is_joined, m_is_stopped, m_main_threads, m_pool_size, m_stop_threads, m_task_cond, and m_task_lock.
Referenced by initialize_threadpool().
|
inlinestatic |
Definition at line 313 of file ThreadPool.hh.
Referenced by destroy_threadpool(), execute_on_all_threads(), get_task_arena(), and initialize_threadpool().
|
inline |
|
static |
Definition at line 94 of file ThreadPool.cc.
References f_use_tbb.
Referenced by PTL::TaskRunManager::Initialize().
|
staticprivate |
Definition at line 265 of file ThreadPool.hh.
Referenced by add_thread_id(), destroy_threadpool(), get_this_thread_id(), get_thread_ids(), and start_thread().
|
staticprivate |
Definition at line 266 of file ThreadPool.hh.
Referenced by initialize_threadpool(), set_use_tbb(), and using_tbb().
|
private |
Definition at line 261 of file ThreadPool.hh.
Referenced by set_affinity().
|
private |
Definition at line 236 of file ThreadPool.hh.
Referenced by add_task(), add_tasks(), destroy_threadpool(), initialize_threadpool(), is_alive(), stop_thread(), and ~ThreadPool().
|
private |
Definition at line 232 of file ThreadPool.hh.
Referenced by destroy_threadpool(), and initialize_threadpool().
|
private |
Definition at line 260 of file ThreadPool.hh.
Referenced by execute_thread(), reset_initialization(), and set_initialization().
|
private |
Definition at line 247 of file ThreadPool.hh.
Referenced by destroy_threadpool(), initialize_threadpool(), and stop_thread().
|
private |
Definition at line 248 of file ThreadPool.hh.
Referenced by execute_thread(), and stop_thread().
|
private |
Definition at line 249 of file ThreadPool.hh.
Referenced by destroy_threadpool(), initialize_threadpool(), and stop_thread().
|
private |
Definition at line 235 of file ThreadPool.hh.
Referenced by is_main().
|
private |
Definition at line 234 of file ThreadPool.hh.
Referenced by execute_thread(), get_valid_queue(), initialize_threadpool(), notify(), resize(), size(), stop_thread(), and ThreadPool().
|
private |
Definition at line 237 of file ThreadPool.hh.
Referenced by destroy_threadpool(), execute_thread(), initialize_threadpool(), is_initialized(), state(), and ~ThreadPool().
|
private |
Definition at line 250 of file ThreadPool.hh.
Referenced by execute_thread(), and stop_thread().
|
private |
Definition at line 244 of file ThreadPool.hh.
Referenced by destroy_threadpool(), execute_thread(), notify(), notify_all(), stop_thread(), and ~ThreadPool().
Definition at line 242 of file ThreadPool.hh.
Referenced by destroy_threadpool(), execute_thread(), initialize_threadpool(), notify(), notify_all(), stop_thread(), and ~ThreadPool().
|
private |
Definition at line 255 of file ThreadPool.hh.
Referenced by add_tasks(), destroy_threadpool(), get_queue(), initialize_threadpool(), insert(), resize(), and ThreadPool().
|
private |
Definition at line 256 of file ThreadPool.hh.
Referenced by destroy_threadpool(), and get_task_arena().
|
private |
Definition at line 257 of file ThreadPool.hh.
Referenced by destroy_threadpool(), execute_on_all_threads(), execute_on_specific_threads(), initialize_threadpool(), and run_on_this().
|
private |
Definition at line 231 of file ThreadPool.hh.
Referenced by add_task(), destroy_threadpool(), execute_on_all_threads(), execute_on_specific_threads(), initialize_threadpool(), is_tbb_threadpool(), and run_on_this().
|
private |
Definition at line 239 of file ThreadPool.hh.
Referenced by destroy_threadpool(), record_entry(), and record_exit().
|
private |
Definition at line 238 of file ThreadPool.hh.
Referenced by execute_thread(), get_active_threads_count(), and notify().
|
private |
Definition at line 252 of file ThreadPool.hh.
Referenced by destroy_threadpool(), and initialize_threadpool().
|
private |
Definition at line 251 of file ThreadPool.hh.
Referenced by destroy_threadpool(), initialize_threadpool(), and ~ThreadPool().
|
private |
Definition at line 230 of file ThreadPool.hh.
Referenced by initialize_threadpool(), and using_affinity().
|
private |
Definition at line 233 of file ThreadPool.hh.
Referenced by get_verbose(), initialize_threadpool(), set_affinity(), set_verbose(), and ThreadPool().