33#include <sys/resource.h>
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>
89 "Decide if the local random generator is to be used" };
92 this,
"SleepFraction", 0.0,
93 "Fraction of time, between 0 and 1, when an algorithm is actually sleeping instead of crunching" };
101 using CHM = tbb::concurrent_hash_map<std::string, unsigned int>;
112#define ON_DEBUG if ( msgLevel( MSG::DEBUG ) )
113#define DEBUG_MSG ON_DEBUG debug()
115#define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) )
116#define VERBOSE_MSG ON_VERBOSE verbose()
126 CHM::accessor name_ninstances;
128 name_ninstances->second += 1;
139 if ( !sc )
return sc;
143 fatal() <<
"unable to acquire CPUCruncSvc" <<
endmsg;
177 VERBOSE_MSG <<
"found late-attributed output: " << outputHandle->objKey() <<
endmsg;
209 auto getGausRandom = [](
double mean,
double sigma ) ->
double {
210 unsigned int seed = std::clock();
212 auto getUnifRandom = [](
unsigned int& seed ) ->
double {
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;
224 unif1 = getUnifRandom( seed );
225 unif2 = getUnifRandom( seed );
228 const double normal =
sqrt( -2. *
log( unif1 ) ) *
cos( 2 * M_PI * unif2 );
230 return normal * sigma + mean;
238 crunchtime =
std::abs( rndmgaus() );
240 unsigned int crunchtime_ms = 1000 * crunchtime;
249 tbb::tick_count starttbb = tbb::tick_count::now();
251 DEBUG_MSG <<
"Crunching time will be: " << crunchtime_ms <<
" ms" <<
endmsg;
258 if ( !inputHandle->isValid() )
continue;
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;
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;
282 if ( !outputHandle->isValid() )
continue;
285 outputHandle->put( std::make_unique<DataObject>() );
288 tbb::tick_count endtbb = tbb::tick_count::now();
289 const double actualRuntime = ( endtbb - starttbb ).
seconds();
291 DEBUG_MSG <<
"Finish event " << context.
evt() <<
" in " << int( 1000 * actualRuntime ) <<
" ms" <<
endmsg;
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;
308 unsigned int ninstances;
311 CHM::const_accessor const_name_ninstances;
313 ninstances = const_name_ninstances->second;
316 constexpr double s2ms = 1000.;
318 if ( ninstances != 0 ) {
319 info() <<
"Summary: name= " <<
name() <<
"\t avg_runtime= " <<
m_avg_runtime * s2ms <<
"\t n_clones= " << ninstances
322 CHM::accessor name_ninstances;
324 name_ninstances->second = 0;
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
#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
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)
const DataObjIDColl & outputDataObjs() const override
void initDataHandleHolder()
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
A DataObject is the base class of any identifiable object on any data store.
This class represents an entry point to all the event specific data.
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
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.
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...
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.
Parameters for the Gauss random number generation.
Small smart pointer class with automatic reference counting for IInterface.
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
GAUDI_API const EventContext & currentContext()