Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4UniformRandPool Class Reference

#include <G4UniformRandPool.hh>

Public Member Functions

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

Static Public Member Functions

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

Detailed Description

Definition at line 55 of file G4UniformRandPool.hh.

Constructor & Destructor Documentation

◆ G4UniformRandPool() [1/2]

G4UniformRandPool::G4UniformRandPool ( )

Definition at line 77 of file G4UniformRandPool.cc.

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

Referenced by flat(), and flatArray().

◆ G4UniformRandPool() [2/2]

G4UniformRandPool::G4UniformRandPool ( G4int ps)
explicit

Definition at line 90 of file G4UniformRandPool.cc.

91 : size(siz)
92{
93 if(sizeof(G4double) * CHAR_BIT == 64)
94 {
95 create_pool_align(buffer, size);
96 }
97 else
98 {
99 create_pool(buffer, size);
100 }
101 Fill(size);
102}

◆ ~G4UniformRandPool()

G4UniformRandPool::~G4UniformRandPool ( )

Definition at line 104 of file G4UniformRandPool.cc.

105{
106 if(sizeof(G4double) * CHAR_BIT == 64)
107 {
108 destroy_pool_align(buffer);
109 }
110 else
111 {
112 destroy_pool(buffer);
113 }
114}
void destroy_pool_align(G4double *&buffer)
void destroy_pool(G4double *&buffer)

Member Function Documentation

◆ flat()

G4double G4UniformRandPool::flat ( )
static

Definition at line 210 of file G4UniformRandPool.cc.

211{
212 if(rndpool == nullptr)
213 {
214 rndpool = new G4UniformRandPool;
215 G4AutoDelete::Register(rndpool);
216 }
217 return rndpool->GetOne();
218}
void Register(T *inst)

◆ flatArray()

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

Definition at line 220 of file G4UniformRandPool.cc.

221{
222 if(rndpool == nullptr)
223 {
224 rndpool = new G4UniformRandPool;
225 G4AutoDelete::Register(rndpool);
226 }
227 rndpool->GetMany(rnds, (unsigned int) howmany);
228}

◆ GetMany()

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

Definition at line 138 of file G4UniformRandPool.cc.

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

◆ 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}

◆ GetPoolSize()

G4int G4UniformRandPool::GetPoolSize ( ) const
inline

Definition at line 94 of file G4UniformRandPool.hh.

95{
96 return size;
97}

◆ Resize()

void G4UniformRandPool::Resize ( G4int newSize)

Definition at line 116 of file G4UniformRandPool.cc.

117{
118 if(newSize != size)
119 {
120 destroy_pool(buffer);
121 create_pool(buffer, newSize);
122 size = newSize;
123 currentIdx = 0;
124 }
125 currentIdx = 0;
126}

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