ForwardSchedulerSvc.h
Go to the documentation of this file.
1 #ifndef GAUDIHIVE_FORWARDSCHEDULERSVC_H
2 #define GAUDIHIVE_FORWARDSCHEDULERSVC_H
3 
4 // Local includes
5 #include "AlgsExecutionStates.h"
6 #include "DataFlowManager.h"
7 #include "EventSlot.h"
8 #include "ExecutionFlowManager.h"
9 
10 // 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 
83 class ForwardSchedulerSvc : public extends<Service, IScheduler>
84 {
85 public:
87  using extends::extends;
88 
90  ~ForwardSchedulerSvc() override = default;
91 
93  StatusCode initialize() override;
94 
96  StatusCode finalize() override;
97 
99  StatusCode pushNewEvent( EventContext* eventContext ) override;
100 
101  // Make multiple events available to the scheduler
102  StatusCode pushNewEvents( std::vector<EventContext*>& eventContexts ) override;
103 
105  StatusCode popFinishedEvent( EventContext*& eventContext ) override;
106 
108  StatusCode tryPopFinishedEvent( EventContext*& eventContext ) override;
109 
111  unsigned int freeSlots() override;
112 
113 private:
114  enum ActivationState { INACTIVE = 0, ACTIVE = 1, FAILURE = 2 };
115 
116  Gaudi::Property<int> m_maxEventsInFlight{this, "MaxEventsInFlight", 0,
117  "Maximum number of event processed simultaneously"};
118  Gaudi::Property<int> m_threadPoolSize{this, "ThreadPoolSize", -1,
119  "Size of the threadpool initialised by TBB; a value of -1 gives TBB the freedom to choose"};
120  Gaudi::Property<std::string> m_whiteboardSvcName{this, "WhiteboardSvc", "EventDataSvc", "The whiteboard name"};
122  "[[deprecated]] Taken from the whiteboard"};
124  this, "AlgosDependencies", {}, "[[deprecated]]"};
125  Gaudi::Property<bool> m_checkDeps{this, "CheckDependencies", false,
126  "Runtime check of Algorithm Data Dependencies"};
128  "Attribute unmet input dependencies to this DataLoader Algorithm"};
129 
130  // Utils and shortcuts ----------------------------------------------------
131 
133  void activate();
134 
137 
140 
143 
145  inline unsigned int algname2index( const std::string& algoname );
146 
149 
151  inline const std::string& index2algname( unsigned int index );
152 
155 
158 
161 
163  std::atomic_int m_freeSlots;
164 
166  tbb::concurrent_bounded_queue<EventContext*> m_finishedEvents;
167 
169  StatusCode eventFailed( EventContext* eventContext );
170 
173 
174  // States management ------------------------------------------------------
175 
177  unsigned int m_algosInFlight = 0;
178 
181  StatusCode updateStates( int si = -1 );
182 
184  StatusCode promoteToControlReady( unsigned int iAlgo, int si );
185  StatusCode promoteToDataReady( unsigned int iAlgo, int si );
186  StatusCode promoteToScheduled( unsigned int iAlgo, int si );
187  StatusCode promoteToExecuted( unsigned int iAlgo, int si, IAlgorithm* algo, EventContext* );
188  StatusCode promoteToFinished( unsigned int iAlgo, int si );
189 
191  StatusCode isStalled( int si );
192 
194  void dumpSchedulerState( int iSlot );
195 
197  bool m_updateNeeded = true;
198 
199  // Algos Management -------------------------------------------------------
202 
205 
206  // Actions management -----------------------------------------------------
207 
209  tbb::concurrent_bounded_queue<action> m_actionsQueue;
210 
211  // helper task to enqueue the scheduler's actions (closures)
212  struct enqueueSchedulerActionTask: public tbb::task {
213 
216 
218  m_closure(_closure), m_scheduler(scheduler) {}
219 
220  tbb::task* execute() override {
221  m_scheduler->m_actionsQueue.push(m_closure);
222  return nullptr;
223  }
224  };
225 
226  // ------------------------------------------------------------------------
227 
230 
231  // Service for thread pool initialization
233 
234  bool m_first = true;
235 
237  {
238 
239  public:
240  SchedulerState( Algorithm* a, EventContext* e, pthread_t t ) : m_a( a ), m_e( *e ), m_t( t ) {}
241 
242  Algorithm* alg() const { return m_a; }
243  EventContext ctx() const { return m_e; }
244  pthread_t thread() const { return m_t; }
245 
247  {
248  os << ss.ctx() << " a: " << ss.alg()->name() << " [" << std::hex << ss.alg() << std::dec << "] t: 0x"
249  << std::hex << ss.thread() << std::dec;
250  return os;
251  }
252 
253  bool operator==( const SchedulerState& ss ) const { return ( m_a == ss.alg() ); }
254 
255  bool operator==( Algorithm* a ) const { return ( m_a == a ); }
256 
257  bool operator<( const SchedulerState& rhs ) const { return ( m_a < rhs.alg() ); }
258 
259  private:
262  pthread_t m_t;
263  };
264 
267 
268 public:
269  void addAlg( Algorithm*, EventContext*, pthread_t );
270  bool delAlg( Algorithm* );
271  void dumpState() override;
272 
273 private:
274  void dumpState( std::ostringstream& );
275 };
276 
277 #endif // GAUDIHIVE_FORWARDSCHEDULERSVC_H
StatusCode deactivate()
Deactivate scheduler.
SchedulerState(Algorithm *a, EventContext *e, pthread_t t)
StatusCode initialize() override
Initialise.
StatusCode eventFailed(EventContext *eventContext)
Method to check if an event failed and take appropriate actions.
SmartIF< IAlgResourcePool > m_algResourcePool
Cache for the algorithm resource pool.
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
StatusCode isStalled(int si)
Check if the scheduling is in a stall.
StatusCode updateStates(int si=-1)
Loop on algorithm in the slots and promote them to successive states (-1 means all slots...
StatusCode finalize() override
Finalise.
SmartIF< IThreadPoolSvc > m_threadPoolSvc
AlgsExecutionStates::State State
Gaudi::Property< bool > m_checkDeps
The SchedulerSvc implements the IScheduler interface.
StatusCode pushNewEvent(EventContext *eventContext) override
Make an event available to the scheduler.
Gaudi::Property< std::vector< std::vector< std::string > > > m_algosDependencies
This class represents an entry point to all the event specific data.
Definition: EventContext.h:25
SmartIF< IHiveWhiteBoard > m_whiteboard
A shortcut to the whiteboard.
StatusCode m_drain()
Drain the actions present in the queue.
Gaudi::Property< unsigned int > m_maxAlgosInFlight
STL class.
enqueueSchedulerActionTask(ForwardSchedulerSvc *scheduler, std::function< StatusCode()> _closure)
std::atomic_int m_freeSlots
Atomic to account for asyncronous updates by the scheduler wrt the rest.
void addAlg(Algorithm *, EventContext *, pthread_t)
unsigned int m_algosInFlight
Number of algoritms presently in flight.
Manage the execution flow using a graph of task precedence rules Once initialized, the graph is const and can be shared across events.
tbb::concurrent_bounded_queue< EventContext * > m_finishedEvents
Queue of finished events.
std::thread m_thread
The thread in which the activate function runs.
bool operator<(const SchedulerState &rhs) const
std::vector< std::string > m_algname_vect
Vector to bookkeep the information necessary to the index2name conversion.
static std::list< SchedulerState > m_sState
Gaudi::Property< std::string > m_useDataLoader
std::vector< EventSlot > m_eventSlots
Vector of events slots.
StatusCode promoteToScheduled(unsigned int iAlgo, int si)
StatusCode promoteToFinished(unsigned int iAlgo, int si)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
bool operator==(Algorithm *a) const
Gaudi::Property< std::string > m_whiteboardSvcName
unsigned int algname2index(const std::string &algoname)
Convert a name to an integer.
StatusCode promoteToExecuted(unsigned int iAlgo, int si, IAlgorithm *algo, EventContext *)
bool operator==(const SchedulerState &ss) const
~ForwardSchedulerSvc() override=default
Destructor.
unsigned int freeSlots() override
Get free slots number.
StatusCode pushNewEvents(std::vector< EventContext * > &eventContexts) override
Gaudi::Property< int > m_maxEventsInFlight
STL class.
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:27
tbb::concurrent_bounded_queue< action > m_actionsQueue
Queue where closures are stored and picked for execution.
bool m_updateNeeded
Keep track of update actions scheduled.
StatusCode tryPopFinishedEvent(EventContext *&eventContext) override
Try to fetch an event from the scheduler.
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:78
STL class.
std::unordered_map< std::string, unsigned int > m_algname_index_map
Map to bookkeep the information necessary to the name2index conversion.
void activate()
Activate scheduler.
Base class used to extend a class implementing other interfaces.
Definition: extends.h:10
SmartIF< IAlgExecStateSvc > m_algExecStateSvc
Algorithm execution state manager.
StatusCode promoteToControlReady(unsigned int iAlgo, int si)
Algorithm promotion: Accepted by the control flow.
concurrency::ExecutionFlowManager m_efManager
Member to take care of the control flow.
StatusCode popFinishedEvent(EventContext *&eventContext) override
Blocks until an event is availble.
void dumpSchedulerState(int iSlot)
Dump the state of the scheduler.
T hex(T...args)
std::function< StatusCode()> action
static std::mutex m_ssMut
const std::string & index2algname(unsigned int index)
Convert an integer to a name.
bool delAlg(Algorithm *)
friend std::ostream & operator<<(std::ostream &os, const SchedulerState &ss)
std::atomic< ActivationState > m_isActive
Flag to track if the scheduler is active or not.
State
Execution states of the algorithms.
Gaudi::Property< int > m_threadPoolSize
STL class.
STL class.
StatusCode promoteToDataReady(unsigned int iAlgo, int si)