The Gaudi Framework  v30r1 (5d4f4ae2)
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 "PrecedenceSvc.h"
8 
9 // Framework include files
10 #include "GaudiKernel/Algorithm.h"
14 #include "GaudiKernel/ICondSvc.h"
16 #include "GaudiKernel/IRunable.h"
17 #include "GaudiKernel/IScheduler.h"
19 #include "GaudiKernel/Service.h"
20 
21 // C++ include files
22 #include <functional>
23 #include <string>
24 #include <thread>
25 #include <unordered_map>
26 #include <vector>
27 
28 // External libs
29 #include "tbb/concurrent_queue.h"
30 #include "tbb/task.h"
31 
34 
35 //---------------------------------------------------------------------------
36 
102 class AvalancheSchedulerSvc : public extends<Service, IScheduler>
103 {
104 public:
106  using extends::extends;
107 
109  ~AvalancheSchedulerSvc() override = default;
110 
112  StatusCode initialize() override;
113 
115  StatusCode finalize() override;
116 
118  StatusCode pushNewEvent( EventContext* eventContext ) override;
119 
120  // Make multiple events available to the scheduler
121  StatusCode pushNewEvents( std::vector<EventContext*>& eventContexts ) override;
122 
124  StatusCode popFinishedEvent( EventContext*& eventContext ) override;
125 
127  StatusCode tryPopFinishedEvent( EventContext*& eventContext ) override;
128 
130  unsigned int freeSlots() override;
131 
133  virtual StatusCode scheduleEventView( EventContext const* sourceContext, std::string const& nodeName,
134  EventContext* viewContext ) override;
135 
136 private:
137  enum ActivationState { INACTIVE = 0, ACTIVE = 1, FAILURE = 2 };
138 
140  this, "ThreadPoolSize", -1,
141  "Size of the threadpool initialised by TBB; a value of -1 gives TBB the freedom to choose"};
142  Gaudi::Property<std::string> m_whiteboardSvcName{this, "WhiteboardSvc", "EventDataSvc", "The whiteboard name"};
143  Gaudi::Property<std::string> m_IOBoundAlgSchedulerSvcName{this, "IOBoundAlgSchedulerSvc", "IOBoundAlgSchedulerSvc"};
144  Gaudi::Property<unsigned int> m_maxIOBoundAlgosInFlight{this, "MaxIOBoundAlgosInFlight", 0,
145  "Maximum number of simultaneous I/O-bound algorithms"};
147  this, "SimulateExecution", false,
148  "Flag to perform single-pass simulation of execution flow before the actual execution"};
150  "The following modes are currently available: PCE, COD, DRE, E"};
151  Gaudi::Property<bool> m_dumpIntraEventDynamics{this, "DumpIntraEventDynamics", false,
152  "Dump intra-event concurrency dynamics to csv file"};
153  Gaudi::Property<bool> m_useIOBoundAlgScheduler{this, "PreemptiveIOBoundTasks", false,
154  "Turn on preemptive way of scheduling of I/O-bound algorithms"};
155 
156  Gaudi::Property<bool> m_checkDeps{this, "CheckDependencies", false, "Runtime check of Algorithm Data Dependencies"};
157 
159  "Attribute unmet input dependencies to this DataLoader Algorithm"};
160 
161  Gaudi::Property<bool> m_enableCondSvc{this, "EnableConditions", false, "Enable ConditionsSvc"};
162 
163  Gaudi::Property<bool> m_showDataDeps{this, "ShowDataDependencies", true,
164  "Show the INPUT and OUTPUT data dependencies of Algorithms"};
165 
166  Gaudi::Property<bool> m_showDataFlow{this, "ShowDataFlow", false,
167  "Show the configuration of DataFlow between Algorithms"};
168 
169  Gaudi::Property<bool> m_showControlFlow{this, "ShowControlFlow", false,
170  "Show the configuration of all Algorithms and Sequences"};
171 
172  // Utils and shortcuts ----------------------------------------------------
173 
175  void activate();
176 
179 
182 
185 
187  inline unsigned int algname2index( const std::string& algoname );
188 
191 
193  inline const std::string& index2algname( unsigned int index );
194 
197 
200 
203 
206 
209 
211  std::atomic_int m_freeSlots;
212 
214  tbb::concurrent_bounded_queue<EventContext*> m_finishedEvents;
215 
217  StatusCode eventFailed( EventContext* eventContext );
218 
221 
224 
225  // States management ------------------------------------------------------
226 
228  unsigned int m_algosInFlight = 0;
229 
231  unsigned int m_IOBoundAlgosInFlight = 0;
232 
235  StatusCode updateStates( int si = -1, int algo_index = -1, EventContext* = nullptr );
236 
238  StatusCode promoteToScheduled( unsigned int iAlgo, int si, EventContext* );
239  StatusCode promoteToAsyncScheduled( unsigned int iAlgo, int si, EventContext* ); // tests of an asynchronous scheduler
240  StatusCode promoteToExecuted( unsigned int iAlgo, int si, IAlgorithm* algo, EventContext* );
241  StatusCode promoteToAsyncExecuted( unsigned int iAlgo, int si, IAlgorithm* algo,
242  EventContext* ); // tests of an asynchronous scheduler
243  StatusCode promoteToFinished( unsigned int iAlgo, int si );
244 
246  StatusCode isStalled( int si );
247 
249  void dumpSchedulerState( int iSlot );
250 
251  // Algos Management -------------------------------------------------------
254 
257 
258  // Actions management -----------------------------------------------------
259 
261  tbb::concurrent_bounded_queue<action> m_actionsQueue;
262 
263  // ------------------------------------------------------------------------
264 
265  // Service for thread pool initialization
269  bool m_first = true;
270 
272  {
273 
274  public:
275  SchedulerState( Algorithm* a, EventContext* e, pthread_t t ) : m_a( a ), m_e( *e ), m_t( t ) {}
276 
277  Algorithm* alg() const { return m_a; }
278  EventContext ctx() const { return m_e; }
279  pthread_t thread() const { return m_t; }
280 
282  {
283  os << ss.ctx() << " a: " << ss.alg()->name() << " [" << std::hex << ss.alg() << std::dec << "] t: 0x"
284  << std::hex << ss.thread() << std::dec;
285  return os;
286  }
287 
288  bool operator==( const SchedulerState& ss ) const { return ( m_a == ss.alg() ); }
289 
290  bool operator==( Algorithm* a ) const { return ( m_a == a ); }
291 
292  bool operator<( const SchedulerState& rhs ) const { return ( m_a < rhs.alg() ); }
293 
294  private:
297  pthread_t m_t;
298  };
299 
302 
303 public:
304  void addAlg( Algorithm*, EventContext*, pthread_t );
305  bool delAlg( Algorithm* );
306  void dumpState() override;
307 
308 private:
309  void dumpState( std::ostringstream& );
310 };
311 
312 #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.
SmartIF< IPrecedenceSvc > m_precSvc
A shortcut to the Precedence Service.
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:737
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 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)
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.
virtual StatusCode scheduleEventView(EventContext const *sourceContext, std::string const &nodeName, EventContext *viewContext) override
Method to inform the scheduler about event views.
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.
StatusCode promoteToScheduled(unsigned int iAlgo, int si, EventContext *)
Algorithm promotion.
StatusCode updateStates(int si=-1, int algo_index=-1, EventContext *=nullptr)
Loop on algorithm in the slots and promote them to successive states (-1 for algo_index means skippin...
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.
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:28
State
Execution states of the algorithms.
StatusCode promoteToAsyncScheduled(unsigned int iAlgo, int si, EventContext *)
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.
Gaudi::Property< bool > m_enableCondSvc
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)
SmartIF< ICondSvc > m_condSvc
A shortcut to service for Conditions handling.
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.
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.