The Gaudi Framework  v33r1 (b1225454)
SequencerTimerTool.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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 // Include files
12 
13 // From ROOT
14 #include "TH1D.h"
15 
16 // from Gaudi
19 #include "GaudiUtils/Aida2ROOT.h"
20 
21 // local
22 #include "SequencerTimerTool.h"
23 
24 //-----------------------------------------------------------------------------
25 // Implementation file for class : SequencerTimerTool
26 //
27 // 2004-05-19 : Olivier Callot
28 //-----------------------------------------------------------------------------
29 
30 // Declaration of the Tool Factory
32 
33 //=============================================================================
34 // Standard constructor, initializes variables
35 //=============================================================================
36 SequencerTimerTool::SequencerTimerTool( const std::string& type, const std::string& name, const IInterface* parent )
38  declareInterface<ISequencerTimerTool>( this );
39 
40  // Histograms are disabled by default in this tool.
41  setProperty( "HistoProduce", false ).ignore();
42 }
43 //=========================================================================
44 //
45 //=========================================================================
48  if ( sc.isFailure() ) return sc;
49  double sum = 0;
50  TimerForSequencer norm( "normalize", m_headerSize, m_normFactor );
51  norm.start();
52  IRndmGenSvc* rsvc = svc<IRndmGenSvc>( "RndmGenSvc", true );
53  { // Use dummy loop suggested by Vanya Belyaev:
55  gauss.initialize( rsvc, Rndm::Gauss( 0., 1.0 ) ).ignore();
56  unsigned int shots = m_shots;
57  while ( 0 < --shots ) { sum += gauss() * sum; }
58  }
59  norm.stop();
60  double time = norm.lastCpu();
61  m_speedRatio = 1. / time;
62  info() << "This machine has a speed about " << format( "%6.2f", 1000. * m_speedRatio )
63  << " times the speed of a 2.8 GHz Xeon." << endmsg;
65  return sc;
66 }
67 
68 //=========================================================================
69 // Finalize : Report timers
70 //=========================================================================
72 
73  if ( !m_timerList.empty() ) {
74  std::string line( m_headerSize + 68, '-' );
75  info() << line << endmsg << "This machine has a speed about " << format( "%6.2f", 1000. * m_speedRatio )
76  << " times the speed of a 2.8 GHz Xeon.";
77  if ( m_normalised ) info() << " *** All times are renormalized ***";
79 
80  std::string lastName;
81  for ( const auto& timr : m_timerList ) {
82  if ( lastName == timr.name() ) continue; // suppress duplicate
83  lastName = timr.name();
84  info() << timr << endmsg;
85  }
86  info() << line << endmsg;
87  }
88  return GaudiHistoTool::finalize();
89 }
90 
91 //=========================================================================
92 // Return the index of a specified name. Trailing and leading spaces ignored
93 //=========================================================================
95  auto beg = name.find_first_not_of( " \t" );
96  auto end = name.find_last_not_of( " \t" );
97  auto temp = name.substr( beg, end - beg + 1 );
98  auto i = std::find_if( std::begin( m_timerList ), std::end( m_timerList ), [&]( const TimerForSequencer& timer ) {
99  beg = timer.name().find_first_not_of( " \t" );
100  end = timer.name().find_last_not_of( " \t" );
101  return timer.name().compare( beg, end - beg + 1, temp ) == 0;
102  } );
103  return i != std::end( m_timerList ) ? std::distance( std::begin( m_timerList ), i ) : -1;
104 }
105 
106 //=========================================================================
107 // Build and save the histograms
108 //=========================================================================
110  if ( produceHistos() ) {
111  info() << "Saving Timing histograms" << endmsg;
112  const size_t bins = m_timerList.size();
113  AIDA::IHistogram1D* histoTime = book( "ElapsedTime", 0, bins, bins );
114  AIDA::IHistogram1D* histoCPU = book( "CPUTime", 0, bins, bins );
115  AIDA::IHistogram1D* histoCount = book( "Count", 0, bins, bins );
116  TH1D* tHtime = Gaudi::Utils::Aida2ROOT::aida2root( histoTime );
117  TH1D* tHCPU = Gaudi::Utils::Aida2ROOT::aida2root( histoCPU );
118  TH1D* tHCount = Gaudi::Utils::Aida2ROOT::aida2root( histoCount );
119  for ( const auto& tfsq : m_timerList ) {
120  tHtime->Fill( tfsq.name().c_str(), tfsq.elapsedTotal() );
121  tHCPU->Fill( tfsq.name().c_str(), tfsq.cpuTotal() );
122  tHCount->Fill( tfsq.name().c_str(), tfsq.count() );
123  }
124  }
125 }
126 
127 //=============================================================================
128 // Add a timer
129 //=============================================================================
131  std::string myName( 2 * m_indent, ' ' );
132  myName += name;
133  if ( myName.size() < m_headerSize ) { myName += std::string( m_headerSize - myName.size(), ' ' ); }
135  return m_timerList.size() - 1;
136 }
137 
138 //=============================================================================
StatusCode initialize() override
initialize method, to compute the normalization factor
T empty(T... args)
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:119
T find_first_not_of(T... args)
double lastCpu() const
returns the last measured time
Gaudi::Property< bool > m_normalised
StatusCode finalize() override
standard finalization method
uint64_t stop()
Stop time measurement and return the last elapsed time.
const std::string & name() const
returns the name
STL namespace.
T end(T... args)
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
Simple class to extend the functionality of class GaudiTool.
virtual const std::string & name() const =0
Retrieve the name of the instance.
STL class.
#define DECLARE_COMPONENT(type)
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:45
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
Definition of the basic interface.
Definition: IInterface.h:254
bool PyHelper() setProperty(IInterface *p, char *name, char *value)
Definition: Bootstrap.cpp:243
static TH1D * aida2root(AIDA::IHistogram1D *aida)
get the underlying pointer for 1D-histogram
Definition: Aida2ROOT.cpp:80
def end
Definition: IOTest.py:123
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)
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:1859
constexpr double gauss
StatusCode initialize() override
standard initialization method
Implements the time measurement inside a sequencer.
std::vector< TimerForSequencer > m_timerList
const Gaudi::Algorithm & parent
T substr(T... args)
bool isFailure() const
Definition: StatusCode.h:145
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:202
bool produceHistos() const
get the flag for histogram production (property "HistoProduce")
Definition: GaudiHistos.h:2381
T compare(T... args)
double m_normFactor
Factor to convert to standard CPU (1 GHz PIII)
T emplace_back(T... args)