| The Gaudi Framework v32r1 (f65d50dc)

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
28 class GAUDI_API HiveNumbers {
29 private:
30 unsigned int m_buffer_index;
31 const unsigned int m_buffer_size;
32 std::vector<double> m_buffer;
33 static HiveNumbersMutex m_genMutex;
34
35 protected:
37 IRndmGen* m_generator;
38
39 public:
41 HiveNumbers();
43 HiveNumbers( const HiveNumbers& copy );
45 HiveNumbers( const SmartIF<IRndmGenSvc>& svc, const IRndmGen::Param& par );
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 )
51 HiveNumbers( IRndmGenSvc* svc, const IRndmGen::Param& par );
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 }
78 StatusCode shootArray( std::vector<double>& array, long num, long start = 0 ) {
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
HiveRndm::HiveNumbers::m_buffer_size
const unsigned int m_buffer_size
Definition: HiveNumbers.h:31
SmartIF< IRndmGenSvc >
HiveRndm::HiveNumbersMutex
tbb::spin_rw_mutex_v3 HiveNumbersMutex
Definition: HiveNumbers.h:26
IRndmGen
Definition of a interface for a generic random number generators.
Definition: IRndmGen.h:34
IRndmGen.h
IOTest.start
def start
Definition: IOTest.py:97
HiveRndm::HiveNumbers::m_buffer
std::vector< double > m_buffer
Definition: HiveNumbers.h:32
compareOutputFiles.par
string par
Definition: compareOutputFiles.py:465
HiveRndm::HiveNumbers::m_generator
IRndmGen * m_generator
Pointer to random number generator.
Definition: HiveNumbers.h:37
IRndmGenSvc
Random Generator service interface definition Definition of a interface for a service to access rando...
Definition: IRndmGenSvc.h:35
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
IRndmGen::Param
Definition: IRndmGen.h:39
SmartIF.h
HiveRndm::HiveNumbers
Definition: HiveNumbers.h:28
HiveRndm::HiveNumbers::operator()
double operator()()
Operator () for the use within STL.
Definition: HiveNumbers.h:61
Containers::array
struct GAUDI_API array
Parametrisation class for redirection array - like implementation.
Definition: KeyedObjectManager.h:27
std::vector< double >
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.
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
HiveRndm::HiveNumbers::shootArray
StatusCode shootArray(std::vector< double > &array, long num, long start=0)
Pop a new number from the buffer.
Definition: HiveNumbers.h:78
HiveRndm::HiveNumbers::m_buffer_index
unsigned int m_buffer_index
Definition: HiveNumbers.h:30
HiveRndm::HiveNumbers::pop
double pop()
Pop a new number from the buffer.
Definition: HiveNumbers.h:63
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:71
HiveRndm::HiveNumbers::m_genMutex
static HiveNumbersMutex m_genMutex
Definition: HiveNumbers.h:33
HiveRndm
Definition: HiveNumbers.h:24
HiveRndm::HiveNumbers::shoot
double shoot()
Pop a new number from the buffer.
Definition: HiveNumbers.h:65