The Gaudi Framework  v33r0 (d5ea422b)
CPUCrunchSvc Class Reference

#include <src/CPUCrunchSvc/CPUCrunchSvc.h>

Inheritance diagram for CPUCrunchSvc:
Collaboration diagram for CPUCrunchSvc:

Public Member Functions

 CPUCrunchSvc (const std::string &name, ISvcLocator *svc)
 
virtual StatusCode initialize () override
 
std::chrono::milliseconds crunch_for (const std::chrono::milliseconds &crunchtime) const override
 
- Public Member Functions inherited from extends< Service, ICPUCrunchSvc >
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 Service
const std::stringname () const override
 Retrieve name of the service. More...
 
StatusCode configure () override
 
StatusCode initialize () override
 
StatusCode start () override
 
StatusCode stop () override
 
StatusCode finalize () override
 
StatusCode terminate () override
 
Gaudi::StateMachine::State FSMState () const override
 
Gaudi::StateMachine::State targetFSMState () const override
 
StatusCode reinitialize () override
 
StatusCode restart () override
 
StatusCode sysInitialize () override
 Initialize Service. More...
 
StatusCode sysStart () override
 Initialize Service. More...
 
StatusCode sysStop () override
 Initialize Service. More...
 
StatusCode sysFinalize () override
 Finalize Service. More...
 
StatusCode sysReinitialize () override
 Re-initialize the Service. More...
 
StatusCode sysRestart () override
 Re-initialize the Service. More...
 
 Service (std::string name, ISvcLocator *svcloc)
 Standard Constructor. More...
 
SmartIF< ISvcLocator > & serviceLocator () const override
 Retrieve pointer to service locator. More...
 
StatusCode setProperties ()
 Method for setting declared properties to the values specified for the job. More...
 
template<class T >
StatusCode service (const std::string &name, const T *&psvc, bool createIf=true) const
 Access a service by name, creating it if it doesn't already exist. More...
 
template<class T >
StatusCode service (const std::string &name, T *&psvc, bool createIf=true) const
 
template<typename IFace = IService>
SmartIF< IFace > service (const std::string &name, bool createIf=true) const
 
template<class T >
StatusCode service (const std::string &svcType, const std::string &svcName, T *&psvc) const
 Access a service by name and type, creating it if it doesn't already exist. More...
 
template<class T >
StatusCode declareTool (ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
 Declare used tool. More...
 
SmartIF< IAuditorSvc > & auditorSvc () const
 The standard auditor service.May not be invoked before sysInitialize() has been invoked. More...
 
- Public Member Functions inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
 PropertyHolder ()=default
 
Gaudi::Details::PropertyBasedeclareProperty (Gaudi::Details::PropertyBase &prop)
 Declare a property. More...
 
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, TYPE &value, const std::string &doc="none")
 Helper to wrap a regular data member and use it as a regular property. More...
 
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, Gaudi::Property< TYPE, VERIFIER, HANDLERS > &prop, const std::string &doc="none")
 Declare a PropertyBase instance setting name and documentation. More...
 
Gaudi::Details::PropertyBasedeclareRemoteProperty (const std::string &name, IProperty *rsvc, const std::string &rname="")
 Declare a remote property. More...
 
StatusCode setProperty (const Gaudi::Details::PropertyBase &p) override
 set the property form another property More...
 
StatusCode setProperty (const std::string &s) override
 set the property from the formatted string More...
 
StatusCode setProperty (const std::string &n, const std::string &v) override
 set the property from name and the value More...
 
StatusCode setProperty (const std::string &name, const TYPE &value)
 set the property form the value More...
 
StatusCode getProperty (Gaudi::Details::PropertyBase *p) const override
 get the property More...
 
const Gaudi::Details::PropertyBasegetProperty (const std::string &name) const override
 get the property by name More...
 
StatusCode getProperty (const std::string &n, std::string &v) const override
 convert the property to the string More...
 
const std::vector< Gaudi::Details::PropertyBase * > & getProperties () const override
 get all properties More...
 
bool hasProperty (const std::string &name) const override
 Return true if we have a property with the given name. More...
 
 PropertyHolder (const PropertyHolder &)=delete
 
PropertyHolderoperator= (const PropertyHolder &)=delete
 
- Public Member Functions inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
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...
 
MSG::Level outputLevel () const
 Backward compatibility function for getting the output level. More...
 
- Public Member Functions inherited from CommonMessagingBase
virtual ~CommonMessagingBase ()=default
 Virtual destructor. More...
 
const SmartIF< IMessageSvc > & msgSvc () const
 The standard message service. More...
 
MsgStreammsgStream () const
 Return an uninitialized MsgStream. More...
 
MsgStreammsgStream (const MSG::Level level) const
 Predefined configurable message stream for the efficient printouts. More...
 
MsgStreamalways () const
 shortcut for the method msgStream(MSG::ALWAYS) More...
 
MsgStreamfatal () const
 shortcut for the method msgStream(MSG::FATAL) More...
 
MsgStreamerr () const
 shortcut for the method msgStream(MSG::ERROR) More...
 
MsgStreamerror () const
 shortcut for the method msgStream(MSG::ERROR) More...
 
MsgStreamwarning () const
 shortcut for the method msgStream(MSG::WARNING) More...
 
MsgStreaminfo () const
 shortcut for the method msgStream(MSG::INFO) More...
 
MsgStreamdebug () const
 shortcut for the method msgStream(MSG::DEBUG) More...
 
MsgStreamverbose () const
 shortcut for the method msgStream(MSG::VERBOSE) More...
 
MsgStreammsg () const
 shortcut for the method msgStream(MSG::INFO) More...
 

Private Member Functions

void calibrate ()
 
unsigned int getNCaliIters (std::chrono::microseconds runtime) const
 
void findPrimes (unsigned int) const
 

Private Attributes

std::vector< unsigned int > m_times_vect
 
Gaudi::Property< std::vector< unsigned int > > m_niters_vect
 
Gaudi::Property< bool > m_shortCalib {this, "shortCalib", false, "Enable coarse grained calibration"}
 
Gaudi::Property< unsigned int > m_minCalibTime
 
Gaudi::Property< int > m_numCalibRuns {this, "numCalibRuns", 1, "Number of times to do calibration run"}
 
Gaudi::Property< float > m_corrFact
 

Additional Inherited Members

- Public Types inherited from extends< Service, ICPUCrunchSvc >
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 Service
using Factory = Gaudi::PluginService::Factory< IService *(const std::string &, ISvcLocator *)>
 
- Public Types inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
using PropertyHolderImpl = PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
 Typedef used to refer to this class from derived classes, as in. More...
 
- Public Types inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
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...
 
- Protected Member Functions inherited from Service
 ~Service () override
 Standard Destructor. More...
 
int outputLevel () const
 get the Service's output level More...
 
- Protected Member Functions inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
Gaudi::Details::PropertyBaseproperty (const std::string &name) const
 
- Protected Member Functions inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
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 Service
Gaudi::StateMachine::State m_state = Gaudi::StateMachine::OFFLINE
 Service state. More...
 
Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::OFFLINE
 Service state. More...
 
Gaudi::Property< int > m_outputLevel {this, "OutputLevel", MSG::NIL, "output level"}
 
Gaudi::Property< bool > m_auditInit {this, "AuditServices", false, " unused"}
 
Gaudi::Property< bool > m_auditorInitialize {this, "AuditInitialize", false, "trigger auditor on initialize()"}
 
Gaudi::Property< bool > m_auditorStart {this, "AuditStart", false, "trigger auditor on start()"}
 
Gaudi::Property< bool > m_auditorStop {this, "AuditStop", false, "trigger auditor on stop()"}
 
Gaudi::Property< bool > m_auditorFinalize {this, "AuditFinalize", false, "trigger auditor on finalize()"}
 
Gaudi::Property< bool > m_auditorReinitialize {this, "AuditReinitialize", false, "trigger auditor on reinitialize()"}
 
Gaudi::Property< bool > m_auditorRestart {this, "AuditRestart", false, "trigger auditor on restart()"}
 
SmartIF< IAuditorSvcm_pAuditorSvc
 Auditor Service. More...
 

Detailed Description

Definition at line 23 of file CPUCrunchSvc.h.

Constructor & Destructor Documentation

◆ CPUCrunchSvc()

CPUCrunchSvc::CPUCrunchSvc ( const std::string name,
ISvcLocator svc 
)

Definition at line 29 of file CPUCrunchSvc.cpp.

29 : base_class( name, svc ) {}
extends base_class
Typedef to this class.
Definition: extends.h:24
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:284

Member Function Documentation

◆ calibrate()

void CPUCrunchSvc::calibrate ( )
private

Definition at line 52 of file CPUCrunchSvc.cpp.

52  {
53  if ( m_niters_vect.value().size() == 0 ) {
54  m_niters_vect = {0, 500, 600, 700, 800, 1000, 1300, 1600, 2000, 2300,
55  2600, 3000, 3300, 3500, 3900, 4200, 5000, 6000, 8000, 10000,
56  12000, 15000, 17000, 20000, 25000, 30000, 35000, 40000, 50000, 60000};
57  if ( !m_shortCalib ) {
58  m_niters_vect.value().push_back( 80000 );
59  m_niters_vect.value().push_back( 100000 );
60  m_niters_vect.value().push_back( 150000 );
61  m_niters_vect.value().push_back( 200000 );
62  }
63  }
64 
65  if ( m_niters_vect.value().at( 0 ) != 0 ) {
66  warning() << "NIterationsVect[0]= " << m_niters_vect.value().at( 0 ) << " but needs to be zero. resetting it."
67  << endmsg;
68  m_niters_vect.value().at( 0 ) = 0;
69  }
70 
71  m_times_vect.resize( m_niters_vect.value().size() );
72  m_times_vect.at( 0 ) = 0;
73 
74  const unsigned int minCalibTime_us = m_minCalibTime * 1000;
75 
76  // warm it up by doing 20k iterations
77  findPrimes( 20000 );
78 
79  for ( int irun = 0; irun < m_numCalibRuns; ++irun ) {
80 
81  debug() << "Starting calibration run " << irun + 1 << " ..." << endmsg;
82  for ( unsigned int i = 1; i < m_niters_vect.value().size(); ++i ) {
83  unsigned int niters = m_niters_vect.value().at( i );
84  unsigned int trials = 30;
85  do {
86  auto start_cali = tbb::tick_count::now();
87  findPrimes( niters );
88  auto stop_cali = tbb::tick_count::now();
89  auto deltat = ( stop_cali - start_cali ).seconds();
90  m_times_vect.at( i ) = deltat * 1000000; // in microseconds
91  debug() << " Calibration: # iters = " << niters << " => " << m_times_vect.at( i ) << " us" << endmsg;
92  trials--;
93  } while ( trials > 0 && m_times_vect.at( i ) < m_times_vect.at( i - 1 ) ); // make sure that they are monotonic
94 
95  if ( i == m_niters_vect.value().size() - 1 && minCalibTime_us != 0 ) {
96  if ( m_times_vect.at( i ) < minCalibTime_us ) {
97  debug() << " increasing calib vect with " << int( m_niters_vect.value().back() * 1.2 )
98  << " iterations to reach min calib time of " << m_minCalibTime.value() << " ms " << endmsg;
99  m_niters_vect.value().push_back( int( m_niters_vect.value().back() * 1.2 ) );
100  m_times_vect.push_back( 0. );
101  }
102  }
103  }
104  }
105  if ( m_corrFact != 1. ) {
106  debug() << "Adjusting times with correction factor " << m_corrFact.value() << endmsg;
107  for ( auto& t : m_times_vect ) { t = t * m_corrFact; }
108  }
109  debug() << "Calibration finished!" << endmsg;
110 }
Gaudi::Property< unsigned int > m_minCalibTime
Definition: CPUCrunchSvc.h:45
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
T resize(T... args)
T at(T... args)
T push_back(T... args)
Gaudi::Property< bool > m_shortCalib
Definition: CPUCrunchSvc.h:44
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Gaudi::Property< float > m_corrFact
Definition: CPUCrunchSvc.h:48
Gaudi::Property< int > m_numCalibRuns
Definition: CPUCrunchSvc.h:47
void findPrimes(unsigned int) const
std::vector< unsigned int > m_times_vect
Definition: CPUCrunchSvc.h:40
Gaudi::Property< std::vector< unsigned int > > m_niters_vect
Definition: CPUCrunchSvc.h:42
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202

◆ crunch_for()

std::chrono::milliseconds CPUCrunchSvc::crunch_for ( const std::chrono::milliseconds crunchtime) const
override

Definition at line 204 of file CPUCrunchSvc.cpp.

204  {
205 
206  const unsigned int niters = getNCaliIters( crunchtime );
207 
208  auto start_cali = tbb::tick_count::now();
209  findPrimes( niters );
210  auto stop_cali = tbb::tick_count::now();
211 
212  std::chrono::milliseconds actual( int( 1000 * ( stop_cali - start_cali ).seconds() ) );
213 
214  DEBUG_MSG << "crunch for " << crunchtime.count() << " ms == " << niters << " iter. actual time: " << actual.count()
215  << " ms. ratio: " << float( actual.count() ) / crunchtime.count() << endmsg;
216 
217  return actual;
218 }
unsigned int getNCaliIters(std::chrono::microseconds runtime) const
void findPrimes(unsigned int) const
#define DEBUG_MSG
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202

◆ findPrimes()

void CPUCrunchSvc::findPrimes ( unsigned int  n_iterations) const
private

Definition at line 152 of file CPUCrunchSvc.cpp.

152  {
153  // Flag to trigger the allocation
154  bool is_prime;
155 
156  // Let's prepare the material for the allocations
157  unsigned int primes_size = 1;
158  unsigned long* primes = new unsigned long[primes_size];
159  primes[0] = 2;
160 
161  unsigned long i = 2;
162 
163  // Loop on numbers
164  for ( unsigned long int iiter = 0; iiter < n_iterations; iiter++ ) {
165  // Once at max, it returns to 0
166  i += 1;
167 
168  // Check if it can be divided by the smaller ones
169  is_prime = true;
170  for ( unsigned long j = 2; j < i && is_prime; ++j ) {
171  if ( i % j == 0 ) is_prime = false;
172  } // end loop on numbers < than tested one
173 
174  if ( is_prime ) {
175  // copy the array of primes (INEFFICIENT ON PURPOSE!)
176  unsigned int new_primes_size = 1 + primes_size;
177  unsigned long* new_primes = new unsigned long[new_primes_size];
178 
179  for ( unsigned int prime_index = 0; prime_index < primes_size; prime_index++ ) {
180  new_primes[prime_index] = primes[prime_index];
181  }
182  // attach the last prime
183  new_primes[primes_size] = i;
184 
185  // Update primes array
186  delete[] primes;
187  primes = new_primes;
188  primes_size = new_primes_size;
189  } // end is prime
190 
191  } // end of while loop
192 
193  // Fool Compiler optimisations:
194  for ( unsigned int prime_index = 0; prime_index < primes_size; prime_index++ )
195  if ( primes[prime_index] == 4 )
196  debug() << "This does never happen, but it's necessary too fool aggressive compiler optimisations!" << endmsg;
197 
198  delete[] primes;
199 }
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202

◆ getNCaliIters()

unsigned int CPUCrunchSvc::getNCaliIters ( std::chrono::microseconds  runtime) const
private

Definition at line 114 of file CPUCrunchSvc.cpp.

114  {
115 
116  unsigned int smaller_i = 0;
117  double time = 0.;
118  bool found = false;
119  double corrRuntime = runtime.count(); // * m_corrFact;
120  // We know that the first entry is 0, so we start to iterate from 1
121  for ( unsigned int i = 1; i < m_times_vect.size(); i++ ) {
122  time = m_times_vect.at( i );
123  if ( time > corrRuntime ) {
124  smaller_i = i - 1;
125  found = true;
126  break;
127  }
128  }
129 
130  // Case 1: we are outside the interpolation range, we take the last 2 points
131  if ( not found ) smaller_i = m_times_vect.size() - 2;
132 
133  // Case 2: we maeke a linear interpolation
134  // y=mx+q
135  const auto x0 = m_times_vect.at( smaller_i );
136  const auto x1 = m_times_vect.at( smaller_i + 1 );
137  const auto y0 = m_niters_vect.value().at( smaller_i );
138  const auto y1 = m_niters_vect.value().at( smaller_i + 1 );
139  const double m = (double)( y1 - y0 ) / (double)( x1 - x0 );
140  const double q = y0 - m * x0;
141 
142  const unsigned int nCaliIters = m * corrRuntime + q;
143 
144  VERBOSE_MSG << "x0: " << x0 << " x1: " << x1 << " y0: " << y0 << " y1: " << y1 << " m: " << m << " q: " << q
145  << " itr: " << nCaliIters << endmsg;
146 
147  return nCaliIters;
148 }
T at(T... args)
#define VERBOSE_MSG
constexpr double m
T size(T... args)
std::vector< unsigned int > m_times_vect
Definition: CPUCrunchSvc.h:40
Gaudi::Property< std::vector< unsigned int > > m_niters_vect
Definition: CPUCrunchSvc.h:42
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202

◆ initialize()

StatusCode CPUCrunchSvc::initialize ( )
overridevirtual

Definition at line 33 of file CPUCrunchSvc.cpp.

33  {
34  debug() << "initialize" << endmsg;
35 
36  if ( base_class::initialize().isFailure() ) {
37  error() << "Error initializing base class" << endmsg;
38  return StatusCode::FAILURE;
39  }
40 
41  calibrate();
42 
43  return StatusCode::SUCCESS;
44 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:96
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
void calibrate()
constexpr static const auto FAILURE
Definition: StatusCode.h:97
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202

Member Data Documentation

◆ m_corrFact

Gaudi::Property<float> CPUCrunchSvc::m_corrFact
private
Initial value:
{this, "calibCorrectionFactor", 1.,
"Calibration correction factor to take into account things like turbo boost"}

Definition at line 48 of file CPUCrunchSvc.h.

◆ m_minCalibTime

Gaudi::Property<unsigned int> CPUCrunchSvc::m_minCalibTime
private
Initial value:
{this, "minCalibTime", 0,
"Minimum number of milliseconds to do calibration"}

Definition at line 45 of file CPUCrunchSvc.h.

◆ m_niters_vect

Gaudi::Property<std::vector<unsigned int> > CPUCrunchSvc::m_niters_vect
private
Initial value:
{
this, "NIterationsVect", {}, "Number of iterations for the calibration."}

Definition at line 42 of file CPUCrunchSvc.h.

◆ m_numCalibRuns

Gaudi::Property<int> CPUCrunchSvc::m_numCalibRuns {this, "numCalibRuns", 1, "Number of times to do calibration run"}
private

Definition at line 47 of file CPUCrunchSvc.h.

◆ m_shortCalib

Gaudi::Property<bool> CPUCrunchSvc::m_shortCalib {this, "shortCalib", false, "Enable coarse grained calibration"}
private

Definition at line 44 of file CPUCrunchSvc.h.

◆ m_times_vect

std::vector<unsigned int> CPUCrunchSvc::m_times_vect
private

Definition at line 40 of file CPUCrunchSvc.h.


The documentation for this class was generated from the following files: