OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
RNGXCI.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_XCI_H
12 #define RNG_XCI_H
13 
14 /***********************************************************************
15  *
16  * class RNGXCI
17  * class RNGXCISequence : public SequenceGen<RNGXCI>
18  *
19  * RNGXCI is a simple class that implements random number generator from LANL
20  * XCI group (Forrest Brown), in the range (0...1). The first class is used
21  * as a scalar, as an element of a Field or ParticleAttrib, or as a template
22  * parameter to SequenceGen. The second class is derived from SequenceGen,
23  * and makes it easier to use this RNG in an expression as a sequence of
24  * numbers that will fill a Field or ParticleAttrib container.
25  * Use RNGXCI as a scalar or container element, and use
26  * RNGXCISequence when you need a sequence of numbers to fill a container.
27  *
28  ***********************************************************************/
29 
30 // include files
31 #include "Utility/SequenceGen.h"
32 
33 #ifdef IPPL_USE_SINGLE_PRECISION
34  #define CTYPE float
35 #else
36  #define CTYPE double
37 #endif
38 
39 // define type that we ensure is 8 bytes long
40 #define LONG_IS_8_BYTES (((1L<<16)<<16)<<16)
41 typedef long long RNlong;
42 
43 
44 class RNGXCI {
45 
46 public:
47  // return type
48  typedef CTYPE Return_t;
49 
50 public:
51  // default constructor
52  RNGXCI(RNlong advance = 0L)
53  : Seed(1L) { AdvanceSeed(advance); }
54 
55  // copy constructor
56  RNGXCI(const RNGXCI& rng)
57  : Seed(rng.Seed) {}
58 
59  // destructor
60  ~RNGXCI(void) {}
61 
62  // advance seed for random number generator n times
63  inline void AdvanceSeed(RNlong n = 0L) {
64  // skip ahead n RNs: Seed*RN_MULT^n mod RN_MOD
65  while( n<0 ) n += RN_PERIOD; // add period till >0
66  n &= RN_MASK; // mod RN_MOD
67  RNlong gen=1L, g=RN_MULT; // get gen=RN_MULT^n, in
68  for( ; n; n>>=1 ) { // log2(n) ops, not n ops !
69  if( n&1 ) gen = gen*g & RN_MASK;
70  g = g*g & RN_MASK;
71  }
72  Seed = gen*Seed & RN_MASK;
73  return;
74  }
75 
76  // set seed to user-specified value
77  inline void SetSeed(RNlong s) {
78  Seed = (s|1) & RN_MASK; // must be odd to get full period
79  }
80 
81  // get seed value
82  inline RNlong GetSeed(void) const { return Seed; }
83 
84  // return the next pseudo-random number (from 0 ... 1)
85  inline Return_t GetRandom(void) const {
86  Return_t r = Seed*RN_NORM;
88  return r;
89  }
90 
91  // pseudonym for GetRandom()
92  inline Return_t operator()(void) const { return GetRandom(); }
93 
94  // conversion to Return_t, same as GetRandom()
95  inline operator Return_t() const { return GetRandom(); }
96 
97  // return the period of the RNG
98  static Return_t GetRandMax(void) { return Return_t(RN_MASK); }
99 
100 private:
101  mutable RNlong Seed;
102 
103  // constants for RN gen: Seed = Seed*RN_MULT mod RN_MOD
104  static const RNlong RN_MULT; // 5^19
105  static const RNlong RN_MOD; // 2^48
106  static const RNlong RN_PERIOD; // period
107  static const double RN_NORM; // normalize to (0,1)
108  static const RNlong RN_MASK; // 48-bit mask
109 };
110 
112 
113 
114 // A version of SequenceGen with extra constructors to make using this
115 // class easier. This is the version that people should use to fill
116 // containers with a random number sequence in an expression. This
117 // class is PETE-aware via its inheritance from SequenceGen.
118 
120 
121 public:
122  // default constructor
123  RNGXCISequence(int advance = 0)
124  : SequenceGen<RNGXCI>(RNGXCI(advance)) {}
125 
126  // copy constructor
128  : SequenceGen<RNGXCI>(rngseq.getGenerator()) {}
129 
130  // destructor
132 
133  // wrappers around RNG generator functions
134  inline void AdvanceSeed(RNlong adv = 0L) {
135  getGenerator().AdvanceSeed(adv);
136  }
137  inline void SetSeed(RNlong seed) { getGenerator().SetSeed(seed); }
138  inline RNlong GetSeed(void) const { return getGenerator().GetSeed(); }
139  inline Return_t GetRandom(void) { return getGenerator().GetRandom(); }
140  inline Return_t operator()(void) { return getGenerator().GetRandom(); }
141  static Return_t GetRandMax(void) { return RNGXCI::GetRandMax(); }
142 };
143 
144 
145 #endif // RNG_XCI_H
146 
147 /***************************************************************************
148  * $RCSfile: RNGXCI.h,v $ $Author: adelmann $
149  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
150  * IPPL_VERSION_ID: $Id: RNGXCI.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
151  ***************************************************************************/
152 
int seed
The current random seed.
Definition: Options.cpp:41
static const double RN_NORM
Definition: RNGXCI.h:107
#define RNG_BASIC_MATH(GEN)
Definition: SequenceGen.h:65
#define CTYPE
Definition: RNGXCI.h:36
Return_t operator()(void) const
Definition: RNGXCI.h:92
RNGXCISequence(const RNGXCISequence &rngseq)
Definition: RNGXCI.h:127
RNGXCI(RNlong advance=0L)
Definition: RNGXCI.h:52
static const RNlong RN_MOD
Definition: RNGXCI.h:105
double Return_t
Definition: RNGXCI.h:48
void SetSeed(RNlong seed)
Definition: RNGXCI.h:137
static Return_t GetRandMax(void)
Definition: RNGXCI.h:141
RNGXCI::Return_t Return_t
Definition: SequenceGen.h:89
RNlong GetSeed(void) const
Definition: RNGXCI.h:138
~RNGXCI(void)
Definition: RNGXCI.h:60
RNGXCISequence(int advance=0)
Definition: RNGXCI.h:123
static const RNlong RN_PERIOD
Definition: RNGXCI.h:106
~RNGXCISequence(void)
Definition: RNGXCI.h:131
RNGXCI(const RNGXCI &rng)
Definition: RNGXCI.h:56
static const RNlong RN_MASK
Definition: RNGXCI.h:108
Return_t operator()(void)
Definition: RNGXCI.h:140
static Return_t GetRandMax(void)
Definition: RNGXCI.h:98
void AdvanceSeed(RNlong adv=0L)
Definition: RNGXCI.h:134
static const RNlong RN_MULT
Definition: RNGXCI.h:104
Return_t GetRandom(void) const
Definition: RNGXCI.h:85
long long RNlong
Definition: RNGXCI.h:41
void SetSeed(RNlong s)
Definition: RNGXCI.h:77
RNlong GetSeed(void) const
Definition: RNGXCI.h:82
Definition: RNGXCI.h:44
Return_t GetRandom(void)
Definition: RNGXCI.h:139
void AdvanceSeed(RNlong n=0L)
Definition: RNGXCI.h:63
RNlong Seed
Definition: RNGXCI.h:101