The Gaudi Framework  v33r2 (a6f0ec87)
AlgTask.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_ALGTASK_H
12 #define GAUDIHIVE_ALGTASK_H
13 
14 #include "AvalancheSchedulerSvc.h"
15 #include "RetCodeGuard.h"
16 
17 // Framework include files
21 #include "GaudiKernel/IProperty.h"
24 #include "GaudiKernel/SmartIF.h"
26 #include <Gaudi/Algorithm.h>
27 
28 #include <functional>
29 
30 namespace Gaudi {
31  namespace Concurrency {
32  extern thread_local bool ThreadInitDone;
33  }
34 } // namespace Gaudi
35 
36 template <class T>
37 class AlgTask : public T {
38 public:
41 
43  IAlgExecStateSvc* aem )
44  : m_ts( std::move( ts ) ), m_scheduler( scheduler ), m_aess( aem ), m_serviceLocator( svcLocator ){};
45 
46  T* execute() override {
47 
49  MsgStream log( messageSvc, "AlgTask" );
50 
51  // Get task specification dynamically if it was not provided statically
52  if ( !m_ts.algPtr )
53  if ( !m_scheduler->m_scheduledQueue.try_pop( m_ts ) ) {
54  log << MSG::WARNING << "Specification not complete or void while task is running" << endmsg;
55  return nullptr;
56  }
57 
58  EventContext& evtCtx = *( m_ts.contextPtr );
59  IAlgorithm*& iAlgoPtr = m_ts.algPtr;
60 
61  Gaudi::Algorithm* this_algo = dynamic_cast<Gaudi::Algorithm*>( iAlgoPtr );
62  if ( !this_algo ) { throw GaudiException( "Cast to Algorithm failed!", "AlgTask", StatusCode::FAILURE ); }
63 
64  bool eventfailed = false;
66 
67  // Get the IProperty interface of the ApplicationMgr to pass it to RetCodeGuard
68  const SmartIF<IProperty> appmgr( m_serviceLocator );
69 
71  log << MSG::DEBUG << "New thread detected: 0x" << std::hex << pthread_self() << std::dec
72  << ". Doing thread local initialization." << endmsg;
73  if ( SmartIF<IThreadPoolSvc> tps{m_serviceLocator->service( "ThreadPoolSvc" )} ) {
74  tps->initThisThread();
75  } else {
76  log << MSG::ERROR << "Unable to get the ThreadPoolSvc to trigger thread local initialization" << endmsg;
77  throw GaudiException( "Retrieval of ThreadPoolSvc failed", "AlgTask", StatusCode::FAILURE );
78  }
79  }
80 
81  // select the appropriate store
82  this_algo->whiteboard()->selectStore( evtCtx.valid() ? evtCtx.slot() : 0 ).ignore();
83  try {
85 
86  if ( UNLIKELY( iAlgoPtr->sysExecute( evtCtx ).isFailure() ) ) {
87  log << MSG::WARNING << "Execution of algorithm " << m_ts.algName << " failed" << endmsg;
88  eventfailed = true;
89  }
90  rcg.ignore(); // disarm the guard
91  } catch ( const GaudiException& Exception ) {
92  log << MSG::FATAL << ".executeEvent(): Exception with tag=" << Exception.tag() << " thrown by " << m_ts.algName
93  << endmsg;
94  log << MSG::ERROR << Exception << endmsg;
95  eventfailed = true;
96  } catch ( const std::exception& Exception ) {
97  log << MSG::FATAL << ".executeEvent(): Standard std::exception thrown by " << m_ts.algName << endmsg;
98  log << MSG::ERROR << Exception.what() << endmsg;
99  eventfailed = true;
100  } catch ( ... ) {
101  log << MSG::FATAL << ".executeEvent(): UNKNOWN Exception thrown by " << m_ts.algName << endmsg;
102  eventfailed = true;
103  }
104 
105  // A FAILURE in algorithm execution must be communicated to the framework
106  m_aess->updateEventStatus( eventfailed, evtCtx );
107 
108  // Release algorithm
110 
111  // schedule a sign-off of the Algorithm execution
113  [schdlr = this->m_scheduler, ts = std::move( this->m_ts )]() { return schdlr->signoff( ts ); } );
114 
116 
117  return nullptr;
118  }
119 
120  void operator()() { execute(); };
121 
122 private:
127 };
128 
129 #endif
Struct to hold entries in the alg queues.
#define UNLIKELY(x)
Definition: Kernel.h:106
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:34
ContextID_t slot() const
Definition: EventContext.h:51
Define general base for Gaudi exception.
Helper class to set the application return code in case of early exit (e.g.
Definition: RetCodeGuard.h:19
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:35
IAlgExecStateSvc * m_aess
Definition: AlgTask.h:125
SmartIF< IHiveWhiteBoard > & whiteboard() const
Definition: Algorithm.cpp:591
virtual const std::string & tag() const
name tag for the exception, or exception type
STL namespace.
AlgTask(AvalancheSchedulerSvc::TaskSpec &&ts, AvalancheSchedulerSvc *scheduler, ISvcLocator *svcLocator, IAlgExecStateSvc *aem)
Definition: AlgTask.h:42
AvalancheSchedulerSvc * m_scheduler
Definition: AlgTask.h:124
This class represents an entry point to all the event specific data.
Definition: EventContext.h:34
bool valid() const
Definition: EventContext.h:54
constexpr int UnhandledException
Definition: AppReturnCode.h:37
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:86
AlgTask(AvalancheSchedulerSvc *scheduler, ISvcLocator *svcLocator, IAlgExecStateSvc *aem)
Definition: AlgTask.h:39
virtual StatusCode selectStore(size_t partitionIndex)=0
Activate an given 'slot' for all subsequent calls within the same thread id.
SmartIF< IAlgResourcePool > m_algResourcePool
Cache for the algorithm resource pool.
GAUDI_API ISvcLocator * svcLocator()
T what(T... args)
Abstract interface for a service that manages the Algorithm execution states.
virtual StatusCode sysExecute(const EventContext &)=0
System execution. This method invokes the execute() method of a concrete algorithm.
T * execute() override
Definition: AlgTask.h:46
AvalancheSchedulerSvc::TaskSpec m_ts
Definition: AlgTask.h:120
STL class.
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:38
GAUDI_API void setCurrentContext(const EventContext *ctx)
T move(T... args)
virtual void updateEventStatus(const bool &b, const EventContext &ctx)=0
void operator()()
Definition: AlgTask.h:120
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:168
thread_local bool ThreadInitDone
tbb::concurrent_priority_queue< TaskSpec, AlgQueueSort > m_scheduledQueue
Queues for scheduled algorithms.
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:89
constexpr static const auto FAILURE
Definition: StatusCode.h:101
GAUDI_API void setCurrentContextEvt(long int evtN)
T hex(T... args)
void ignore()
Definition: RetCodeGuard.h:22
bool isFailure() const
Definition: StatusCode.h:145
SmartIF< ISvcLocator > m_serviceLocator
Definition: AlgTask.h:126
virtual StatusCode releaseAlgorithm(std::string_view name, IAlgorithm *&algo)=0
Release a certain algorithm.
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
tbb::concurrent_bounded_queue< action > m_actionsQueue
Queue where closures are stored and picked for execution.