Bridge++  Ver. 1.3.x
test_RandomNumbers_MT19937_Global.cpp
Go to the documentation of this file.
1 
14 #include "test.h"
15 
16 #include "randomNumbers_MT19937.h"
17 #include "field_G.h"
18 #include "fieldIO_Text.h" // for gather field
19 #include "io_format_gauge.h"
20 
21 //====================================================================
23 
33  const std::string test_name = "RandomNumbers.MT19937.Global";
34 
35  //- test-private parameters
36  namespace {
37  const std::string filename_input = "test_RandomNumbers_MT19937_Global.yaml";
38  const std::string filename_output = "stdout";
39 
40  class Parameters_Test_RandomNumbers : public Parameters {
41  public:
42  Parameters_Test_RandomNumbers()
43  {
44  Register_int("seed", 0);
45 
46  Register_string("verbose_level", "NULL");
47 
48  Register_double("expected_result", 0.0);
49  }
50  };
51  }
52 
53  //- prototype declaration
54  int test_global(void);
55 
56 #ifdef USE_TESTMANAGER_AUTOREGISTER
57  namespace {
58  static const bool is_registered = TestManager::RegisterTest(
59  test_name,
61  );
62  }
63 #endif
64 
65  //====================================================================
66  int test_global(void)
67  {
68  // #### parameter setup ####
69  unique_ptr<Parameters> params_test(new Parameters_Test_RandomNumbers);
70  unique_ptr<Parameters> params_all(new Parameters);
71 
72  params_all->Register_Parameters("Test_RandomNumbers", params_test);
73 
74  ParameterManager::read(filename_input, params_all);
75 
76  int seed = params_test->get_int("seed");
77  const string str_vlevel = params_test->get_string("verbose_level");
78 
79  const bool do_check = params_test->is_set("expected_result");
80  const double expected_result = do_check ? params_test->get_double("expected_result") : 0.0;
81 
83 
84  //- print input parameters
85  vout.general(vl, " seed = %d\n", seed);
86  vout.general(vl, " vlevel = %s\n", str_vlevel.c_str());
87  vout.general(vl, "\n");
88 
89 
90  // #### object setup #####
91  unique_ptr<Timer> timer(new Timer(test_name));
92 
93 
94  // #### Execution main part ####
95  timer->start();
96 
97  vout.general(vl, "\n");
98  vout.general(vl, "Serial and Node-parallel test of Random Number Generator:\n");
99 
100  // sample field size
101  int nin = 8;
102  int nex = 4;
103 
104  int nvol = CommonParameters::Nvol();
105  int lvol = CommonParameters::Lvol();
106 
107 
108  // buffer for checks
109  const size_t nsample = 1024;
110  double data[nsample];
111  for (size_t i = 0; i < nsample; ++i) {
112  data[i] = 0.0;
113  }
114 
115 
116  // 1. generate field in parallel
117  Field field1(nin, nvol, nex);
118 
119  if (true) {
122 
123  // fill field with uniform random numbers
124  rand->uniform_lex_global(field1);
125 
126  // generate additional random numbers to check rng state.
127  for (size_t i = 0; i < nsample; ++i) {
128  data[i] = rand->get();
129  }
130  }
131 
132  // 2. generate field at rank 0 with the same seed
133  Field field2(0, 0, 0);
134 
135  if (Communicator::is_primary()) {
137 
138  field2.reset(nin, lvol, nex);
139 
140  double *p = field2.ptr(0);
141 
142  for (size_t i = 0, n = field2.size(); i < n; ++i) {
143  *p++ = rand->get();
144  }
145  }
146 
147  // 3. gather parallel field to rank 0
148  Field field1b(0, 0, 0);
149 
150  if (Communicator::is_primary()) {
151  field1b.reset(nin, lvol, nex);
152  }
153 
155 
156  fieldio.gather(&field1b, &field1);
157 
158  // 4. compare
159  int err1 = 0;
160 
161  if (Communicator::is_primary()) {
162  if (field1b.size() != field2.size()) {
163  vout.crucial(vl, "%s: field size mismatch.\n", test_name.c_str());
164  exit(EXIT_FAILURE);
165  }
166 
167  double *p1 = field1b.ptr(0);
168  double *p2 = field2.ptr(0);
169 
170  for (size_t i = 0, n = field2.size(); i < n; ++i) {
171  if (*p1++ != *p2++) ++err1;
172  }
173  }
174 
175  Communicator::broadcast(1, &err1, 0);
176  vout.general(vl, "%s: serial and parallel: err = %d\n", test_name.c_str(), err1);
177 
178  // 5. check rng state
179  int err2 = 0;
180  double buf[nsample];
181 
182  for (int ipe = 1, npe = Communicator::size(); ipe < npe; ++ipe) {
183  Communicator::send_1to1(nsample, buf, data, 0, ipe, ipe);
184 
185  if (Communicator::is_primary()) {
186  for (size_t i = 0; i < nsample; ++i) {
187  if (data[i] != buf[i]) ++err2;
188  }
189  }
190 
191  Communicator::broadcast(1, &err2, 0);
192  vout.general(vl, "%s: check local state at rank %d, err = %d\n", test_name.c_str(), ipe, err2);
193  }
194 
195  // 6. check save and restore
196  int err3 = 0;
197 
198  if (true) {
201 
202  // save current state to file
203  rand->writefile("RNG_MT19937.state");
204 
205  vout.detailed("%s: number of samples = %lu\n", test_name.c_str(), nsample);
206 
207  for (size_t i = 0; i < nsample; ++i) {
208  data[i] = rand->get();
209  }
210 
211  // restore state from file
212  rand->readfile("RNG_MT19937.state");
213 
214  // check if same series are generated.
215  int err3_part = 0;
216 
217  for (size_t i = 0; i < nsample; ++i) {
218  if (data[i] != rand->get()) ++err3_part;
219  }
220 
221  Communicator::reduce_sum(1, &err3, &err3_part);
222 
223  vout.general(vl, "%s: save/restore test: err = %d\n", test_name.c_str(), err3);
224  }
225 
226  // 7. check gaussian field
227  field1.reset(nin, nvol, nex);
228 
229  if (true) {
232 
233  // fill field with gaussian random numbers
234  rand->gauss_lex_global(field1);
235  }
236 
237  if (Communicator::is_primary()) {
239 
240  field2.reset(nin, lvol, nex);
241 
242  double *p = field2.ptr(0);
243 
244  if (field2.nin() % 2 == 0) {
245  double r1, r2;
246 
247  for (int j = 0, Nex = field2.nex(); j < Nex; ++j) {
248  for (int isite = 0, Nvol = field2.nvol(); isite < Nvol; ++isite) {
249  for (int i = 0, Nin = field2.nin(); i < Nin; i += 2) {
250  rand->gauss(r1, r2);
251  *p++ = r1;
252  *p++ = r2;
253  }
254  }
255  }
256  } else {
257  double r1, r2;
258 
259  for (int j = 0, Nex = field2.nex(); j < Nex; ++j) {
260  for (int isite = 0, Nvol = field2.nvol(); isite < Nvol; ++isite) {
261  for (int i = 0, Nin = field2.nin(); i < Nin; ++i) {
262  rand->gauss(r1, r2);
263  *p++ = r1;
264  }
265  }
266  }
267  }
268  }
269 
270  if (Communicator::is_primary()) {
271  field1b.reset(nin, lvol, nex);
272  }
273 
274  fieldio.gather(&field1b, &field1);
275 
276  int err4 = 0;
277 
278  if (Communicator::is_primary()) {
279  if (field1b.size() != field2.size()) {
280  vout.crucial(vl, "%s: field size mismatch.\n", test_name.c_str());
281  exit(EXIT_FAILURE);
282  }
283 
284  double *p1 = field1b.ptr(0);
285  double *p2 = field2.ptr(0);
286 
287  for (size_t i = 0, n = field2.size(); i < n; ++i) {
288  if (*p1++ != *p2++) ++err4;
289  }
290  }
291 
292  Communicator::broadcast(1, &err4, 0);
293  vout.general(vl, "%s: serial and parallel for gaussian: err = %d\n", test_name.c_str(), err4);
294 
295 
296  // 8. summary
297  double result = err1 + err2 + err3 + err4;
298 
299  timer->report();
300 
301 
302  if (do_check) {
303  return Test::verify(result, expected_result);
304  } else {
305  vout.detailed(vl, "check skipped: expected_result not set.\n\n");
306  return EXIT_SKIP;
307  }
308  }
309 } // namespace Test_RandomNumbers
#define EXIT_SKIP
Definition: test.h:17
BridgeIO vout
Definition: bridgeIO.cpp:278
void writefile(const std::string &)
void detailed(const char *format,...)
Definition: bridgeIO.cpp:82
virtual double get()=0
const double * ptr(const int jin, const int site, const int jex) const
Definition: field.h:133
void set_parameter_verboselevel(const Bridge::VerboseLevel vl)
Definition: randomNumbers.h:61
virtual void uniform_lex_global(Field &)
uniform random number defined on global lattice.
void general(const char *format,...)
Definition: bridgeIO.cpp:65
Container of Field-type object.
Definition: field.h:39
int nvol() const
Definition: field.h:116
FieldIO_Text class for file I/O of Field data in plain text format.
Definition: fieldIO_Text.h:32
const Format * ILDG
Definition: io_format.cpp:28
void readfile(const std::string &)
int get_int(const string &key) const
Definition: parameters.cpp:42
Class for parameters.
Definition: parameters.h:38
static int Lvol()
virtual void gauss_lex_global(Field &)
gaussian random number defined on global lattice.
static int send_1to1(int count, double *recv_buf, double *send_buf, int p_to, int p_from, int tag)
send array of double from rank p_from to rank p_to. communication distinguished by tag...
int nin() const
Definition: field.h:115
static bool RegisterTest(const std::string &key, const Test_function func)
Definition: testManager.h:80
bool is_set(const string &) const
Definition: parameters.cpp:372
double get_double(const string &key) const
Definition: parameters.cpp:27
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=COMPLEX)
Definition: field.h:84
int nex() const
Definition: field.h:117
void start()
Definition: timer.cpp:44
void crucial(const char *format,...)
Definition: bridgeIO.cpp:48
void Register_Parameters(const string &, Parameters *const)
Definition: parameters.cpp:358
int verify(const double result, const double expected, double eps)
Definition: test.cpp:27
static int size()
size of small world.
Bridge::VerboseLevel vl
Definition: checker.cpp:18
VerboseLevel
Definition: bridgeIO.h:39
static int reduce_sum(int count, double *recv_buf, double *send_buf, int pattern=0)
make a global sum of an array of double over the communicator. pattern specifies the dimensions to be...
static void read(const std::string &params_file, Parameters *params)
static int broadcast(int count, double *data, int sender)
broadcast array of double from sender.
Definition: timer.h:31
static bool is_primary()
check if the present node is primary in small communicator.
string get_string(const string &key) const
Definition: parameters.cpp:87
void gather(Field *vglobal, Field *vlocal)
gather data on parallel nodes to primary node.
Definition: fieldIO.cpp:116
void report(const Bridge::VerboseLevel vl=Bridge::GENERAL)
Definition: timer.cpp:128
void gauss(double &rand1, double &rand2)
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:28
int size() const
Definition: field.h:121