The Gaudi Framework master (934d2c3c)
Loading...
Searching...
No Matches
AlgResourcePool.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 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
12// Framework includes
13#include <Gaudi/Sequence.h>
18#include <GaudiKernel/Service.h>
19
20// STL includes
21#include <atomic>
22#include <functional>
23#include <list>
24#include <map>
25#include <mutex>
26#include <queue>
27#include <sstream>
28#include <string>
29#include <string_view>
30#include <tbb/concurrent_queue.h>
31#include <unordered_map>
32#include <vector>
33
42class AlgResourcePool : public extends<Service, IAlgResourcePool> {
43public:
44 // Standard constructor
45 using extends::extends;
46 // Standard destructor
47 ~AlgResourcePool() override;
48
49 StatusCode start() override;
50 StatusCode initialize() override;
52 StatusCode acquireAlgorithm( std::string_view name, IAlgorithm*& algo, bool blocking = false ) override;
54 StatusCode releaseAlgorithm( std::string_view name, IAlgorithm*& algo ) override;
56 StatusCode acquireResource( std::string_view name, unsigned int value ) override;
58 StatusCode releaseResource( std::string_view name, unsigned int value ) override;
59
62
63 StatusCode stop() override;
64 StatusCode finalize() override;
65
66private:
67 typedef tbb::concurrent_bounded_queue<IAlgorithm*> concurrentQueueIAlgPtr;
68
70
74
77
80 unsigned int recursionDepth = 0 );
81
83 void dumpInstanceMisses() const;
86
87 Gaudi::Property<bool> m_lazyCreation{ this, "CreateLazily", false, "" };
89 this, "TopAlg", {}, "Names of the algorithms to be passed to the algorithm manager" };
90 Gaudi::Property<bool> m_overrideUnClonable{ this, "OverrideUnClonable", false,
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" };
100
103
106
109};
110
111// Instantiation of a static factory class used by clients to create instances of this service
113
114#define ON_DEBUG if ( msgLevel( MSG::DEBUG ) )
115#define DEBUG_MSG ON_DEBUG debug()
116
117//---------------------------------------------------------------------------
118
119// Destructor
121 for ( auto& algoId_algoQueue : m_algqueue_map ) {
122 auto* queue = algoId_algoQueue.second;
123 delete queue;
124 }
125}
126
127//---------------------------------------------------------------------------
128
129// Initialize the pool with the list of algorithms known to the IAlgManager
131
133 if ( !sc.isSuccess() ) warning() << "Base class could not be started" << endmsg;
134
135 // Try to recover the topAlgList from the ApplicationManager for backward-compatibility
136 if ( m_topAlgNames.value().empty() ) {
137 info() << "TopAlg list empty. Recovering the one of Application Manager" << endmsg;
138 const Gaudi::Utils::TypeNameString appMgrName( "ApplicationMgr/ApplicationMgr" );
139 SmartIF<IProperty> appMgrProps( serviceLocator()->service( appMgrName ) );
140 m_topAlgNames.assign( appMgrProps->getProperty( "TopAlg" ) );
141 }
142
143 sc = decodeTopAlgs();
144 if ( sc.isFailure() ) warning() << "Algorithms could not be properly decoded." << endmsg;
145
147
148 // Check if an algorithm requests more of a resource than is available
149 for ( const IAlgorithm* algo : m_flatUniqueAlgList ) {
150 for ( const auto& [res_name, res_required] : algo->neededResources() ) {
151 auto res = m_availableResources.find( res_name );
152 auto res_available = ( res != m_availableResources.end() ) ? res->second : 0;
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;
156 return StatusCode::FAILURE;
157 }
158 }
159 }
160
161 return StatusCode::SUCCESS;
162}
163
164//---------------------------------------------------------------------------
165
167
168 StatusCode startSc = Service::start();
169 if ( !startSc.isSuccess() ) return startSc;
170
171 // sys-Start the algorithms
172 for ( IAlgorithm* ialgo : m_algList ) {
173 startSc = ialgo->sysStart();
174 if ( startSc.isFailure() ) {
175 error() << "Unable to start Algorithm: " << ialgo->name() << endmsg;
176 return startSc;
177 }
178 }
179 return StatusCode::SUCCESS;
180}
181
182//---------------------------------------------------------------------------
183
184StatusCode AlgResourcePool::acquireAlgorithm( std::string_view name, IAlgorithm*& algo, bool blocking ) {
185
186 std::hash<std::string_view> hash_function;
187 size_t algo_id = hash_function( name );
188 auto itQueueIAlgPtr = m_algqueue_map.find( algo_id );
189
190 if ( itQueueIAlgPtr == m_algqueue_map.end() ) {
191 error() << "Algorithm " << name << " requested, but not recognised" << endmsg;
192 algo = nullptr;
193 return StatusCode::FAILURE;
194 }
195
196 StatusCode sc;
197 if ( blocking ) {
198 itQueueIAlgPtr->second->pop( algo );
199 } else {
200 if ( !itQueueIAlgPtr->second->try_pop( algo ) ) {
201 if ( m_countAlgInstMisses ) {
202 auto result = m_algInstanceMisses.find( name );
203 if ( result != m_algInstanceMisses.end() )
204 ++( result->second );
205 else
207 }
209 }
210 }
211
212 // Note that reentrant algorithms are not consumed so we put them
213 // back immediately in the queue at the end of this function.
214 // Now we may still be called again in between and get this
215 // error. In such a case, the Scheduler will retry later.
216 // This is of course not optimal, but should only happen very
217 // seldom and thud won't affect the global efficiency
218 if ( sc.isFailure() )
219 DEBUG_MSG << "No instance of algorithm " << name << " could be retrieved in non-blocking mode" << endmsg;
220
221 if ( sc.isSuccess() ) {
222 // Try to acquire all the resources the algorithm needs
223 if ( !algo->neededResources().empty() ) {
224 std::scoped_lock lock( m_resource_mutex );
225
226 auto tmpResources = m_availableResources; // backup resources
227 for ( const auto& [res_name, res_value] : algo->neededResources() ) {
228 auto res = m_availableResources.find( res_name );
229 if ( res != m_availableResources.end() && res->second >= res_value ) {
230 res->second -= res_value;
231 } else {
233 const auto lvl = static_cast<MSG::Level>( m_missingResourceMsgLevel.value() );
234 if ( msgLevel( lvl ) ) {
235 msgStream( lvl ) << "Failure to allocate resource '" << res_name << "' for algorithm " << name
236 << " (required: " << res_value << ", available: " << res->second << ")" << endmsg;
237 }
238 break;
239 }
240 }
241
242 // Could not acquire all resources
243 if ( sc.isFailure() ) {
244 // Restore resources
245 m_availableResources = std::move( tmpResources );
246
247 // in case of not reentrant, push it back. Reentrant ones are pushed back
248 // in all cases further down
249 if ( !algo->isReEntrant() ) { itQueueIAlgPtr->second->push( algo ); }
250 }
251 }
252
253 if ( algo->isReEntrant() ) {
254 // push back reentrant algorithms immediately as it can be reused
255 itQueueIAlgPtr->second->push( algo );
256 }
257 }
258 return sc;
259}
260
261//---------------------------------------------------------------------------
262
264
265 std::hash<std::string_view> hash_function;
266 size_t algo_id = hash_function( name );
267
268 // release resources used by the algorithm
269 {
270 std::scoped_lock lock( m_resource_mutex );
271 for ( const auto& [res_name, res_value] : algo->neededResources() ) {
272 auto res = m_availableResources.find( res_name );
273 if ( res != m_availableResources.end() ) { res->second += res_value; }
274 }
275 }
276
277 // release algorithm itself if not reentrant
278 if ( !algo->isReEntrant() ) { m_algqueue_map[algo_id]->push( algo ); }
279 return StatusCode::SUCCESS;
280}
281
282//---------------------------------------------------------------------------
283
284StatusCode AlgResourcePool::acquireResource( std::string_view name, unsigned int value ) {
285 std::scoped_lock lock( m_resource_mutex );
286 auto res = m_availableResources.find( Gaudi::StringKey( name ) );
287 if ( res != m_availableResources.end() && res->second >= value ) {
288 res->second -= value;
289 return StatusCode::SUCCESS;
290 }
291
292 return StatusCode::FAILURE;
293}
294
295//---------------------------------------------------------------------------
296
297StatusCode AlgResourcePool::releaseResource( std::string_view name, unsigned int value ) {
298 std::scoped_lock lock( m_resource_mutex );
299 auto res = m_availableResources.find( Gaudi::StringKey( name ) );
300 if ( res == m_availableResources.end() ) { return StatusCode::FAILURE; }
301
302 res->second += value;
303 return StatusCode::SUCCESS;
304}
305
306//---------------------------------------------------------------------------
307
309 unsigned int recursionDepth ) {
310
312
313 if ( algo->isSequence() ) {
314 auto seq = dynamic_cast<Gaudi::Sequence*>( algo );
315 if ( seq == 0 ) {
316 error() << "Unable to dcast Algorithm " << algo->name() << " to a Sequence, but it has isSequence==true"
317 << endmsg;
318 return StatusCode::FAILURE;
319 }
320
321 auto subAlgorithms = seq->subAlgorithms();
322
323 // Recursively unroll
324 ++recursionDepth;
325
326 for ( auto subalgo : *subAlgorithms ) {
327 sc = flattenSequencer( subalgo, alglist, recursionDepth );
328 if ( sc.isFailure() ) {
329 error() << "Algorithm " << subalgo->name() << " could not be flattened" << endmsg;
330 return sc;
331 }
332 }
333 } else {
334 alglist.emplace_back( algo );
335 return sc;
336 }
337 return sc;
338}
339
340//---------------------------------------------------------------------------
341
343
345 if ( !algMan.isValid() ) {
346 error() << "Algorithm manager could not be properly fetched." << endmsg;
347 return StatusCode::FAILURE;
348 }
349
351
352 // Fill the top algorithm list ----
353 for ( const std::string& typeName : m_topAlgNames ) {
354 IAlgorithm* algo = algMan->algorithm( typeName, /*createIf*/ true ).get();
355 if ( !algo ) {
356 error() << "Unable to create or initialize Algorithm: " << typeName << endmsg;
357 return StatusCode::FAILURE;
358 }
359 sc = algo->sysInitialize();
360 if ( sc.isFailure() ) {
361 error() << "Unable to initialize Algorithm: " << algo->name() << endmsg;
362 return sc;
363 }
364 m_topAlgList.push_back( algo );
365 }
366 // Top algorithm list filled ----
367
368 // Now we unroll it ----
369 for ( IAlgorithm* ialgo : m_topAlgList ) { sc = flattenSequencer( ialgo, m_flatUniqueAlgList ); }
370 // stupid O(N^2) unique-ification..
371 for ( auto i = begin( m_flatUniqueAlgList ); i != end( m_flatUniqueAlgList ); ++i ) {
372 auto n = next( i );
373 while ( n != end( m_flatUniqueAlgList ) ) {
374 if ( *n == *i )
375 n = m_flatUniqueAlgList.erase( n );
376 else
377 ++n;
378 }
379 }
380 ON_DEBUG {
381 debug() << "List of algorithms is: " << endmsg;
382 for ( IAlgorithm* algo : m_flatUniqueAlgList )
383 debug() << " o " << algo->type() << "/" << algo->name() << " @ " << algo << endmsg;
384 }
385
386 // Unrolled ---
387
388 // Now let's manage the clones
389 std::hash<std::string> hash_function;
390 for ( IAlgorithm* ialgo : m_flatUniqueAlgList ) {
391
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 );
396 m_algqueue_map[algo_id] = queue;
397
398 if ( msgLevel( MSG::VERBOSE ) ) {
399 verbose() << "Treating resource management and clones of " << item_name << endmsg;
400 }
401
402 queue->push( ialgo );
403 m_algList.push_back( 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;
408 }
409 m_n_of_allowed_instances[algo_id] = 1;
410 } else if ( ialgo->isClonable() ) {
411 m_n_of_allowed_instances[algo_id] = ialgo->cardinality();
412 } else {
413 if ( ialgo->cardinality() == 1 ) {
414 m_n_of_allowed_instances[algo_id] = 1;
415 } else {
416 if ( !m_overrideUnClonable ) {
417 info() << "Algorithm " << ialgo->name() << " is un-Clonable but Cardinality was set to "
418 << ialgo->cardinality() << ". Only creating 1 instance" << endmsg;
419 m_n_of_allowed_instances[algo_id] = 1;
420 } else {
421 warning() << "Overriding UnClonability of Algorithm " << ialgo->name() << ". Setting Cardinality to "
422 << ialgo->cardinality() << endmsg;
423 m_n_of_allowed_instances[algo_id] = ialgo->cardinality();
424 }
425 }
426 }
427 m_n_of_created_instances[algo_id] = 1;
428
429 // potentially create clones; if not lazy creation we have to do it now
430 if ( !m_lazyCreation ) {
431 for ( unsigned int i = 1, end = m_n_of_allowed_instances[algo_id]; i < end; ++i ) {
432 DEBUG_MSG << "type/name to create clone of: " << item_type << "/" << item_name << endmsg;
433 IAlgorithm* ialgoClone( nullptr );
434
435 if ( StatusCode createAlgSc = algMan->createAlgorithm( item_type, item_name, ialgoClone, /*managed*/ true,
436 /*checkIfExists*/ false );
437 createAlgSc.isFailure() ) {
438 return createAlgSc;
439 }
440 ialgoClone->setIndex( i );
441 queue->push( ialgoClone );
442 m_n_of_created_instances[algo_id] += 1;
443 }
444 }
445 }
446
447 return sc;
448}
449
450//---------------------------------------------------------------------------
451
453
454//---------------------------------------------------------------------------
455
457
458//---------------------------------------------------------------------------
460
462
463 for ( auto& p : m_algInstanceMisses ) sortedAlgInstanceMisses.insert( { p.second, p.first } );
464
465 // determine optimal indentation
466 int indnt = std::to_string( sortedAlgInstanceMisses.cbegin()->first ).length();
467
468 std::ostringstream out;
469
470 out << "Hit parade of algorithm instance misses:\n"
471 << std::right << std::setfill( ' ' )
472 << " ===============================================================================\n"
473 << std::setw( indnt + 7 ) << "Misses "
474 << "| Algorithm (# of clones) \n"
475 << " ===============================================================================\n";
476
477 std::hash<std::string_view> hash_function;
478
479 out << std::right << std::setfill( ' ' );
480 for ( const auto& p : sortedAlgInstanceMisses ) {
481 out << std::setw( indnt + 7 ) << std::to_string( p.first ) + " "
482 << " " << p.second << " (" << m_n_of_allowed_instances.at( hash_function( p.second ) ) << ")\n";
483 }
484
485 info() << out.str() << endmsg;
486}
487
488//---------------------------------------------------------------------------
489
491
492 StatusCode stopSc = Service::stop();
493 if ( !stopSc.isSuccess() ) return stopSc;
494
495 // sys-Stop the algorithm
496 for ( IAlgorithm* ialgo : m_algList ) {
497 stopSc = ialgo->sysStop();
498 if ( stopSc.isFailure() ) {
499 error() << "Unable to stop Algorithm: " << ialgo->name() << endmsg;
500 return stopSc;
501 }
502 }
504
505 return StatusCode::SUCCESS;
506}
507
512 return extends::finalize();
513}
#define DEBUG_MSG
#define ON_DEBUG
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
constexpr _OI move(_II __first, _II __last, _OI __result)
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
_Setw setw(int __n)
_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 clear() 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 end() noexcept
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.
Definition Property.h:35
const ValueType & value() const
Definition Property.h:246
bool assign(const Details::PropertyBase &source) override
get the value from another property
Definition Property.h:412
Helper class for efficient "key" access for strings.
Definition StringKey.h:68
Helper class to parse a string of format "type/name".
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition IAlgorithm.h:37
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.
Definition Service.cpp:336
const std::string & name() const override
Retrieve name of the service.
Definition Service.cpp:333
StatusCode stop() override
Definition Service.cpp:181
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition Service.h:79
StatusCode start() override
Definition Service.cpp:187
StatusCode initialize() override
Definition Service.cpp:118
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
TYPE * get() const
Get interface pointer.
Definition SmartIF.h:82
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 isFailure() const
Definition StatusCode.h:118
bool isSuccess() const
Definition StatusCode.h:302
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
Base class used to extend a class implementing other interfaces.
Definition extends.h:19
@ DEBUG
Definition IMessageSvc.h:22
@ VERBOSE
Definition IMessageSvc.h:22