The Gaudi Framework  master (37c0b60a)
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 (std::string algtype, 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...
 
const 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::stringname () 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 * 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::stringgetInterfaceNames () 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...
 
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_view, SmartIF< IAlgorithm > > m_algsMap
 algorithms maintained by AlgorithmManager More...
 
std::vector< IAlgorithm * > m_listOfPtrs
 List of pointers to the know services used to implement getAlgorithms() 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 42 of file AlgorithmManager.h.

Member Typedef Documentation

◆ AlgTypeAliasesMap

typedefs and classes

Definition at line 52 of file AlgorithmManager.h.

Constructor & Destructor Documentation

◆ AlgorithmManager()

AlgorithmManager::AlgorithmManager ( IInterface iface)

default creator

Definition at line 28 of file AlgorithmManager.cpp.

28  : base_class( application, IAlgorithm::interfaceID() ) {
29  addRef(); // Initial count set to 1
30 }

Member Function Documentation

◆ addAlgorithm()

StatusCode AlgorithmManager::addAlgorithm ( IAlgorithm alg)
override

implementation of IAlgManager::addAlgorithm

Definition at line 33 of file AlgorithmManager.cpp.

33  {
34  m_algs.push_back( alg );
35  m_algsMap.emplace( alg->name(), alg );
36  return StatusCode::SUCCESS;
37 }

◆ algorithm()

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

Definition at line 108 of file AlgorithmManager.cpp.

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

◆ createAlgorithm()

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

implementation of IAlgManager::createAlgorithm

Definition at line 54 of file AlgorithmManager.cpp.

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

◆ existsAlgorithm()

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

implementation of IAlgManager::existsAlgorithm

Definition at line 123 of file AlgorithmManager.cpp.

123  {
124  return m_algsMap.find( name ) != m_algsMap.end();
125 }

◆ finalize()

StatusCode AlgorithmManager::finalize ( )
override

Finalize (from INITIALIZED to CONFIGURED).

Definition at line 166 of file AlgorithmManager.cpp.

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

◆ getAlgorithms()

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

implementation of IAlgManager::getAlgorithms

Definition at line 128 of file AlgorithmManager.cpp.

128  {
130  m_listOfPtrs.reserve( m_algs.size() );
132  []( const AlgorithmItem& alg ) { return alg.algorithm; } );
133  return m_listOfPtrs;
134 }

◆ initialize()

StatusCode AlgorithmManager::initialize ( )
override

Initialization (from CONFIGURED to INITIALIZED).

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::INITIALIZED ) continue;
140  rc = it.algorithm->sysInitialize();
141  if ( rc.isFailure() ) return rc;
142  }
143  return rc;
144 }

◆ name()

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

Return the name of the manager (implementation of INamedInterface)

Definition at line 85 of file AlgorithmManager.h.

85  {
86  static const std::string _name = "AlgorithmManager";
87  return _name;
88  }

◆ outputLevelUpdate()

void AlgorithmManager::outputLevelUpdate ( )
override

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

Definition at line 222 of file AlgorithmManager.cpp.

222  {
223  resetMessaging();
224  for ( auto& algItem : m_algs ) {
225  const auto alg = dynamic_cast<Gaudi::Algorithm*>( algItem.algorithm );
226  if ( alg ) alg->resetMessaging();
227  }
228 }

◆ reinitialize()

StatusCode AlgorithmManager::reinitialize ( )
override

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

Definition at line 187 of file AlgorithmManager.cpp.

187  {
188  StatusCode rc;
189  for ( auto& it : m_algs ) {
190  if ( !it.managed ) continue;
191  rc = it.algorithm->sysReinitialize();
192  if ( rc.isFailure() ) {
193  this->error() << "Unable to re-initialize algorithm: " << it.algorithm->name() << endmsg;
194  return rc;
195  }
196  }
197  return rc;
198 }

◆ removeAlgorithm()

StatusCode AlgorithmManager::removeAlgorithm ( IAlgorithm alg)
override

implementation of IAlgManager::removeAlgorithm

Definition at line 40 of file AlgorithmManager.cpp.

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

◆ restart()

StatusCode AlgorithmManager::restart ( )
override

Initialization (from RUNNING to RUNNING, via INITIALIZED).

Definition at line 200 of file AlgorithmManager.cpp.

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

◆ start()

StatusCode AlgorithmManager::start ( )
override

Start (from INITIALIZED to RUNNING).

Definition at line 146 of file AlgorithmManager.cpp.

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

◆ stop()

StatusCode AlgorithmManager::stop ( )
override

Stop (from RUNNING to INITIALIZED).

Definition at line 156 of file AlgorithmManager.cpp.

156  {
157  StatusCode rc;
158  for ( auto& it : m_algs ) {
159  if ( !it.managed ) continue;
160  rc = it.algorithm->sysStop();
161  if ( rc.isFailure() ) return rc;
162  }
163  return rc;
164 }

◆ typeAliases() [1/2]

AlgTypeAliasesMap& AlgorithmManager::typeAliases ( )
inline

Definition at line 92 of file AlgorithmManager.h.

92 { return m_algTypeAliases; }

◆ typeAliases() [2/2]

const AlgTypeAliasesMap& AlgorithmManager::typeAliases ( ) const
inline

Definition at line 93 of file AlgorithmManager.h.

93 { return m_algTypeAliases; }

Member Data Documentation

◆ m_algs

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

algorithms maintained by AlgorithmManager

Definition at line 99 of file AlgorithmManager.h.

◆ m_algsMap

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

algorithms maintained by AlgorithmManager

Definition at line 100 of file AlgorithmManager.h.

◆ m_algTypeAliases

AlgTypeAliasesMap AlgorithmManager::m_algTypeAliases
private

Definition at line 106 of file AlgorithmManager.h.

◆ m_listOfPtrs

std::vector<IAlgorithm*> AlgorithmManager::m_listOfPtrs
mutableprivate

List of pointers to the know services used to implement getAlgorithms()

Definition at line 104 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: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:123
std::move
T move(T... args)
CommonMessaging< implements< IComponentManager > >::resetMessaging
MSG::Level resetMessaging()
Reinitialize internal states.
Definition: CommonMessaging.h:179
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
std::vector::reserve
T reserve(T... args)
std::unordered_multimap::find
T find(T... args)
std::back_inserter
T back_inserter(T... args)
GaudiPartProp.decorators.get
get
decorate the vector of properties
Definition: decorators.py:283
std::unordered_multimap::emplace
T emplace(T... args)
extends< ComponentManager, IAlgManager >::base_class
extends base_class
Typedef to this class.
Definition: extends.h:24
std::vector::clear
T clear(T... args)
ManySmallAlgs.alg
alg
Definition: ManySmallAlgs.py:81
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
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:54
Gaudi::Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:90
std::unordered_multimap::erase
T erase(T... args)
SmartIF< IAlgExecStateSvc >
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
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
AlgorithmManager::algorithm
SmartIF< IAlgorithm > & algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Definition: AlgorithmManager.cpp:108
std::unordered_multimap::equal_range
T equal_range(T... args)
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
isValidInterface
bool isValidInterface(IFace *i)
Templated function that throws an exception if the version if the interface implemented by the object...
Definition: IInterface.h:336
std::string::empty
T empty(T... args)
std::unordered_multimap::end
T end(T... args)
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
System::getLastErrorString
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
Definition: System.cpp:279
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:97