Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  typedef tbb::spin_rw_mutex_v3 HiveNumbersMutex;
27 
29  private:
30  unsigned int m_buffer_index;
31  const unsigned int m_buffer_size;
33  static HiveNumbersMutex m_genMutex;
34 
35  protected:
38 
39  public:
41  HiveNumbers();
43  HiveNumbers( const HiveNumbers& copy );
47  virtual ~HiveNumbers();
49  virtual StatusCode initialize( const SmartIF<IRndmGenSvc>& svc, const IRndmGen::Param& par );
50 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
54  virtual StatusCode initialize( IRndmGenSvc* svc, const IRndmGen::Param& par );
55 #endif
56  virtual StatusCode finalize();
59  operator bool() const { return m_generator != 0; }
61  double operator()() { return this->shoot(); }
63  double pop() { return this->shoot(); }
65  double shoot() {
66  if ( 0 != m_generator ) {
67  if ( m_buffer_index == 0 ) { // we are out of numbers
68  this->shootArray( m_buffer, m_buffer_size );
69  m_buffer_index = m_buffer_size - 1;
70  }
71  const double number = m_buffer[m_buffer_index];
72  m_buffer_index--;
73  return number;
74  }
75  return -1;
76  }
79  if ( 0 != m_generator ) {
80  StatusCode status;
81  {
82  HiveNumbersMutex::scoped_lock lock( m_genMutex );
83  status = m_generator->shootArray( array, num, start );
84  }
85  return status;
86  }
87  return StatusCode::FAILURE;
88  }
89  };
90 
91 } // namespace HiveRndm
92 
93 #endif // GAUDIHIVE_HIVENumbers_H
const unsigned int m_buffer_size
Definition: HiveNumbers.h:31
tbb::spin_rw_mutex_v3 HiveNumbersMutex
Definition: HiveNumbers.h:26
Definition of a interface for a generic random number generators.
Definition: IRndmGen.h:34
std::vector< double > m_buffer
Definition: HiveNumbers.h:32
IRndmGen * m_generator
Pointer to random number generator.
Definition: HiveNumbers.h:37
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:50
start
Definition: IOTest.py:97
double operator()()
Operator () for the use within STL.
Definition: HiveNumbers.h:61
struct GAUDI_API array
Parametrisation class for redirection array - like implementation.
constexpr static const auto FAILURE
Definition: StatusCode.h:86
StatusCode shootArray(std::vector< double > &array, long num, long start=0)
Pop a new number from the buffer.
Definition: HiveNumbers.h:78
unsigned int m_buffer_index
Definition: HiveNumbers.h:30
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:63
#define GAUDI_API
Definition: Kernel.h:71
static HiveNumbersMutex m_genMutex
Definition: HiveNumbers.h:33
double shoot()
Pop a new number from the buffer.
Definition: HiveNumbers.h:65