Loading [MathJax]/jax/output/HTML-CSS/config.js
The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
CPUCrunchSvc.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 "CPUCrunchSvc.h"
12 
14 
15 #include <sys/times.h>
16 #include <tbb/tick_count.h>
17 
19 
20 #define ON_DEBUG if ( msgLevel( MSG::DEBUG ) )
21 #define DEBUG_MSG ON_DEBUG debug()
22 
23 #define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) )
24 #define VERBOSE_MSG ON_VERBOSE verbose()
25 
26 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
27 
29 
30 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
31 
33  debug() << "initialize" << endmsg;
34 
35  if ( base_class::initialize().isFailure() ) {
36  error() << "Error initializing base class" << endmsg;
37  return StatusCode::FAILURE;
38  }
39 
40  calibrate();
41 
42  return StatusCode::SUCCESS;
43 }
44 
45 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
46 
47 /*
48 Calibrate the crunching finding the right relation between max number to be searched and time spent.
49 The relation is a sqrt for times greater than 10^-4 seconds.
50 */
52  if ( m_niters_vect.value().size() == 0 ) {
53  m_niters_vect = { 0, 500, 600, 700, 800, 1000, 1300, 1600, 2000, 2300,
54  2600, 3000, 3300, 3500, 3900, 4200, 5000, 6000, 8000, 10000,
55  12000, 15000, 17000, 20000, 25000, 30000, 35000, 40000, 50000, 60000 };
56  if ( !m_shortCalib ) {
57  m_niters_vect.value().push_back( 80000 );
58  m_niters_vect.value().push_back( 100000 );
59  m_niters_vect.value().push_back( 150000 );
60  m_niters_vect.value().push_back( 200000 );
61  }
62  }
63 
64  if ( m_niters_vect.value().at( 0 ) != 0 ) {
65  warning() << "NIterationsVect[0]= " << m_niters_vect.value().at( 0 ) << " but needs to be zero. resetting it."
66  << endmsg;
67  m_niters_vect.value().at( 0 ) = 0;
68  }
69 
71  m_times_vect.at( 0 ) = 0;
72 
73  const unsigned int minCalibTime_us = m_minCalibTime * 1000;
74 
75  // warm it up by doing 20k iterations
76  findPrimes( 20000 );
77 
78  for ( int irun = 0; irun < m_numCalibRuns; ++irun ) {
79 
80  debug() << "Starting calibration run " << irun + 1 << " ..." << endmsg;
81  for ( unsigned int i = 1; i < m_niters_vect.value().size(); ++i ) {
82  unsigned int niters = m_niters_vect.value().at( i );
83  unsigned int trials = 30;
84  do {
85  auto start_cali = tbb::tick_count::now();
86  findPrimes( niters );
87  auto stop_cali = tbb::tick_count::now();
88  auto deltat = ( stop_cali - start_cali ).seconds();
89  m_times_vect.at( i ) = deltat * 1000000; // in microseconds
90  debug() << " Calibration: # iters = " << niters << " => " << m_times_vect.at( i ) << " us" << endmsg;
91  trials--;
92  } while ( trials > 0 && m_times_vect.at( i ) < m_times_vect.at( i - 1 ) ); // make sure that they are monotonic
93 
94  if ( i == m_niters_vect.value().size() - 1 && minCalibTime_us != 0 ) {
95  if ( m_times_vect.at( i ) < minCalibTime_us ) {
96  debug() << " increasing calib vect with " << int( m_niters_vect.value().back() * 1.2 )
97  << " iterations to reach min calib time of " << m_minCalibTime.value() << " ms " << endmsg;
98  m_niters_vect.value().push_back( int( m_niters_vect.value().back() * 1.2 ) );
99  m_times_vect.push_back( 0. );
100  }
101  }
102  }
103  }
104  if ( m_corrFact != 1. ) {
105  debug() << "Adjusting times with correction factor " << m_corrFact.value() << endmsg;
106  for ( auto& t : m_times_vect ) { t = t * m_corrFact; }
107  }
108  debug() << "Calibration finished!" << endmsg;
109 }
110 
111 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
112 
114 
115  unsigned int smaller_i = 0;
116  double time = 0.;
117  bool found = false;
118  double corrRuntime = runtime.count(); // * m_corrFact;
119  // We know that the first entry is 0, so we start to iterate from 1
120  for ( unsigned int i = 1; i < m_times_vect.size(); i++ ) {
121  time = m_times_vect.at( i );
122  if ( time > corrRuntime ) {
123  smaller_i = i - 1;
124  found = true;
125  break;
126  }
127  }
128 
129  // Case 1: we are outside the interpolation range, we take the last 2 points
130  if ( not found ) smaller_i = m_times_vect.size() - 2;
131 
132  // Case 2: we maeke a linear interpolation
133  // y=mx+q
134  const auto x0 = m_times_vect.at( smaller_i );
135  const auto x1 = m_times_vect.at( smaller_i + 1 );
136  const auto y0 = m_niters_vect.value().at( smaller_i );
137  const auto y1 = m_niters_vect.value().at( smaller_i + 1 );
138  const double m = (double)( y1 - y0 ) / (double)( x1 - x0 );
139  const double q = y0 - m * x0;
140 
141  const unsigned int nCaliIters = m * corrRuntime + q;
142 
143  VERBOSE_MSG << "x0: " << x0 << " x1: " << x1 << " y0: " << y0 << " y1: " << y1 << " m: " << m << " q: " << q
144  << " itr: " << nCaliIters << endmsg;
145 
146  return nCaliIters;
147 }
148 
149 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
150 
151 void CPUCrunchSvc::findPrimes( const unsigned int n_iterations ) const {
152  // Flag to trigger the allocation
153  bool is_prime;
154 
155  // Let's prepare the material for the allocations
156  unsigned int primes_size = 1;
157  unsigned long* primes = new unsigned long[primes_size];
158  primes[0] = 2;
159 
160  unsigned long i = 2;
161 
162  // Loop on numbers
163  for ( unsigned long int iiter = 0; iiter < n_iterations; iiter++ ) {
164  // Once at max, it returns to 0
165  i += 1;
166 
167  // Check if it can be divided by the smaller ones
168  is_prime = true;
169  for ( unsigned long j = 2; j < i && is_prime; ++j ) {
170  if ( i % j == 0 ) is_prime = false;
171  } // end loop on numbers < than tested one
172 
173  if ( is_prime ) {
174  // copy the array of primes (INEFFICIENT ON PURPOSE!)
175  unsigned int new_primes_size = 1 + primes_size;
176  unsigned long* new_primes = new unsigned long[new_primes_size];
177 
178  for ( unsigned int prime_index = 0; prime_index < primes_size; prime_index++ ) {
179  new_primes[prime_index] = primes[prime_index];
180  }
181  // attach the last prime
182  new_primes[primes_size] = i;
183 
184  // Update primes array
185  delete[] primes;
186  primes = new_primes;
187  primes_size = new_primes_size;
188  } // end is prime
189 
190  } // end of while loop
191 
192  // Fool Compiler optimisations:
193  for ( unsigned int prime_index = 0; prime_index < primes_size; prime_index++ )
194  if ( primes[prime_index] == 4 )
195  debug() << "This does never happen, but it's necessary too fool aggressive compiler optimisations!" << endmsg;
196 
197  delete[] primes;
198 }
199 
200 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
201 
202 // Translate the required crunch time into interations, and do it
204 
205  const unsigned int niters = getNCaliIters( crunchtime );
206 
207  auto start_cali = tbb::tick_count::now();
208  findPrimes( niters );
209  auto stop_cali = tbb::tick_count::now();
210 
211  std::chrono::milliseconds actual( int( 1000 * ( stop_cali - start_cali ).seconds() ) );
212 
213  DEBUG_MSG << "crunch for " << crunchtime.count() << " ms == " << niters << " iter. actual time: " << actual.count()
214  << " ms. ratio: " << float( actual.count() ) / crunchtime.count() << endmsg;
215 
216  return actual;
217 }
std::vector::resize
T resize(T... args)
CPUCrunchSvc::m_minCalibTime
Gaudi::Property< unsigned int > m_minCalibTime
Definition: CPUCrunchSvc.h:45
CPUCrunchSvc::crunch_for
std::chrono::milliseconds crunch_for(const std::chrono::milliseconds &crunchtime) const override
Definition: CPUCrunchSvc.cpp:203
std::string
STL class.
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:22
std::vector::size
T size(T... args)
ISvcLocator
Definition: ISvcLocator.h:46
fmt::runtime
const T & runtime(const T &v)
Definition: MessageSvcSink.cpp:27
std::chrono::microseconds
std::vector::push_back
T push_back(T... args)
VERBOSE_MSG
#define VERBOSE_MSG
Definition: CPUCrunchSvc.cpp:24
bug_34121.t
t
Definition: bug_34121.py:30
TimingHistograms.name
name
Definition: TimingHistograms.py:25
StatusCode
Definition: StatusCode.h:65
std::vector::at
T at(T... args)
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:108
ProduceConsume.j
j
Definition: ProduceConsume.py:101
CPUCrunchSvc::getNCaliIters
unsigned int getNCaliIters(std::chrono::microseconds runtime) const
Definition: CPUCrunchSvc.cpp:113
CommonMessaging
Definition: CommonMessaging.h:66
CPUCrunchSvc::m_shortCalib
Gaudi::Property< bool > m_shortCalib
Definition: CPUCrunchSvc.h:44
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:239
CPUCrunchSvc::m_corrFact
Gaudi::Property< float > m_corrFact
Definition: CPUCrunchSvc.h:48
CPUCrunchSvc::calibrate
void calibrate()
Definition: CPUCrunchSvc.cpp:51
CPUCrunchSvc
Definition: CPUCrunchSvc.h:23
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
CPUCrunchSvc.h
plotSpeedupsPyRoot.time
time
Definition: plotSpeedupsPyRoot.py:180
CPUCrunchSvc::m_numCalibRuns
Gaudi::Property< int > m_numCalibRuns
Definition: CPUCrunchSvc.h:47
CPUCrunchSvc::findPrimes
void findPrimes(unsigned int) const
Definition: CPUCrunchSvc.cpp:151
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
std::chrono::milliseconds::count
T count(T... args)
CPUCrunchSvc::m_times_vect
std::vector< unsigned int > m_times_vect
Definition: CPUCrunchSvc.h:40
CPUCrunchSvc::CPUCrunchSvc
CPUCrunchSvc(const std::string &name, ISvcLocator *svc)
Definition: CPUCrunchSvc.cpp:28
CPUCrunchSvc::initialize
virtual StatusCode initialize() override
Definition: CPUCrunchSvc.cpp:32
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
DEBUG_MSG
#define DEBUG_MSG
Definition: CPUCrunchSvc.cpp:21
ISvcLocator.h
CPUCrunchSvc::m_niters_vect
Gaudi::Property< std::vector< unsigned int > > m_niters_vect
Definition: CPUCrunchSvc.h:42