The Gaudi Framework  v28r3 (cc1cf868)
AvalancheSchedulerSvc.h
Go to the documentation of this file.
1 #ifndef GAUDIHIVE_AVALANCHESCHEDULERSVC_H
2 #define GAUDIHIVE_AVALANCHESCHEDULERSVC_H
3 
4 // Local includes
5 #include "AlgsExecutionStates.h"
6 #include "EventSlot.h"
7 #include "ExecutionFlowManager.h"
8 
9 // Framework include files
14 #include "GaudiKernel/IRunable.h"
15 #include "GaudiKernel/IScheduler.h"
17 #include "GaudiKernel/Service.h"
18 
19 // C++ include files
20 #include <functional>
21 #include <string>
22 #include <thread>
23 #include <unordered_map>
24 #include <vector>
25 
26 // External libs
27 #include "tbb/concurrent_queue.h"
28 #include "tbb/task.h"
29 
32 
33 //---------------------------------------------------------------------------
34 
100 class AvalancheSchedulerSvc : public extends<Service, IScheduler>
101 {
102 public:
104  using extends::extends;
105 
107  ~AvalancheSchedulerSvc() override = default;
108 
110  StatusCode initialize() override;
111 
113  StatusCode finalize() override;
114 
116  StatusCode pushNewEvent( EventContext* eventContext ) override;
117 
118  // Make multiple events available to the scheduler
119  StatusCode pushNewEvents( std::vector<EventContext*>& eventContexts ) override;
120 
122  StatusCode popFinishedEvent( EventContext*& eventContext ) override;
123 
125  StatusCode tryPopFinishedEvent( EventContext*& eventContext ) override;
126 
128  unsigned int freeSlots() override;
129 
130 private:
131  enum ActivationState { INACTIVE = 0, ACTIVE = 1, FAILURE = 2 };
132 
133  Gaudi::Property<int> m_threadPoolSize{this, "ThreadPoolSize", -1,
134  "Size of the threadpool initialised by TBB; a value of -1 gives TBB the freedom to choose"};
135  Gaudi::Property<std::string> m_whiteboardSvcName{this, "WhiteboardSvc", "EventDataSvc",
136  "The whiteboard name"};
138  "IOBoundAlgSchedulerSvc"};
139  Gaudi::Property<unsigned int> m_maxIOBoundAlgosInFlight{this, "MaxIOBoundAlgosInFlight", 0,
140  "Maximum number of simultaneous I/O-bound algorithms"};
141  Gaudi::Property<bool> m_simulateExecution{this, "SimulateExecution", false,
142  "Flag to perform single-pass simulation of execution flow before the actual execution"};
144  "The following modes are currently available: PCE, COD, DRE, E"};
145  Gaudi::Property<bool> m_dumpIntraEventDynamics{this, "DumpIntraEventDynamics", false,
146  "Dump intra-event concurrency dynamics to csv file"};
147  Gaudi::Property<bool> m_useIOBoundAlgScheduler{this, "PreemptiveIOBoundTasks", false,
148  "Turn on preemptive way of scheduling of I/O-bound algorithms"};
149 
150  Gaudi::Property<bool> m_checkDeps{this, "CheckDependencies", false,
151  "Runtime check of Algorithm Data Dependencies"};
152 
154  "Attribute unmet input dependencies to this DataLoader Algorithm"};
155 
156  Gaudi::Property<bool> m_showDataDeps{this, "ShowDataDependencies", true,
157  "Show the INPUT and OUTPUT data dependencies of Algorithms"};
158 
159  Gaudi::Property<bool> m_showDataFlow{this, "ShowDataFlow", false,
160  "Show the configuration of DataFlow between Algorithms"};
161 
162  Gaudi::Property<bool> m_showControlFlow{this, "ShowControlFlow", false,
163  "Show the configuration of all Algorithms and Sequences"};
164 
165  // Utils and shortcuts ----------------------------------------------------
166 
168  void activate();
169 
172 
175 
178 
180  inline unsigned int algname2index( const std::string& algoname );
181 
184 
186  inline const std::string& index2algname( unsigned int index );
187 
190 
193 
196 
199 
201  std::atomic_int m_freeSlots;
202 
204  tbb::concurrent_bounded_queue<EventContext*> m_finishedEvents;
205 
207  StatusCode eventFailed( EventContext* eventContext );
208 
211 
212  // States management ------------------------------------------------------
213 
215  unsigned int m_algosInFlight = 0;
216 
218  unsigned int m_IOBoundAlgosInFlight = 0;
219 
222  StatusCode updateStates( int si = -1, const std::string& algo_name = std::string() );
223 
225  StatusCode promoteToScheduled( unsigned int iAlgo, int si );
226  StatusCode promoteToAsyncScheduled( unsigned int iAlgo, int si ); // tests of an asynchronous scheduler
227  StatusCode promoteToExecuted( unsigned int iAlgo, int si, IAlgorithm* algo, EventContext* );
228  StatusCode promoteToAsyncExecuted( unsigned int iAlgo, int si, IAlgorithm* algo,
229  EventContext* ); // tests of an asynchronous scheduler
230  StatusCode promoteToFinished( unsigned int iAlgo, int si );
231 
233  StatusCode isStalled( int si );
234 
236  void dumpSchedulerState( int iSlot );
237 
239  bool m_updateNeeded = true;
240 
241  // Algos Management -------------------------------------------------------
244 
247 
248  // Actions management -----------------------------------------------------
249 
251  tbb::concurrent_bounded_queue<action> m_actionsQueue;
252 
253  // helper task to enqueue the scheduler's actions (closures)
254  struct enqueueSchedulerActionTask: public tbb::task {
255 
258 
260  m_closure(_closure), m_scheduler(scheduler) {}
261 
262  tbb::task* execute() override {
263  m_scheduler->m_actionsQueue.push(m_closure);
264  return nullptr;
265  }
266  };
267 
268  // ------------------------------------------------------------------------
269 
272 
273  // Service for thread pool initialization
276  size_t m_maxAlgosInFlight {1};
277  bool m_first = true;
278 
280  {
281 
282  public:
283  SchedulerState( Algorithm* a, EventContext* e, pthread_t t ) : m_a( a ), m_e( *e ), m_t( t ) {}
284 
285  Algorithm* alg() const { return m_a; }
286  EventContext ctx() const { return m_e; }
287  pthread_t thread() const { return m_t; }
288 
290  {
291  os << ss.ctx() << " a: " << ss.alg()->name() << " [" << std::hex << ss.alg() << std::dec << "] t: 0x"
292  << std::hex << ss.thread() << std::dec;
293  return os;
294  }
295 
296  bool operator==( const SchedulerState& ss ) const { return ( m_a == ss.alg() ); }
297 
298  bool operator==( Algorithm* a ) const { return ( m_a == a ); }
299 
300  bool operator<( const SchedulerState& rhs ) const { return ( m_a < rhs.alg() ); }
301 
302  private:
305  pthread_t m_t;
306  };
307 
310 
311 public:
312  void addAlg( Algorithm*, EventContext*, pthread_t );
313  bool delAlg( Algorithm* );
314  void dumpState() override;
315 
316 private:
317  void dumpState( std::ostringstream& );
319 
320 };
321 
322 #endif // GAUDIHIVE_AVALANCHESCHEDULERSVC_H
Gaudi::Property< bool > m_showDataFlow
StatusCode tryPopFinishedEvent(EventContext *&eventContext) override
Try to fetch an event from the scheduler.
Gaudi::Property< std::string > m_whiteboardSvcName
unsigned int m_IOBoundAlgosInFlight
Number of algoritms presently in flight.
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:750
Implementation of property with value of concrete type.
Definition: Property.h:319
Gaudi::Property< bool > m_dumpIntraEventDynamics
Gaudi::Property< bool > m_showDataDeps
bool operator==(const SchedulerState &ss) const
StatusCode initialize() override
Initialise.
void dumpSchedulerState(int iSlot)
Dump the state of the scheduler.
StatusCode promoteToScheduled(unsigned int iAlgo, int si)
Algorithm promotion.
enqueueSchedulerActionTask(AvalancheSchedulerSvc *scheduler, std::function< StatusCode()> _closure)
StatusCode isStalled(int si)
Check if the scheduling is in a stall.
void activate()
Activate scheduler.
Gaudi::Property< std::string > m_useDataLoader
Gaudi::Property< std::string > m_optimizationMode
friend std::ostream & operator<<(std::ostream &os, const SchedulerState &ss)
StatusCode promoteToAsyncScheduled(unsigned int iAlgo, int si)
This class represents an entry point to all the event specific data.
Definition: EventContext.h:24
unsigned int algname2index(const std::string &algoname)
Convert a name to an integer.
AlgsExecutionStates::State State
void addAlg(Algorithm *, EventContext *, pthread_t)
std::atomic< ActivationState > m_isActive
Flag to track if the scheduler is active or not.
std::unordered_map< std::string, unsigned int > m_algname_index_map
Map to bookkeep the information necessary to the name2index conversion.
Gaudi::Property< bool > m_checkDeps
STL class.
Gaudi::Property< bool > m_useIOBoundAlgScheduler
bool operator<(const SchedulerState &rhs) const
std::atomic_int m_freeSlots
Atomic to account for asyncronous updates by the scheduler wrt the rest.
StatusCode pushNewEvents(std::vector< EventContext * > &eventContexts) override
unsigned int m_algosInFlight
Number of algoritms presently in flight.
static std::list< SchedulerState > m_sState
SmartIF< IAlgResourcePool > m_algResourcePool
Cache for the algorithm resource pool.
Manage the execution flow using a graph of task precedence rules Once initialized, the graph is const and can be shared across events.
StatusCode popFinishedEvent(EventContext *&eventContext) override
Blocks until an event is availble.
Gaudi::Property< bool > m_showControlFlow
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
SmartIF< IHiveWhiteBoard > m_whiteboard
A shortcut to the whiteboard.
tbb::concurrent_bounded_queue< EventContext * > m_finishedEvents
Queue of finished events.
std::vector< std::string > m_algname_vect
Vector to bookkeep the information necessary to the index2name conversion.
SchedulerState(Algorithm *a, EventContext *e, pthread_t t)
Gaudi::Property< std::string > m_IOBoundAlgSchedulerSvcName
std::function< StatusCode()> action
StatusCode finalize() override
Finalise.
~AvalancheSchedulerSvc() override=default
Destructor.
concurrency::ExecutionFlowManager m_efManager
Member to take care of the control flow.
Gaudi::Property< int > m_threadPoolSize
SmartIF< IThreadPoolSvc > m_threadPoolSvc
STL class.
SmartIF< IAccelerator > m_IOBoundAlgScheduler
A shortcut to IO-bound algorithm scheduler.
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:27
bool m_updateNeeded
Keep track of update actions scheduled.
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:78
StatusCode promoteToFinished(unsigned int iAlgo, int si)
StatusCode pushNewEvent(EventContext *eventContext) override
Make an event available to the scheduler.
STL class.
Gaudi::Property< bool > m_simulateExecution
Base class used to extend a class implementing other interfaces.
Definition: extends.h:10
StatusCode eventFailed(EventContext *eventContext)
Method to check if an event failed and take appropriate actions.
const std::string & index2algname(unsigned int index)
Convert an integer to a name.
StatusCode promoteToExecuted(unsigned int iAlgo, int si, IAlgorithm *algo, EventContext *)
The call to this method is triggered only from within the AlgoExecutionTask.
T hex(T...args)
unsigned int freeSlots() override
Get free slots number.
std::vector< EventSlot > m_eventSlots
Vector of events slots.
StatusCode promoteToAsyncExecuted(unsigned int iAlgo, int si, IAlgorithm *algo, EventContext *)
The call to this method is triggered only from within the IOBoundAlgTask.
StatusCode deactivate()
Deactivate scheduler.
concurrency::PrecedenceRulesGraph * m_efg
StatusCode updateStates(int si=-1, const std::string &algo_name=std::string())
Loop on algorithm in the slots and promote them to successive states (-1 means all slots...
State
Execution states of the algorithms.
STL class.
STL class.
SmartIF< IAlgExecStateSvc > m_algExecStateSvc
Algorithm execution state manager.
Gaudi::Property< unsigned int > m_maxIOBoundAlgosInFlight
tbb::concurrent_bounded_queue< action > m_actionsQueue
Queue where closures are stored and picked for execution.
std::thread m_thread
The thread in which the activate function runs.
StatusCode m_drain()
Drain the actions present in the queue.