The Gaudi Framework  master (37c0b60a)
TimingAlg.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 <GaudiKernel/Algorithm.h>
12 #include <GaudiKernel/Chrono.h>
15 #include <boost/format.hpp>
16 #include <cmath>
17 
18 namespace GaudiTestSuite {
29  class TimingAlg : public Algorithm {
30  public:
32  StatusCode execute() override;
33  StatusCode finalize() override;
34 
35  protected:
37  double doSomething();
38 
39  private:
41  Gaudi::Property<unsigned long> m_cycles{ this, "Cycles", 10000, "The number of cycles" };
42 
43  private:
47  };
49  double result = 0;
50  Rndm::Numbers gauss( randSvc(), Rndm::Gauss( 0.0, 1.0 ) );
51  for ( unsigned long i = 0; i < m_cycles; ++i ) result += sin( gauss() );
52  return result;
53  }
55  { // make some cpu consuming evaluations:
56  Chrono chrono( chronoSvc(), name() + ": the first loop" );
57  debug() << "the result of the first loop is " << doSomething() << endmsg;
58  }
59  { // make some cpu consuming evaluations:
60  Chrono chrono( name() + ": the second loop", chronoSvc() );
61  debug() << "the result of the second loop is " << doSomething() << endmsg;
62  }
63  { // make some cpu consuming evaluations:
64  Chrono chrono( &m_chrono1 );
65  debug() << "the result of the third loop is " << doSomething() << endmsg;
66  }
67  { // make some cpu consuming evaluations:
68  Chrono chrono( m_chrono2 );
69  debug() << "the result of the fourth loop is " << doSomething() << endmsg;
70  }
71  { // make some cpu consuming evaluations:
72  m_chrono3.start();
73  debug() << "the result of the 5th loop is " << doSomething() << endmsg;
74  m_chrono3.stop();
75  }
76  { // make some cpu consuming evaluations:
77  chronoSvc()->chronoStart( name() + ": the 5th loop" );
78  debug() << "the result of the 5th loop is " << doSomething() << endmsg;
79  chronoSvc()->chronoStop( name() + ": the 5th loop" );
80  }
81  return StatusCode::SUCCESS;
82  }
83 
85  const std::string format = "| %1$-6d| %2$-12.5g| %3$12.5g+-%4$-12.5g|%6$12.5g/%7$-12.5g|";
86  const std::string header = "| |%1$=7.7s|%2$=13.13s|%3$13.13s+-%4$-12.12s|%6$12.12g/%7$-12.12s|";
87 
88  using namespace boost::io;
89  boost::format hdr( header );
90  hdr.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
91 
92  hdr % "#" % "Total" % "Mean" % "RMS" % "ErrMean" % "Min" % "Max";
93 
94  always() << "The timing is (in us)" << std::endl
95  << hdr.str() << std::endl
98  << "| (3U) " << m_chrono3.outputUserTime( format, System::microSec ) << endmsg;
99 
100  return Algorithm::finalize();
101  }
102 
104 } // end of namespace GaudiTestSuite
105 
106 namespace GaudiExamples {
108  using GaudiTestSuite::TimingAlg::TimingAlg;
109  };
111 } // namespace GaudiExamples
GaudiTestSuite::TimingAlg::execute
StatusCode execute() override
Definition: TimingAlg.cpp:54
GaudiTestSuite::TimingAlg::doSomething
double doSomething()
do something CPU-intensive
Definition: TimingAlg.cpp:48
std::string
STL class.
ChronoEntity
Definition: ChronoEntity.h:31
RndmGenerators.h
Gaudi::Algorithm::randSvc
SmartIF< IRndmGenSvc > & randSvc() const
The standard RandomGen service, Return a pointer to the service if present.
Definition: Algorithm.cpp:563
ChronoEntity::stop
IChronoSvc::ChronoStatus stop()
stop the chrono
Definition: ChronoEntity.cpp:61
Gaudi::Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:526
Algorithm
Alias for backward compatibility.
Definition: Algorithm.h:58
IRndmGenSvc.h
Gaudi::Utils::Histos::Formats::header
GAUDI_API std::string header(const int ID=Default)
get the recommended header by enum
Definition: HistoTableFormat.cpp:186
Algorithm.h
System::microSec
@ microSec
Definition: Timing.h:67
GaudiExamples
Definition: LoggingAuditor.cpp:77
ChronoEntity::start
IChronoSvc::ChronoStatus start()
start the current chrono
Definition: ChronoEntity.cpp:46
Gaudi::Algorithm::chronoSvc
SmartIF< IChronoStatSvc > & chronoSvc() const
The standard Chrono & Stat service, Return a pointer to the service if present.
Definition: Algorithm.cpp:556
Chrono
Definition: Chrono.h:35
StatusCode
Definition: StatusCode.h:65
Rndm::Gauss
Parameters for the Gauss random number generation.
Definition: RndmGenerators.h:32
Rndm::Numbers
Random number accessor This small class encapsulates the use of the random number generator.
Definition: RndmGenerators.h:359
ChronoEntity::outputUserTime
std::string outputUserTime() const
print the chrono ;
Definition: ChronoEntity.cpp:82
GaudiTestSuite::TimingAlg::m_chrono2
ChronoEntity m_chrono2
Definition: TimingAlg.cpp:45
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
GaudiTestSuite::TimingAlg::m_cycles
Gaudi::Property< unsigned long > m_cycles
the length of the internal loops (property)
Definition: TimingAlg.cpp:41
Gaudi::Algorithm::finalize
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition: Algorithm.h:184
Gaudi::Units::gauss
constexpr double gauss
Definition: SystemOfUnits.h:252
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
std::endl
T endl(T... args)
GaudiTestSuite::TimingAlg::m_chrono1
ChronoEntity m_chrono1
Definition: TimingAlg.cpp:44
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
GaudiTestSuite::TimingAlg::m_chrono3
ChronoEntity m_chrono3
Definition: TimingAlg.cpp:46
Chrono.h
GaudiTestSuite::TimingAlg::finalize
StatusCode finalize() override
Definition: TimingAlg.cpp:84
GaudiExamples::TimingAlg
Definition: TimingAlg.cpp:107
GaudiTestSuite::TimingAlg
Definition: TimingAlg.cpp:29
Gaudi::Algorithm::Algorithm
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition: Algorithm.h:101
GaudiTestSuite
Definition: LoggingAuditor.cpp:22
Gaudi::Property< unsigned long >