The Gaudi Framework  master (37c0b60a)
CpuHungryAlg.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 files
12 // Standard:
13 #include <math.h>
14 #include <unistd.h>
15 // Gaudi:
16 #include <GaudiKernel/Algorithm.h>
17 
21 class CpuHungryAlg : public Algorithm {
22 public:
25 
26  StatusCode initialize() override;
27  StatusCode execute() override;
28  StatusCode finalize() override;
29 
30 private:
31  double mysin();
32  double mycos();
33  double mytan();
34  double myatan();
35 
36 private:
37  Gaudi::Property<long> m_loops{ this, "Loops", 1000000, "" };
38  int m_nevent = 0;
39 };
40 
41 // Register algorithm
43 
46  StatusCode sc = Algorithm::initialize(); // must be executed first
47  if ( sc.isFailure() ) return sc; // error printed already by Algorithm
48 
49  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Initialize" << endmsg;
50  return StatusCode::SUCCESS;
51 }
52 
55  double result = 0;
56  for ( long i = 0; i <= m_loops; ++i ) { result += sin( i ); }
57  return result;
58 }
59 
61  double result = 0;
62  for ( long i = 0; i <= m_loops; ++i ) { result += cos( i ); }
63  return result;
64 }
65 
67  double result = 0;
68  for ( long i = 0; i <= m_loops; ++i ) { result += tan( i ); }
69  result += myatan();
70  return result;
71 }
72 
74  double result = 0;
75  for ( long i = 0; i <= m_loops; ++i ) { result += tan( i ); }
76  return result;
77 }
78 
80  m_nevent++;
81  double result = 0;
82  if ( name() == "Alg1" ) {
83  result = mysin();
84  } else if ( name() == "Alg2" ) {
85  result = mycos();
86  } else {
87  result = mytan();
88  }
89  // This part is pointless, but prevent a warning about a
90  // set, but unused, variable (result).
91  if ( msgLevel( MSG::DEBUG ) ) debug() << "Result = " << result << endmsg;
92  return StatusCode::SUCCESS;
93 }
94 
97  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Finalize" << endmsg;
98 
99  return Algorithm::finalize(); // must be called after all other actions
100 }
101 
102 // ============================================================================
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
Gaudi::Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:526
CpuHungryAlg::mycos
double mycos()
Definition: CpuHungryAlg.cpp:60
Gaudi::Algorithm::initialize
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition: Algorithm.h:178
CpuHungryAlg::execute
StatusCode execute() override
Algorithm execution.
Definition: CpuHungryAlg.cpp:79
Algorithm
Alias for backward compatibility.
Definition: Algorithm.h:58
Algorithm.h
CpuHungryAlg::myatan
double myatan()
Definition: CpuHungryAlg.cpp:73
CommonMessaging< implements< IAlgorithm, IDataHandleHolder, IProperty, IStateful > >::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:148
CpuHungryAlg::m_loops
Gaudi::Property< long > m_loops
Definition: CpuHungryAlg.cpp:37
StatusCode
Definition: StatusCode.h:65
CpuHungryAlg
Algorithm which consume a lot of CPU.
Definition: CpuHungryAlg.cpp:21
CpuHungryAlg::m_nevent
int m_nevent
Definition: CpuHungryAlg.cpp:38
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
CpuHungryAlg::finalize
StatusCode finalize() override
Algorithm finalization.
Definition: CpuHungryAlg.cpp:96
Gaudi::Algorithm::finalize
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition: Algorithm.h:184
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
CpuHungryAlg::initialize
StatusCode initialize() override
Algorithm initialization.
Definition: CpuHungryAlg.cpp:45
CpuHungryAlg::mytan
double mytan()
Definition: CpuHungryAlg.cpp:66
Gaudi::Algorithm::Algorithm
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition: Algorithm.h:101
CpuHungryAlg::mysin
double mysin()
Execute.
Definition: CpuHungryAlg.cpp:54
Gaudi::Property< long >