Geant4 10.7.0
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 76 of file G4UniformRandPool.cc.

78 , buffer(0)
79 , currentIdx(0)
80{
81 if(sizeof(G4double) * CHAR_BIT == 64)
82 {
83 create_pool_align(buffer, size);
84 }
85 else
86 {
87 create_pool(buffer, size);
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

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 {
99 create_pool_align(buffer, size);
100 }
101 else
102 {
103 create_pool(buffer, size);
104 }
105 Fill(size);
106}

◆ ~G4UniformRandPool()

G4UniformRandPool::~G4UniformRandPool ( )

Definition at line 108 of file G4UniformRandPool.cc.

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

Member Function Documentation

◆ flat()

G4double G4UniformRandPool::flat ( )
static

Definition at line 214 of file G4UniformRandPool.cc.

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

◆ flatArray()

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

Definition at line 224 of file G4UniformRandPool.cc.

225{
226 if(rndpool == 0)
227 {
228 rndpool = new G4UniformRandPool;
229 G4AutoDelete::Register(rndpool);
230 }
231 rndpool->GetMany(rnds, (unsigned int) howmany);
232}

◆ 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 {
196 Fill(currentIdx < size ? currentIdx : size);
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

◆ 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 120 of file G4UniformRandPool.cc.

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

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