The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
AlgorithmManager Class Reference

The AlgorithmManager class is in charge of the creation of concrete instances of Algorithms. More...

#include </builds/gaudi/Gaudi/GaudiCoreSvc/src/ApplicationMgr/AlgorithmManager.h>

Inheritance diagram for AlgorithmManager:
Collaboration diagram for AlgorithmManager:

Classes

struct  AlgorithmItem
 

Public Types

typedef std::map< std::string, std::string, std::less<> > AlgTypeAliasesMap
 typedefs and classes
 
- Public Types inherited from extends< ComponentManager, IAlgManager >
using base_class
 Typedef to this class.
 
using extend_interfaces_base
 Typedef to the base of this class.
 
- Public Types inherited from CommonMessaging< implements< IComponentManager > >
using base_class
 
- Public Types inherited from extend_interfaces< Interfaces... >
using ext_iids
 take union of the ext_iids of all Interfaces...
 

Public Member Functions

 AlgorithmManager (IInterface *iface)
 default creator
 
StatusCode addAlgorithm (IAlgorithm *alg) override
 implementation of IAlgManager::addAlgorithm
 
StatusCode removeAlgorithm (IAlgorithm *alg) override
 implementation of IAlgManager::removeAlgorithm
 
StatusCode createAlgorithm (const std::string &algtype, const std::string &algname, IAlgorithm *&algorithm, bool managed=false, bool checkIfExists=true) override
 implementation of IAlgManager::createAlgorithm
 
bool existsAlgorithm (std::string_view name) const override
 implementation of IAlgManager::existsAlgorithm
 
std::vector< IAlgorithm * > getAlgorithms () const override
 implementation of IAlgManager::getAlgorithms
 
StatusCode initialize () override
 Initialization (from CONFIGURED to INITIALIZED).
 
StatusCode start () override
 Start (from INITIALIZED to RUNNING).
 
StatusCode stop () override
 Stop (from RUNNING to INITIALIZED).
 
StatusCode finalize () override
 Finalize (from INITIALIZED to CONFIGURED).
 
StatusCode reinitialize () override
 Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
 
StatusCode restart () override
 Initialization (from RUNNING to RUNNING, via INITIALIZED).
 
const std::string & name () const override
 Return the name of the manager (implementation of INamedInterface)
 
SmartIF< IAlgorithm > & algorithm (const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
 
AlgTypeAliasesMaptypeAliases ()
 
const AlgTypeAliasesMaptypeAliases () const
 
void outputLevelUpdate () override
 Function to call to update the outputLevel of the components (after a change in MessageSvc).
 
- Public Member Functions inherited from extends< ComponentManager, IAlgManager >
void const * i_cast (const InterfaceID &tid) const override
 Implementation of IInterface::i_cast.
 
StatusCode queryInterface (const InterfaceID &ti, void **pp) override
 Implementation of IInterface::queryInterface.
 
std::vector< std::string > getInterfaceNames () const override
 Implementation of IInterface::getInterfaceNames.
 
- Public Member Functions inherited from ComponentManager
 ComponentManager (IInterface *application, const InterfaceID &baseIID)
 Constructor.
 
const InterfaceIDcomponentBaseInterface () const override
 Basic interface id of the managed components.
 
StatusCode queryInterface (const InterfaceID &iid, void **pinterface) override
 Specialized queryInterface implementation.
 
void const * i_cast (const InterfaceID &iid) const override
 Specialized i_cast implementation.
 
SmartIF< ISvcLocator > & serviceLocator () const override
 
StatusCode configure () override
 Configuration (from OFFLINE to CONFIGURED).
 
StatusCode initialize () override
 Initialization (from CONFIGURED to INITIALIZED).
 
StatusCode start () override
 Start (from INITIALIZED to RUNNING).
 
StatusCode stop () override
 Stop (from RUNNING to INITIALIZED).
 
StatusCode finalize () override
 Finalize (from INITIALIZED to CONFIGURED).
 
StatusCode terminate () override
 Initialization (from CONFIGURED to OFFLINE).
 
StatusCode reinitialize () override
 Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
 
StatusCode restart () override
 Initialization (from RUNNING to RUNNING, via INITIALIZED).
 
Gaudi::StateMachine::State FSMState () const override
 Get the current state.
 
Gaudi::StateMachine::State targetFSMState () const override
 When we are in the middle of a transition, get the state where the transition is leading us.
 
- Public Member Functions inherited from CommonMessaging< implements< IComponentManager > >
MSG::Level msgLevel () const
 get the cached level (originally extracted from the embedded MsgStream)
 
bool msgLevel (MSG::Level lvl) const
 get the output level from the embedded MsgStream
 
- Public Member Functions inherited from CommonMessagingBase
virtual ~CommonMessagingBase ()=default
 Virtual destructor.
 
const SmartIF< IMessageSvc > & msgSvc () const
 The standard message service.
 
MsgStreammsgStream () const
 Return an uninitialized MsgStream.
 
MsgStreammsgStream (const MSG::Level level) const
 Predefined configurable message stream for the efficient printouts.
 
MsgStreamalways () const
 shortcut for the method msgStream(MSG::ALWAYS)
 
MsgStreamfatal () const
 shortcut for the method msgStream(MSG::FATAL)
 
MsgStreamerr () const
 shortcut for the method msgStream(MSG::ERROR)
 
MsgStreamerror () const
 shortcut for the method msgStream(MSG::ERROR)
 
MsgStreamwarning () const
 shortcut for the method msgStream(MSG::WARNING)
 
MsgStreaminfo () const
 shortcut for the method msgStream(MSG::INFO)
 
MsgStreamdebug () const
 shortcut for the method msgStream(MSG::DEBUG)
 
MsgStreamverbose () const
 shortcut for the method msgStream(MSG::VERBOSE)
 
MsgStreammsg () const
 shortcut for the method msgStream(MSG::INFO)
 

Private Attributes

std::vector< AlgorithmItemm_algs
 algorithms maintained by AlgorithmManager
 
std::unordered_multimap< std::string, SmartIF< IAlgorithm > > m_algsMap
 algorithms maintained by AlgorithmManager
 
AlgTypeAliasesMap m_algTypeAliases
 

Additional Inherited Members

- Protected Member Functions inherited from CommonMessaging< implements< IComponentManager > >
MSG::Level setUpMessaging () const
 Set up local caches.
 
MSG::Level resetMessaging ()
 Reinitialize internal states.
 
void updateMsgStreamOutputLevel (int level)
 Update the output level of the cached MsgStream.
 
- Protected Attributes inherited from ComponentManager
SmartIF< IInterfacem_application
 Pointer to the owner of the manager.
 
SmartIF< IStatefulm_stateful
 Pointer to the IStateful interface of the owner.
 
InterfaceID m_basicInterfaceId
 Basic interface id of the managed components.
 
SmartIF< ISvcLocatorm_svcLocator
 Service locator (needed to access the MessageSvc)
 
friend ApplicationMgr
 

Detailed Description

The AlgorithmManager class is in charge of the creation of concrete instances of Algorithms.

The ApplicationMgr delegates the creation and bookkeeping of algorithms to the algorithm factory. In order to be able to create algorithms from which it does not know the concrete type it requires that the algorithm has been declared in one of 3 possible ways: an abstract static creator function, a dynamic link library or an abstract factory reference.

Author
Pere Mato

Definition at line 39 of file AlgorithmManager.h.

Member Typedef Documentation

◆ AlgTypeAliasesMap

typedef std::map<std::string, std::string, std::less<> > AlgorithmManager::AlgTypeAliasesMap

typedefs and classes

Definition at line 49 of file AlgorithmManager.h.

Constructor & Destructor Documentation

◆ AlgorithmManager()

AlgorithmManager::AlgorithmManager ( IInterface * iface)

default creator

Definition at line 25 of file AlgorithmManager.cpp.

25 : base_class( application, IAlgorithm::interfaceID() ) {
26 addRef(); // Initial count set to 1
27}

Member Function Documentation

◆ addAlgorithm()

StatusCode AlgorithmManager::addAlgorithm ( IAlgorithm * alg)
override

implementation of IAlgManager::addAlgorithm

Definition at line 29 of file AlgorithmManager.cpp.

29 {
30 m_algs.push_back( alg );
31 m_algsMap.emplace( alg->name(), alg );
33}
std::vector< AlgorithmItem > m_algs
algorithms maintained by AlgorithmManager
std::unordered_multimap< std::string, SmartIF< IAlgorithm > > m_algsMap
algorithms maintained by AlgorithmManager
constexpr static const auto SUCCESS
Definition StatusCode.h:99

◆ algorithm()

SmartIF< IAlgorithm > & AlgorithmManager::algorithm ( const Gaudi::Utils::TypeNameString & typeName,
const bool createIf = true )
override

Definition at line 100 of file AlgorithmManager.cpp.

100 {
101 auto it = m_algsMap.find( typeName.name() );
102 if ( it != m_algsMap.end() ) { // found
103 return it->second;
104 }
105 if ( createIf ) {
106 IAlgorithm* alg;
107 if ( createAlgorithm( typeName.type(), typeName.name(), alg, true ).isSuccess() ) {
108 return algorithm( typeName, false );
109 }
110 }
111 return no_algorithm;
112}
SmartIF< IAlgorithm > & algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
StatusCode createAlgorithm(const std::string &algtype, const std::string &algname, IAlgorithm *&algorithm, bool managed=false, bool checkIfExists=true) override
implementation of IAlgManager::createAlgorithm
bool isSuccess() const
Definition StatusCode.h:314
std::string typeName(const std::type_info &typ)

◆ createAlgorithm()

StatusCode AlgorithmManager::createAlgorithm ( const std::string & algtype,
const std::string & algname,
IAlgorithm *& algorithm,
bool managed = false,
bool checkIfExists = true )
override

implementation of IAlgManager::createAlgorithm

Definition at line 48 of file AlgorithmManager.cpp.

49 {
50 // Check if the algorithm is already existing
51 if ( checkIfExists ) {
52 if ( existsAlgorithm( algname ) ) {
53 // return an error because an algorithm with that name already exists
55 }
56 }
57 std::string actualalgtype( algtype );
58 // a '\' in front of the type name prevents alias replacement
59 if ( ( actualalgtype.size() > 8 ) && ( actualalgtype.compare( 0, 8, "unalias:" ) == 0 ) ) {
60 actualalgtype = actualalgtype.substr( 8 );
61 } else {
62 auto typeAlias = m_algTypeAliases.find( algtype );
63 if ( typeAlias != m_algTypeAliases.end() ) { actualalgtype = typeAlias->second; }
64 }
65 algorithm = Gaudi::Algorithm::Factory::create( actualalgtype, algname, serviceLocator().get() ).release();
66 if ( !algorithm ) {
67 this->error() << "Algorithm of type " << actualalgtype << " is unknown (No factory available)." << endmsg;
68 errno =
69 0xAFFEDEAD; // code used by Gaudi for library load errors: forces getLastErrorString do use dlerror (on Linux)
70 std::string err = System::getLastErrorString();
71 if ( !err.empty() ) this->error() << err << endmsg;
72 this->error() << "More information may be available by setting the global jobOpt \"PluginDebugLevel\" to 1"
73 << endmsg;
75 }
76 // Check the compatibility of the version of the interface obtained
77 if ( !isValidInterface( algorithm ) ) {
78 fatal() << "Incompatible interface IAlgorithm version for " << actualalgtype << endmsg;
80 }
81 m_algs.emplace_back( algorithm, managed );
82 m_algsMap.emplace( algorithm->name(), algorithm );
83 // let the algorithm know its type
84 algorithm->setType( std::move( actualalgtype ) );
85 // this is needed to keep the reference count correct, since isValidInterface(algorithm)
86 // implies an increment of the counter by 1
87 algorithm->release();
88 StatusCode rc;
89 if ( managed ) {
90 // Bring the created algorithm to the target state of the ApplicationMgr
92 rc = algorithm->sysInitialize();
93 if ( rc.isSuccess() && targetFSMState() >= Gaudi::StateMachine::RUNNING ) { rc = algorithm->sysStart(); }
94 }
95 if ( !rc.isSuccess() ) { this->error() << "Failed to initialize algorithm: [" << algname << "]" << endmsg; }
96 }
97 return rc;
98}
bool isValidInterface(IFace *i)
Templated function that throws an exception if the version if the interface implemented by the object...
Definition IInterface.h:354
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
AlgTypeAliasesMap m_algTypeAliases
bool existsAlgorithm(std::string_view name) const override
implementation of IAlgManager::existsAlgorithm
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MsgStream & err() const
shortcut for the method msgStream(MSG::ERROR)
Gaudi::StateMachine::State targetFSMState() const override
When we are in the middle of a transition, get the state where the transition is leading us.
SmartIF< ISvcLocator > & serviceLocator() const override
constexpr static const auto FAILURE
Definition StatusCode.h:100
get
decorate the vector of properties
Definition decorators.py:94
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
Definition System.cpp:234

◆ existsAlgorithm()

bool AlgorithmManager::existsAlgorithm ( std::string_view name) const
override

implementation of IAlgManager::existsAlgorithm

Definition at line 114 of file AlgorithmManager.cpp.

114 {
115 return m_algsMap.find( std::string( name ) ) != m_algsMap.end();
116}
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)

◆ finalize()

StatusCode AlgorithmManager::finalize ( )
override

Finalize (from INITIALIZED to CONFIGURED).

Definition at line 156 of file AlgorithmManager.cpp.

156 {
157 StatusCode rc;
158 auto it = m_algs.begin();
159 while ( it != m_algs.end() ) { // finalize and remove from the list the managed algorithms
160 if ( it->managed ) {
161 auto range = m_algsMap.equal_range( it->algorithm->name() );
162 auto itm = std::find_if( range.first, range.second, [&]( auto const& p ) { return p.second == it->algorithm; } );
163 if ( itm == range.second ) { return StatusCode::FAILURE; }
164
165 rc = it->algorithm->sysFinalize();
166 if ( rc.isFailure() ) return rc;
167
168 it = m_algs.erase( it );
169 m_algsMap.erase( itm );
170 } else {
171 ++it;
172 }
173 }
174 return rc;
175}
decltype(auto) range(Args &&... args)
Zips multiple containers together to form a single range.
Definition details.h:86

◆ getAlgorithms()

std::vector< IAlgorithm * > AlgorithmManager::getAlgorithms ( ) const
override

implementation of IAlgManager::getAlgorithms

Definition at line 118 of file AlgorithmManager.cpp.

118 {
119 std::vector<IAlgorithm*> listOfPtrs;
120 listOfPtrs.reserve( m_algs.size() );
121 std::transform( std::begin( m_algs ), std::end( m_algs ), std::back_inserter( listOfPtrs ),
122 []( const AlgorithmItem& alg ) { return alg.algorithm; } );
123 return listOfPtrs;
124}

◆ initialize()

StatusCode AlgorithmManager::initialize ( )
override

Initialization (from CONFIGURED to INITIALIZED).

Definition at line 126 of file AlgorithmManager.cpp.

126 {
127 StatusCode rc;
128 for ( auto& it : m_algs ) {
129 if ( !it.managed || it.algorithm->FSMState() >= Gaudi::StateMachine::INITIALIZED ) continue;
130 rc = it.algorithm->sysInitialize();
131 if ( rc.isFailure() ) return rc;
132 }
133 return rc;
134}

◆ name()

const std::string & AlgorithmManager::name ( ) const
inlineoverride

Return the name of the manager (implementation of INamedInterface)

Definition at line 82 of file AlgorithmManager.h.

82 {
83 static const std::string _name = "AlgorithmManager";
84 return _name;
85 }

◆ outputLevelUpdate()

void AlgorithmManager::outputLevelUpdate ( )
override

Function to call to update the outputLevel of the components (after a change in MessageSvc).

Definition at line 212 of file AlgorithmManager.cpp.

212 {
214 for ( auto& algItem : m_algs ) {
215 const auto alg = dynamic_cast<Gaudi::Algorithm*>( algItem.algorithm );
216 if ( alg ) alg->resetMessaging();
217 }
218}

◆ reinitialize()

StatusCode AlgorithmManager::reinitialize ( )
override

Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).

Definition at line 177 of file AlgorithmManager.cpp.

177 {
178 StatusCode rc;
179 for ( auto& it : m_algs ) {
180 if ( !it.managed ) continue;
181 rc = it.algorithm->sysReinitialize();
182 if ( rc.isFailure() ) {
183 this->error() << "Unable to re-initialize algorithm: " << it.algorithm->name() << endmsg;
184 return rc;
185 }
186 }
187 return rc;
188}

◆ removeAlgorithm()

StatusCode AlgorithmManager::removeAlgorithm ( IAlgorithm * alg)
override

implementation of IAlgManager::removeAlgorithm

Definition at line 35 of file AlgorithmManager.cpp.

35 {
36 auto it = std::find( m_algs.begin(), m_algs.end(), alg );
37 if ( it == m_algs.end() ) { return StatusCode::FAILURE; }
38
39 auto range = m_algsMap.equal_range( alg->name() );
40 auto itm = std::find_if( range.first, range.second, [&]( auto const& p ) { return p.second == alg; } );
41 if ( itm == range.second ) { return StatusCode::FAILURE; }
42
43 m_algs.erase( it );
44 m_algsMap.erase( itm );
46}

◆ restart()

StatusCode AlgorithmManager::restart ( )
override

Initialization (from RUNNING to RUNNING, via INITIALIZED).

Definition at line 190 of file AlgorithmManager.cpp.

190 {
191 SmartIF<IAlgExecStateSvc> m_aess;
192 m_aess = serviceLocator()->service( "AlgExecStateSvc" );
193 if ( !m_aess.isValid() ) {
194 fatal() << "Error retrieving AlgExecStateSvc" << endmsg;
195 return StatusCode::FAILURE;
196 }
197
198 StatusCode rc;
199
200 for ( auto& it : m_algs ) {
201 if ( !it.managed ) continue;
202 rc = it.algorithm->sysRestart();
203 m_aess->resetErrorCount( it.algorithm );
204 if ( rc.isFailure() ) {
205 this->error() << "Unable to re-initialize algorithm: " << it.algorithm->name() << endmsg;
206 return rc;
207 }
208 }
209 return rc;
210}
virtual SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
bool isValid() const
Allow for check if smart pointer is valid.
Definition SmartIF.h:69

◆ start()

StatusCode AlgorithmManager::start ( )
override

Start (from INITIALIZED to RUNNING).

Definition at line 136 of file AlgorithmManager.cpp.

136 {
137 StatusCode rc;
138 for ( auto& it : m_algs ) {
139 if ( !it.managed || it.algorithm->FSMState() >= Gaudi::StateMachine::RUNNING ) continue;
140 rc = it.algorithm->sysStart();
141 if ( rc.isFailure() ) return rc;
142 }
143 return rc;
144}

◆ stop()

StatusCode AlgorithmManager::stop ( )
override

Stop (from RUNNING to INITIALIZED).

Definition at line 146 of file AlgorithmManager.cpp.

146 {
147 StatusCode rc;
148 for ( auto& it : m_algs ) {
149 if ( !it.managed ) continue;
150 rc = it.algorithm->sysStop();
151 if ( rc.isFailure() ) return rc;
152 }
153 return rc;
154}

◆ typeAliases() [1/2]

AlgTypeAliasesMap & AlgorithmManager::typeAliases ( )
inline

Definition at line 89 of file AlgorithmManager.h.

89{ return m_algTypeAliases; }

◆ typeAliases() [2/2]

const AlgTypeAliasesMap & AlgorithmManager::typeAliases ( ) const
inline

Definition at line 90 of file AlgorithmManager.h.

90{ return m_algTypeAliases; }

Member Data Documentation

◆ m_algs

std::vector<AlgorithmItem> AlgorithmManager::m_algs
private

algorithms maintained by AlgorithmManager

Definition at line 96 of file AlgorithmManager.h.

◆ m_algsMap

std::unordered_multimap<std::string, SmartIF<IAlgorithm> > AlgorithmManager::m_algsMap
private

algorithms maintained by AlgorithmManager

Definition at line 97 of file AlgorithmManager.h.

◆ m_algTypeAliases

AlgTypeAliasesMap AlgorithmManager::m_algTypeAliases
private

Definition at line 100 of file AlgorithmManager.h.


The documentation for this class was generated from the following files: