The Gaudi Framework  v29r0 (ff2e7097)
SequencerTimerTool.cpp
Go to the documentation of this file.
1 // Include files
2 
3 // From ROOT
4 #include "TH1D.h"
5 
6 // from Gaudi
9 #include "GaudiUtils/Aida2ROOT.h"
10 
11 // local
12 #include "SequencerTimerTool.h"
13 
14 //-----------------------------------------------------------------------------
15 // Implementation file for class : SequencerTimerTool
16 //
17 // 2004-05-19 : Olivier Callot
18 //-----------------------------------------------------------------------------
19 
20 // Declaration of the Tool Factory
22 
23 //=============================================================================
24 // Standard constructor, initializes variables
25 //=============================================================================
26 SequencerTimerTool::SequencerTimerTool( const std::string& type, const std::string& name, const IInterface* parent )
27  : GaudiHistoTool( type, name, parent )
28 {
29  declareInterface<ISequencerTimerTool>( this );
30 
31  // Histograms are disabled by default in this tool.
32  setProperty( "HistoProduce", false ).ignore();
33 }
34 //=========================================================================
35 //
36 //=========================================================================
38 {
40  if ( sc.isFailure() ) return sc;
41  double sum = 0;
42  TimerForSequencer norm( "normalize", m_headerSize, m_normFactor );
43  norm.start();
44  IRndmGenSvc* rsvc = svc<IRndmGenSvc>( "RndmGenSvc", true );
45  { // Use dummy loop suggested by Vanya Belyaev:
47  gauss.initialize( rsvc, Rndm::Gauss( 0., 1.0 ) ).ignore();
48  unsigned int shots = m_shots;
49  while ( 0 < --shots ) {
50  sum += gauss() * sum;
51  }
52  }
53  norm.stop();
54  double time = norm.lastCpu();
55  m_speedRatio = 1. / time;
56  info() << "This machine has a speed about " << format( "%6.2f", 1000. * m_speedRatio )
57  << " times the speed of a 2.8 GHz Xeon." << endmsg;
59  return sc;
60 }
61 
62 //=========================================================================
63 // Finalize : Report timers
64 //=========================================================================
66 {
67 
68  std::string line( m_headerSize + 68, '-' );
69  info() << line << endmsg << "This machine has a speed about " << format( "%6.2f", 1000. * m_speedRatio )
70  << " times the speed of a 2.8 GHz Xeon.";
71  if ( m_normalised ) info() << " *** All times are renormalized ***";
73 
74  std::string lastName;
75  for ( const auto& timr : m_timerList ) {
76  if ( lastName == timr.name() ) continue; // suppress duplicate
77  lastName = timr.name();
78  info() << timr << endmsg;
79  }
80  info() << line << endmsg;
81 
82  return GaudiHistoTool::finalize();
83 }
84 
85 //=========================================================================
86 // Return the index of a specified name. Trailing and leading spaces ignored
87 //=========================================================================
89 {
90  auto beg = name.find_first_not_of( " \t" );
91  auto end = name.find_last_not_of( " \t" );
92  auto temp = name.substr( beg, end - beg + 1 );
93  auto i = std::find_if( std::begin( m_timerList ), std::end( m_timerList ), [&]( const TimerForSequencer& timer ) {
94  beg = timer.name().find_first_not_of( " \t" );
95  end = timer.name().find_last_not_of( " \t" );
96  return timer.name().compare( beg, end - beg + 1, temp ) == 0;
97  } );
98  return i != std::end( m_timerList ) ? std::distance( std::begin( m_timerList ), i ) : -1;
99 }
100 
101 //=========================================================================
102 // Build and save the histograms
103 //=========================================================================
105 {
106  if ( produceHistos() ) {
107  info() << "Saving Timing histograms" << endmsg;
108  const size_t bins = m_timerList.size();
109  AIDA::IHistogram1D* histoTime = book( "ElapsedTime", 0, bins, bins );
110  AIDA::IHistogram1D* histoCPU = book( "CPUTime", 0, bins, bins );
111  AIDA::IHistogram1D* histoCount = book( "Count", 0, bins, bins );
112  TH1D* tHtime = Gaudi::Utils::Aida2ROOT::aida2root( histoTime );
113  TH1D* tHCPU = Gaudi::Utils::Aida2ROOT::aida2root( histoCPU );
114  TH1D* tHCount = Gaudi::Utils::Aida2ROOT::aida2root( histoCount );
115  for ( const auto& tfsq : m_timerList ) {
116  tHtime->Fill( tfsq.name().c_str(), tfsq.elapsedTotal() );
117  tHCPU->Fill( tfsq.name().c_str(), tfsq.cpuTotal() );
118  tHCount->Fill( tfsq.name().c_str(), tfsq.count() );
119  }
120  }
121 }
122 
123 //=============================================================================
124 // Add a timer
125 //=============================================================================
127 {
128  std::string myName( 2 * m_indent, ' ' );
129  myName += name;
130  if ( myName.size() < m_headerSize ) {
131  myName += std::string( m_headerSize - myName.size(), ' ' );
132  }
134  return m_timerList.size() - 1;
135 }
136 
137 //=============================================================================
StatusCode initialize() override
initialize method, to compute the normalization factor
StatusCode setProperty(IProperty *component, const std::string &name, const TYPE &value, const std::string &doc)
simple function to set the property of the given object from the value
Definition: Property.h:1173
Auxiliary class.
static std::string header(std::string::size_type size)
header matching the previous format
T distance(T...args)
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
T find_first_not_of(T...args)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Gaudi::Property< bool > m_normalised
StatusCode finalize() override
standard finalization method
double sum(double x, double y, double z)
uint64_t stop()
Stop time measurement and return the last elapsed time.
STL namespace.
T end(T...args)
double lastCpu() const
returns the last measured time
Parameters for the Gauss random number generation.
void start()
Start a time measurement.
int indexByName(const std::string &name) override
returns the index of the counter with that name, or -1
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
Simple class to extend the functionality of class GaudiTool.
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:33
STL class.
Gaudi::Property< int > m_shots
void saveHistograms() override
prepares and saves the timing histograms
Random number accessor This small class encapsulates the use of the random number generator...
Random Generator service interface definition Definition of a interface for a service to access rando...
Definition: IRndmGenSvc.h:35
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
Definition of the basic interface.
Definition: IInterface.h:277
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:64
static TH1D * aida2root(AIDA::IHistogram1D *aida)
get the underlying pointer for 1D-histogram
Definition: Aida2ROOT.cpp:73
AIDA::IHistogram1D * book(const std::string &title, const double low=0, const double high=100, const unsigned long bins=100) const
book the 1D histogram
Definition: GaudiHistos.h:1865
int addTimer(const std::string &name) override
add a timer entry with the specified name
T move(T...args)
T find_last_not_of(T...args)
int m_indent
Amount of indentation.
T find_if(T...args)
T size(T...args)
T begin(T...args)
constexpr double gauss
StatusCode initialize() override
standard initialization method
bool produceHistos() const
get the flag for histogram production (property "HistoProduce")
Definition: GaudiHistos.h:2396
Implements the time measurement inside a sequencer.
const std::string & name() const
returns the name
std::vector< TimerForSequencer > m_timerList
T substr(T...args)
virtual StatusCode initialize(const SmartIF< IRndmGenSvc > &svc, const IRndmGen::Param &par)
Initialization.
void ignore() const
Definition: StatusCode.h:109
Gaudi::Property< std::string::size_type > m_headerSize
StatusCode finalize() override
finalize method, to print the time summary table
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
virtual const std::string & name() const =0
Retrieve the name of the instance.
T compare(T...args)
double m_normFactor
Factor to convert to standard CPU (1 GHz PIII)
T emplace_back(T...args)