The Gaudi Framework  v38r0 (2143aa4c)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 class AlgTask {
37 public:
39  : m_scheduler( scheduler ), m_aess( aem ), m_serviceLocator( svcLocator ), m_blocking( blocking ){};
40 
41  void operator()() const {
42 
44  MsgStream log( messageSvc, "AlgTask" );
45 
46  // Get task specification dynamically if it was not provided statically
48  if ( !m_scheduler->next( ts, m_blocking ) ) {
49  log << MSG::WARNING << "Missing specification while task is running" << endmsg;
50  return;
51  }
52 
53  EventContext& evtCtx = *( ts.contextPtr );
54  IAlgorithm*& iAlgoPtr = ts.algPtr;
55 
56  Gaudi::Algorithm* this_algo = dynamic_cast<Gaudi::Algorithm*>( iAlgoPtr );
57  if ( !this_algo ) { throw GaudiException( "Cast to Algorithm failed!", "AlgTask", StatusCode::FAILURE ); }
58 
59  bool eventfailed = false;
61 
62  // Get the IProperty interface of the ApplicationMgr to pass it to RetCodeGuard
63  const SmartIF<IProperty> appmgr( m_serviceLocator );
64 
66  log << MSG::DEBUG << "New thread detected: 0x" << std::hex << pthread_self() << std::dec
67  << ". Doing thread local initialization." << endmsg;
68  if ( SmartIF<IThreadPoolSvc> tps{ m_serviceLocator->service( "ThreadPoolSvc" ) } ) {
69  tps->initThisThread();
70  } else {
71  log << MSG::ERROR << "Unable to get the ThreadPoolSvc to trigger thread local initialization" << endmsg;
72  throw GaudiException( "Retrieval of ThreadPoolSvc failed", "AlgTask", StatusCode::FAILURE );
73  }
74  }
75 
76  // select the appropriate store
77  this_algo->whiteboard()->selectStore( evtCtx.valid() ? evtCtx.slot() : 0 ).ignore();
78  try {
79  RetCodeGuard rcg( appmgr, Gaudi::ReturnCode::UnhandledException );
80 
81  if ( iAlgoPtr->sysExecute( evtCtx ).isFailure() ) {
82  log << MSG::WARNING << "Execution of algorithm " << ts.algName << " failed" << endmsg;
83  eventfailed = true;
84  }
85  rcg.ignore(); // disarm the guard
86  } catch ( const GaudiException& Exception ) {
87  log << MSG::FATAL << ".executeEvent(): Exception with tag=" << Exception.tag() << " thrown by " << ts.algName
88  << endmsg;
89  log << MSG::ERROR << Exception << endmsg;
90  eventfailed = true;
91  } catch ( const std::exception& Exception ) {
92  log << MSG::FATAL << ".executeEvent(): Standard std::exception thrown by " << ts.algName << endmsg;
93  log << MSG::ERROR << Exception.what() << endmsg;
94  eventfailed = true;
95  } catch ( ... ) {
96  log << MSG::FATAL << ".executeEvent(): UNKNOWN Exception thrown by " << ts.algName << endmsg;
97  eventfailed = true;
98  }
99 
100  // A FAILURE in algorithm execution must be communicated to the framework
101  m_aess->updateEventStatus( eventfailed, evtCtx );
102 
103  // Release algorithm
104  m_scheduler->m_algResourcePool->releaseAlgorithm( ts.algName, iAlgoPtr ).ignore();
105 
106  // schedule a sign-off of the Algorithm execution
108  [schdlr = this->m_scheduler, ts = std::move( ts )]() { return schdlr->signoff( ts ); } );
109 
111  }
112 
113 private:
114  // Shortcuts to services
118  // Marks the task as CPU-blocking or not
119  bool m_blocking{ false };
120 };
121 
122 #endif
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
EventContext::valid
bool valid() const
Definition: EventContext.h:54
IAlgorithm::sysExecute
virtual StatusCode sysExecute(const EventContext &)=0
System execution. This method invokes the execute() method of a concrete algorithm.
Gaudi::Hive::setCurrentContext
GAUDI_API void setCurrentContext(const EventContext *ctx)
Definition: ThreadLocalContext.cpp:41
AvalancheSchedulerSvc::TaskSpec
Struct to hold entries in the alg queues.
Definition: AvalancheSchedulerSvc.h:291
std::exception
STL class.
Gaudi.Configuration.log
log
Definition: Configuration.py:29
std::move
T move(T... args)
AlgTask::AlgTask
AlgTask(AvalancheSchedulerSvc *scheduler, ISvcLocator *svcLocator, IAlgExecStateSvc *aem, bool blocking=false)
Definition: AlgTask.h:38
ISvcLocator
Definition: ISvcLocator.h:46
GaudiException
Definition: GaudiException.h:31
Gaudi::Concurrency::ThreadInitDone
thread_local bool ThreadInitDone
Definition: ThreadInitTask.cpp:22
MSG::WARNING
@ WARNING
Definition: IMessageSvc.h:25
IMessageSvc.h
AlgTask::m_serviceLocator
SmartIF< ISvcLocator > m_serviceLocator
Definition: AlgTask.h:117
std::hex
T hex(T... args)
Gaudi::Algorithm::whiteboard
SmartIF< IHiveWhiteBoard > & whiteboard() const
Definition: Algorithm.cpp:570
RetCodeGuard.h
Gaudi::svcLocator
GAUDI_API ISvcLocator * svcLocator()
SmartIF.h
IAlgorithm
Definition: IAlgorithm.h:38
AlgTask::m_blocking
bool m_blocking
Definition: AlgTask.h:119
compareRootHistos.ts
ts
Definition: compareRootHistos.py:488
EventContext::slot
ContextID_t slot() const
Definition: EventContext.h:51
Gaudi::Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:90
AvalancheSchedulerSvc
Definition: AvalancheSchedulerSvc.h:112
IAlgExecStateSvc
Abstract interface for a service that manages the Algorithm execution states.
Definition: IAlgExecStateSvc.h:80
Algorithm.h
SmartIF< IMessageSvc >
Gaudi::ReturnCode::UnhandledException
constexpr int UnhandledException
Definition: AppReturnCode.h:37
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
AvalancheSchedulerSvc::m_algResourcePool
SmartIF< IAlgResourcePool > m_algResourcePool
Cache for the algorithm resource pool.
Definition: AvalancheSchedulerSvc.h:283
MsgStream
Definition: MsgStream.h:34
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
MSG::FATAL
@ FATAL
Definition: IMessageSvc.h:25
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
ThreadLocalContext.h
EventContext.h
AlgTask::m_aess
IAlgExecStateSvc * m_aess
Definition: AlgTask.h:116
MSG::ERROR
@ ERROR
Definition: IMessageSvc.h:25
EventContext
Definition: EventContext.h:34
AvalancheSchedulerSvc::m_actionsQueue
tbb::concurrent_bounded_queue< action > m_actionsQueue
Queue where closures are stored and picked for execution.
Definition: AvalancheSchedulerSvc.h:288
AsyncIncidents.scheduler
scheduler
Definition: AsyncIncidents.py:51
AlgTask
Definition: AlgTask.h:36
IProperty.h
AlgTask::operator()
void operator()() const
Definition: AlgTask.h:41
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ISvcLocator.h
IAlgExecStateSvc.h
AvalancheSchedulerSvc::next
bool next(TaskSpec &ts, bool blocking=false)
Definition: AvalancheSchedulerSvc.h:344
IAlgExecStateSvc::updateEventStatus
virtual void updateEventStatus(const bool &b, const EventContext &ctx)=0
IThreadPoolSvc.h
Gaudi::Hive::setCurrentContextEvt
GAUDI_API void setCurrentContextEvt(long int evtN)
Definition: ThreadLocalContext.cpp:32
AvalancheSchedulerSvc.h
AlgTask::m_scheduler
AvalancheSchedulerSvc * m_scheduler
Definition: AlgTask.h:115