The Gaudi Framework master (9cc87df9)
Loading...
Searching...
No Matches
CPUCruncher.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// Local includes
13#include "HiveNumbers.h"
14
15// Framework includes
23
24// STL includes
25#include <chrono>
26#include <cmath>
27#include <ctime>
28#include <limits>
29#include <string>
30#include <vector>
31
32// System includes
33#include <sys/resource.h>
34#include <sys/times.h>
35
36// TBB includes
37#include <tbb/blocked_range.h>
38#include <tbb/concurrent_hash_map.h>
39#include <tbb/parallel_for.h>
40#include <tbb/tick_count.h>
41
42//------------------------------------------------------------------------------
43
51class CPUCruncher : public Algorithm {
52
53public:
54 bool isClonable() const override { return true; }
55
57 StatusCode execute() override;
59 StatusCode initialize() override;
61 StatusCode finalize() override;
62
63 CPUCruncher( const std::string& name, // the algorithm instance name
64 ISvcLocator* pSvc ); // the Service Locator
65
67 virtual ~CPUCruncher();
68
69private:
71 CPUCruncher(); // no default constructor
73 CPUCruncher( const CPUCruncher& ); // no copy constructor
75 CPUCruncher& operator=( const CPUCruncher& ); // no assignement
77
80 bool m_declAugmented{ false };
81 Gaudi::Property<bool> m_loader{ this, "Loader", false, "Declare the algorithm to be a data loader" };
82
85
86 Gaudi::Property<double> m_avg_runtime{ this, "avgRuntime", 1., "Average runtime of the module." };
87 Gaudi::Property<double> m_var_runtime{ this, "varRuntime", 0.01, "Variance of the runtime of the module." };
88 Gaudi::Property<bool> m_local_rndm_gen{ this, "localRndm", true,
89 "Decide if the local random generator is to be used" };
90 Gaudi::Property<unsigned int> m_rwRepetitions{ this, "RwRepetitions", 1, "Increase access to the WB" };
92 this, "SleepFraction", 0.0,
93 "Fraction of time, between 0 and 1, when an algorithm is actually sleeping instead of crunching" };
94 Gaudi::Property<bool> m_invertCFD{ this, "InvertDecision", false, "Invert control flow decision." };
95 Gaudi::Property<unsigned int> m_failNEvents{ this, "FailNEvents", 0, "Return FAILURE on every Nth event" };
96 Gaudi::Property<int> m_nParallel{ this, "NParallel", -1, "Run N parallel crunching tasks" };
97
100
101 using CHM = tbb::concurrent_hash_map<std::string, unsigned int>;
103
104 // CPUCrunchSvc
106};
107
109
111
112#define ON_DEBUG if ( msgLevel( MSG::DEBUG ) )
113#define DEBUG_MSG ON_DEBUG debug()
114
115#define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) )
116#define VERBOSE_MSG ON_VERBOSE verbose()
117
118//------------------------------------------------------------------------------
119
120CPUCruncher::CPUCruncher( const std::string& name, // the algorithm instance name
121 ISvcLocator* pSvc )
122 : Algorithm( name, pSvc ) {
123
124 // Register the algo in the static concurrent hash map in order to
125 // monitor the # of copies
126 CHM::accessor name_ninstances;
127 m_name_ncopies_map.insert( name_ninstances, name );
128 name_ninstances->second += 1;
129}
130
132 for ( uint i = 0; i < m_inputHandles.size(); ++i ) delete m_inputHandles[i];
133
134 for ( uint i = 0; i < m_outputHandles.size(); ++i ) delete m_outputHandles[i];
135}
136
138 auto sc = Algorithm::initialize();
139 if ( !sc ) return sc;
140
141 m_crunchSvc = serviceLocator()->service( "CPUCrunchSvc" );
142 if ( !m_crunchSvc.isValid() ) {
143 fatal() << "unable to acquire CPUCruncSvc" << endmsg;
144 return StatusCode::FAILURE;
145 }
146
147 // This is a bit ugly. There is no way to declare a vector of DataObjectHandles, so
148 // we need to wait until initialize when we've read in the input and output key
149 // properties, and know their size, and then turn them
150 // into Handles and register them with the framework by calling declareProperty. We
151 // could call declareInput/declareOutput on them too.
152
153 int i = 0;
154 for ( auto k : m_inpKeys ) {
155 DEBUG_MSG << "adding input key " << k << endmsg;
157 declareProperty( "dummy_in_" + std::to_string( i ), *( m_inputHandles.back() ) );
158 i++;
159 }
160
161 i = 0;
162 for ( auto k : m_outKeys ) {
163 DEBUG_MSG << "adding output key " << k << endmsg;
165 declareProperty( "dummy_out_" + std::to_string( i ), *( m_outputHandles.back() ) );
166 i++;
167 }
168
169 return sc;
170}
171
172//------------------------------------------------------------------------------
174 //
175 for ( const auto& k : outputDataObjs() ) {
176 auto outputHandle = new DataObjectHandle<DataObject>( k, Gaudi::DataHandle::Writer, this );
177 VERBOSE_MSG << "found late-attributed output: " << outputHandle->objKey() << endmsg;
178 m_outputHandles.push_back( outputHandle );
179 declareProperty( "dummy_out_" + outputHandle->objKey(), *( m_outputHandles.back() ) );
180 }
181
183
184 m_declAugmented = true;
185}
186
187//------------------------------------------------------------------------------
188
189StatusCode CPUCruncher::execute() // the execution of the algorithm
190{
191
193
194 double crunchtime;
195
196 if ( m_local_rndm_gen ) {
197 /* This will disappear with a thread safe random number generator service.
198 * Use basic Box-Muller to generate Gaussian random numbers.
199 * The quality is not good for in depth study given that the generator is a
200 * linear congruent.
201 * Throw away basically a free number: we are in a cpu cruncher after all.
202 * The seed is taken from the clock, but we could assign a seed per module to
203 * ensure reproducibility.
204 *
205 * This is not an overkill but rather an exercise towards a thread safe
206 * random number generation.
207 */
208
209 auto getGausRandom = []( double mean, double sigma ) -> double {
210 unsigned int seed = std::clock();
211
212 auto getUnifRandom = []( unsigned int& seed ) -> double {
213 // from "Numerical Recipes"
214 constexpr unsigned int m = 232;
215 constexpr unsigned int a = 1664525;
216 constexpr unsigned int c = 1013904223;
217 seed = ( a * seed + c ) % m;
218 const double unif = double( seed ) / m;
219 return unif;
220 };
221
222 double unif1, unif2;
223 do {
224 unif1 = getUnifRandom( seed );
225 unif2 = getUnifRandom( seed );
226 } while ( unif1 < std::numeric_limits<double>::epsilon() );
227
228 const double normal = sqrt( -2. * log( unif1 ) ) * cos( 2 * M_PI * unif2 );
229
230 return normal * sigma + mean;
231 };
232
233 crunchtime = std::abs( getGausRandom( m_avg_runtime * ( 1. - m_sleepFraction ), m_var_runtime ) );
234 // End Of temp block
235 } else {
236 // Should be a member.
238 crunchtime = std::abs( rndmgaus() );
239 }
240 unsigned int crunchtime_ms = 1000 * crunchtime;
241
242 // Prepare to sleep (even if we won't enter the following if clause for sleeping).
243 // This is needed to distribute evenly among all algorithms the overhead (around sleeping) which is harmful when
244 // trying to achieve uniform distribution of algorithm timings.
245 const double dreamtime = m_avg_runtime * m_sleepFraction;
246 const std::chrono::duration<double> dreamtime_duration( dreamtime );
247
248 // Start to measure the total time here, together with the dreaming process straight ahead
249 tbb::tick_count starttbb = tbb::tick_count::now();
250
251 DEBUG_MSG << "Crunching time will be: " << crunchtime_ms << " ms" << endmsg;
252 const EventContext& context = Gaudi::Hive::currentContext();
253 DEBUG_MSG << "Start event " << context.evt() << " in slot " << context.slot() << " on pthreadID " << std::hex
254 << pthread_self() << std::dec << endmsg;
255
256 VERBOSE_MSG << "inputs number: " << m_inputHandles.size() << endmsg;
257 for ( auto& inputHandle : m_inputHandles ) {
258 if ( !inputHandle->isValid() ) continue;
259
260 VERBOSE_MSG << "get from TS: " << inputHandle->objKey() << endmsg;
261 const DataObject* obj = nullptr;
262 for ( unsigned int i = 0; i < m_rwRepetitions; ++i ) { obj = inputHandle->get(); }
263 if ( obj == nullptr ) error() << "A read object was a null pointer." << endmsg;
264 }
265
266 if ( m_nParallel > 1 ) {
267 tbb::parallel_for( tbb::blocked_range<size_t>( 0, m_nParallel ), [&]( tbb::blocked_range<size_t> r ) {
269 debug() << "CPUCrunch complete in TBB parallel for block " << r.begin() << " to " << r.end() << endmsg;
270 } );
271 } else {
273 }
274
275 // Return error on fraction of events if configured
276 if ( m_failNEvents > 0 && context.evt() > 0 && ( context.evt() % m_failNEvents ) == 0 ) {
277 return StatusCode::FAILURE;
278 }
279
280 VERBOSE_MSG << "outputs number: " << m_outputHandles.size() << endmsg;
281 for ( auto& outputHandle : m_outputHandles ) {
282 if ( !outputHandle->isValid() ) continue;
283
284 VERBOSE_MSG << "put to TS: " << outputHandle->objKey() << endmsg;
285 outputHandle->put( std::make_unique<DataObject>() );
286 }
287
288 tbb::tick_count endtbb = tbb::tick_count::now();
289 const double actualRuntime = ( endtbb - starttbb ).seconds();
290
291 DEBUG_MSG << "Finish event " << context.evt() << " in " << int( 1000 * actualRuntime ) << " ms" << endmsg;
292
293 DEBUG_MSG << "Timing: ExpectedCrunchtime= " << crunchtime_ms << " ms. ExpectedDreamtime= " << int( 1000 * dreamtime )
294 << " ms. ActualTotalRuntime= " << int( 1000 * actualRuntime )
295 << " ms. Ratio= " << ( crunchtime + dreamtime ) / actualRuntime << endmsg;
296
298
299 return StatusCode::SUCCESS;
300}
301
302//------------------------------------------------------------------------------
303
304StatusCode CPUCruncher::finalize() // the finalization of the algorithm
305{
306 MsgStream log( msgSvc(), name() );
307
308 unsigned int ninstances;
309
310 {
311 CHM::const_accessor const_name_ninstances;
312 m_name_ncopies_map.find( const_name_ninstances, name() );
313 ninstances = const_name_ninstances->second;
314 }
315
316 constexpr double s2ms = 1000.;
317 // do not show repetitions
318 if ( ninstances != 0 ) {
319 info() << "Summary: name= " << name() << "\t avg_runtime= " << m_avg_runtime * s2ms << "\t n_clones= " << ninstances
320 << endmsg;
321
322 CHM::accessor name_ninstances;
323 m_name_ncopies_map.find( name_ninstances, name() );
324 name_ninstances->second = 0;
325 }
326
327 return Algorithm::finalize();
328}
329
330//------------------------------------------------------------------------------
#define DEBUG_MSG
#define VERBOSE_MSG
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
duration< int64_t > seconds
duration< int64_t, milli > milliseconds
complex< _Tp > log(const complex< _Tp > &)
_Tp abs(const complex< _Tp > &)
complex< _Tp > cos(const complex< _Tp > &)
complex< _Tp > sqrt(const complex< _Tp > &)
ios_base & hex(ios_base &__base)
ios_base & dec(ios_base &__base)
static constexpr _Tp epsilon() noexcept
constexpr void push_back(const value_type &__x)
constexpr const_reference back() const noexcept
constexpr size_type size() const noexcept
A class that implements a search for prime numbers.
Gaudi::Property< bool > m_invertCFD
std::vector< DataObjectHandle< DataObject > * > m_outputHandles
CPUCruncher & operator=(const CPUCruncher &)
the assignement operator is disabled
CPUCruncher()
the default constructor is disabled
StatusCode execute() override
the execution of the algorithm
virtual ~CPUCruncher()
virtual & protected destructor
bool m_declAugmented
void declareRuntimeRequestedOutputs()
The CPU intensive function.
Gaudi::Property< std::vector< std::string > > m_outKeys
StatusCode finalize() override
the finalization of the algorithm
std::vector< DataObjectHandle< DataObject > * > m_inputHandles
static CHM m_name_ncopies_map
SmartIF< ICPUCrunchSvc > m_crunchSvc
tbb::concurrent_hash_map< std::string, unsigned int > CHM
Gaudi::Property< bool > m_local_rndm_gen
Gaudi::Property< double > m_var_runtime
Gaudi::Property< std::vector< std::string > > m_inpKeys
StatusCode initialize() override
Its initialization.
Gaudi::Property< int > m_nParallel
bool isClonable() const override
Gaudi::Property< double > m_avg_runtime
Gaudi::Property< bool > m_loader
Gaudi::Property< double > m_sleepFraction
CPUCruncher(const CPUCruncher &)
the copy constructor is disabled
Gaudi::Property< unsigned int > m_rwRepetitions
CPUCruncher(const std::string &name, ISvcLocator *pSvc)
Gaudi::Property< unsigned int > m_failNEvents
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
const SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
A DataObject is the base class of any identifiable object on any data store.
Definition DataObject.h:37
This class represents an entry point to all the event specific data.
ContextEvt_t evt() const
ContextID_t slot() const
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition Algorithm.h:98
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition Algorithm.h:286
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition Algorithm.h:175
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition Algorithm.h:181
SmartIF< IRndmGenSvc > & randSvc() const
The standard RandomGen service, Return a pointer to the service if present.
SmartIF< ISvcLocator > & serviceLocator() const override
The standard service locator.
const std::string & name() const override
The identifying name of the algorithm object.
Implementation of property with value of concrete type.
Definition Property.h:35
void setFilterPassed(bool state) const
Set the filter passed flag to the specified state.
virtual std::chrono::milliseconds crunch_for(const std::chrono::milliseconds &) const =0
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition ISvcLocator.h:42
virtual SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
Definition of the MsgStream class used to transmit messages.
Definition MsgStream.h:29
Parameters for the Gauss random number generation.
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
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
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
GAUDI_API const EventContext & currentContext()