The Gaudi Framework  v40r0 (475e45c1)
AlgorithmManager Class Reference

#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 More...
 
- Public Types inherited from extends< ComponentManager, IAlgManager >
using base_class = extends
 Typedef to this class. More...
 
using extend_interfaces_base = extend_interfaces< Interfaces... >
 Typedef to the base of this class. More...
 
- Public Types inherited from CommonMessaging< implements< IComponentManager > >
using base_class = CommonMessaging
 
- Public Types inherited from extend_interfaces< Interfaces... >
using ext_iids = typename Gaudi::interface_list_cat< typename Interfaces::ext_iids... >::type
 take union of the ext_iids of all Interfaces... More...
 

Public Member Functions

 AlgorithmManager (IInterface *iface)
 default creator More...
 
StatusCode addAlgorithm (IAlgorithm *alg) override
 implementation of IAlgManager::addAlgorithm More...
 
StatusCode removeAlgorithm (IAlgorithm *alg) override
 implementation of IAlgManager::removeAlgorithm More...
 
StatusCode createAlgorithm (const std::string &algtype, const std::string &algname, IAlgorithm *&algorithm, bool managed=false, bool checkIfExists=true) override
 implementation of IAlgManager::createAlgorithm More...
 
bool existsAlgorithm (std::string_view name) const override
 implementation of IAlgManager::existsAlgorithm More...
 
std::vector< IAlgorithm * > getAlgorithms () const override
 implementation of IAlgManager::getAlgorithms More...
 
StatusCode initialize () override
 Initialization (from CONFIGURED to INITIALIZED). More...
 
StatusCode start () override
 Start (from INITIALIZED to RUNNING). More...
 
StatusCode stop () override
 Stop (from RUNNING to INITIALIZED). More...
 
StatusCode finalize () override
 Finalize (from INITIALIZED to CONFIGURED). More...
 
StatusCode reinitialize () override
 Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED). More...
 
StatusCode restart () override
 Initialization (from RUNNING to RUNNING, via INITIALIZED). More...
 
const std::string & name () const override
 Return the name of the manager (implementation of INamedInterface) More...
 
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). More...
 
- Public Member Functions inherited from extends< ComponentManager, IAlgManager >
void const * i_cast (const InterfaceID &tid) const override
 Implementation of IInterface::i_cast. More...
 
StatusCode queryInterface (const InterfaceID &ti, void **pp) override
 Implementation of IInterface::queryInterface. More...
 
std::vector< std::string > getInterfaceNames () const override
 Implementation of IInterface::getInterfaceNames. More...
 
- Public Member Functions inherited from ComponentManager
 ComponentManager (IInterface *application, const InterfaceID &baseIID)
 Constructor. More...
 
const InterfaceIDcomponentBaseInterface () const override
 Basic interface id of the managed components. More...
 
StatusCode queryInterface (const InterfaceID &iid, void **pinterface) override
 Specialized queryInterface implementation. More...
 
void const * i_cast (const InterfaceID &iid) const override
 Specialized i_cast implementation. More...
 
SmartIF< ISvcLocator > & serviceLocator () const override
 
StatusCode configure () override
 Configuration (from OFFLINE to CONFIGURED). More...
 
StatusCode initialize () override
 Initialization (from CONFIGURED to INITIALIZED). More...
 
StatusCode start () override
 Start (from INITIALIZED to RUNNING). More...
 
StatusCode stop () override
 Stop (from RUNNING to INITIALIZED). More...
 
StatusCode finalize () override
 Finalize (from INITIALIZED to CONFIGURED). More...
 
StatusCode terminate () override
 Initialization (from CONFIGURED to OFFLINE). More...
 
StatusCode reinitialize () override
 Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED). More...
 
StatusCode restart () override
 Initialization (from RUNNING to RUNNING, via INITIALIZED). More...
 
Gaudi::StateMachine::State FSMState () const override
 Get the current state. More...
 
Gaudi::StateMachine::State targetFSMState () const override
 When we are in the middle of a transition, get the state where the transition is leading us. More...
 
- Public Member Functions inherited from CommonMessaging< implements< IComponentManager > >
MSG::Level msgLevel () const
 get the cached level (originally extracted from the embedded MsgStream) More...
 
bool msgLevel (MSG::Level lvl) const
 get the output level from the embedded MsgStream More...
 

Private Attributes

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

Additional Inherited Members

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

27  addRef(); // Initial count set to 1
28 }

Member Function Documentation

◆ addAlgorithm()

StatusCode AlgorithmManager::addAlgorithm ( IAlgorithm alg)
override

implementation of IAlgManager::addAlgorithm

Definition at line 30 of file AlgorithmManager.cpp.

30  {
31  m_algs.push_back( alg );
32  m_algsMap.emplace( alg->name(), alg );
33  return StatusCode::SUCCESS;
34 }

◆ algorithm()

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

Definition at line 103 of file AlgorithmManager.cpp.

103  {
104  auto it = m_algsMap.find( typeName.name() );
105  if ( it != m_algsMap.end() ) { // found
106  return it->second;
107  }
108  if ( createIf ) {
109  IAlgorithm* alg;
110  if ( createAlgorithm( typeName.type(), typeName.name(), alg, true ).isSuccess() ) {
111  return algorithm( typeName, false );
112  }
113  }
114  return no_algorithm;
115 }

◆ 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 49 of file AlgorithmManager.cpp.

50  {
51  // Check if the algorithm is already existing
52  if ( checkIfExists ) {
53  if ( existsAlgorithm( algname ) ) {
54  // return an error because an algorithm with that name already exists
55  return StatusCode::FAILURE;
56  }
57  }
58  std::string actualalgtype( algtype );
59  // a '\' in front of the type name prevents alias replacement
60  if ( ( actualalgtype.size() > 8 ) && ( actualalgtype.compare( 0, 8, "unalias:" ) == 0 ) ) {
61  actualalgtype = actualalgtype.substr( 8 );
62  } else {
63  auto typeAlias = m_algTypeAliases.find( algtype );
64  if ( typeAlias != m_algTypeAliases.end() ) { actualalgtype = typeAlias->second; }
65  }
66  algorithm = Gaudi::Algorithm::Factory::create( actualalgtype, algname, serviceLocator().get() ).release();
67  if ( !algorithm ) {
68  this->error() << "Algorithm of type " << actualalgtype << " is unknown (No factory available)." << endmsg;
69 #ifndef _WIN32
70  errno =
71  0xAFFEDEAD; // code used by Gaudi for library load errors: forces getLastErrorString do use dlerror (on Linux)
72 #endif
73  std::string err = System::getLastErrorString();
74  if ( !err.empty() ) this->error() << err << endmsg;
75  this->error() << "More information may be available by setting the global jobOpt \"PluginDebugLevel\" to 1"
76  << endmsg;
77  return StatusCode::FAILURE;
78  }
79  // Check the compatibility of the version of the interface obtained
80  if ( !isValidInterface( algorithm ) ) {
81  fatal() << "Incompatible interface IAlgorithm version for " << actualalgtype << endmsg;
82  return StatusCode::FAILURE;
83  }
84  m_algs.emplace_back( algorithm, managed );
85  m_algsMap.emplace( algorithm->name(), algorithm );
86  // let the algorithm know its type
87  algorithm->setType( std::move( actualalgtype ) );
88  // this is needed to keep the reference count correct, since isValidInterface(algorithm)
89  // implies an increment of the counter by 1
90  algorithm->release();
91  StatusCode rc;
92  if ( managed ) {
93  // Bring the created algorithm to the target state of the ApplicationMgr
95  rc = algorithm->sysInitialize();
96  if ( rc.isSuccess() && targetFSMState() >= Gaudi::StateMachine::RUNNING ) { rc = algorithm->sysStart(); }
97  }
98  if ( !rc.isSuccess() ) { this->error() << "Failed to initialize algorithm: [" << algname << "]" << endmsg; }
99  }
100  return rc;
101 }

◆ existsAlgorithm()

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

implementation of IAlgManager::existsAlgorithm

Definition at line 117 of file AlgorithmManager.cpp.

117  {
118  return m_algsMap.find( std::string( name ) ) != m_algsMap.end();
119 }

◆ finalize()

StatusCode AlgorithmManager::finalize ( )
override

Finalize (from INITIALIZED to CONFIGURED).

Definition at line 159 of file AlgorithmManager.cpp.

159  {
160  StatusCode rc;
161  auto it = m_algs.begin();
162  while ( it != m_algs.end() ) { // finalize and remove from the list the managed algorithms
163  if ( it->managed ) {
164  auto range = m_algsMap.equal_range( it->algorithm->name() );
165  auto itm = std::find_if( range.first, range.second, [&]( auto const& p ) { return p.second == it->algorithm; } );
166  if ( itm == range.second ) { return StatusCode::FAILURE; }
167 
168  rc = it->algorithm->sysFinalize();
169  if ( rc.isFailure() ) return rc;
170 
171  it = m_algs.erase( it );
172  m_algsMap.erase( itm );
173  } else {
174  ++it;
175  }
176  }
177  return rc;
178 }

◆ getAlgorithms()

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

implementation of IAlgManager::getAlgorithms

Definition at line 121 of file AlgorithmManager.cpp.

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

◆ initialize()

StatusCode AlgorithmManager::initialize ( )
override

Initialization (from CONFIGURED to INITIALIZED).

Definition at line 129 of file AlgorithmManager.cpp.

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

◆ 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 215 of file AlgorithmManager.cpp.

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

◆ reinitialize()

StatusCode AlgorithmManager::reinitialize ( )
override

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

Definition at line 180 of file AlgorithmManager.cpp.

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

◆ removeAlgorithm()

StatusCode AlgorithmManager::removeAlgorithm ( IAlgorithm alg)
override

implementation of IAlgManager::removeAlgorithm

Definition at line 36 of file AlgorithmManager.cpp.

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

◆ restart()

StatusCode AlgorithmManager::restart ( )
override

Initialization (from RUNNING to RUNNING, via INITIALIZED).

Definition at line 193 of file AlgorithmManager.cpp.

193  {
195  m_aess = serviceLocator()->service( "AlgExecStateSvc" );
196  if ( !m_aess.isValid() ) {
197  fatal() << "Error retrieving AlgExecStateSvc" << endmsg;
198  return StatusCode::FAILURE;
199  }
200 
201  StatusCode rc;
202 
203  for ( auto& it : m_algs ) {
204  if ( !it.managed ) continue;
205  rc = it.algorithm->sysRestart();
206  m_aess->resetErrorCount( it.algorithm );
207  if ( rc.isFailure() ) {
208  this->error() << "Unable to re-initialize algorithm: " << it.algorithm->name() << endmsg;
209  return rc;
210  }
211  }
212  return rc;
213 }

◆ start()

StatusCode AlgorithmManager::start ( )
override

Start (from INITIALIZED to RUNNING).

Definition at line 139 of file AlgorithmManager.cpp.

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

◆ stop()

StatusCode AlgorithmManager::stop ( )
override

Stop (from RUNNING to INITIALIZED).

Definition at line 149 of file AlgorithmManager.cpp.

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

◆ 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:
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:76
AlgorithmManager::m_algTypeAliases
AlgTypeAliasesMap m_algTypeAliases
Definition: AlgorithmManager.h:100
AlgorithmManager::m_algsMap
std::unordered_multimap< std::string, SmartIF< IAlgorithm > > m_algsMap
algorithms maintained by AlgorithmManager
Definition: AlgorithmManager.h:97
AlgorithmManager::existsAlgorithm
bool existsAlgorithm(std::string_view name) const override
implementation of IAlgManager::existsAlgorithm
Definition: AlgorithmManager.cpp:117
CommonMessaging< implements< IComponentManager > >::resetMessaging
MSG::Level resetMessaging()
Reinitialize internal states.
Definition: CommonMessaging.h:178
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
GaudiPartProp.decorators.get
get
decorate the vector of properties
Definition: decorators.py:283
extend_interfaces< INamedInterface, IStateful >::interfaceID
static const InterfaceID & interfaceID()
Definition: extend_interfaces.h:29
extends< ComponentManager, IAlgManager >::base_class
extends base_class
Typedef to this class.
Definition: extends.h:23
ManySmallAlgs.alg
alg
Definition: ManySmallAlgs.py:81
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:135
SmartIF::isValid
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:69
StatusCode
Definition: StatusCode.h:64
IAlgorithm
Definition: IAlgorithm.h:36
Gaudi::Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:87
SmartIF< IAlgExecStateSvc >
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:198
ComponentManager::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Definition: ComponentManager.h:42
AlgorithmManager::m_algs
std::vector< AlgorithmItem > m_algs
algorithms maintained by AlgorithmManager
Definition: AlgorithmManager.h:96
AlgorithmManager::name
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
Definition: AlgorithmManager.h:82
Gaudi::StateMachine::RUNNING
@ RUNNING
Definition: StateMachine.h:25
AlgorithmManager::algorithm
SmartIF< IAlgorithm > & algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Definition: AlgorithmManager.cpp:103
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:99
GaudiDict::typeName
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:31
Gaudi::StateMachine::INITIALIZED
@ INITIALIZED
Definition: StateMachine.h:24
isValidInterface
bool isValidInterface(IFace *i)
Templated function that throws an exception if the version if the interface implemented by the object...
Definition: IInterface.h:354
IOTest.end
end
Definition: IOTest.py:125
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:100
System::getLastErrorString
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
Definition: System.cpp:265
gaudirun.application
application
Definition: gaudirun.py:323
IOTest.rc
rc
Definition: IOTest.py:114
Gaudi::Functional::details::zip::range
decltype(auto) range(Args &&... args)
Zips multiple containers together to form a single range.
Definition: details.h:102
AlgorithmManager::createAlgorithm
StatusCode createAlgorithm(const std::string &algtype, const std::string &algname, IAlgorithm *&algorithm, bool managed=false, bool checkIfExists=true) override
implementation of IAlgManager::createAlgorithm
Definition: AlgorithmManager.cpp:49