Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
PTL::TaskManager Class Reference

#include <TaskManager.hh>

Public Types

using this_type = TaskManager
 
using size_type = ThreadPool::size_type
 

Public Member Functions

 TaskManager (ThreadPool *, bool _manage_pool=true)
 
virtual ~TaskManager () noexcept(false)
 
 TaskManager (const TaskManager &)=delete
 
 TaskManager (TaskManager &&)=default
 
TaskManageroperator= (const TaskManager &)=delete
 
TaskManageroperator= (TaskManager &&)=default
 
ThreadPoolthread_pool () const
 
size_type size () const
 
void finalize ()
 
template<typename... Args>
void exec (Task< Args... > *_task)
 
template<typename RetT , typename FuncT , typename... Args>
std::shared_ptr< PackagedTask< RetT, Args... > > async (FuncT &&func, Args &&... args)
 
template<typename RetT , typename FuncT >
std::shared_ptr< PackagedTask< RetT > > async (FuncT &&func)
 
template<typename FuncT , typename... Args>
auto async (FuncT &&func, Args... args) -> std::shared_ptr< PackagedTask< decay_t< decltype(func(args...))>, Args... > >
 
template<typename RetT , typename ArgT , typename FuncT , typename... Args>
std::shared_ptr< Task< RetT, ArgT, Args... > > wrap (TaskGroup< RetT, ArgT > &tg, FuncT &&func, Args &&... args)
 
template<typename RetT , typename ArgT , typename FuncT >
std::shared_ptr< Task< RetT, ArgT > > wrap (TaskGroup< RetT, ArgT > &tg, FuncT &&func)
 
template<typename RetT , typename ArgT , typename FuncT , typename... Args>
void exec (TaskGroup< RetT, ArgT > &tg, FuncT &&func, Args &&... args)
 
template<typename RetT , typename ArgT , typename FuncT >
void exec (TaskGroup< RetT, ArgT > &tg, FuncT &&func)
 
template<typename RetT , typename ArgT , typename FuncT , typename... Args>
void rexec (TaskGroup< RetT, ArgT > &tg, FuncT &&func, Args &&... args)
 
template<typename RetT , typename ArgT , typename FuncT >
void rexec (TaskGroup< RetT, ArgT > &tg, FuncT &&func)
 
template<typename FuncT , typename... Args>
void rexec (TaskGroup< void, void > &tg, FuncT &&func, Args &&... args)
 
template<typename FuncT >
void rexec (TaskGroup< void, void > &tg, FuncT &&func)
 

Static Public Member Functions

static TaskManagerGetInstance ()
 get the singleton pointer
 
static TaskManagerGetInstanceIfExists ()
 
static unsigned ncores ()
 

Protected Attributes

ThreadPoolm_pool = nullptr
 
bool m_is_finalized = false
 

Detailed Description

Definition at line 48 of file TaskManager.hh.

Member Typedef Documentation

◆ size_type

◆ this_type

Definition at line 51 of file TaskManager.hh.

Constructor & Destructor Documentation

◆ TaskManager() [1/3]

PTL::TaskManager::TaskManager ( ThreadPool _pool,
bool  _manage_pool = true 
)
inlineexplicit

Definition at line 258 of file TaskManager.hh.

259: m_pool(_pool)
260, m_is_finalized(!_manage_pool)
261{
262 if(!fgInstance())
263 fgInstance() = this;
264}
ThreadPool * m_pool
Definition: TaskManager.hh:212

◆ ~TaskManager()

PTL::TaskManager::~TaskManager ( )
inlinevirtual

Definition at line 268 of file TaskManager.hh.

269{
270 finalize();
271 if(fgInstance() == this)
272 fgInstance() = nullptr;
273}

◆ TaskManager() [2/3]

PTL::TaskManager::TaskManager ( const TaskManager )
delete

◆ TaskManager() [3/3]

PTL::TaskManager::TaskManager ( TaskManager &&  )
default

Member Function Documentation

◆ async() [1/3]

template<typename RetT , typename FuncT >
std::shared_ptr< PackagedTask< RetT > > PTL::TaskManager::async ( FuncT &&  func)
inline

Definition at line 121 of file TaskManager.hh.

122 {
123 using task_type = PackagedTask<RetT>;
124
125 if(!m_pool)
126 throw std::runtime_error("Nullptr to thread-pool");
127
128 auto _ptask = std::make_shared<task_type>(std::forward<FuncT>(func));
129 m_pool->add_task(_ptask);
130 return _ptask;
131 }
size_type add_task(task_pointer &&task, int bin=-1)
Definition: ThreadPool.hh:450

◆ async() [2/3]

template<typename RetT , typename FuncT , typename... Args>
std::shared_ptr< PackagedTask< RetT, Args... > > PTL::TaskManager::async ( FuncT &&  func,
Args &&...  args 
)
inline

Definition at line 107 of file TaskManager.hh.

108 {
109 using task_type = PackagedTask<RetT, Args...>;
110
111 if(!m_pool)
112 throw std::runtime_error("Nullptr to thread-pool");
113
114 auto _ptask = std::make_shared<task_type>(std::forward<FuncT>(func),
115 std::forward<Args>(args)...);
116 m_pool->add_task(_ptask);
117 return _ptask;
118 }

Referenced by G4TaskRunManagerKernel::ExecuteWorkerInit(), G4TaskRunManagerKernel::ExecuteWorkerTask(), and G4TaskRunManagerKernel::InitializeWorker().

◆ async() [3/3]

template<typename FuncT , typename... Args>
auto PTL::TaskManager::async ( FuncT &&  func,
Args...  args 
) -> std::shared_ptr<PackagedTask<decay_t<decltype(func(args...))>, Args...>>
inline

Definition at line 134 of file TaskManager.hh.

136 {
137 using RetT = decay_t<decltype(func(args...))>;
138 using task_type = PackagedTask<RetT, Args...>;
139
140 if(!m_pool)
141 throw std::runtime_error("Nullptr to thread-pool");
142
143 auto _ptask = std::make_shared<task_type>(std::forward<FuncT>(func),
144 std::forward<Args>(args)...);
145 m_pool->add_task(_ptask);
146 return _ptask;
147 }
typename std::decay< T >::type decay_t
Definition: Globals.hh:54

◆ exec() [1/3]

template<typename... Args>
void PTL::TaskManager::exec ( Task< Args... > *  _task)
inline

Definition at line 96 of file TaskManager.hh.

97 {
98 if(!m_pool)
99 throw std::runtime_error("Nullptr to thread-pool");
100 m_pool->add_task(_task);
101 }

◆ exec() [2/3]

template<typename RetT , typename ArgT , typename FuncT >
void PTL::TaskManager::exec ( TaskGroup< RetT, ArgT > &  tg,
FuncT &&  func 
)
inline

Definition at line 178 of file TaskManager.hh.

179 {
180 tg.exec(std::forward<FuncT>(func));
181 }

◆ exec() [3/3]

template<typename RetT , typename ArgT , typename FuncT , typename... Args>
void PTL::TaskManager::exec ( TaskGroup< RetT, ArgT > &  tg,
FuncT &&  func,
Args &&...  args 
)
inline

Definition at line 172 of file TaskManager.hh.

173 {
174 tg.exec(std::forward<FuncT>(func), std::forward<Args>(args)...);
175 }

◆ finalize()

void PTL::TaskManager::finalize ( )
inline

Definition at line 81 of file TaskManager.hh.

82 {
84 return;
85 m_is_finalized = true;
86 if(m_pool)
88 }
size_type destroy_threadpool()
Definition: ThreadPool.cc:572

◆ GetInstance()

PTL::TaskManager * PTL::TaskManager::GetInstance ( )
inlinestatic

get the singleton pointer

Definition at line 236 of file TaskManager.hh.

237{
238 if(!fgInstance())
239 {
240 auto nthreads = std::thread::hardware_concurrency();
241 std::cout << "Allocating mad::TaskManager with " << nthreads << " thread(s)..."
242 << std::endl;
243 new TaskManager(TaskRunManager::GetMasterRunManager()->GetThreadPool());
244 }
245 return fgInstance();
246}
TaskManager(ThreadPool *, bool _manage_pool=true)
Definition: TaskManager.hh:258
static TaskRunManager * GetMasterRunManager(bool useTBB=false)

◆ GetInstanceIfExists()

PTL::TaskManager * PTL::TaskManager::GetInstanceIfExists ( )
inlinestatic

Definition at line 251 of file TaskManager.hh.

252{
253 return fgInstance();
254}

◆ ncores()

static unsigned PTL::TaskManager::ncores ( )
inlinestatic

Definition at line 68 of file TaskManager.hh.

68{ return std::thread::hardware_concurrency(); }

◆ operator=() [1/2]

TaskManager & PTL::TaskManager::operator= ( const TaskManager )
delete

◆ operator=() [2/2]

TaskManager & PTL::TaskManager::operator= ( TaskManager &&  )
default

◆ rexec() [1/4]

template<typename RetT , typename ArgT , typename FuncT >
void PTL::TaskManager::rexec ( TaskGroup< RetT, ArgT > &  tg,
FuncT &&  func 
)
inline

Definition at line 190 of file TaskManager.hh.

191 {
192 tg.exec(std::forward<FuncT>(func));
193 }

◆ rexec() [2/4]

template<typename RetT , typename ArgT , typename FuncT , typename... Args>
void PTL::TaskManager::rexec ( TaskGroup< RetT, ArgT > &  tg,
FuncT &&  func,
Args &&...  args 
)
inline

Definition at line 184 of file TaskManager.hh.

185 {
186 tg.exec(std::forward<FuncT>(func), std::forward<Args>(args)...);
187 }

◆ rexec() [3/4]

template<typename FuncT >
void PTL::TaskManager::rexec ( TaskGroup< void, void > &  tg,
FuncT &&  func 
)
inline

Definition at line 204 of file TaskManager.hh.

205 {
206 tg.exec(std::forward<FuncT>(func));
207 }

◆ rexec() [4/4]

template<typename FuncT , typename... Args>
void PTL::TaskManager::rexec ( TaskGroup< void, void > &  tg,
FuncT &&  func,
Args &&...  args 
)
inline

Definition at line 198 of file TaskManager.hh.

199 {
200 tg.exec(std::forward<FuncT>(func), std::forward<Args>(args)...);
201 }

◆ size()

size_type PTL::TaskManager::size ( ) const
inline

Definition at line 77 of file TaskManager.hh.

77{ return (m_pool) ? m_pool->size() : 0; }
size_type size() const
Definition: ThreadPool.hh:252

◆ thread_pool()

ThreadPool * PTL::TaskManager::thread_pool ( ) const
inline

Definition at line 73 of file TaskManager.hh.

73{ return m_pool; }

◆ wrap() [1/2]

template<typename RetT , typename ArgT , typename FuncT >
std::shared_ptr< Task< RetT, ArgT > > PTL::TaskManager::wrap ( TaskGroup< RetT, ArgT > &  tg,
FuncT &&  func 
)
inline

Definition at line 162 of file TaskManager.hh.

163 {
164 return tg.wrap(std::forward<FuncT>(func));
165 }

◆ wrap() [2/2]

template<typename RetT , typename ArgT , typename FuncT , typename... Args>
std::shared_ptr< Task< RetT, ArgT, Args... > > PTL::TaskManager::wrap ( TaskGroup< RetT, ArgT > &  tg,
FuncT &&  func,
Args &&...  args 
)
inline

Definition at line 155 of file TaskManager.hh.

157 {
158 return tg.wrap(std::forward<FuncT>(func), std::forward<Args>(args)...);
159 }

Member Data Documentation

◆ m_is_finalized

bool PTL::TaskManager::m_is_finalized = false
protected

Definition at line 213 of file TaskManager.hh.

Referenced by finalize().

◆ m_pool

ThreadPool* PTL::TaskManager::m_pool = nullptr
protected

Definition at line 212 of file TaskManager.hh.

Referenced by async(), exec(), finalize(), size(), and thread_pool().


The documentation for this class was generated from the following file: