OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
RNGLattice.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef RNG_LATTICE_H
12 #define RNG_LATTICE_H
13 
14 /***********************************************************************
15  *
16  * class RNGLattice
17  * class RNGLatticeSequence : public SequenceGen<RNGLattice>
18  *
19  * Lattice class that implements random number generator by just
20  * providing an evenly spaced set of points between two endpoints.
21  *
22  * Note that this only works properly automatically on 1 PE; with multipple PE's
23  * you'll get a redundant set of points, the same on every PE, unless you take
24  * care to provide appropriate different seeds for each PE and call SetSeed
25  * before using it.
26  * Use RNGLattice as a scalar or container element, and use
27  * RNGLatticeSequence when you need a sequence of numbers to fill a container.
28  *
29  ***********************************************************************/
30 
31 #include "Utility/SequenceGen.h"
32 
33 
34 template<class T>
35 class RNGLattice {
36 
37 public:
38  // return type
39  typedef T Return_t;
40 
41  // constructor
42  RNGLattice(T minval, T maxval, unsigned long numpoints,
43  bool includeEndpoints = true)
44  : MinVal(minval), MaxVal(maxval), CurrPoint(0), NumPoints(numpoints),
45  IncludeEndpoints(includeEndpoints)
46  {
47  if (IncludeEndpoints) {
48  if (NumPoints == 0 || NumPoints == 1) {
49  Spacing = 0;
50  }
51  else {
52  Spacing = (MaxVal - MinVal)/(NumPoints - 1);
53  }
54  }
55  else {
56  if (NumPoints == 0) {
57  Spacing = 0;
58  }
59  else {
61  }
62  }
63  }
64 
65  // copy constructor
67  : MinVal(rng.MinVal), MaxVal(rng.MaxVal), CurrPoint(rng.CurrPoint),
70 
71  // destructor
72  ~RNGLattice(void) {}
73 
74  // advance current point by n, modulo NumPoints
75  inline void AdvanceSeed(unsigned long n = 0) {
77  return;
78  }
79 
80  // set current point to nth point, modulo NumPoints
81  inline void SetSeed(unsigned long n = 0) {
82  CurrPoint = n % NumPoints;
83  }
84 
85  // get current point
86  inline unsigned long GetSeed(void) const { return CurrPoint; }
87 
88  // return the next pseudo-random number (from MinVal ... MaxVal)
89  inline Return_t GetRandom(void) const {
90  Return_t currVal;
91  if (IncludeEndpoints)
92  currVal = MinVal + CurrPoint * Spacing;
93  else
94  currVal = MinVal + (CurrPoint + 0.5) * Spacing;
95  CurrPoint++;
96  if (CurrPoint == NumPoints) CurrPoint = 0;
97  return currVal;
98  }
99 
100  // pseudonym for GetRandom()
101  inline Return_t operator()(void) const { return GetRandom(); }
102 
103  // conversion to Return_t, same as GetRandom()
104  inline operator Return_t() const { return GetRandom(); }
105 
106  // return the period of the RNG
107  unsigned long GetRandMax(void) const { return NumPoints; }
108 
109 private:
111  mutable unsigned long CurrPoint;
112  unsigned long NumPoints;
114 };
115 
116 #ifdef IPPL_USE_SINGLE_PRECISION
118 #else
120 #endif
121 
122 // A version of RNGLattice with extra constructors to make using this
123 // class easier. This is the version that people should use to fill
124 // containers with a random number sequence in an expression. This
125 // class is PETE-aware via its inheritance from SequenceGen.
126 
127 template<class T>
128 class RNGLatticeSequence : public SequenceGen< RNGLattice<T> > {
129 
130 public:
131  // constructor
132  RNGLatticeSequence(T minval, T maxval, unsigned long numpoints,
133  bool includeEndpoints = true)
134  : SequenceGen< RNGLattice<T> >(RNGLattice<T>(minval,maxval,numpoints,
135  includeEndpoints)) {}
136 
137  // copy constructor
139  : SequenceGen< RNGLattice<T> >(rngseq.getGenerator()) {}
140 
141  // destructor
143 
144  // wrappers around RNG generator functions
145  inline void AdvanceSeed(unsigned long adv = 0) {
146  this->getGenerator().AdvanceSeed(adv);
147  }
148  inline void SetSeed(unsigned long seed) { this->getGenerator().SetSeed(seed); }
149  inline unsigned long GetSeed(void) const { return this->getGenerator().GetSeed(); }
150  inline
151  typename RNGLattice<T>::Return_t
152  GetRandom(void) { return this->getGenerator().GetRandom(); }
153  inline
154  typename RNGLattice<T>::Return_t
155  operator()(void) { return this->getGenerator().GetRandom(); }
156  unsigned long GetRandMax(void) const { return this->getGenerator().GetRandMax(); }
157 };
158 
159 
160 #endif // RNG_LATTICE_H
161 
162 /***************************************************************************
163  * $RCSfile: RNGLattice.h,v $ $Author: adelmann $
164  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
165  * IPPL_VERSION_ID: $Id: RNGLattice.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
166  ***************************************************************************/
167 
int seed
The current random seed.
Definition: Options.cpp:41
unsigned long NumPoints
Definition: RNGLattice.h:112
Definition: rbendmap.h:8
#define RNG_BASIC_MATH(GEN)
Definition: SequenceGen.h:65
void AdvanceSeed(unsigned long adv=0)
Definition: RNGLattice.h:145
Return_t operator()(void) const
Definition: RNGLattice.h:101
unsigned long GetRandMax(void) const
Definition: RNGLattice.h:156
void SetSeed(unsigned long n=0)
Definition: RNGLattice.h:81
RNGLattice(T minval, T maxval, unsigned long numpoints, bool includeEndpoints=true)
Definition: RNGLattice.h:42
RNGLattice< T >::Return_t GetRandom(void)
Definition: RNGLattice.h:152
Return_t GetRandom(void) const
Definition: RNGLattice.h:89
bool IncludeEndpoints
Definition: RNGLattice.h:113
void SetSeed(unsigned long seed)
Definition: RNGLattice.h:148
RNGLatticeSequence(T minval, T maxval, unsigned long numpoints, bool includeEndpoints=true)
Definition: RNGLattice.h:132
RNGLattice(const RNGLattice< T > &rng)
Definition: RNGLattice.h:66
~RNGLatticeSequence(void)
Definition: RNGLattice.h:142
RNGLatticeSequence(const RNGLatticeSequence< T > &rngseq)
Definition: RNGLattice.h:138
RNGLattice< T >::Return_t operator()(void)
Definition: RNGLattice.h:155
unsigned long CurrPoint
Definition: RNGLattice.h:111
unsigned long GetRandMax(void) const
Definition: RNGLattice.h:107
RNGLattice< T > & getGenerator()
Definition: SequenceGen.h:92
void AdvanceSeed(unsigned long n=0)
Definition: RNGLattice.h:75
unsigned long GetSeed(void) const
Definition: RNGLattice.h:149
unsigned long GetSeed(void) const
Definition: RNGLattice.h:86
~RNGLattice(void)
Definition: RNGLattice.h:72