The Gaudi Framework  master (e68eea06)
Loading...
Searching...
No Matches
AlgorithmManager.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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#include "AlgorithmManager.h"
12#include <Gaudi/Algorithm.h>
17#include <GaudiKernel/System.h>
19
20#include <errno.h>
21
23static SmartIF<IAlgorithm> no_algorithm;
24
26 addRef(); // Initial count set to 1
27}
28
30 m_algs.push_back( alg );
31 m_algsMap.emplace( alg->name(), alg );
33}
34
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}
47
48StatusCode AlgorithmManager::createAlgorithm( const std::string& algtype, const std::string& algname,
49 IAlgorithm*& algorithm, bool managed, bool checkIfExists ) {
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 m_algs.emplace_back( algorithm, managed );
77 m_algsMap.emplace( algorithm->name(), algorithm );
78 // let the algorithm know its type
79 algorithm->setType( std::move( actualalgtype ) );
80 StatusCode rc;
81 if ( managed ) {
82 // Bring the created algorithm to the target state of the ApplicationMgr
84 rc = algorithm->sysInitialize();
85 if ( rc.isSuccess() && targetFSMState() >= Gaudi::StateMachine::RUNNING ) { rc = algorithm->sysStart(); }
86 }
87 if ( !rc.isSuccess() ) { this->error() << "Failed to initialize algorithm: [" << algname << "]" << endmsg; }
88 }
89 return rc;
90}
91
93 auto it = m_algsMap.find( typeName.name() );
94 if ( it != m_algsMap.end() ) { // found
95 return it->second;
96 }
97 if ( createIf ) {
98 IAlgorithm* alg;
99 if ( createAlgorithm( typeName.type(), typeName.name(), alg, true ).isSuccess() ) {
100 return algorithm( typeName, false );
101 }
102 }
103 return no_algorithm;
104}
105
106bool AlgorithmManager::existsAlgorithm( std::string_view name ) const {
107 return m_algsMap.find( std::string( name ) ) != m_algsMap.end();
108}
109
110std::vector<IAlgorithm*> AlgorithmManager::getAlgorithms() const {
111 std::vector<IAlgorithm*> listOfPtrs;
112 listOfPtrs.reserve( m_algs.size() );
113 std::transform( std::begin( m_algs ), std::end( m_algs ), std::back_inserter( listOfPtrs ),
114 []( const AlgorithmItem& alg ) { return alg.algorithm; } );
115 return listOfPtrs;
116}
117
119 StatusCode rc;
120 for ( auto& it : m_algs ) {
121 if ( !it.managed || it.algorithm->FSMState() >= Gaudi::StateMachine::INITIALIZED ) continue;
122 rc = it.algorithm->sysInitialize();
123 if ( rc.isFailure() ) return rc;
124 }
125 return rc;
126}
127
129 StatusCode rc;
130 for ( auto& it : m_algs ) {
131 if ( !it.managed || it.algorithm->FSMState() >= Gaudi::StateMachine::RUNNING ) continue;
132 rc = it.algorithm->sysStart();
133 if ( rc.isFailure() ) return rc;
134 }
135 return rc;
136}
137
139 StatusCode rc;
140 for ( auto& it : m_algs ) {
141 if ( !it.managed ) continue;
142 rc = it.algorithm->sysStop();
143 if ( rc.isFailure() ) return rc;
144 }
145 return rc;
146}
147
149 StatusCode rc;
150 auto it = m_algs.begin();
151 while ( it != m_algs.end() ) { // finalize and remove from the list the managed algorithms
152 if ( it->managed ) {
153 auto range = m_algsMap.equal_range( it->algorithm->name() );
154 auto itm = std::find_if( range.first, range.second, [&]( auto const& p ) { return p.second == it->algorithm; } );
155 if ( itm == range.second ) { return StatusCode::FAILURE; }
156
157 rc = it->algorithm->sysFinalize();
158 if ( rc.isFailure() ) return rc;
159
160 it = m_algs.erase( it );
161 m_algsMap.erase( itm );
162 } else {
163 ++it;
164 }
165 }
166 return rc;
167}
168
170 StatusCode rc;
171 for ( auto& it : m_algs ) {
172 if ( !it.managed ) continue;
173 rc = it.algorithm->sysReinitialize();
174 if ( rc.isFailure() ) {
175 this->error() << "Unable to re-initialize algorithm: " << it.algorithm->name() << endmsg;
176 return rc;
177 }
178 }
179 return rc;
180}
181
184 m_aess = serviceLocator()->service( "AlgExecStateSvc" );
185 if ( !m_aess.isValid() ) {
186 fatal() << "Error retrieving AlgExecStateSvc" << endmsg;
187 return StatusCode::FAILURE;
188 }
189
190 StatusCode rc;
191
192 for ( auto& it : m_algs ) {
193 if ( !it.managed ) continue;
194 rc = it.algorithm->sysRestart();
195 m_aess->resetErrorCount( it.algorithm );
196 if ( rc.isFailure() ) {
197 this->error() << "Unable to re-initialize algorithm: " << it.algorithm->name() << endmsg;
198 return rc;
199 }
200 }
201 return rc;
202}
203
206 for ( auto& algItem : m_algs ) {
207 const auto alg = dynamic_cast<Gaudi::Algorithm*>( algItem.algorithm );
208 if ( alg ) alg->resetMessaging();
209 }
210}
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
SmartIF< IAlgorithm > & algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
AlgorithmManager(IInterface *iface)
default creator
std::vector< IAlgorithm * > getAlgorithms() const override
implementation of IAlgManager::getAlgorithms
AlgTypeAliasesMap m_algTypeAliases
StatusCode start() override
Start (from INITIALIZED to RUNNING).
StatusCode finalize() override
Finalize (from INITIALIZED to CONFIGURED).
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
StatusCode addAlgorithm(IAlgorithm *alg) override
implementation of IAlgManager::addAlgorithm
bool existsAlgorithm(std::string_view name) const override
implementation of IAlgManager::existsAlgorithm
std::vector< AlgorithmItem > m_algs
algorithms maintained by AlgorithmManager
StatusCode removeAlgorithm(IAlgorithm *alg) override
implementation of IAlgManager::removeAlgorithm
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
StatusCode createAlgorithm(const std::string &algtype, const std::string &algname, IAlgorithm *&algorithm, bool managed=false, bool checkIfExists=true) override
implementation of IAlgManager::createAlgorithm
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
std::unordered_multimap< std::string, SmartIF< IAlgorithm > > m_algsMap
algorithms maintained by AlgorithmManager
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
void outputLevelUpdate() override
Function to call to update the outputLevel of the components (after a change in MessageSvc).
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
Base class from which all concrete algorithm classes should be derived.
Definition Algorithm.h:87
Helper class to parse a string of format "type/name".
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition IAlgorithm.h:36
Definition of the basic interface.
Definition IInterface.h:225
virtual SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
bool isValid() const
Allow for check if smart pointer is valid.
Definition SmartIF.h:69
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
bool isSuccess() const
Definition StatusCode.h:314
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
Definition System.cpp:234
static const InterfaceID & interfaceID()