8 #include "tbb/task_scheduler_init.h" 9 #include "tbb/task_scheduler_observer.h" 10 #include "tbb/tbb_thread.h" 11 #include "tbb/tick_count.h" 21 declareProperty(
"ThreadInitTools", m_threadInitTools,
"ToolHandleArray of IThreadInitTools" );
32 warning() <<
"Base class could not be initialized" <<
endmsg;
36 if ( m_threadInitTools.retrieve().isFailure() ) {
37 error() <<
"Unable to retrieve ThreadInitTools Array" <<
endmsg;
41 if ( m_threadInitTools.size() != 0 ) {
42 info() <<
"retrieved " << m_threadInitTools.size() <<
" thread init tools" <<
endmsg;
44 info() <<
"no thread init tools attached" <<
endmsg;
56 warning() <<
"Looks like the ThreadPoolSvc was created, but thread pool " 57 <<
"was never initialized" <<
endmsg;
68 tbb::spin_mutex::scoped_lock lock( m_initMutex );
70 m_threadPoolSize = poolSize;
72 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"ThreadPoolSvc::initPool() poolSize = " << poolSize <<
endmsg;
84 if ( -100 != m_threadPoolSize ) {
85 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"Initialising a thread pool of size " << m_threadPoolSize <<
endmsg;
89 int thePoolSize = m_threadPoolSize;
90 if ( thePoolSize != -1 ) thePoolSize += 1;
93 m_tbbSchedInit = std::make_unique<tbb::task_scheduler_init>( thePoolSize );
95 if ( m_threadPoolSize <= -1 ) thePoolSize = m_tbbSchedInit->default_num_threads();
97 debug() <<
"creating barrier of size " << thePoolSize <<
endmsg;
101 m_barrier = std::make_unique<boost::barrier>( thePoolSize );
108 const bool terminate =
false;
111 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"Thread Pool initialization complete!" <<
endmsg;
122 tbb::spin_mutex::scoped_lock lock( m_initMutex );
123 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"ThreadPoolSvc::terminatePool()" <<
endmsg;
126 error() <<
"Trying to terminate uninitialized thread pool!" <<
endmsg;
131 const bool terminate =
true;
134 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"Thread pool termination complete!" <<
endmsg;
146 const std::string taskType = terminate ?
"termination" :
"initialization";
150 if ( m_tbbSchedInit ) {
153 for (
int i = 0; i < m_threadPoolSize; ++i ) {
154 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"creating ThreadInitTask " << i <<
endmsg;
155 tbb::task* t =
new ( tbb::task::allocate_root() )
156 ThreadInitTask( m_threadInitTools, m_barrier.get(), serviceLocator(), terminate );
159 tbb::task::enqueue( *t );
160 this_tbb_thread::sleep( tbb::tick_count::interval_t( .1 ) );
164 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"waiting at barrier for all ThreadInitTool to finish executing" <<
endmsg;
169 for (
auto& t : m_threadInitTools ) {
171 int numInit = t->nInit();
173 int expectedNumInit = terminate ? 0 : m_threadPoolSize;
174 if ( numInit != expectedNumInit ) {
175 error() <<
"not all threads " << ( terminate ?
"terminated" :
"initialized" ) <<
" for tool " << t <<
" : " 176 << t->nInit() <<
" out of " << m_threadPoolSize <<
" are currently active" <<
endmsg;
186 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"launching ThreadInitTask " << taskType <<
"in this thread." <<
endmsg;
187 boost::barrier* noBarrier =
nullptr;
188 ThreadInitTask theTask( m_threadInitTools, noBarrier, serviceLocator(), terminate );
194 error() <<
"a ThreadInitTask failed to execute successfully" <<
endmsg;
StatusCode finalize() override final
Finalise.
static GAUDI_API void setNumThreads(const std::size_t &nT)
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.
bool isSuccess() const
Test for a status code of SUCCESS.
Special TBB task used by ThreadPoolSvc to wrap execution of IThreadInitTools.
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.
#define DECLARE_SERVICE_FACTORY(x)
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.