30#include <tbb/concurrent_queue.h>
45 using extends::extends;
80 unsigned int recursionDepth = 0 );
89 this,
"TopAlg", {},
"Names of the algorithms to be passed to the algorithm manager" };
91 "Override the un-clonability of algorithms. Use with caution!" };
93 this,
"CountAlgorithmInstanceMisses",
false,
94 "Count and print out algorithm instance misses. Useful for finding ways to improve throughput scalability." };
96 this,
"AvailableResources", {},
"Available named resources and their amounts" };
98 this,
"MissingResourceMessageLevel",
MSG::DEBUG,
99 "Message level in case an algorithm cannot be schedule due to a missing resource" };
114#define ON_DEBUG if ( msgLevel( MSG::DEBUG ) )
115#define DEBUG_MSG ON_DEBUG debug()
122 auto* queue = algoId_algoQueue.second;
137 info() <<
"TopAlg list empty. Recovering the one of Application Manager" <<
endmsg;
150 for (
const auto& [res_name, res_required] : algo->neededResources() ) {
153 if ( res_required > res_available ) {
154 error() <<
"Cannot satisfy resource requirement '" << res_name <<
"' for algorithm '" << algo->name()
155 <<
" (required: " << res_required <<
", available: " << res_available <<
")" <<
endmsg;
169 if ( !startSc.
isSuccess() )
return startSc;
173 startSc = ialgo->sysStart();
175 error() <<
"Unable to start Algorithm: " << ialgo->name() <<
endmsg;
187 size_t algo_id = hash_function(
name );
191 error() <<
"Algorithm " <<
name <<
" requested, but not recognised" <<
endmsg;
198 itQueueIAlgPtr->second->pop( algo );
200 if ( !itQueueIAlgPtr->second->try_pop( algo ) ) {
204 ++( result->second );
219 DEBUG_MSG <<
"No instance of algorithm " <<
name <<
" could be retrieved in non-blocking mode" <<
endmsg;
230 res->second -= res_value;
235 msgStream( lvl ) <<
"Failure to allocate resource '" << res_name <<
"' for algorithm " <<
name
236 <<
" (required: " << res_value <<
", available: " << res->second <<
")" <<
endmsg;
249 if ( !algo->
isReEntrant() ) { itQueueIAlgPtr->second->push( algo ); }
255 itQueueIAlgPtr->second->push( algo );
266 size_t algo_id = hash_function(
name );
288 res->second -= value;
302 res->second += value;
309 unsigned int recursionDepth ) {
316 error() <<
"Unable to dcast Algorithm " << algo->name() <<
" to a Sequence, but it has isSequence==true"
321 auto subAlgorithms = seq->subAlgorithms();
326 for (
auto subalgo : *subAlgorithms ) {
329 error() <<
"Algorithm " << subalgo->name() <<
" could not be flattened" <<
endmsg;
334 alglist.emplace_back( algo );
346 error() <<
"Algorithm manager could not be properly fetched." <<
endmsg;
354 IAlgorithm* algo = algMan->algorithm( typeName,
true ).
get();
356 error() <<
"Unable to create or initialize Algorithm: " << typeName <<
endmsg;
361 error() <<
"Unable to initialize Algorithm: " << algo->name() <<
endmsg;
383 debug() <<
" o " << algo->type() <<
"/" << algo->name() <<
" @ " << algo <<
endmsg;
392 const std::string& item_name = ialgo->name();
393 const std::string& item_type = ialgo->type();
394 size_t algo_id = hash_function( item_name );
399 verbose() <<
"Treating resource management and clones of " << item_name <<
endmsg;
402 queue->push( ialgo );
404 if ( ialgo->isReEntrant() ) {
405 if ( ialgo->cardinality() != 0 ) {
406 info() <<
"Algorithm " << ialgo->name() <<
" is ReEntrant, but Cardinality was set to " << ialgo->cardinality()
407 <<
". Only creating 1 instance" <<
endmsg;
410 }
else if ( ialgo->isClonable() ) {
413 if ( ialgo->cardinality() == 1 ) {
417 info() <<
"Algorithm " << ialgo->name() <<
" is un-Clonable but Cardinality was set to "
418 << ialgo->cardinality() <<
". Only creating 1 instance" <<
endmsg;
421 warning() <<
"Overriding UnClonability of Algorithm " << ialgo->name() <<
". Setting Cardinality to "
422 << ialgo->cardinality() <<
endmsg;
432 DEBUG_MSG <<
"type/name to create clone of: " << item_type <<
"/" << item_name <<
endmsg;
435 if (
StatusCode createAlgSc = algMan->createAlgorithm( item_type, item_name, ialgoClone,
true,
437 createAlgSc.isFailure() ) {
441 queue->push( ialgoClone );
466 int indnt = std::to_string( sortedAlgInstanceMisses.
cbegin()->first ).length();
468 std::ostringstream out;
470 out <<
"Hit parade of algorithm instance misses:\n"
472 <<
" ===============================================================================\n"
474 <<
"| Algorithm (# of clones) \n"
475 <<
" ===============================================================================\n";
480 for (
const auto& p : sortedAlgInstanceMisses ) {
481 out <<
std::setw( indnt + 7 ) << std::to_string( p.first ) +
" "
493 if ( !stopSc.
isSuccess() )
return stopSc;
497 stopSc = ialgo->sysStop();
499 error() <<
"Unable to stop Algorithm: " << ialgo->name() <<
endmsg;
512 return extends::finalize();
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
#define DECLARE_COMPONENT(type)
constexpr _OI move(_II __first, _II __last, _OI __result)
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
_Setfill< _CharT > setfill(_CharT __c)
ios_base & right(ios_base &__base)
constexpr auto begin(_Container &__cont) -> decltype(__cont.begin())
constexpr auto end(_Container &__cont) -> decltype(__cont.end())
void push_back(const value_type &__x)
iterator erase(const_iterator __first, const_iterator __last) noexcept
void insert(_InputIterator __first, _InputIterator __last)
const_iterator cbegin() const noexcept
bool empty() const noexcept
const_iterator end() const noexcept
mapped_type & at(const key_type &__k)
iterator find(const key_type &__x)
iterator find(const key_type &__x)
The AlgResourcePool is a concrete implementation of the IAlgResourcePool interface.
std::list< IAlgorithm * > getFlatAlgList() override
std::map< size_t, size_t > m_n_of_allowed_instances
Gaudi::Property< bool > m_lazyCreation
std::list< IAlgorithm * > m_algList
The list of all algorithms created within the Pool which are not top.
std::map< size_t, concurrentQueueIAlgPtr * > m_algqueue_map
StatusCode initialize() override
std::mutex m_resource_mutex
StatusCode releaseAlgorithm(std::string_view name, IAlgorithm *&algo) override
Release a certain algorithm.
Gaudi::Property< std::vector< std::string > > m_topAlgNames
Gaudi::Property< bool > m_countAlgInstMisses
~AlgResourcePool() override
StatusCode acquireResource(std::string_view name, unsigned int value) override
Acquire units of a certain resource.
std::list< IAlgorithm * > m_flatUniqueAlgList
The flat list of algorithms w/o clones.
void dumpInstanceMisses() const
Dump recorded Algorithm instance misses.
Gaudi::Property< int > m_missingResourceMsgLevel
StatusCode releaseResource(std::string_view name, unsigned int value) override
Release a certain resource.
std::list< IAlgorithm * > getTopAlgList() override
StatusCode finalize() override
tbb::concurrent_bounded_queue< IAlgorithm * > concurrentQueueIAlgPtr
Gaudi::Property< IAlgorithm::AlgResources_t > m_availableResources
std::list< IAlgorithm * > m_topAlgList
The list of top algorithms.
StatusCode stop() override
std::map< size_t, unsigned int > m_n_of_created_instances
StatusCode acquireAlgorithm(std::string_view name, IAlgorithm *&algo, bool blocking=false) override
Acquire a certain algorithm using its name.
std::unordered_map< std::string_view, unsigned int > m_algInstanceMisses
Counters for Algorithm instance misses.
StatusCode start() override
StatusCode decodeTopAlgs()
Decode the top Algorithm list.
Gaudi::Property< bool > m_overrideUnClonable
StatusCode flattenSequencer(IAlgorithm *sequencer, std::list< IAlgorithm * > &alglist, unsigned int recursionDepth=0)
Recursively flatten an algList.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
MsgStream & msgStream() const
Return an uninitialized MsgStream.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Implementation of property with value of concrete type.
const ValueType & value() const
bool assign(const Details::PropertyBase &source) override
get the value from another property
Helper class for efficient "key" access for strings.
Helper class to parse a string of format "type/name".
The IAlgorithm is the interface implemented by the Algorithm base class.
virtual const AlgResources_t & neededResources() const =0
Named, non thread-safe resources used during event processing.
virtual StatusCode sysInitialize()=0
Initialization method invoked by the framework.
virtual bool isSequence() const =0
Are we a Sequence?
virtual void setIndex(const unsigned int &idx)=0
Set instantiation index of Alg.
virtual bool isReEntrant() const =0
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
const std::string & name() const override
Retrieve name of the service.
StatusCode stop() override
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
StatusCode start() override
StatusCode initialize() override
Small smart pointer class with automatic reference counting for IInterface.
TYPE * get() const
Get interface pointer.
bool isValid() const
Allow for check if smart pointer is valid.
This class is used for returning status codes from appropriate routines.
constexpr static const auto SUCCESS
constexpr static const auto FAILURE
Base class used to extend a class implementing other interfaces.