Bridge++  Ver. 2.0.2
randomNumbers_Mseries.cpp
Go to the documentation of this file.
1 
14 #include "randomNumbers_Mseries.h"
15 
16 
17 #ifdef USE_FACTORY_AUTOREGISTER
18 namespace {
19  bool init = RandomNumbers_Mseries::register_factory();
20 }
21 #endif
22 
23 
24 const std::string RandomNumbers_Mseries::class_name = "RandomNumbers_Mseries";
25 const double RandomNumbers_Mseries::Fnorm = 4.656612870908988e-10;
26 
27 //====================================================================
28 void RandomNumbers_Mseries::reset(unsigned long seed)
29 {
30  initset((int)seed);
31 }
32 
33 
34 //====================================================================
35 void RandomNumbers_Mseries::initset(const int ndelay)
36 {
37  vout.general(m_vl, " Random number init: ndelay =%10d\n", ndelay);
38 
39  int IP = Np;
40  int IQ = Nq;
41 
42  int MACRM = 40;
43  int MACRI = 1;
44  // unsigned IB[IP];
45  unsigned IB[Np];
46 
47  unsigned IX = MACRI;
48  for (int i = 0; i < IP; ++i) {
49  IX = IX * 69069;
50  IB[i] = IX >> 31;
51  }
52 
53  int JR = 0;
54  int KR = IP - IQ;
55  for (int j = 0; j < IP; ++j) {
56  unsigned iwork = 0;
57 
58  for (int i = 0; i < 32; ++i) {
59  iwork = iwork * 2 + IB[JR];
60  IB[JR] = IB[JR] ^ IB[KR];
61  ++JR;
62  if (JR == IP) JR = 0;
63  ++KR;
64  if (KR == IP) KR = 0;
65  }
66 
67  w[j] = iwork >> 1;
68  }
69 
70  jr = JR;
71  kr = KR;
72 
73  delay3(ndelay);
74 }
75 
76 
77 //====================================================================
78 void RandomNumbers_Mseries::delay3(const int ndelay)
79 {
80  int IP = Np;
81  int IQ = Nq;
82 
83  int LAMBDA = ndelay;
84 
85  int MACRM = 40;
86  int MACRI = 1;
87 
88  //unsigned IWK[2 * IP - 1];
89  //unsigned C[2 * IP];
90  //unsigned IB[IP + 32];
91  unsigned IWK[2 * Np - 1];
92  unsigned C[2 * Np];
93  unsigned IB[Np + 32];
94 
95  int MU = MACRM;
96 
97  for (int i = 0; i < IP; ++i) {
98  IWK[i] = w[i];
99  }
100 
101  for (int i = IP; i < 2 * IP - 1; ++i) {
102  IWK[i] = IWK[i - IP] ^ IWK[i - IQ];
103  }
104 
105  for (int i = 0; i < MU; ++i) {
106  IB[i] = 0;
107  }
108  int M = LAMBDA;
109  int NB = MU - 1;
110 
111 continued220:
112  if (M <= IP - 1) goto continued300;
113  ++NB;
114  IB[NB] = M % 2;
115  M = M / 2;
116  goto continued220;
117 
118 continued300:
119  for (int i = 0; i < IP; ++i) {
120  C[i] = 0;
121  }
122 
123  C[M] = 1;
124  for (int j = NB; j >= 0; --j) {
125  for (int i = IP - 1; i >= 0; --i) {
126  C[2 * i + IB[j]] = C[i];
127  C[2 * i + 1 - IB[j]] = 0;
128  }
129  for (int i = 2 * IP - 1; i >= IP; --i) {
130  C[i - IP] = C[i - IP] ^ C[i];
131  C[i - IQ] = C[i - IQ] ^ C[i];
132  }
133  }
134 
135  for (int j = 0; j < IP; ++j) {
136  unsigned iwork = 0;
137  for (int i = 0; i < IP; ++i) {
138  iwork = iwork ^ (C[i] * IWK[j + i]);
139  }
140  w[j] = iwork;
141  }
142 }
143 
144 
145 //====================================================================
146 void RandomNumbers_Mseries::write_file(const std::string& filename)
147 {
148  vout.general(m_vl, "%s: write down to file = %s\n", class_name.c_str(), filename.c_str());
149 
150  if (Communicator::is_primary()) {
151  std::ofstream outfile;
152  outfile.open(filename.c_str());
153 
154  if (!outfile) {
155  vout.crucial(m_vl, "Error at %s: unable to open output file.\n", class_name.c_str());
156  exit(EXIT_FAILURE);
157  }
158 
159  outfile << " " << jr << " " << kr << std::endl;
160 
161  for (int i = 0; i < Np; i++) {
162  outfile << " " << w[i] << std::endl;
163  }
164 
165  outfile.close();
166  }
167 }
168 
169 
170 //====================================================================
171 void RandomNumbers_Mseries::read_file(const std::string& filename)
172 {
173  vout.general(m_vl, "%s: read from file = %s\n", class_name.c_str(), filename.c_str());
174 
175  if (Communicator::is_primary()) {
176  std::ifstream infile;
177  infile.open(filename.c_str());
178 
179  if (!infile) {
180  vout.crucial(m_vl, "Error at %s: unable to open input file.\n", class_name.c_str());
181  exit(EXIT_FAILURE);
182  }
183 
184  infile >> jr >> kr;
185 
186  for (int i = 0; i < Np; i++) {
187  infile >> w[i];
188  }
189 
190  infile.close();
191  }
192 
194  Communicator::broadcast(1, &jr, 0);
195  Communicator::broadcast(1, &kr, 0);
196 }
197 
198 
199 //============================================================END=====
randomNumbers_Mseries.h
RandomNumbers_Mseries::class_name
static const std::string class_name
Definition: randomNumbers_Mseries.h:52
Communicator::broadcast
static int broadcast(int count, dcomplex *data, int sender)
broadcast array of dcomplex from sender.
Definition: communicator.cpp:170
RandomNumbers_Mseries::w
int w[Np]
Definition: randomNumbers_Mseries.h:58
RandomNumbers_Mseries::delay3
void delay3(const int ndelay)
Definition: randomNumbers_Mseries.cpp:78
RandomNumbers_Mseries::Nq
static constexpr int Nq
Definition: randomNumbers_Mseries.h:57
RandomNumbers_Mseries::Np
static constexpr int Np
Definition: randomNumbers_Mseries.h:56
RandomNumbers_Mseries::initset
void initset(const int ndelay)
Definition: randomNumbers_Mseries.cpp:35
RandomNumbers_Mseries::reset
void reset(unsigned long seed)
reset state with new seed.
Definition: randomNumbers_Mseries.cpp:28
RandomNumbers_Mseries::Fnorm
static const double Fnorm
initialized in .cpp file.
Definition: randomNumbers_Mseries.h:49
RandomNumbers_Mseries::write_file
void write_file(const std::string &)
Definition: randomNumbers_Mseries.cpp:146
RandomNumbers_Mseries::kr
int kr
Definition: randomNumbers_Mseries.h:59
Communicator::is_primary
static bool is_primary()
check if the present node is primary in small communicator.
Definition: communicator.cpp:60
RandomNumbers::m_vl
Bridge::VerboseLevel m_vl
Definition: randomNumbers.h:49
Bridge::BridgeIO::crucial
void crucial(const char *format,...)
Definition: bridgeIO.cpp:180
RandomNumbers_Mseries::read_file
void read_file(const std::string &)
save and load random number status.
Definition: randomNumbers_Mseries.cpp:171
Bridge::BridgeIO::general
void general(const char *format,...)
Definition: bridgeIO.cpp:200
Bridge::vout
BridgeIO vout
Definition: bridgeIO.cpp:512
RandomNumbers_Mseries::jr
int jr
Definition: randomNumbers_Mseries.h:59