Geant4-11
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
G4UniformRandPool Class Reference

#include <G4UniformRandPool.hh>

Public Member Functions

 G4UniformRandPool ()
 
 G4UniformRandPool (G4int ps)
 
void GetMany (G4double *rnds, G4int howMany)
 
G4double GetOne ()
 
G4int GetPoolSize () const
 
void Resize (G4int newSize)
 
 ~G4UniformRandPool ()
 

Static Public Member Functions

static G4double flat ()
 
static void flatArray (G4int howmany, G4double *rnds)
 

Private Member Functions

void Fill (G4int howmany)
 

Private Attributes

G4doublebuffer
 
G4int currentIdx
 
G4int size
 

Detailed Description

Definition at line 55 of file G4UniformRandPool.hh.

Constructor & Destructor Documentation

◆ G4UniformRandPool() [1/2]

G4UniformRandPool::G4UniformRandPool ( )

Definition at line 76 of file G4UniformRandPool.cc.

78 , buffer(0)
79 , currentIdx(0)
80{
81 if(sizeof(G4double) * CHAR_BIT == 64)
82 {
84 }
85 else
86 {
88 }
89 Fill(size);
90}
double G4double
Definition: G4Types.hh:83
void create_pool(G4double *&buffer, G4int ps)
void create_pool_align(G4double *&buffer, G4int ps)
#define G4UNIFORMRANDPOOL_DEFAULT_POOLSIZE
void Fill(G4int howmany)

References buffer, create_pool(), create_pool_align(), Fill(), and size.

Referenced by flat(), and flatArray().

◆ G4UniformRandPool() [2/2]

G4UniformRandPool::G4UniformRandPool ( G4int  ps)

Definition at line 92 of file G4UniformRandPool.cc.

93 : size(siz)
94 , buffer(0)
95 , currentIdx(0)
96{
97 if(sizeof(G4double) * CHAR_BIT == 64)
98 {
100 }
101 else
102 {
104 }
105 Fill(size);
106}

References buffer, create_pool(), create_pool_align(), Fill(), and size.

◆ ~G4UniformRandPool()

G4UniformRandPool::~G4UniformRandPool ( )

Definition at line 108 of file G4UniformRandPool.cc.

109{
110 if(sizeof(G4double) * CHAR_BIT == 64)
111 {
113 }
114 else
115 {
117 }
118}
void destroy_pool_align(G4double *&buffer)
void destroy_pool(G4double *&buffer)

References buffer, destroy_pool(), and destroy_pool_align().

Member Function Documentation

◆ Fill()

void G4UniformRandPool::Fill ( G4int  howmany)
private

Definition at line 132 of file G4UniformRandPool.cc.

133{
134 assert(howmany > 0 && howmany <= size);
135
136 // Fill buffer with random numbers
137 //
138 G4Random::getTheEngine()->flatArray(howmany, buffer);
139 currentIdx = 0;
140}

References buffer, currentIdx, and size.

Referenced by G4UniformRandPool(), GetMany(), and GetOne().

◆ flat()

G4double G4UniformRandPool::flat ( )
static

Definition at line 214 of file G4UniformRandPool.cc.

215{
216 if(rndpool == 0)
217 {
220 }
221 return rndpool->GetOne();
222}
void Register(T *inst)
Definition: G4AutoDelete.hh:65
G4ThreadLocal G4UniformRandPool * rndpool

References G4UniformRandPool(), GetOne(), G4AutoDelete::Register(), and anonymous_namespace{G4UniformRandPool.cc}::rndpool.

◆ flatArray()

void G4UniformRandPool::flatArray ( G4int  howmany,
G4double rnds 
)
static

Definition at line 224 of file G4UniformRandPool.cc.

225{
226 if(rndpool == 0)
227 {
230 }
231 rndpool->GetMany(rnds, (unsigned int) howmany);
232}
void GetMany(G4double *rnds, G4int howMany)

References G4UniformRandPool(), GetMany(), G4AutoDelete::Register(), and anonymous_namespace{G4UniformRandPool.cc}::rndpool.

◆ GetMany()

void G4UniformRandPool::GetMany ( G4double rnds,
G4int  howMany 
)

Definition at line 142 of file G4UniformRandPool.cc.

143{
144 assert(rnds != 0 && howmany > 0);
145
146 // if ( howmany <= 0 ) return;
147 // We generate at max "size" numbers at once, and
148 // We do not want to use recursive calls (expensive).
149 // We need to deal with the case howmany>size
150 // So:
151 // how many times I need to get "size" numbers?
152
153 const G4int maxcycles = howmany / size;
154
155 // This is the rest
156 //
157 const G4int peel = howmany % size;
158 assert(peel < size);
159
160 // Ok from now on I will get random numbers in group of "size"
161 // Note that if howmany<size maxcycles == 0
162 //
163 G4int cycle = 0;
164
165 // Consider the case howmany>size, then maxcycles>=1
166 // and we will request at least "size" rng, so
167 // let's start with a fresh buffer of numbers if needed
168 //
169 if(maxcycles > 0 && currentIdx > 0)
170 {
171 assert(currentIdx <= size);
172 Fill(currentIdx); //<size?currentIdx:size);
173 }
174 for(; cycle < maxcycles; ++cycle)
175 {
176 // We can use memcpy of std::copy, it turns out that the two are basically
177 // performance-wise equivalent (expected), since in my tests memcpy is a
178 // little bit faster, I use that
179 //
180 memcpy(rnds + (cycle * size), buffer, sizeof(G4double) * size);
181 // std::copy(buffer,buffer+size,rnds+(cycle*size));
182
183 // Get a new set of numbers
184 //
185 Fill(size); // Now currentIdx is 0 again
186 }
187
188 // If maxcycles>0 last think we did was to call Fill(size)
189 // so currentIdx == 0
190 // and it is guaranteed that peel<size, we have enough fresh random numbers
191 // but if maxcycles==0 currentIdx can be whatever, let's make sure we have
192 // enough fresh numbers
193 //
194 if(currentIdx + peel >= size)
195 {
197 }
198 memcpy(rnds + (cycle * size), buffer + currentIdx, sizeof(G4double) * peel);
199 // std::copy(buffer+currentIdx,buffer+(currentIdx+peel), rnds+(cycle*size));
200
201 // Advance index, we are done
202 //
203 currentIdx += peel;
204 assert(currentIdx <= size);
205}
int G4int
Definition: G4Types.hh:85

References buffer, currentIdx, geant4_check_module_cycles::cycle, Fill(), and size.

Referenced by flatArray().

◆ GetOne()

G4double G4UniformRandPool::GetOne ( )
inline

Definition at line 82 of file G4UniformRandPool.hh.

83{
84 // No more available numbers, re-fill
85 //
86 if(currentIdx >= /*(unsigned int)*/ size)
87 {
88 Fill(/*(unsigned int)*/ size);
89 }
90
91 return buffer[currentIdx++];
92}

References buffer, currentIdx, Fill(), and size.

Referenced by flat().

◆ GetPoolSize()

G4int G4UniformRandPool::GetPoolSize ( ) const
inline

Definition at line 94 of file G4UniformRandPool.hh.

95{
96 return size;
97}

References size.

◆ Resize()

void G4UniformRandPool::Resize ( G4int  newSize)

Definition at line 120 of file G4UniformRandPool.cc.

121{
122 if(newSize != size)
123 {
125 create_pool(buffer, newSize);
126 size = newSize;
127 currentIdx = 0;
128 }
129 currentIdx = 0;
130}

References buffer, create_pool(), currentIdx, destroy_pool(), and size.

Field Documentation

◆ buffer

G4double* G4UniformRandPool::buffer
private

◆ currentIdx

G4int G4UniformRandPool::currentIdx
private

Definition at line 79 of file G4UniformRandPool.hh.

Referenced by Fill(), GetMany(), GetOne(), and Resize().

◆ size

G4int G4UniformRandPool::size
private

Definition at line 77 of file G4UniformRandPool.hh.

Referenced by Fill(), G4UniformRandPool(), GetMany(), GetOne(), GetPoolSize(), and Resize().


The documentation for this class was generated from the following files: