The Gaudi Framework  v30r3 (a5ef0a68)
HiveNumbers.h
Go to the documentation of this file.
1 #ifndef GAUDIHIVE_HIVENUMBERS_H
2 #define GAUDIHIVE_HIVENUMBERS_H
3 
4 // Framework include files
5 #include "GaudiKernel/IRndmGen.h"
6 #include "GaudiKernel/SmartIF.h"
7 
8 // STL includes
9 #include <memory>
10 #include <vector>
11 
12 #include "tbb/spin_rw_mutex.h"
13 
14 // Forward declarations
15 class IRndmGen;
16 class IRndmGenSvc;
17 
18 /*
19  * This is a first solution to the problem of the thread safe random generation.
20  * It is locking, but the locking is "diluted" by a caching mechanism of the
21  * random numbers.
22  */
23 
24 namespace HiveRndm
25 {
26 
27  typedef tbb::spin_rw_mutex_v3 HiveNumbersMutex;
28 
30  {
31  private:
32  unsigned int m_buffer_index;
33  const unsigned int m_buffer_size;
35  static HiveNumbersMutex m_genMutex;
36 
37  protected:
40 
41  public:
43  HiveNumbers();
45  HiveNumbers( const HiveNumbers& copy );
49  virtual ~HiveNumbers();
51  virtual StatusCode initialize( const SmartIF<IRndmGenSvc>& svc, const IRndmGen::Param& par );
52 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
56  virtual StatusCode initialize( IRndmGenSvc* svc, const IRndmGen::Param& par );
57 #endif
58  virtual StatusCode finalize();
61  operator bool() const { return m_generator != 0; }
63  double operator()() { return this->shoot(); }
65  double pop() { return this->shoot(); }
67  double shoot()
68  {
69  if ( 0 != m_generator ) {
70  if ( m_buffer_index == 0 ) { // we are out of numbers
71  this->shootArray( m_buffer, m_buffer_size );
72  m_buffer_index = m_buffer_size - 1;
73  }
74  const double number = m_buffer[m_buffer_index];
75  m_buffer_index--;
76  return number;
77  }
78  return -1;
79  }
82  {
83  if ( 0 != m_generator ) {
84  StatusCode status;
85  {
86  HiveNumbersMutex::scoped_lock lock( m_genMutex );
87  status = m_generator->shootArray( array, num, start );
88  }
89  return status;
90  }
91  return StatusCode::FAILURE;
92  }
93  };
94 
95 } // end of Rndm namespace
96 
97 #endif // GAUDIHIVE_HIVENumbers_H
const unsigned int m_buffer_size
Definition: HiveNumbers.h:33
constexpr static const auto FAILURE
Definition: StatusCode.h:88
tbb::spin_rw_mutex_v3 HiveNumbersMutex
Definition: HiveNumbers.h:27
Definition of a interface for a generic random number generators.
Definition: IRndmGen.h:34
std::vector< double > m_buffer
Definition: HiveNumbers.h:34
IRndmGen * m_generator
Pointer to random number generator.
Definition: HiveNumbers.h:39
Random Generator service interface definition Definition of a interface for a service to access rando...
Definition: IRndmGenSvc.h:35
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
start
Definition: IOTest.py:99
double operator()()
Operator () for the use within STL.
Definition: HiveNumbers.h:63
struct GAUDI_API array
Parametrisation class for redirection array - like implementation.
StatusCode shootArray(std::vector< double > &array, long num, long start=0)
Pop a new number from the buffer.
Definition: HiveNumbers.h:81
unsigned int m_buffer_index
Definition: HiveNumbers.h:32
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.
double pop()
Pop a new number from the buffer.
Definition: HiveNumbers.h:65
#define GAUDI_API
Definition: Kernel.h:104
static HiveNumbersMutex m_genMutex
Definition: HiveNumbers.h:35
double shoot()
Pop a new number from the buffer.
Definition: HiveNumbers.h:67