| The Gaudi Framework v32r1 (f65d50dc)

AlgContextSvc.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // Include files
3 // ============================================================================
4 // Local
5 // ============================================================================
6 #include "AlgContextSvc.h"
7 // ============================================================================
8 // GaudiKernel
9 // ============================================================================
10 #include "GaudiKernel/ConcurrencyFlags.h"
11 #include "GaudiKernel/IIncidentSvc.h"
12 #include "GaudiKernel/ISvcLocator.h"
13 #include "GaudiKernel/MsgStream.h"
14
15 // ============================================================================
23 // ============================================================================
27 DECLARE_COMPONENT( AlgContextSvc )
28 // ============================================================================
29 // standard initialization of the service
30 // ============================================================================
31 StatusCode AlgContextSvc::initialize() {
32 // Initialize the base class
33 StatusCode sc = Service::initialize();
34 if ( sc.isFailure() ) { return sc; }
35 // Incident Service
36 if ( m_inc ) {
37 m_inc->removeListener( this );
38 m_inc.reset();
39 }
40 // perform more checks?
41 auto numSlots = Gaudi::Concurrency::ConcurrencyFlags::numConcurrentEvents();
42 numSlots = ( 1 > numSlots ) ? 1 : numSlots;
43 if ( numSlots > 1000 ) {
44 warning() << "Num Slots are greater than 1000. Is this correct? numSlots=" << numSlots << endmsg;
45 numSlots = 1000;
46 warning() << "Setting numSlots to " << numSlots << endmsg;
47 }
48 m_inEvtLoop.resize( numSlots, 0 );
49
50 if ( m_check ) {
51 m_inc = Service::service( "IncidentSvc", true );
52 if ( !m_inc ) {
53 error() << "Could not locate 'IncidentSvc'" << endmsg;
54 return StatusCode::FAILURE;
55 }
56 m_inc->addListener( this, IncidentType::BeginEvent );
57 m_inc->addListener( this, IncidentType::EndEvent );
58 }
59 if ( m_algorithms.get() && !m_algorithms->empty() ) {
60 warning() << "Non-empty stack of algorithms #" << m_algorithms->size() << endmsg;
61 }
62 return StatusCode::SUCCESS;
63 }
64
65 // implementation of start
66 // needs to be removed once we have a proper service
67 // for getting configuration information at initialization time
68 // S. Kama
69 StatusCode AlgContextSvc::start() {
70 auto sc = Service::start();
71 auto numSlots = Gaudi::Concurrency::ConcurrencyFlags::numConcurrentEvents();
72 numSlots = ( 1 > numSlots ) ? 1 : numSlots;
73 if ( numSlots > 1000 ) {
74 warning() << "Num Slots are greater than 1000. Is this correct? numSlots=" << numSlots << endmsg;
75 numSlots = 1000;
76 }
77 m_inEvtLoop.resize( numSlots, 0 );
78
79 return sc;
80 }
81
82 // ============================================================================
83 // standard finalization of the service @see IService
84 // ============================================================================
85 StatusCode AlgContextSvc::finalize() {
86 if ( m_algorithms.get() && !m_algorithms->empty() ) {
87 warning() << "Non-empty stack of algorithms #" << m_algorithms->size() << endmsg;
88 }
89 // Incident Service
90 if ( m_inc ) {
91 m_inc->removeListener( this );
92 m_inc.reset();
93 }
94 // finalize the base class
95 return Service::finalize();
96 }
97 // ============================================================================
98 // set the currently executing algorithm ("push_back") @see IAlgContextSvc
99 // ============================================================================
100 StatusCode AlgContextSvc::setCurrentAlg( IAlgorithm* a, const EventContext& context ) {
101 if ( !a ) {
102 warning() << "IAlgorithm* points to NULL" << endmsg;
103 return StatusCode::RECOVERABLE;
104 }
105 if ( !m_bypassInc ) {
106 if ( !m_inEvtLoop[context.valid() ? context.slot() : 0] ) return StatusCode::SUCCESS;
107 }
108 // check whether thread-local algorithm list already exists
109 // if not, create it
110 if ( !m_algorithms.get() ) { m_algorithms.reset( new IAlgContextSvc::Algorithms() ); }
111 if ( a->type() != "IncidentProcAlg" ) m_algorithms->push_back( a );
112
113 return StatusCode::SUCCESS;
114 }
115 // ============================================================================
116 // remove the algorithm ("pop_back") @see IAlgContextSvc
117 // ============================================================================
118 StatusCode AlgContextSvc::unSetCurrentAlg( IAlgorithm* a, const EventContext& context ) {
119 // check whether thread-local algorithm list already exists
120 // if not, create it
121 if ( !m_algorithms.get() ) { m_algorithms.reset( new IAlgContextSvc::Algorithms() ); }
122
123 if ( !a ) {
124 warning() << "IAlgorithm* points to NULL" << endmsg;
125 return StatusCode::RECOVERABLE;
126 }
127
128 if ( !m_bypassInc ) {
129 if ( !m_inEvtLoop[context.valid() ? context.slot() : 0] ) return StatusCode::SUCCESS;
130 }
131
132 if ( a->type() != "IncidentProcAlg" ) {
133 // if ( m_algorithms->empty() || m_algorithms->back() != a ){
134 // error() << "Algorithm stack is invalid" << endmsg ;
135 // return StatusCode::FAILURE ;
136 // }
137 if ( !m_algorithms->empty() ) {
138 if ( m_algorithms->back() == a ) { m_algorithms->pop_back(); }
139 }
140 }
141 return StatusCode::SUCCESS;
142 }
143 // ============================================================================
145 // ============================================================================
146 IAlgorithm* AlgContextSvc::currentAlg() const {
147 return ( m_algorithms.get() && !m_algorithms->empty() ) ? m_algorithms->back() : nullptr;
148 }
149 // ============================================================================
150 // handle incident @see IIncidentListener
151 // ============================================================================
152 void AlgContextSvc::handle( const Incident& inc ) {
153 // some false sharing is possible but it should be negligable
154 auto currSlot = inc.context().slot();
155 if ( currSlot == EventContext::INVALID_CONTEXT_ID ) { currSlot = 0; }
156 if ( inc.type() == "BeginEvent" ) {
157 m_inEvtLoop[currSlot] = 1;
158 } else if ( inc.type() == "EndEvent" ) {
159 m_inEvtLoop[currSlot] = 0;
160 }
161
162 // This check is invalidated with RTTI AlgContext object.
163 // Whole service needs to be rewritten. Commenting the error until then
164 // to prevent test failures.
165 // if ( m_algorithms.get() && !m_algorithms->empty() ) {
166 // //skip incident processing algorithm endevent incident
167 // if((m_algorithms->size()!=1) ||
168 // (m_algorithms->back()->type()!="IncidentProcAlg")){
169 // error() << "Non-empty stack of algorithms #"
170 // << m_algorithms->size() << endmsg ;
171 // }
172 // }
173 }
174 // ============================================================================
175 // ============================================================================
177 // ============================================================================
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:60
AlgContextSvc::m_inEvtLoop
std::vector< int > m_inEvtLoop
Definition: AlgContextSvc.h:73
EventContext::slot
ContextID_t slot() const
Definition: EventContext.h:41
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:164
IAlgorithm::type
virtual const std::string & type() const =0
The type of the algorithm.
MsgStream.h
CommonMessagingBase::warning
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Definition: CommonMessaging.h:101
Incident::type
const std::string & type() const
Access to the incident type.
Definition: Incident.h:38
Service::start
StatusCode start() override
Definition: Service.cpp:129
AlgContextSvc::m_bypassInc
Gaudi::Property< bool > m_bypassInc
Definition: AlgContextSvc.h:71
AlgContextSvc::handle
void handle(const Incident &) override
handle incident
Definition: AlgContextSvc.cpp:152
AlgContextSvc::start
StatusCode start() override
Definition: AlgContextSvc.cpp:69
StatusCode::RECOVERABLE
constexpr static const auto RECOVERABLE
Definition: StatusCode.h:87
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
AlgContextSvc::m_inc
SmartIF< IIncidentSvc > m_inc
pointer to Incident Service
Definition: AlgContextSvc.h:68
AlgContextSvc::setCurrentAlg
StatusCode setCurrentAlg(IAlgorithm *a, const EventContext &context) override
set the currently executing algorithm ("push_back")
Definition: AlgContextSvc.cpp:100
EventContext
This class represents an entry point to all the event specific data.
Definition: EventContext.h:24
EventContext::valid
bool valid() const
Definition: EventContext.h:44
Incident::context
EventContext context() const
Access to the EventContext of the source of the incident.
Definition: Incident.h:50
AlgContextSvc
Definition: AlgContextSvc.h:33
AlgContextSvc::m_algorithms
boost::thread_specific_ptr< IAlgContextSvc::Algorithms > m_algorithms
the stack of current algorithms
Definition: AlgContextSvc.h:66
std::vector::resize
T resize(T... args)
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
AlgContextSvc.h
IIncidentSvc.h
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
EventContext::INVALID_CONTEXT_ID
static constexpr ContextID_t INVALID_CONTEXT_ID
Definition: EventContext.h:29
IAlgorithm
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:28
std::vector< IAlgorithm * >
AlgContextSvc::unSetCurrentAlg
StatusCode unSetCurrentAlg(IAlgorithm *a, const EventContext &context) override
remove the algorithm ("pop_back")
Definition: AlgContextSvc.cpp:118
Incident
Base class for all Incidents (computing events).
Definition: Incident.h:17
ConcurrencyFlags.h
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
Service::service
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:83
AlgContextSvc::finalize
StatusCode finalize() override
standard finalization of the service
Definition: AlgContextSvc.cpp:85
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:86
IIncidentSvc::removeListener
virtual void removeListener(IIncidentListener *lis, const std::string &type="")=0
Remove listener.
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:130
AlgContextSvc::currentAlg
IAlgorithm * currentAlg() const override
accessor to current algorithm:
Definition: AlgContextSvc.cpp:146
ISvcLocator.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
Gaudi::Concurrency::ConcurrencyFlags::numConcurrentEvents
static GAUDI_API std::size_t numConcurrentEvents()
number of Concurrent Events (for MT)
Definition: ConcurrencyFlags.h:47