The Gaudi Framework master (9cc87df9)
Loading...
Searching...
No Matches
ThreadPoolSvc Class Reference

A service which initializes a TBB thread pool. More...

#include <ThreadPoolSvc.h>

Inheritance diagram for ThreadPoolSvc:
Collaboration diagram for ThreadPoolSvc:

Public Member Functions

 ThreadPoolSvc (const std::string &name, ISvcLocator *svc)
 Constructor.
StatusCode initialize () override final
 Initialise.
StatusCode finalize () override final
 Finalise.
StatusCode initPool (const int &poolSize, const int &maxParallelismExtra) override final
 Initialize the thread pool and launch the ThreadInitTasks.
StatusCode terminatePool () override final
 Terminate the thread pool and launch thread termination tasks.
int poolSize () const override final
virtual bool isInit () const
virtual void initThisThread () override
tbb::task_arena * getArena ()
Public Member Functions inherited from extends< Service, IThreadPoolSvc, Gaudi::Concurrency::ConcurrencyManager >
void const * i_cast (const InterfaceID &tid) const override
 Implementation of IInterface::i_cast.
StatusCode queryInterface (const InterfaceID &ti, void **pp) override
 Implementation of IInterface::queryInterface.
std::vector< std::string > getInterfaceNames () const override
 Implementation of IInterface::getInterfaceNames.
Public Member Functions inherited from Service
const std::string & name () const override
 Retrieve name of the service.
StatusCode configure () override
StatusCode initialize () override
StatusCode start () override
StatusCode stop () override
StatusCode finalize () override
StatusCode terminate () override
Gaudi::StateMachine::State FSMState () const override
Gaudi::StateMachine::State targetFSMState () const override
StatusCode reinitialize () override
StatusCode restart () override
StatusCode sysInitialize () override
 Initialize Service.
StatusCode sysStart () override
 Initialize Service.
StatusCode sysStop () override
 Initialize Service.
StatusCode sysFinalize () override
 Finalize Service.
StatusCode sysReinitialize () override
 Re-initialize the Service.
StatusCode sysRestart () override
 Re-initialize the Service.
 Service (std::string name, ISvcLocator *svcloc)
 Standard Constructor.
SmartIF< ISvcLocator > & serviceLocator () const override
 Retrieve pointer to service locator.
template<typename IFace = IService>
SmartIF< IFace > service (const std::string &name, bool createIf=true) const
template<class T>
Gaudi::Details::PropertyBase * declareProperty (const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
template<class T>
StatusCode declareTool (ToolHandle< T > &handle, bool createIf=true)
template<class T>
StatusCode declareTool (ToolHandle< T > &handle, const std::string &toolTypeAndName, bool createIf=true)
 Declare used tool.
template<class T>
Gaudi::Details::PropertyBase * declareProperty (const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
template<class T>
void addToolsArray (ToolHandleArray< T > &hndlArr)
const std::vector< IAlgTool * > & tools () const
SmartIF< IAuditorSvc > & auditorSvc () const
 The standard auditor service.May not be invoked before sysInitialize() has been invoked.
Public Member Functions inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
StatusCode setProperty (const Gaudi::Details::PropertyBase &p)
 Set the property from a property.
StatusCode setProperty (const std::string &name, const char *v)
 Special case for string literals.
StatusCode setProperty (const std::string &name, const std::string &v)
 Special case for std::string.
StatusCode setProperty (const std::string &name, const TYPE &value)
 set the property form the value
 PropertyHolder ()=default
Gaudi::Details::PropertyBase & declareProperty (Gaudi::Details::PropertyBase &prop)
 Declare a property.
Gaudi::Details::PropertyBase * declareRemoteProperty (const std::string &name, IProperty *rsvc, const std::string &rname="")
 Declare a remote property.
StatusCode setPropertyRepr (const std::string &n, const std::string &r) override
 set the property from name and value string representation
StatusCode getProperty (Gaudi::Details::PropertyBase *p) const override
 get the property
const std::vector< Gaudi::Details::PropertyBase * > & getProperties () const override
 get all properties
bool hasProperty (std::string_view name) const override
 Return true if we have a property with the given name.
Gaudi::Details::PropertyBase * property (std::string_view name) const
 \fixme property and bindPropertiesTo should be protected
void bindPropertiesTo (Gaudi::Interfaces::IOptionsSvc &optsSvc)
PropertyHolder & operator= (const PropertyHolder &)=delete
Public Member Functions inherited from CommonMessagingBase
virtual ~CommonMessagingBase ()=default
 Virtual destructor.
const SmartIF< IMessageSvc > & msgSvc () const
 The standard message service.
MsgStream & msgStream () const
 Return an uninitialized MsgStream.
MsgStream & msgStream (const MSG::Level level) const
 Predefined configurable message stream for the efficient printouts.
MsgStream & always () const
 shortcut for the method msgStream(MSG::ALWAYS)
MsgStream & fatal () const
 shortcut for the method msgStream(MSG::FATAL)
MsgStream & err () const
 shortcut for the method msgStream(MSG::ERROR)
MsgStream & error () const
 shortcut for the method msgStream(MSG::ERROR)
MsgStream & warning () const
 shortcut for the method msgStream(MSG::WARNING)
MsgStream & info () const
 shortcut for the method msgStream(MSG::INFO)
MsgStream & debug () const
 shortcut for the method msgStream(MSG::DEBUG)
MsgStream & verbose () const
 shortcut for the method msgStream(MSG::VERBOSE)
MsgStream & msg () const
 shortcut for the method msgStream(MSG::INFO)
MSG::Level msgLevel () const
 get the cached level (originally extracted from the embedded MsgStream)
bool msgLevel (MSG::Level lvl) const
 get the output level from the embedded MsgStream

Private Member Functions

StatusCode launchTasks (bool finalize=false)
 Launch tasks to execute the ThreadInitTools.

Private Attributes

ToolHandleArray< IThreadInitTool > m_threadInitTools = { this }
 Handle array of thread init tools.
bool m_init = false
 Was the thread pool initialized?
int m_threadPoolSize = 0
 Size of the thread pool allocated.
tbb::spin_mutex m_initMutex
 Mutex used to protect the initPool and terminatePool methods.
std::unique_ptr< std::barrier<> > m_barrier
 Barrier used to synchronization thread init tasks.
std::unique_ptr< tbb::global_control > m_tbbgc
 TBB global control parameter.
tbb::task_arena m_arena
 TBB task arena to run all algorithms.
std::atomic< int > m_threadInitCount = 0
 Counter for all threads that are initialised.

Additional Inherited Members

Public Types inherited from extends< Service, IThreadPoolSvc, Gaudi::Concurrency::ConcurrencyManager >
using base_class
 Typedef to this class.
using extend_interfaces_base
 Typedef to the base of this class.
Public Types inherited from Service
using Factory = Gaudi::PluginService::Factory<IService*( const std::string&, ISvcLocator* )>
Public Types inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
using PropertyHolderImpl
 Typedef used to refer to this class from derived classes, as in.
Public Types inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
using base_class
Public Types inherited from extend_interfaces< Interfaces... >
using ext_iids
 take union of the ext_iids of all Interfaces...
Protected Member Functions inherited from Service
std::vector< IAlgTool * > & tools ()
 ~Service () override
int outputLevel () const
 get the Service's output level
Protected Member Functions inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
MSG::Level setUpMessaging () const override final
 Set up local caches.
MSG::Level resetMessaging ()
 Reinitialize internal states.
void updateMsgStreamOutputLevel (int level)
 Update the output level of the cached MsgStream.
Protected Attributes inherited from Service
Gaudi::StateMachine::State m_state = Gaudi::StateMachine::OFFLINE
 Service state.
Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::OFFLINE
 Service state.
Gaudi::Property< int > m_outputLevel { this, "OutputLevel", MSG::NIL, "output level" }
 flag indicating whether ToolHandle tools have been added to m_tools
Gaudi::Property< bool > m_auditorInitialize { this, "AuditInitialize", false, "trigger auditor on initialize()" }
Gaudi::Property< bool > m_auditorStart { this, "AuditStart", false, "trigger auditor on start()" }
Gaudi::Property< bool > m_auditorStop { this, "AuditStop", false, "trigger auditor on stop()" }
Gaudi::Property< bool > m_auditorFinalize { this, "AuditFinalize", false, "trigger auditor on finalize()" }
Gaudi::Property< bool > m_auditorReinitialize { this, "AuditReinitialize", false, "trigger auditor on reinitialize()" }
Gaudi::Property< bool > m_auditorRestart { this, "AuditRestart", false, "trigger auditor on restart()" }
Gaudi::Property< bool > m_autoRetrieveTools
Gaudi::Property< bool > m_checkToolDeps
SmartIF< IAuditorSvc > m_pAuditorSvc
 Auditor Service.

Detailed Description

A service which initializes a TBB thread pool.

This service can be configured with an array of IThreadInitTools which will each be invoked concurrently on each worker thread. A ThreadInitTask is created for each thread and given the list of tools. A std::barrier is used to synchronize the calling of each tool concurrently on all threads at the same time.

Definition at line 42 of file ThreadPoolSvc.h.

Constructor & Destructor Documentation

◆ ThreadPoolSvc()

ThreadPoolSvc::ThreadPoolSvc ( const std::string & name,
ISvcLocator * svc )

Constructor.

Definition at line 37 of file ThreadPoolSvc.cpp.

37 : extends( name, svcLoc ) {
38 declareProperty( "ThreadInitTools", m_threadInitTools, "ToolHandleArray of IThreadInitTools" );
39}
const std::string & name() const override
Retrieve name of the service.
Definition Service.cpp:333
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition Service.h:91
ToolHandleArray< IThreadInitTool > m_threadInitTools
Handle array of thread init tools.

Member Function Documentation

◆ finalize()

StatusCode ThreadPoolSvc::finalize ( )
finaloverride

Finalise.

Definition at line 67 of file ThreadPoolSvc.cpp.

67 {
68
69 if ( !m_init )
70 warning() << "Looks like the ThreadPoolSvc was created, but thread pool "
71 << "was never initialized" << endmsg;
72
74}
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
constexpr static const auto SUCCESS
Definition StatusCode.h:99
bool m_init
Was the thread pool initialized?

◆ getArena()

tbb::task_arena * ThreadPoolSvc::getArena ( )
inline

Definition at line 65 of file ThreadPoolSvc.h.

65{ return &m_arena; }
tbb::task_arena m_arena
TBB task arena to run all algorithms.

◆ initialize()

StatusCode ThreadPoolSvc::initialize ( )
finaloverride

Initialise.

Definition at line 43 of file ThreadPoolSvc.cpp.

43 {
44
45 // Initialise mother class (read properties, ...)
46 if ( Service::initialize().isFailure() ) {
47 warning() << "Base class could not be initialized" << endmsg;
49 }
50
52 error() << "Unable to retrieve ThreadInitTools Array" << endmsg;
53
55 }
56
57 if ( m_threadInitTools.size() != 0 )
58 info() << "retrieved " << m_threadInitTools.size() << " thread init tools" << endmsg;
59 else
60 info() << "no thread init tools attached" << endmsg;
61
63}
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
size_type size() const
StatusCode retrieve() override
Retrieve all tools.
StatusCode initialize() override
Definition Service.cpp:118
bool isFailure() const
Definition StatusCode.h:118
constexpr static const auto FAILURE
Definition StatusCode.h:100

◆ initPool()

StatusCode ThreadPoolSvc::initPool ( const int & poolSize,
const int & maxParallelismExtra )
finaloverride

Initialize the thread pool and launch the ThreadInitTasks.

Definition at line 78 of file ThreadPoolSvc.cpp.

78 {
79
80 tbb::spin_mutex::scoped_lock lock( m_initMutex );
81
83
84 ON_DEBUG debug() << "ThreadPoolSvc::initPool() poolSize = " << poolSize << endmsg;
85 // There is a problem in the piece of the code below. if
86 // m_threadPoolSize is set to something negative which is < -1,
87 // algorithm below might not behave as expected. For the time being
88 // I've choosen to create the barrier with the default number of
89 // threads created by the task scheduler init assuming that a
90 // negative value will choose automatic thread creation which will
91 // create default number of threads.
92 // SK
93
94 // -100 prevents the creation of the pool and the scheduler directly
95 // executes the tasks.
96 // -1 means use all available hardware threads
97
98 if ( -100 != m_threadPoolSize ) {
99 ON_DEBUG debug() << "Initialising a thread pool of size " << m_threadPoolSize << endmsg;
100
101 if ( m_threadPoolSize == -1 ) {
102 m_threadPoolSize = std::thread::hardware_concurrency();
103 } else if ( m_threadPoolSize < -1 ) {
104 fatal() << "Unexpected ThreadPoolSize \"" << m_threadPoolSize << "\". Allowed negative values are "
105 << "-1 (use all available cores) and -100 (don't use a thread pool)" << endmsg;
106 return StatusCode::FAILURE;
107 }
108
109 ON_VERBOSE verbose() << "Maximum allowed parallelism before adjusting: "
110 << tbb::global_control::active_value( tbb::global_control::max_allowed_parallelism ) << endmsg;
111
112 // to get the number of threads we need, request one thread more to account for how TBB calculates
113 // its soft limit of the number of threads for the global thread pool
114 m_tbbgc = std::make_unique<tbb::global_control>( tbb::global_control::max_allowed_parallelism,
115 m_threadPoolSize + maxParallelismExtra + 1 );
116
118 // Create the task arena to run all algorithms
119 m_arena.initialize( m_threadPoolSize + 1 );
120
121 // Create the barrier for task synchronization at termination
122 // (here we increase the number of threads by one to account for calling thread)
123 m_barrier = std::make_unique<std::barrier<>>( m_threadPoolSize + 1 );
124
125 } else {
126 // don't use a thread pool
128 m_tbbgc = std::make_unique<tbb::global_control>( tbb::global_control::max_allowed_parallelism, 0 );
129 }
130
131 ON_DEBUG debug() << "Thread Pool initialization complete. Maximum allowed parallelism: "
132 << tbb::global_control::active_value( tbb::global_control::max_allowed_parallelism ) << endmsg;
133
134 m_init = true;
135
136 return StatusCode::SUCCESS;
137}
#define ON_VERBOSE
#define ON_DEBUG
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
static void setNumThreads(const std::size_t &nT)
Set the number of worker threads (for MT).
std::unique_ptr< tbb::global_control > m_tbbgc
TBB global control parameter.
std::unique_ptr< std::barrier<> > m_barrier
Barrier used to synchronization thread init tasks.
int m_threadPoolSize
Size of the thread pool allocated.
int poolSize() const override final
tbb::spin_mutex m_initMutex
Mutex used to protect the initPool and terminatePool methods.

◆ initThisThread()

void ThreadPoolSvc::initThisThread ( )
overridevirtual

Definition at line 238 of file ThreadPoolSvc.cpp.

238 {
239
241 // this should never happen
242 error() << "initThisThread triggered, but thread already initialized" << endmsg;
243 throw GaudiException( "initThisThread triggered, but thread already initialized", name(), StatusCode::FAILURE );
244 }
245
247 std::barrier<>* noBarrier = nullptr;
248 ThreadInitTask( m_threadInitTools, noBarrier, serviceLocator(), false )();
249}
GaudiException(std::string Message, std::string Tag, StatusCode Code)
Constructor (1).
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition Service.cpp:336
ThreadInitTask(ToolHandleArray< IThreadInitTool > &tools, std::barrier<> *b, ISvcLocator *svcLocator, bool terminate=false)
std::atomic< int > m_threadInitCount
Counter for all threads that are initialised.

◆ isInit()

virtual bool ThreadPoolSvc::isInit ( ) const
inlinevirtual

Definition at line 61 of file ThreadPoolSvc.h.

61{ return m_init; }

◆ launchTasks()

StatusCode ThreadPoolSvc::launchTasks ( bool finalize = false)
private

Launch tasks to execute the ThreadInitTools.

Definition at line 162 of file ThreadPoolSvc.cpp.

162 {
163
164 const std::string taskType = terminate ? "termination" : "initialization";
165
166 // If we have a thread pool (via a scheduler), then we want to queue
167 // the tasks in TBB to execute on each thread.
168 if ( tbb::global_control::active_value( tbb::global_control::max_allowed_parallelism ) > 0 ) {
169
170 // Make a warning message if not all threads can be finalised
172 warning() << "Finalising " << m_threadPoolSize << " threads, but " << m_threadInitCount.load()
173 << " threads were initialised" << endmsg;
174 }
175
176 // Create one task for each worker thread in the pool
177 for ( int i = 0; i < m_threadPoolSize; ++i ) {
178 ON_DEBUG debug() << "creating ThreadInitTask " << i << endmsg;
179
180 // Queue the task
184 }
185
186 // Now wait for all the workers to reach the barrier
187 ON_DEBUG debug() << "waiting at barrier for all ThreadInitTool to finish executing" << endmsg;
188 m_barrier->arrive_and_wait();
189
190 // Check to make sure all Tools were invoked.
191 // I'm not sure this mechanism is worthwhile.
192 for ( auto& t : m_threadInitTools ) {
193 // Number of threads initialized but not terminated.
194 int numInit = t->nInit();
195 // Expected number based on the type of task.
196 int expectedNumInit = terminate ? 0 : m_threadPoolSize;
197 if ( numInit != expectedNumInit ) {
199 ost << "not all threads " << ( terminate ? "terminated" : "initialized" ) << " for tool " << t << " : "
200 << t->nInit() << " out of " << m_threadPoolSize << " are currently active.";
201 if ( terminate ) {
202 // it is likely the case that TBB activated new threads
203 // late in the game, and extra initializations were done
204 info() << ost.str() << endmsg;
205 } else {
206 error() << ost.str() << endmsg;
207 return StatusCode::FAILURE;
208 }
209 }
210 }
211
212 }
213
214 // In single-threaded mode, there is no scheduler, so we simply call
215 // the task wrapper directly in this thread.
216 else {
217 ON_DEBUG debug() << "launching ThreadInitTask " << taskType << "in this thread." << endmsg;
218
219 std::barrier<>* noBarrier = nullptr;
221 }
222
223 // Now, we do some error checking
225 error() << "a ThreadInitTask failed to execute successfully" << endmsg;
226 return StatusCode::FAILURE;
227 }
228
229 return StatusCode::SUCCESS;
230}
duration< int64_t, milli > milliseconds
basic_ostringstream< char > ostringstream
basic_string< char > string
void sleep_for(const chrono::duration< _Rep, _Period > &__rtime)
__string_type str() const
constexpr pointer get() const noexcept
bool empty() const override
Return whether the list of tools is empty.
StatusCode terminate() override
Definition Service.h:54
static bool execFailed()

◆ poolSize()

int ThreadPoolSvc::poolSize ( ) const
inlinefinaloverride

Definition at line 59 of file ThreadPoolSvc.h.

59{ return m_threadPoolSize; }

◆ terminatePool()

StatusCode ThreadPoolSvc::terminatePool ( )
finaloverride

Terminate the thread pool and launch thread termination tasks.

Definition at line 141 of file ThreadPoolSvc.cpp.

141 {
142 tbb::spin_mutex::scoped_lock lock( m_initMutex );
143
144 ON_DEBUG debug() << "ThreadPoolSvc::terminatePool()" << endmsg;
145
146 if ( !m_init ) {
147 error() << "Trying to terminate uninitialized thread pool!" << endmsg;
148 return StatusCode::FAILURE;
149 }
150
151 // Launch the termination tasks
152 const bool terminate = true;
153 if ( launchTasks( terminate ).isFailure() ) return StatusCode::FAILURE;
154
155 ON_DEBUG debug() << "Thread pool termination complete!" << endmsg;
156
157 return StatusCode::SUCCESS;
158}
StatusCode launchTasks(bool finalize=false)
Launch tasks to execute the ThreadInitTools.

Member Data Documentation

◆ m_arena

tbb::task_arena ThreadPoolSvc::m_arena
private

TBB task arena to run all algorithms.

Definition at line 90 of file ThreadPoolSvc.h.

◆ m_barrier

std::unique_ptr<std::barrier<> > ThreadPoolSvc::m_barrier
private

Barrier used to synchronization thread init tasks.

Definition at line 84 of file ThreadPoolSvc.h.

◆ m_init

bool ThreadPoolSvc::m_init = false
private

Was the thread pool initialized?

Definition at line 75 of file ThreadPoolSvc.h.

◆ m_initMutex

tbb::spin_mutex ThreadPoolSvc::m_initMutex
private

Mutex used to protect the initPool and terminatePool methods.

Definition at line 81 of file ThreadPoolSvc.h.

◆ m_tbbgc

std::unique_ptr<tbb::global_control> ThreadPoolSvc::m_tbbgc
private

TBB global control parameter.

Definition at line 87 of file ThreadPoolSvc.h.

◆ m_threadInitCount

std::atomic<int> ThreadPoolSvc::m_threadInitCount = 0
private

Counter for all threads that are initialised.

Definition at line 93 of file ThreadPoolSvc.h.

◆ m_threadInitTools

ToolHandleArray<IThreadInitTool> ThreadPoolSvc::m_threadInitTools = { this }
private

Handle array of thread init tools.

Definition at line 72 of file ThreadPoolSvc.h.

◆ m_threadPoolSize

int ThreadPoolSvc::m_threadPoolSize = 0
private

Size of the thread pool allocated.

Definition at line 78 of file ThreadPoolSvc.h.


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