7 #include "tbb/task_scheduler_init.h" 8 #include "tbb/task_scheduler_observer.h" 9 #include "tbb/tbb_thread.h" 10 #include "tbb/tick_count.h" 20 declareProperty(
"ThreadInitTools", m_threadInitTools,
"ToolHandleArray of IThreadInitTools" );
31 warning() <<
"Base class could not be initialized" <<
endmsg;
35 if ( m_threadInitTools.retrieve().isFailure() ) {
36 error() <<
"Unable to retrieve ThreadInitTools Array" <<
endmsg;
40 if ( m_threadInitTools.size() != 0 ) {
41 info() <<
"retrieved " << m_threadInitTools.size() <<
" thread init tools" <<
endmsg;
43 info() <<
"no thread init tools attached" <<
endmsg;
55 warning() <<
"Looks like the ThreadPoolSvc was created, but thread pool " 56 <<
"was never initialized" <<
endmsg;
67 tbb::spin_mutex::scoped_lock lock( m_initMutex );
69 m_threadPoolSize = poolSize;
71 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"ThreadPoolSvc::initPool() poolSize = " << poolSize <<
endmsg;
83 if ( -100 != m_threadPoolSize ) {
84 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"Initialising a thread pool of size " << m_threadPoolSize <<
endmsg;
88 int thePoolSize = m_threadPoolSize;
89 if ( thePoolSize != -1 ) thePoolSize += 1;
92 m_tbbSchedInit = std::make_unique<tbb::task_scheduler_init>( thePoolSize );
94 if ( m_threadPoolSize <= -1 ) thePoolSize = m_tbbSchedInit->default_num_threads();
96 debug() <<
"creating barrier of size " << thePoolSize <<
endmsg;
100 m_barrier = std::make_unique<boost::barrier>( thePoolSize );
107 const bool terminate =
false;
110 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"Thread Pool initialization complete!" <<
endmsg;
121 tbb::spin_mutex::scoped_lock lock( m_initMutex );
122 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"ThreadPoolSvc::terminatePool()" <<
endmsg;
125 error() <<
"Trying to terminate uninitialized thread pool!" <<
endmsg;
130 const bool terminate =
true;
133 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"Thread pool termination complete!" <<
endmsg;
145 const std::string taskType = terminate ?
"termination" :
"initialization";
149 if ( m_tbbSchedInit ) {
152 for (
int i = 0; i < m_threadPoolSize; ++i ) {
153 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"creating ThreadInitTask " << i <<
endmsg;
154 tbb::task* t =
new ( tbb::task::allocate_root() )
155 ThreadInitTask( m_threadInitTools, m_barrier.get(), serviceLocator(), terminate );
158 tbb::task::enqueue( *t );
159 this_tbb_thread::sleep( tbb::tick_count::interval_t( .1 ) );
163 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"waiting at barrier for all ThreadInitTool to finish executing" <<
endmsg;
168 for (
auto& t : m_threadInitTools ) {
170 int numInit = t->nInit();
172 int expectedNumInit = terminate ? 0 : m_threadPoolSize;
173 if ( numInit != expectedNumInit ) {
174 error() <<
"not all threads " << ( terminate ?
"terminated" :
"initialized" ) <<
" for tool " << t <<
" : " 175 << t->nInit() <<
" out of " << m_threadPoolSize <<
" are currently active" <<
endmsg;
185 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"launching ThreadInitTask " << taskType <<
"in this thread." <<
endmsg;
186 boost::barrier* noBarrier =
nullptr;
187 ThreadInitTask theTask( m_threadInitTools, noBarrier, serviceLocator(), terminate );
193 error() <<
"a ThreadInitTask failed to execute successfully" <<
endmsg;
StatusCode finalize() override final
Finalise.
static GAUDI_API void setNumThreads(const std::size_t &nT)
constexpr static const auto FAILURE
StatusCode initialize() override
tbb::task * execute() override
Execute the task.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
StatusCode terminatePool() override final
Terminate the thread pool and launch thread termination tasks.
Special TBB task used by ThreadPoolSvc to wrap execution of IThreadInitTools.
#define DECLARE_COMPONENT(type)
This class is used for returning status codes from appropriate routines.
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
StatusCode initialize() override final
Initialise.
constexpr static const auto SUCCESS
Base class used to extend a class implementing other interfaces.
A service which initializes a TBB thread pool.
StatusCode initPool(const int &poolSize) override final
Initialize the thread pool and launch the ThreadInitTasks.
StatusCode launchTasks(bool finalize=false)
Launch tasks to execute the ThreadInitTools.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.