The Gaudi Framework  v30r1 (5d4f4ae2)
ThreadPoolSvc.cpp
Go to the documentation of this file.
1 #include "ThreadPoolSvc.h"
2 
5 #include "ThreadInitTask.h"
6 
7 #include "tbb/task.h"
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"
12 
13 using namespace tbb;
14 
16 
17 //=============================================================================
18 
19 ThreadPoolSvc::ThreadPoolSvc( const std::string& name, ISvcLocator* svcLoc ) : extends( name, svcLoc )
20 {
21  declareProperty( "ThreadInitTools", m_threadInitTools, "ToolHandleArray of IThreadInitTools" );
22 }
23 
24 //-----------------------------------------------------------------------------
25 
27 {
28 
29  // Initialise mother class (read properties, ...)
31  if ( !sc.isSuccess() ) {
32  warning() << "Base class could not be initialized" << endmsg;
33  return StatusCode::FAILURE;
34  }
35 
36  if ( m_threadInitTools.retrieve().isFailure() ) {
37  error() << "Unable to retrieve ThreadInitTools Array" << endmsg;
38 
39  return StatusCode::FAILURE;
40  }
41  if ( m_threadInitTools.size() != 0 ) {
42  info() << "retrieved " << m_threadInitTools.size() << " thread init tools" << endmsg;
43  } else {
44  info() << "no thread init tools attached" << endmsg;
45  }
46 
47  return StatusCode::SUCCESS;
48 }
49 
50 //-----------------------------------------------------------------------------
51 
53 {
54 
55  if ( !m_init ) {
56  warning() << "Looks like the ThreadPoolSvc was created, but thread pool "
57  << "was never initialized" << endmsg;
58  }
59 
60  return StatusCode::SUCCESS;
61 }
62 
63 //-----------------------------------------------------------------------------
64 
65 StatusCode ThreadPoolSvc::initPool( const int& poolSize )
66 {
67 
68  tbb::spin_mutex::scoped_lock lock( m_initMutex );
69 
70  m_threadPoolSize = poolSize;
71 
72  if ( msgLevel( MSG::DEBUG ) ) debug() << "ThreadPoolSvc::initPool() poolSize = " << poolSize << endmsg;
73  // There is a problem in the piece of the code below. if
74  // m_threadPoolSize is set to something negative which is < -1,
75  // algorithm below might not behave as expected. For the time being
76  // I've choosen to create the barrier with the default number of
77  // threads created by the task scheduler init assuming that a
78  // negative value will choose automatic thread creation which will
79  // create default number of threads.
80  // SK
81 
82  // -100 prevents the creation of the pool and the scheduler directly
83  // executes the tasks.
84  if ( -100 != m_threadPoolSize ) {
85  if ( msgLevel( MSG::DEBUG ) ) debug() << "Initialising a thread pool of size " << m_threadPoolSize << endmsg;
86 
87  // Leave -1 in case selected, increment otherwise
88  // - What?
89  int thePoolSize = m_threadPoolSize;
90  if ( thePoolSize != -1 ) thePoolSize += 1;
91 
92  // Create the TBB task scheduler
93  m_tbbSchedInit = std::make_unique<tbb::task_scheduler_init>( thePoolSize );
94  // Create the barrier for task synchronization
95  if ( m_threadPoolSize <= -1 ) thePoolSize = m_tbbSchedInit->default_num_threads();
96  if ( msgLevel( MSG::DEBUG ) ) {
97  debug() << "creating barrier of size " << thePoolSize << endmsg;
98  }
100 
101  m_barrier = std::make_unique<boost::barrier>( thePoolSize );
102 
103  } else {
105  }
106 
107  // Launch the init tool tasks
108  const bool terminate = false;
109  if ( launchTasks( terminate ).isFailure() ) return StatusCode::FAILURE;
110 
111  if ( msgLevel( MSG::DEBUG ) ) debug() << "Thread Pool initialization complete!" << endmsg;
112 
113  m_init = true;
114 
115  return StatusCode::SUCCESS;
116 }
117 
118 //-----------------------------------------------------------------------------
119 
121 {
122  tbb::spin_mutex::scoped_lock lock( m_initMutex );
123  if ( msgLevel( MSG::DEBUG ) ) debug() << "ThreadPoolSvc::terminatePool()" << endmsg;
124 
125  if ( !m_init ) {
126  error() << "Trying to terminate uninitialized thread pool!" << endmsg;
127  return StatusCode::FAILURE;
128  }
129 
130  // Launch the termination tasks
131  const bool terminate = true;
132  if ( launchTasks( terminate ).isFailure() ) return StatusCode::FAILURE;
133 
134  if ( msgLevel( MSG::DEBUG ) ) debug() << "Thread pool termination complete!" << endmsg;
135 
136  return StatusCode::SUCCESS;
137 }
138 
139 //-----------------------------------------------------------------------------
140 
142 {
143 
144  if ( m_threadInitTools.empty() ) return StatusCode::SUCCESS;
145 
146  const std::string taskType = terminate ? "termination" : "initialization";
147 
148  // If we have a thread pool (via a scheduler), then we want to queue
149  // the tasks in TBB to execute on each thread.
150  if ( m_tbbSchedInit ) {
151 
152  // Create one task for each worker thread in the pool
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 );
157 
158  // Queue the task
159  tbb::task::enqueue( *t );
160  this_tbb_thread::sleep( tbb::tick_count::interval_t( .1 ) );
161  }
162 
163  // Now wait for all the workers to reach the barrier
164  if ( msgLevel( MSG::DEBUG ) ) debug() << "waiting at barrier for all ThreadInitTool to finish executing" << endmsg;
165  m_barrier->wait();
166 
167  // Check to make sure all Tools were invoked.
168  // I'm not sure this mechanism is worthwhile.
169  for ( auto& t : m_threadInitTools ) {
170  // Number of threads initialized but not terminated.
171  int numInit = t->nInit();
172  // Expected number based on the type of task.
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;
177  return StatusCode::FAILURE;
178  }
179  }
180 
181  }
182 
183  // In single-threaded mode, there is no scheduler, so we simply call
184  // the task wrapper directly in this thread.
185  else {
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 );
189  theTask.execute();
190  }
191 
192  // Now, we do some error checking
193  if ( ThreadInitTask::execFailed() ) {
194  error() << "a ThreadInitTask failed to execute successfully" << endmsg;
195  return StatusCode::FAILURE;
196  }
197 
198  return StatusCode::SUCCESS;
199 }
StatusCode finalize() override final
Finalise.
static GAUDI_API void setNumThreads(const std::size_t &nT)
StatusCode initialize() override
Definition: Service.cpp:64
tbb::task * execute() override
Execute the task.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
StatusCode terminatePool() override final
Terminate the thread pool and launch thread termination tasks.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:50
Special TBB task used by ThreadPoolSvc to wrap execution of IThreadInitTools.
STL namespace.
STL class.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
static bool execFailed()
StatusCode initialize() override final
Initialise.
#define DECLARE_SERVICE_FACTORY(x)
Definition: Service.h:211
Base class used to extend a class implementing other interfaces.
Definition: extends.h:10
A service which initializes a TBB thread pool.
Definition: ThreadPoolSvc.h:26
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.
Definition: MsgStream.h:209