Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v38r1p1 (ae26267b)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AlgorithmManager.cpp
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 // Include files
12 #include "AlgorithmManager.h"
14 #include "GaudiKernel/IAlgorithm.h"
16 #include "GaudiKernel/MsgStream.h"
17 #include "GaudiKernel/System.h"
19 #include <Gaudi/Algorithm.h>
20 #include <iostream>
21 #ifndef _WIN32
22 # include <errno.h>
23 #endif
24 
26 static SmartIF<IAlgorithm> no_algorithm;
27 
28 // constructor
30  addRef(); // Initial count set to 1
31 }
32 
33 // addAlgorithm
35  m_algs.push_back( alg );
36  m_algsMap.emplace( alg->name(), alg );
37  return StatusCode::SUCCESS;
38 }
39 
40 // removeAlgorithm
42  auto it = std::find( m_algs.begin(), m_algs.end(), alg );
43  if ( it == m_algs.end() ) { return StatusCode::FAILURE; }
44 
45  auto range = m_algsMap.equal_range( alg->name() );
46  auto itm = std::find_if( range.first, range.second, [&]( auto const& p ) { return p.second == alg; } );
47  if ( itm == range.second ) { return StatusCode::FAILURE; }
48 
49  m_algs.erase( it );
50  m_algsMap.erase( itm );
51  return StatusCode::SUCCESS;
52 }
53 
54 // createService
56  bool managed, bool checkIfExists ) {
57  // Check is the algorithm is already existing
58  if ( checkIfExists ) {
59  if ( existsAlgorithm( algname ) ) {
60  // return an error because an algorithm with that name already exists
61  return StatusCode::FAILURE;
62  }
63  }
64  std::string actualalgtype( algtype );
65  // a '\' in front of the type name prevents alias replacement
66  if ( ( actualalgtype.size() > 8 ) && ( actualalgtype.compare( 0, 8, "unalias:" ) == 0 ) ) {
67  actualalgtype = actualalgtype.substr( 8 );
68  } else {
69  auto typeAlias = m_algTypeAliases.find( algtype );
70  if ( typeAlias != m_algTypeAliases.end() ) { actualalgtype = typeAlias->second; }
71  }
72  algorithm = Gaudi::Algorithm::Factory::create( actualalgtype, algname, serviceLocator().get() ).release();
73  if ( !algorithm ) {
74  this->error() << "Algorithm of type " << actualalgtype << " is unknown (No factory available)." << endmsg;
75 #ifndef _WIN32
76  errno =
77  0xAFFEDEAD; // code used by Gaudi for library load errors: forces getLastErrorString do use dlerror (on Linux)
78 #endif
80  if ( !err.empty() ) this->error() << err << endmsg;
81  this->error() << "More information may be available by setting the global jobOpt \"PluginDebugLevel\" to 1"
82  << endmsg;
83  return StatusCode::FAILURE;
84  }
85  // Check the compatibility of the version of the interface obtained
86  if ( !isValidInterface( algorithm ) ) {
87  fatal() << "Incompatible interface IAlgorithm version for " << actualalgtype << endmsg;
88  return StatusCode::FAILURE;
89  }
90  m_algs.emplace_back( algorithm, managed );
92  // let the algorithm know its type
93  algorithm->setType( std::move( actualalgtype ) );
94  // this is needed to keep the reference count correct, since isValidInterface(algorithm)
95  // implies an increment of the counter by 1
96  algorithm->release();
97  StatusCode rc;
98  if ( managed ) {
99  // Bring the created algorithm to the target state of the ApplicationMgr
101  rc = algorithm->sysInitialize();
102  if ( rc.isSuccess() && targetFSMState() >= Gaudi::StateMachine::RUNNING ) { rc = algorithm->sysStart(); }
103  }
104  if ( !rc.isSuccess() ) { this->error() << "Failed to initialize algorithm: [" << algname << "]" << endmsg; }
105  }
106  return rc;
107 }
108 
110  auto it = m_algsMap.find( typeName.name() );
111  if ( it != m_algsMap.end() ) { // found
112  return it->second;
113  }
114  if ( createIf ) {
115  IAlgorithm* alg;
116  if ( createAlgorithm( typeName.type(), typeName.name(), alg, true ).isSuccess() ) {
117  return algorithm( typeName, false );
118  }
119  }
120  return no_algorithm;
121 }
122 
123 // existsAlgorithm
124 bool AlgorithmManager::existsAlgorithm( std::string_view name ) const {
125  return m_algsMap.find( name ) != m_algsMap.end();
126 }
127 
128 // Return the list of Algorithms
131  m_listOfPtrs.reserve( m_algs.size() );
133  []( const AlgorithmItem& alg ) { return alg.algorithm; } );
134  return m_listOfPtrs;
135 }
136 
138  StatusCode rc;
139  for ( auto& it : m_algs ) {
140  if ( !it.managed || it.algorithm->FSMState() >= Gaudi::StateMachine::INITIALIZED ) continue;
141  rc = it.algorithm->sysInitialize();
142  if ( rc.isFailure() ) return rc;
143  }
144  return rc;
145 }
146 
148  StatusCode rc;
149  for ( auto& it : m_algs ) {
150  if ( !it.managed || it.algorithm->FSMState() >= Gaudi::StateMachine::RUNNING ) continue;
151  rc = it.algorithm->sysStart();
152  if ( rc.isFailure() ) return rc;
153  }
154  return rc;
155 }
156 
158  StatusCode rc;
159  for ( auto& it : m_algs ) {
160  if ( !it.managed ) continue;
161  rc = it.algorithm->sysStop();
162  if ( rc.isFailure() ) return rc;
163  }
164  return rc;
165 }
166 
168  StatusCode rc;
169  auto it = m_algs.begin();
170  while ( it != m_algs.end() ) { // finalize and remove from the list the managed algorithms
171  if ( it->managed ) {
172  auto range = m_algsMap.equal_range( it->algorithm->name() );
173  auto itm = std::find_if( range.first, range.second, [&]( auto const& p ) { return p.second == it->algorithm; } );
174  if ( itm == range.second ) { return StatusCode::FAILURE; }
175 
176  rc = it->algorithm->sysFinalize();
177  if ( rc.isFailure() ) return rc;
178 
179  it = m_algs.erase( it );
180  m_algsMap.erase( itm );
181  } else {
182  ++it;
183  }
184  }
185  return rc;
186 }
187 
189  StatusCode rc;
190  for ( auto& it : m_algs ) {
191  if ( !it.managed ) continue;
192  rc = it.algorithm->sysReinitialize();
193  if ( rc.isFailure() ) {
194  this->error() << "Unable to re-initialize algorithm: " << it.algorithm->name() << endmsg;
195  return rc;
196  }
197  }
198  return rc;
199 }
200 
203  m_aess = serviceLocator()->service( "AlgExecStateSvc" );
204  if ( !m_aess.isValid() ) {
205  fatal() << "Error retrieving AlgExecStateSvc" << endmsg;
206  return StatusCode::FAILURE;
207  }
208 
209  StatusCode rc;
210 
211  for ( auto& it : m_algs ) {
212  if ( !it.managed ) continue;
213  rc = it.algorithm->sysRestart();
214  m_aess->resetErrorCount( it.algorithm );
215  if ( rc.isFailure() ) {
216  this->error() << "Unable to re-initialize algorithm: " << it.algorithm->name() << endmsg;
217  return rc;
218  }
219  }
220  return rc;
221 }
222 
224  resetMessaging();
225  for ( auto& algItem : m_algs ) {
226  const auto alg = dynamic_cast<Gaudi::Algorithm*>( algItem.algorithm );
227  if ( alg ) alg->resetMessaging();
228  }
229 }
ComponentManager::targetFSMState
Gaudi::StateMachine::State targetFSMState() const override
When we are in the middle of a transition, get the state where the transition is leading us.
Definition: ComponentManager.h:73
AlgorithmManager::m_algTypeAliases
AlgTypeAliasesMap m_algTypeAliases
Definition: AlgorithmManager.h:106
std::string
STL class.
AlgorithmManager::existsAlgorithm
bool existsAlgorithm(std::string_view name) const override
implementation of IAlgManager::existsAlgorithm
Definition: AlgorithmManager.cpp:124
std::move
T move(T... args)
AlgorithmManager::restart
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
Definition: AlgorithmManager.cpp:201
CommonMessaging< implements< IComponentManager > >::resetMessaging
MSG::Level resetMessaging()
Reinitialize internal states.
Definition: CommonMessaging.h:179
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
System.h
std::vector::reserve
T reserve(T... args)
std::vector< IAlgorithm * >
std::find
T find(T... args)
std::string::size
T size(T... args)
std::back_inserter
T back_inserter(T... args)
AlgorithmManager::outputLevelUpdate
void outputLevelUpdate() override
Function to call to update the outputLevel of the components (after a change in MessageSvc).
Definition: AlgorithmManager.cpp:223
GaudiPartProp.decorators.get
get
decorate the vector of properties
Definition: decorators.py:282
std::unordered_multimap::emplace
T emplace(T... args)
AlgorithmManager.h
std::vector::clear
T clear(T... args)
AlgorithmManager::finalize
StatusCode finalize() override
Finalize (from INITIALIZED to CONFIGURED).
Definition: AlgorithmManager.cpp:167
ManySmallAlgs.alg
alg
Definition: ManySmallAlgs.py:80
AlgorithmManager::m_listOfPtrs
std::vector< IAlgorithm * > m_listOfPtrs
List of pointers to the know services used to implement getAlgorithms()
Definition: AlgorithmManager.h:104
SmartIF::isValid
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:72
Gaudi::Utils::TypeNameString
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:20
StatusCode
Definition: StatusCode.h:65
IAlgorithm
Definition: IAlgorithm.h:38
AlgorithmManager::createAlgorithm
StatusCode createAlgorithm(std::string algtype, std::string algname, IAlgorithm *&algorithm, bool managed=false, bool checkIfExists=true) override
implementation of IAlgManager::createAlgorithm
Definition: AlgorithmManager.cpp:55
CommonMessaging
Definition: CommonMessaging.h:66
Gaudi::Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:90
AlgorithmManager::initialize
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).
Definition: AlgorithmManager.cpp:137
std::string::compare
T compare(T... args)
AlgorithmManager::removeAlgorithm
StatusCode removeAlgorithm(IAlgorithm *alg) override
implementation of IAlgManager::removeAlgorithm
Definition: AlgorithmManager.cpp:41
std::unordered_multimap::erase
T erase(T... args)
Algorithm.h
SmartIF< IAlgorithm >
AlgorithmManager::start
StatusCode start() override
Start (from INITIALIZED to RUNNING).
Definition: AlgorithmManager.cpp:147
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
ComponentManager::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Definition: ComponentManager.h:39
std::transform
T transform(T... args)
AlgorithmManager::m_algs
std::vector< AlgorithmItem > m_algs
algorithms maintained by AlgorithmManager
Definition: AlgorithmManager.h:99
AlgorithmManager::name
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
Definition: AlgorithmManager.h:85
Gaudi::StateMachine::RUNNING
@ RUNNING
Definition: StateMachine.h:26
TypeNameString.h
AlgorithmManager::algorithm
SmartIF< IAlgorithm > & algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Definition: AlgorithmManager.cpp:109
std::string::substr
T substr(T... args)
AlgorithmManager::AlgorithmItem
Definition: AlgorithmManager.h:44
AlgorithmManager::addAlgorithm
StatusCode addAlgorithm(IAlgorithm *alg) override
implementation of IAlgManager::addAlgorithm
Definition: AlgorithmManager.cpp:34
std::unordered_multimap::equal_range
T equal_range(T... args)
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:76
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
AlgorithmManager::m_algsMap
std::unordered_multimap< std::string_view, SmartIF< IAlgorithm > > m_algsMap
algorithms maintained by AlgorithmManager
Definition: AlgorithmManager.h:100
GaudiDict::typeName
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:31
std::begin
T begin(T... args)
Gaudi::StateMachine::INITIALIZED
@ INITIALIZED
Definition: StateMachine.h:25
IInterface
Definition: IInterface.h:237
isValidInterface
bool isValidInterface(IFace *i)
Templated function that throws an exception if the version if the interface implemented by the object...
Definition: IInterface.h:334
AlgorithmManager::AlgorithmManager
AlgorithmManager(IInterface *iface)
default creator
Definition: AlgorithmManager.cpp:29
std::string::empty
T empty(T... args)
std::map::end
T end(T... args)
IAlgorithm.h
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ISvcLocator.h
IAlgExecStateSvc.h
System::getLastErrorString
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
Definition: System.cpp:277
AlgorithmManager::reinitialize
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
Definition: AlgorithmManager.cpp:188
gaudirun.application
application
Definition: gaudirun.py:323
IOTest.rc
rc
Definition: IOTest.py:112
AlgorithmManager::getAlgorithms
const std::vector< IAlgorithm * > & getAlgorithms() const override
implementation of IAlgManager::getAlgorithms
Definition: AlgorithmManager.cpp:129
AlgorithmManager::stop
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
Definition: AlgorithmManager.cpp:157
MsgStream.h
Gaudi::Functional::details::zip::range
decltype(auto) range(Args &&... args)
Zips multiple containers together to form a single range.
Definition: details.h:98