The Gaudi Framework  v33r1 (b1225454)
AvalancheSchedulerSvc.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIHIVE_AVALANCHESCHEDULERSVC_H
12 #define GAUDIHIVE_AVALANCHESCHEDULERSVC_H
13 
14 // Local includes
15 #include "AlgsExecutionStates.h"
16 #include "EventSlot.h"
17 #include "PrecedenceSvc.h"
18 
19 // Framework include files
23 #include "GaudiKernel/ICondSvc.h"
25 #include "GaudiKernel/IRunable.h"
26 #include "GaudiKernel/IScheduler.h"
28 #include "GaudiKernel/Service.h"
29 
30 // C++ include files
31 #include <functional>
32 #include <queue>
33 #include <string>
34 #include <thread>
35 #include <unordered_map>
36 #include <vector>
37 
38 // External libs
39 #include "tbb/concurrent_priority_queue.h"
40 #include "tbb/concurrent_queue.h"
41 #include "tbb/task.h"
42 
43 class IAlgorithm;
44 
45 //---------------------------------------------------------------------------
46 
112 class AvalancheSchedulerSvc : public extends<Service, IScheduler> {
113  friend class AlgoExecutionTask;
114 
115 public:
117  using extends::extends;
118 
120  ~AvalancheSchedulerSvc() override = default;
121 
123  StatusCode initialize() override;
124 
126  StatusCode finalize() override;
127 
129  StatusCode pushNewEvent( EventContext* eventContext ) override;
130 
131  // Make multiple events available to the scheduler
132  StatusCode pushNewEvents( std::vector<EventContext*>& eventContexts ) override;
133 
135  StatusCode popFinishedEvent( EventContext*& eventContext ) override;
136 
138  StatusCode tryPopFinishedEvent( EventContext*& eventContext ) override;
139 
141  unsigned int freeSlots() override;
142 
144  virtual StatusCode scheduleEventView( const EventContext* sourceContext, const std::string& nodeName,
145  std::unique_ptr<EventContext> viewContext ) override;
146 
147 private:
150 
151  enum ActivationState { INACTIVE = 0, ACTIVE = 1, FAILURE = 2 };
152 
154  this, "ThreadPoolSize", -1,
155  "Size of the threadpool initialised by TBB; a value of -1 gives TBB the freedom to choose"};
156  Gaudi::Property<std::string> m_whiteboardSvcName{this, "WhiteboardSvc", "EventDataSvc", "The whiteboard name"};
157  Gaudi::Property<std::string> m_IOBoundAlgSchedulerSvcName{this, "IOBoundAlgSchedulerSvc", "IOBoundAlgSchedulerSvc"};
158  Gaudi::Property<unsigned int> m_maxIOBoundAlgosInFlight{this, "MaxIOBoundAlgosInFlight", 0,
159  "Maximum number of simultaneous I/O-bound algorithms"};
161  this, "SimulateExecution", false,
162  "Flag to perform single-pass simulation of execution flow before the actual execution"};
164  "The following modes are currently available: PCE, COD, DRE, E"};
165  Gaudi::Property<bool> m_dumpIntraEventDynamics{this, "DumpIntraEventDynamics", false,
166  "Dump intra-event concurrency dynamics to csv file"};
167  Gaudi::Property<bool> m_useIOBoundAlgScheduler{this, "PreemptiveIOBoundTasks", false,
168  "Turn on preemptive way of scheduling of I/O-bound algorithms"};
169 
170  Gaudi::Property<bool> m_checkDeps{this, "CheckDependencies", false, "Runtime check of Algorithm Data Dependencies"};
171 
173  "Attribute unmet input dependencies to this DataLoader Algorithm"};
174 
175  Gaudi::Property<bool> m_enableCondSvc{this, "EnableConditions", false, "Enable ConditionsSvc"};
176 
177  Gaudi::Property<bool> m_showDataDeps{this, "ShowDataDependencies", true,
178  "Show the INPUT and OUTPUT data dependencies of Algorithms"};
179 
180  Gaudi::Property<bool> m_showDataFlow{this, "ShowDataFlow", false,
181  "Show the configuration of DataFlow between Algorithms"};
182 
183  Gaudi::Property<bool> m_showControlFlow{this, "ShowControlFlow", false,
184  "Show the configuration of all Algorithms and Sequences"};
185 
186  Gaudi::Property<bool> m_verboseSubSlots{this, "VerboseSubSlots", false, "Dump algorithm states for all sub-slots"};
187 
188  // Utils and shortcuts ----------------------------------------------------
189 
191  void activate();
192 
195 
198 
201 
203  inline unsigned int algname2index( const std::string& algoname ) { return m_algname_index_map[algoname]; };
204 
207 
209  inline const std::string& index2algname( unsigned int index ) { return m_algname_vect[index]; };
210 
213 
216 
219 
222 
225 
227  std::atomic_int m_freeSlots;
228 
230  tbb::concurrent_bounded_queue<EventContext*> m_finishedEvents;
231 
234 
237 
239  unsigned int m_algosInFlight = 0;
240 
242  unsigned int m_IOBoundAlgosInFlight = 0;
243 
244  // States management ------------------------------------------------------
245 
248 
249  // Update algorithm state in the appropriate event slot
250  StatusCode setAlgState( unsigned int iAlgo, EventContext* contextPtr, AState state, bool iterate = false );
251 
253  StatusCode enqueue( unsigned int iAlgo, int si, EventContext* );
254  StatusCode promoteToAsyncScheduled( unsigned int iAlgo, int si, EventContext* ); // tests of an asynchronous scheduler
255  StatusCode promoteToExecuted( unsigned int iAlgo, int si, EventContext* );
256  StatusCode promoteToAsyncExecuted( unsigned int iAlgo, int si, IAlgorithm* algo,
257  EventContext* ); // tests of an asynchronous scheduler
258  StatusCode promoteToFinished( unsigned int iAlgo, int si );
259 
261  bool isStalled( const EventSlot& ) const;
263  void eventFailed( EventContext* eventContext );
264 
266  void dumpSchedulerState( int iSlot );
267 
268  // Algos Management -------------------------------------------------------
269 
272 
273  // Actions management -----------------------------------------------------
274 
276  tbb::concurrent_bounded_queue<action> m_actionsQueue;
277 
279  struct AlgQueueEntry {
280  unsigned int algIndex;
283  unsigned int rank;
285  };
286 
288  struct AlgQueueSort {
289  bool operator()( const AlgQueueEntry& i, const AlgQueueEntry& j ) const { return ( i.rank < j.rank ); }
290  };
291 
293  tbb::concurrent_priority_queue<AlgQueueEntry, AlgQueueSort> m_scheduledQueue;
295 
296  // Prompt the scheduler to call updateStates
298 
299  // ------------------------------------------------------------------------
300 
301  // Service for thread pool initialization
305 };
306 
307 #endif // GAUDIHIVE_AVALANCHESCHEDULERSVC_H
Gaudi::Property< bool > m_showDataFlow
bool isStalled(const EventSlot &) const
Check if scheduling in a particular slot is in a stall.
EventContext * contextPtr
StatusCode tryPopFinishedEvent(EventContext *&eventContext) override
Try to fetch an event from the scheduler.
virtual StatusCode scheduleEventView(const EventContext *sourceContext, const std::string &nodeName, std::unique_ptr< EventContext > viewContext) override
Method to inform the scheduler about event views.
Class representing an event slot.
Definition: EventSlot.h:24
Gaudi::Property< std::string > m_whiteboardSvcName
unsigned int m_IOBoundAlgosInFlight
Number of algorithms presently in flight.
SmartIF< IPrecedenceSvc > m_precSvc
A shortcut to the Precedence Service.
Implementation of property with value of concrete type.
Definition: Property.h:370
Gaudi::Property< bool > m_dumpIntraEventDynamics
const std::string & index2algname(unsigned int index)
Convert an integer to a name.
Gaudi::Property< bool > m_showDataDeps
std::atomic< bool > m_needsUpdate
StatusCode initialize() override
Initialise.
void dumpSchedulerState(int iSlot)
Dump the state of the scheduler.
void activate()
Activate scheduler.
Gaudi::Property< std::string > m_useDataLoader
Gaudi::Property< std::string > m_optimizationMode
Struct to hold entries in the alg queues.
This class represents an entry point to all the event specific data.
Definition: EventContext.h:34
unsigned int algIndex
unsigned int algname2index(const std::string &algoname)
Convert a name to an integer.
bool operator()(const AlgQueueEntry &i, const AlgQueueEntry &j) const
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.
Comparison operator to sort the queues.
Gaudi::Property< bool > m_checkDeps
STL class.
Gaudi::Property< bool > m_useIOBoundAlgScheduler
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 algorithms presently in flight.
Gaudi::Property< bool > m_verboseSubSlots
SmartIF< IAlgResourcePool > m_algResourcePool
Cache for the algorithm resource pool.
StatusCode popFinishedEvent(EventContext *&eventContext) override
Blocks until an event is available.
Gaudi::Property< bool > m_showControlFlow
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
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.
Gaudi::Property< std::string > m_IOBoundAlgSchedulerSvcName
StatusCode finalize() override
Finalise.
~AvalancheSchedulerSvc() override=default
Destructor.
Gaudi::Property< int > m_threadPoolSize
SmartIF< IThreadPoolSvc > m_threadPoolSvc
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:38
State
Execution states of the algorithms.
StatusCode promoteToAsyncScheduled(unsigned int iAlgo, int si, EventContext *)
StatusCode promoteToFinished(unsigned int iAlgo, int si)
StatusCode pushNewEvent(EventContext *eventContext) override
Make an event available to the scheduler.
STL class.
STL class.
Gaudi::Property< bool > m_simulateExecution
StatusCode setAlgState(unsigned int iAlgo, EventContext *contextPtr, AState state, bool iterate=false)
Base class used to extend a class implementing other interfaces.
Definition: extends.h:20
StatusCode updateStates()
Loop on algorithm in the slots and promote them to successive states.
int slotIndex
Gaudi::Property< bool > m_enableCondSvc
IAlgorithm * algPtr
StatusCode enqueue(unsigned int iAlgo, int si, EventContext *)
Algorithm promotion.
SmartIF< ICondSvc > m_condSvc
A shortcut to service for Conditions handling.
void eventFailed(EventContext *eventContext)
Method to execute if an event failed.
unsigned int freeSlots() override
Get free slots number.
unsigned int rank
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.
tbb::concurrent_priority_queue< AlgQueueEntry, AlgQueueSort > m_scheduledQueue
Queues for scheduled algorithms.
std::queue< AlgQueueEntry > m_retryQueue
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.
StatusCode promoteToExecuted(unsigned int iAlgo, int si, EventContext *)
The call to this method is triggered only from within the AlgoExecutionTask.
std::thread m_thread
The thread in which the activate function runs.