The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
HiveNumbers.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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#pragma once
12
13// Framework include files
15#include <GaudiKernel/SmartIF.h>
16
17// STL includes
18#include <memory>
19#include <vector>
20
21#include <tbb/spin_rw_mutex.h>
22
23// Forward declarations
24class IRndmGen;
25class IRndmGenSvc;
26
27/*
28 * This is a first solution to the problem of the thread safe random generation.
29 * It is locking, but the locking is "diluted" by a caching mechanism of the
30 * random numbers.
31 */
32
33namespace HiveRndm {
34
35 typedef tbb::spin_rw_mutex HiveNumbersMutex;
36
38 private:
39 unsigned int m_buffer_index;
40 const unsigned int m_buffer_size;
41 std::vector<double> m_buffer;
43
44 protected:
47
48 public:
52 HiveNumbers( const HiveNumbers& copy );
54 HiveNumbers( const SmartIF<IRndmGenSvc>& svc, const IRndmGen::Param& par );
56 virtual ~HiveNumbers();
58 virtual StatusCode initialize( const SmartIF<IRndmGenSvc>& svc, const IRndmGen::Param& par );
60 virtual StatusCode finalize();
62 operator bool() const { return m_generator != 0; }
64 double operator()() { return this->shoot(); }
66 double pop() { return this->shoot(); }
68 double shoot() {
69 if ( 0 != m_generator ) {
70 if ( m_buffer_index == 0 ) { // we are out of numbers
71 this->shootArray( m_buffer, m_buffer_size ).ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ );
73 }
74 const double number = m_buffer[m_buffer_index];
76 return number;
77 }
78 return -1;
79 }
80
81 StatusCode shootArray( std::vector<double>& array, long num, long start = 0 ) {
82 if ( 0 != m_generator ) {
83 StatusCode status;
84 {
85 HiveNumbersMutex::scoped_lock lock( m_genMutex );
86 status = m_generator->shootArray( array, num, start );
87 }
88 return status;
89 }
91 }
92 };
93
94} // namespace HiveRndm
#define GAUDI_API
Definition Kernel.h:49
const unsigned int m_buffer_size
Definition HiveNumbers.h:40
double shoot()
Pop a new number from the buffer.
Definition HiveNumbers.h:68
double operator()()
Operator () for the use within STL.
Definition HiveNumbers.h:64
double pop()
Pop a new number from the buffer.
Definition HiveNumbers.h:66
unsigned int m_buffer_index
Definition HiveNumbers.h:39
virtual StatusCode finalize()
Finalization.
StatusCode shootArray(std::vector< double > &array, long num, long start=0)
Pop a new number from the buffer.
Definition HiveNumbers.h:81
virtual StatusCode initialize(const SmartIF< IRndmGenSvc > &svc, const IRndmGen::Param &par)
Initialization.
IRndmGen * m_generator
Pointer to random number generator.
Definition HiveNumbers.h:46
static HiveNumbersMutex m_genMutex
Definition HiveNumbers.h:42
HiveNumbers()
Standard constructor.
std::vector< double > m_buffer
Definition HiveNumbers.h:41
Definition of a interface for a generic random number generators.
Definition IRndmGen.h:40
Random Generator service interface definition Definition of a interface for a service to access rando...
Definition IRndmGenSvc.h:40
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
constexpr static const auto FAILURE
Definition StatusCode.h:100
tbb::spin_rw_mutex HiveNumbersMutex
Definition HiveNumbers.h:35