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_maxEventsInFlight{this, "MaxEventsInFlight", 0,
134  "Maximum number of event processed simultaneously"};
135  Gaudi::Property<int> m_threadPoolSize{this, "ThreadPoolSize", -1,
136  "Size of the threadpool initialised by TBB; a value of -1 gives TBB the freedom to choose"};
137  Gaudi::Property<std::string> m_whiteboardSvcName{this, "WhiteboardSvc", "EventDataSvc",
138  "The whiteboard name"};
140  "IOBoundAlgSchedulerSvc"};
142  "[[deprecated]] Taken from the whiteboard"};
143  Gaudi::Property<unsigned int> m_maxIOBoundAlgosInFlight{this, "MaxIOBoundAlgosInFlight", 0,
144  "Maximum number of simultaneous I/O-bound algorithms"};
145  Gaudi::Property<bool> m_simulateExecution{this, "SimulateExecution", false,
146  "Flag to perform single-pass simulation of execution flow before the actual execution"};
148  "The following modes are currently available: PCE, COD, DRE, E"};
149  Gaudi::Property<bool> m_dumpIntraEventDynamics{this, "DumpIntraEventDynamics", false,
150  "Dump intra-event concurrency dynamics to csv file"};
151  Gaudi::Property<bool> m_useIOBoundAlgScheduler{this, "PreemptiveIOBoundTasks", false,
152  "Turn on preemptive way of scheduling of I/O-bound algorithms"};
154  "[[deprecated]]"};
155  Gaudi::Property<bool> m_checkDeps{this, "CheckDependencies", false, "[[deprecated]]"};
156 
157  // Utils and shortcuts ----------------------------------------------------
158 
160  void activate();
161 
164 
167 
170 
172  inline unsigned int algname2index( const std::string& algoname );
173 
176 
178  inline const std::string& index2algname( unsigned int index );
179 
182 
185 
188 
191 
193  std::atomic_int m_freeSlots;
194 
196  tbb::concurrent_bounded_queue<EventContext*> m_finishedEvents;
197 
199  StatusCode eventFailed( EventContext* eventContext );
200 
203 
204  // States management ------------------------------------------------------
205 
207  unsigned int m_algosInFlight = 0;
208 
210  unsigned int m_IOBoundAlgosInFlight = 0;
211 
214  StatusCode updateStates( int si = -1, const std::string& algo_name = std::string() );
215 
217  StatusCode promoteToScheduled( unsigned int iAlgo, int si );
218  StatusCode promoteToAsyncScheduled( unsigned int iAlgo, int si ); // tests of an asynchronous scheduler
219  StatusCode promoteToExecuted( unsigned int iAlgo, int si, IAlgorithm* algo, EventContext* );
220  StatusCode promoteToAsyncExecuted( unsigned int iAlgo, int si, IAlgorithm* algo,
221  EventContext* ); // tests of an asynchronous scheduler
222  StatusCode promoteToFinished( unsigned int iAlgo, int si );
223 
225  StatusCode isStalled( int si );
226 
228  void dumpSchedulerState( int iSlot );
229 
231  bool m_updateNeeded = true;
232 
233  // Algos Management -------------------------------------------------------
236 
239 
240  // Actions management -----------------------------------------------------
241 
243  tbb::concurrent_bounded_queue<action> m_actionsQueue;
244 
245  // helper task to enqueue the scheduler's actions (closures)
246  struct enqueueSchedulerActionTask: public tbb::task {
247 
250 
252  m_closure(_closure), m_scheduler(scheduler) {}
253 
254  tbb::task* execute() override {
255  m_scheduler->m_actionsQueue.push(m_closure);
256  return nullptr;
257  }
258  };
259 
260  // ------------------------------------------------------------------------
261 
264 
265  // Service for thread pool initialization
267 
268  bool m_first = true;
269 
271  {
272 
273  public:
274  SchedulerState( Algorithm* a, EventContext* e, pthread_t t ) : m_a( a ), m_e( *e ), m_t( t ) {}
275 
276  Algorithm* alg() const { return m_a; }
277  EventContext ctx() const { return m_e; }
278  pthread_t thread() const { return m_t; }
279 
281  {
282  os << ss.ctx() << " a: " << ss.alg()->name() << " [" << std::hex << ss.alg() << std::dec << "] t: 0x"
283  << std::hex << ss.thread() << std::dec;
284  return os;
285  }
286 
287  bool operator==( const SchedulerState& ss ) const { return ( m_a == ss.alg() ); }
288 
289  bool operator==( Algorithm* a ) const { return ( m_a == a ); }
290 
291  bool operator<( const SchedulerState& rhs ) const { return ( m_a < rhs.alg() ); }
292 
293  private:
296  pthread_t m_t;
297  };
298 
301 
302 public:
303  void addAlg( Algorithm*, EventContext*, pthread_t );
304  bool delAlg( Algorithm* );
305  void dumpState() override;
306 
307 private:
308  void dumpState( std::ostringstream& );
309 };
310 
311 #endif // GAUDIHIVE_AVALANCHESCHEDULERSVC_H
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:715
Implementation of property with value of concrete type.
Definition: Property.h:314
Gaudi::Property< bool > m_dumpIntraEventDynamics
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::vector< std::vector< std::string > > > m_algosDependencies
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:25
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.
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.
Gaudi::Property< int > m_maxEventsInFlight
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.
Gaudi::Property< unsigned int > m_maxAlgosInFlight
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.
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.