The Gaudi Framework  master (37c0b60a)
HiveNumbers.h
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 #ifndef GAUDIHIVE_HIVENUMBERS_H
12 #define GAUDIHIVE_HIVENUMBERS_H
13 
14 // Framework include files
15 #include <GaudiKernel/IRndmGen.h>
16 #include <GaudiKernel/SmartIF.h>
17 
18 // STL includes
19 #include <memory>
20 #include <vector>
21 
22 #include <tbb/spin_rw_mutex.h>
23 
24 // Forward declarations
25 class IRndmGen;
26 class IRndmGenSvc;
27 
28 /*
29  * This is a first solution to the problem of the thread safe random generation.
30  * It is locking, but the locking is "diluted" by a caching mechanism of the
31  * random numbers.
32  */
33 
34 namespace HiveRndm {
35 
36  typedef tbb::spin_rw_mutex HiveNumbersMutex;
37 
39  private:
40  unsigned int m_buffer_index;
41  const unsigned int m_buffer_size;
44 
45  protected:
48 
49  public:
51  HiveNumbers();
53  HiveNumbers( const HiveNumbers& copy );
57  virtual ~HiveNumbers();
59  virtual StatusCode initialize( const SmartIF<IRndmGenSvc>& svc, const IRndmGen::Param& par );
60 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
61  [[deprecated]] HiveNumbers( IRndmGenSvc* svc, const IRndmGen::Param& par );
64  [[deprecated]] virtual StatusCode initialize( IRndmGenSvc* svc, const IRndmGen::Param& par );
65 #endif
66  virtual StatusCode finalize();
69  operator bool() const { return m_generator != 0; }
71  double operator()() { return this->shoot(); }
73  double pop() { return this->shoot(); }
75  double shoot() {
76  if ( 0 != m_generator ) {
77  if ( m_buffer_index == 0 ) { // we are out of numbers
78  this->shootArray( m_buffer, m_buffer_size ).ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ );
79  m_buffer_index = m_buffer_size - 1;
80  }
81  const double number = m_buffer[m_buffer_index];
82  m_buffer_index--;
83  return number;
84  }
85  return -1;
86  }
89  if ( 0 != m_generator ) {
90  StatusCode status;
91  {
92  HiveNumbersMutex::scoped_lock lock( m_genMutex );
93  status = m_generator->shootArray( array, num, start );
94  }
95  return status;
96  }
97  return StatusCode::FAILURE;
98  }
99  };
100 
101 } // namespace HiveRndm
102 
103 #endif // GAUDIHIVE_HIVENumbers_H
HiveRndm
Definition: HiveNumbers.h:34
HiveRndm::HiveNumbers::m_genMutex
static HiveNumbersMutex m_genMutex
Definition: HiveNumbers.h:43
std::vector< double >
HiveRndm::HiveNumbersMutex
tbb::spin_rw_mutex HiveNumbersMutex
Definition: HiveNumbers.h:36
IOTest.start
start
Definition: IOTest.py:110
HiveRndm::HiveNumbers::operator()
double operator()()
Operator () for the use within STL.
Definition: HiveNumbers.h:71
HiveRndm::HiveNumbers::pop
double pop()
Pop a new number from the buffer.
Definition: HiveNumbers.h:73
compareOutputFiles.par
par
Definition: compareOutputFiles.py:477
SmartIF.h
IRndmGen.h
HiveRndm::HiveNumbers::m_buffer_index
unsigned int m_buffer_index
Definition: HiveNumbers.h:40
HiveRndm::HiveNumbers::m_generator
IRndmGen * m_generator
Pointer to random number generator.
Definition: HiveNumbers.h:47
StatusCode
Definition: StatusCode.h:65
HiveRndm::HiveNumbers::shootArray
StatusCode shootArray(std::vector< double > &array, long num, long start=0)
Pop a new number from the buffer.
Definition: HiveNumbers.h:88
HiveRndm::HiveNumbers::m_buffer
std::vector< double > m_buffer
Definition: HiveNumbers.h:42
IRndmGen::Param
Definition: IRndmGen.h:49
IRndmGenSvc
Definition: IRndmGenSvc.h:45
SmartIF< IRndmGenSvc >
HiveRndm::HiveNumbers::m_buffer_size
const unsigned int m_buffer_size
Definition: HiveNumbers.h:41
IRndmGen::shootArray
virtual StatusCode shootArray(std::vector< double > &array, long howmany, long start=0) const =0
Multiple shots returning vector with random number according to specified distribution.
IRndmGen
Definition: IRndmGen.h:44
Containers::array
struct GAUDI_API array
Parametrisation class for redirection array - like implementation.
Definition: KeyedObjectManager.h:37
HiveRndm::HiveNumbers::shoot
double shoot()
Pop a new number from the buffer.
Definition: HiveNumbers.h:75
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
HiveRndm::HiveNumbers
Definition: HiveNumbers.h:38
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81