Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v38r0 (2143aa4c)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
TimingAlg.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 // ============================================================================
12 // Include files
13 // ============================================================================
14 // STD & STL
15 // ============================================================================
16 #include <cmath>
17 // ============================================================================
18 // GaudiKernel
19 // ============================================================================
20 #include "GaudiKernel/Chrono.h"
23 // ============================================================================
24 // GaudiAlg
25 // ============================================================================
26 #include "GaudiKernel/Algorithm.h"
27 // ============================================================================
28 // Boost
29 // ============================================================================
30 #include "boost/format.hpp"
31 // ============================================================================
32 namespace GaudiExamples {
33  // ==========================================================================
44  class TimingAlg : public Algorithm {
45  public:
46  // ========================================================================
49  // ========================================================================
51  StatusCode execute() override; // the execution of the algorithm
53  StatusCode finalize() override; // the finalization of the algorithm
54  // ========================================================================
55  protected:
56  // ========================================================================
58  double doSomething();
59  // ========================================================================
60  private:
61  // ========================================================================
63  Gaudi::Property<unsigned long> m_cycles{ this, "Cycles", 10000, "The number of cycles" };
64  // ========================================================================
65  private:
66  // ========================================================================
70  // ========================================================================
71  };
72  // ==========================================================================
73 } // end of namespace GaudiExamples
74 // ============================================================================
75 // do something CPU-intensive
76 // ============================================================================
78  double result = 0;
79  Rndm::Numbers gauss( randSvc(), Rndm::Gauss( 0.0, 1.0 ) );
80  for ( unsigned long i = 0; i < m_cycles; ++i ) result += sin( gauss() );
81  return result;
82 }
83 // ========================================================================
84 // the execution of the algorithm
85 // ============================================================================
86 StatusCode GaudiExamples::TimingAlg::execute() // the execution of the algorithm
87 {
88  //
89  { // make some cpu consuming evaluations:
90  Chrono chrono( chronoSvc(), name() + ": the first loop" );
91  debug() << "the result of the first loop is " << doSomething() << endmsg;
92  }
93  //
94  { // make some cpu consuming evaluations:
95  Chrono chrono( name() + ": the second loop", chronoSvc() );
96  debug() << "the result of the second loop is " << doSomething() << endmsg;
97  }
98  //
99  { // make some cpu consuming evaluations:
100  Chrono chrono( &m_chrono1 );
101  debug() << "the result of the third loop is " << doSomething() << endmsg;
102  }
103  //
104  { // make some cpu consuming evaluations:
105  Chrono chrono( m_chrono2 );
106  debug() << "the result of the fourth loop is " << doSomething() << endmsg;
107  }
108  //
109  { // make some cpu consuming evaluations:
110  m_chrono3.start();
111  debug() << "the result of the 5th loop is " << doSomething() << endmsg;
112  m_chrono3.stop();
113  }
114  //
115  { // make some cpu consuming evaluations:
116  chronoSvc()->chronoStart( name() + ": the 5th loop" );
117  debug() << "the result of the 5th loop is " << doSomething() << endmsg;
118  chronoSvc()->chronoStop( name() + ": the 5th loop" );
119  }
120  //
121  return StatusCode::SUCCESS;
122 }
123 // ============================================================================
124 // the finalization of the algorithm
125 // ============================================================================
126 StatusCode GaudiExamples::TimingAlg::finalize() // the finalization of the algorithm
127 {
128  const std::string format = "| %1$-6d| %2$-12.5g| %3$12.5g+-%4$-12.5g|%6$12.5g/%7$-12.5g|";
129  const std::string header = "| |%1$=7.7s|%2$=13.13s|%3$13.13s+-%4$-12.12s|%6$12.12g/%7$-12.12s|";
130 
131  using namespace boost::io;
132  boost::format hdr( header );
133  hdr.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
134 
135  hdr % "#" % "Total" % "Mean" % "RMS" % "ErrMean" % "Min" % "Max";
136 
137  always() << "The timing is (in us)" << std::endl
138  << hdr.str() << std::endl
139  << "| (1U) " << m_chrono1.outputUserTime( format, System::microSec ) << std::endl
140  << "| (2U) " << m_chrono2.outputUserTime( format, System::microSec ) << std::endl
141  << "| (3U) " << m_chrono3.outputUserTime( format, System::microSec ) << endmsg;
142 
143  return Algorithm::finalize();
144 }
145 // ============================================================================
146 // declare the factory (needed for instantiation)
149 // ============================================================================
150 // The END
151 // ============================================================================
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:565
bug_34121.name
name
Definition: bug_34121.py:20
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
GaudiExamples::TimingAlg
Definition: TimingAlg.cpp:44
System::microSec
@ microSec
Definition: Timing.h:67
GaudiExamples
Definition: __init__.py:1
GaudiExamples::TimingAlg::doSomething
double doSomething()
do something CPU-intensive
Definition: TimingAlg.cpp:77
GaudiExamples::TimingAlg::m_chrono2
ChronoEntity m_chrono2
Definition: TimingAlg.cpp:68
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
GaudiExamples::TimingAlg::m_chrono1
ChronoEntity m_chrono1
Definition: TimingAlg.cpp:67
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:203
GaudiExamples::TimingAlg::m_chrono3
ChronoEntity m_chrono3
Definition: TimingAlg.cpp:69
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)
GaudiExamples::TimingAlg::finalize
StatusCode finalize() override
the finalization of the algorithm
Definition: TimingAlg.cpp:126
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
Chrono.h
GaudiExamples::TimingAlg::m_cycles
Gaudi::Property< unsigned long > m_cycles
the length of the internal loops (property)
Definition: TimingAlg.cpp:63
GaudiExamples::TimingAlg::execute
StatusCode execute() override
the execution of the algorithm
Definition: TimingAlg.cpp:86
Gaudi::Algorithm::Algorithm
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition: Algorithm.h:101
Gaudi::Property< unsigned long >