All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 //=============================================================================
27  const std::string& name,
28  const IInterface* parent )
29  : GaudiHistoTool ( type, name , parent )
30  , m_indent( 0 )
31  , m_normFactor( 0.001 )
32  , m_speedRatio(0)
33 {
35 
36  m_shots = 3500000 ; // 1s on 2.8GHz Xeon, gcc 3.2, -o2
37  declareProperty( "shots" , m_shots );
38  declareProperty( "Normalised" , m_normalised = false );
39  declareProperty( "GlobalTiming" , m_globalTiming = false );
40  declareProperty( "NameSize" , m_headerSize = 30,
41  "Number of characters to be used in algorithm name column" );
42  // Histograms are disabled by default in this tool.
43  setProperty("HistoProduce", false).ignore();
44 }
45 //=============================================================================
46 // Destructor
47 //=============================================================================
49 
50 
51 //=========================================================================
52 //
53 //=========================================================================
55 {
57  if ( sc.isFailure() ) return sc;
58  double sum = 0;
59  TimerForSequencer norm( "normalize", m_headerSize, m_normFactor );
60  norm.start();
61  IRndmGenSvc* rsvc = svc<IRndmGenSvc>( "RndmGenSvc", true );
62  { // Use dummy loop suggested by Vanya Belyaev:
63  Rndm::Numbers gauss;
64  gauss.initialize( rsvc , Rndm::Gauss(0.,1.0) ).ignore();
65  unsigned int shots = m_shots;
66  while( 0 < --shots ) { sum += gauss() * sum ; }
67  }
68  norm.stop();
69  double time = norm.lastCpu();
70  m_speedRatio = 1./time;
71  info() << "This machine has a speed about "
72  << format( "%6.2f", 1000.*m_speedRatio)
73  << " times the speed of a 2.8 GHz Xeon." << endmsg ;
74  if ( m_normalised ) {
76  }
77  return sc;
78 }
79 
80 //=========================================================================
81 // Finalize : Report timers
82 //=========================================================================
84 {
85 
86  std::string line(m_headerSize + 68, '-');
87  info() << line << endmsg
88  << "This machine has a speed about "
89  << format( "%6.2f", 1000.*m_speedRatio)
90  << " times the speed of a 2.8 GHz Xeon.";
91  if ( m_normalised ) info() <<" *** All times are renormalized ***";
93  << line << endmsg;
94 
95  std::string lastName = "";
96  for ( unsigned int kk=0 ; m_timerList.size() > kk ; kk++ )
97  {
98  if ( lastName == m_timerList[kk].name() ) continue; // suppress duplicate
99  lastName = m_timerList[kk].name();
100  info() << m_timerList[kk] << endmsg;
101  }
102  info() << line << endmsg;
103 
104  return GaudiHistoTool::finalize();
105 }
106 
107 //=========================================================================
108 // Return the index of a specified name. Trailing and leading spaces ignored
109 //=========================================================================
110 int SequencerTimerTool::indexByName ( const std::string& name )
111 {
112  std::string::size_type beg = name.find_first_not_of(" \t");
113  std::string::size_type end = name.find_last_not_of(" \t");
114  std::string temp = name.substr( beg, end-beg+1 );
115  for ( unsigned int kk=0 ; m_timerList.size() > kk ; kk++ ) {
116  beg = m_timerList[kk].name().find_first_not_of(" \t");
117  end = m_timerList[kk].name().find_last_not_of(" \t");
118  if ( m_timerList[kk].name().substr(beg,end-beg+1) == temp ) return kk;
119  }
120  return -1;
121 }
122 
123 //=========================================================================
124 // Build and save the histograms
125 //=========================================================================
127 {
128  if ( produceHistos() )
129  {
130  info() << "Saving Timing histograms" << endmsg;
131  const size_t bins = m_timerList.size();
132  AIDA::IHistogram1D* histoTime = book("ElapsedTime", 0, bins, bins);
133  AIDA::IHistogram1D* histoCPU = book("CPUTime", 0, bins, bins);
134  AIDA::IHistogram1D* histoCount = book("Count", 0, bins, bins);
135  TH1D* tHtime = Gaudi::Utils::Aida2ROOT::aida2root(histoTime);
136  TH1D* tHCPU = Gaudi::Utils::Aida2ROOT::aida2root(histoCPU);
137  TH1D* tHCount = Gaudi::Utils::Aida2ROOT::aida2root(histoCount);
138  for ( size_t kk = 0 ; bins > kk ; kk++ )
139  {
140  TimerForSequencer &tfsq = m_timerList[kk];
141  tHtime->Fill(tfsq.name().c_str(), tfsq.elapsedTotal());
142  tHCPU->Fill(tfsq.name().c_str(), tfsq.cpuTotal());
143  tHCount->Fill(tfsq.name().c_str(), tfsq.count());
144  }
145  }
146 }
147 
148 //=============================================================================
149 // Add a timer
150 //=============================================================================
151 int SequencerTimerTool::addTimer( const std::string& name )
152 {
153  std::string myName;
154  if ( 0 < m_indent )
155  {
156  const std::string prefix( m_indent, ' ' );
157  myName += prefix;
158  }
159  myName += name;
160  if ( myName.size() < m_headerSize )
161  {
162  const std::string space( m_headerSize - myName.size(), ' ' );
163  myName += space ;
164  }
165 
166  //myName = myName.substr( 0, m_headerSize );
167 
168  m_timerList.push_back( TimerForSequencer( myName,
169  m_headerSize,
170  m_normFactor ) );
171 
172  return m_timerList.size() - 1;
173 }
174 
175 //=============================================================================
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:1212
Auxilliary class.
MsgStream & info() const
shortcut for the method msgStream ( MSG::INFO )
Definition: GaudiCommon.h:497
static std::string header(std::string::size_type size)
header matching the previous format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:133
virtual StatusCode initialize()
standard initialization method
double sum(double x, double y, double z)
int indexByName(const std::string &name)
returns the index of the counter with that name, or -1
double count() const
Returns the number run count.
virtual StatusCode initialize()
initialize method, to compute the normalization factor
int m_shots
Number of shots for CPU normalization.
double lastCpu() const
returns the last measured time
Parameters for the Gauss random number generation.
void start()
Start a time measurement.
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:85
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
virtual const std::string & name() const =0
Retrieve the name of the instance.
double stop()
Stop time measurement and return the last elapsed time.
Random number accessor This small class encapsulates the use of the random number generator...
string type
Definition: gaudirun.py:126
std::string::size_type m_headerSize
Size of the name field.
Random Generator service interface definition Definition of a interface for a service to access rando...
Definition: IRndmGenSvc.h:34
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
Definition of the basic interface.
Definition: IInterface.h:160
Simple class to extend the functionality of class GaudiTool.
static TH1D * aida2root(AIDA::IHistogram1D *aida)
get the underlying pointer for 1D-histogram
Definition: Aida2ROOT.cpp:55
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:2031
tuple end
Definition: IOTest.py:101
virtual void saveHistograms()
prepares and saves the timing histograms
int m_indent
Amount of indentation.
virtual ~SequencerTimerTool()
Destructor.
double elapsedTotal() const
returns the total elapsed time
virtual StatusCode finalize()
standard finalization method
double cpuTotal() const
returns the total cpu time
bool produceHistos() const
get the flag for histogram production (property "HistoProduce")
Definition: GaudiHistos.h:2693
Implements the time measurement inside a sequencer.
const std::string & name() const
returns the name
std::vector< TimerForSequencer > m_timerList
bool m_normalised
Is the time scaled to a nominal PIII ?
virtual StatusCode initialize(const SmartIF< IRndmGenSvc > &svc, const IRndmGen::Param &par)
Initialization.
void ignore() const
Definition: StatusCode.h:107
virtual int addTimer(const std::string &name)
add a timer entry with the specified name
virtual StatusCode finalize()
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:244
int line
Definition: ana.py:50
double m_normFactor
Factor to convert to standard CPU (1 GHz PIII)