The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
TimingAlg.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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\***********************************************************************************/
12#include <GaudiKernel/Chrono.h>
15#include <boost/format.hpp>
16#include <cmath>
17
18namespace 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 }
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
96 << "| (1U) " << m_chrono1.outputUserTime( format, System::microSec ) << std::endl
97 << "| (2U) " << m_chrono2.outputUserTime( format, System::microSec ) << std::endl
98 << "| (3U) " << m_chrono3.outputUserTime( format, System::microSec ) << endmsg;
99
100 return Algorithm::finalize();
101 }
102
104} // end of namespace GaudiTestSuite
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition MsgStream.cpp:93
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
a small helper class for implementation of ChronoStatSvc service, It also could be used as some local...
A small utility class for chronometry of user codes.
Definition Chrono.h:27
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
MsgStream & always() const
shortcut for the method msgStream(MSG::ALWAYS)
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition Algorithm.h:98
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition Algorithm.h:181
SmartIF< IChronoStatSvc > & chronoSvc() const
The standard Chrono & Stat service, Return a pointer to the service if present.
SmartIF< IRndmGenSvc > & randSvc() const
The standard RandomGen service, Return a pointer to the service if present.
const std::string & name() const override
The identifying name of the algorithm object.
Implementation of property with value of concrete type.
Definition PropertyFwd.h:27
The primitive class to illustrate the usage of IChronoSvc , ChronoEntity & Chrono objects.
Definition TimingAlg.cpp:29
double doSomething()
do something CPU-intensive
Definition TimingAlg.cpp:48
StatusCode execute() override
Definition TimingAlg.cpp:54
Gaudi::Property< unsigned long > m_cycles
the length of the internal loops (property)
Definition TimingAlg.cpp:41
StatusCode finalize() override
Definition TimingAlg.cpp:84
Parameters for the Gauss random number generation.
Random number accessor This small class encapsulates the use of the random number generator.
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
constexpr static const auto SUCCESS
Definition StatusCode.h:99
@ microSec
Definition Timing.h:45