Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 54 of file G4UniformRandPool.hh.

Constructor & Destructor Documentation

G4UniformRandPool::G4UniformRandPool ( )

Definition at line 103 of file G4UniformRandPool.cc.

104  : size(G4UNIFORMRANDPOOL_DEFAULT_POOLSIZE), buffer(0), currentIdx(0)
105 {
106  if ( sizeof(G4double)*CHAR_BIT==64 )
107  {
108  create_pool_align(buffer,size);
109  }
110  else
111  {
112  create_pool(buffer,size);
113  }
114  Fill(size);
115 }
#define G4UNIFORMRANDPOOL_DEFAULT_POOLSIZE
void create_pool_align(G4double *&buffer, G4int ps)
void create_pool(G4double *&buffer, G4int ps)
double G4double
Definition: G4Types.hh:76

Here is the call graph for this function:

Here is the caller graph for this function:

G4UniformRandPool::G4UniformRandPool ( G4int  ps)

Definition at line 117 of file G4UniformRandPool.cc.

118  : size(siz), buffer(0), currentIdx(0)
119 {
120  if ( sizeof(G4double)*CHAR_BIT==64 )
121  {
122  create_pool_align(buffer,size);
123  }
124  else
125  {
126  create_pool(buffer,size);
127  }
128  Fill(size);
129 
130 }
void create_pool_align(G4double *&buffer, G4int ps)
void create_pool(G4double *&buffer, G4int ps)
double G4double
Definition: G4Types.hh:76

Here is the call graph for this function:

G4UniformRandPool::~G4UniformRandPool ( )

Definition at line 132 of file G4UniformRandPool.cc.

133 {
134  if ( sizeof(G4double)*CHAR_BIT==64 )
135  {
136  destroy_pool_align(buffer);
137  }
138  else
139  {
140  destroy_pool(buffer);
141  }
142 }
void destroy_pool(G4double *&buffer)
void destroy_pool_align(G4double *&buffer)
double G4double
Definition: G4Types.hh:76

Here is the call graph for this function:

Member Function Documentation

G4double G4UniformRandPool::flat ( )
static

Definition at line 231 of file G4UniformRandPool.cc.

232 {
233  if ( rndpool == 0 )
234  {
235  rndpool = new G4UniformRandPool;
236  G4AutoDelete::Register(rndpool);
237  }
238  return rndpool->GetOne();
239 }
void Register(T *inst)
Definition: G4AutoDelete.hh:65

Here is the call graph for this function:

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

Definition at line 241 of file G4UniformRandPool.cc.

242 {
243  if ( rndpool == 0 )
244  {
245  rndpool = new G4UniformRandPool;
246  G4AutoDelete::Register(rndpool);
247  }
248  rndpool->GetMany(rnds,(unsigned int)howmany);
249 }
void Register(T *inst)
Definition: G4AutoDelete.hh:65

Here is the call graph for this function:

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

Definition at line 156 of file G4UniformRandPool.cc.

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

Definition at line 84 of file G4UniformRandPool.hh.

85 {
86  // No more available numbers, re-fill
87  //
88  if ( currentIdx >= /*(unsigned int)*/size )
89  {
90  Fill(/*(unsigned int)*/size);
91  }
92 
93  return buffer[currentIdx++];
94 }
G4int G4UniformRandPool::GetPoolSize ( ) const
inline

Definition at line 96 of file G4UniformRandPool.hh.

97 {
98  return size;
99 }
void G4UniformRandPool::Resize ( G4int  newSize)

Definition at line 144 of file G4UniformRandPool.cc.

145 {
146  if ( newSize != size )
147  {
148  destroy_pool(buffer);
149  create_pool(buffer,newSize);
150  size=newSize;
151  currentIdx = 0;
152  }
153  currentIdx = 0;
154 }
void destroy_pool(G4double *&buffer)
void create_pool(G4double *&buffer, G4int ps)

Here is the call graph for this function:


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