| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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