Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fieldIO_LIME.cpp
Go to the documentation of this file.
1 
14 #include <fstream>
15 #include <string.h>
16 
17 #include "fieldIO_LIME.h"
18 #include "io_format_gauge.h"
19 
20 #if USE_ILDG_METADATA
21 #include "ildg_metadata.h"
22 #endif
23 
24 #include "bridgeIO.h"
25 using Bridge::vout;
26 
27 
28 #ifdef USE_LIMELIB
29 
30 //typedef unsigned short int n_uint16_t;
31 //typedef unsigned int n_uint32_t;
32 //typedef unsigned long int n_uint64_t;
33 
34 #ifdef off_t
35 #undef off_t
36 #endif
37 #define off_t n_uint64_t
38 
39 static const off_t block_size = 64;
40 
41 const std::string FieldIO_LIME::class_name = "FieldIO_LIME";
42 
43 #if USE_ILDG_METADATA
44 
45 //================================================================
46 void FieldIO_LIME::check_metadata(const ILDG_Format::Params *params)
47 {
48  // check if config is as expected.
49  const int Lx = CommonParameters::Lx();
50  const int Ly = CommonParameters::Ly();
51  const int Lz = CommonParameters::Lz();
52  const int Lt = CommonParameters::Lt();
53 
54  if (!((params->Lx == Lx) &&
55  (params->Ly == Ly) &&
56  (params->Lz == Lz) &&
57  (params->Lt == Lt))) {
58  vout.crucial(m_vl, "Error at %s: lattice size mismatch. config=(%d,%d,%d,%d), expected=(%d,%d,%d,%d).\n",
59  class_name.c_str(),
60  params->Lx, params->Ly, params->Lz, params->Lt,
61  Lx, Ly, Lz, Lt);
62  exit(EXIT_FAILURE);
63  }
64 
65  if (params->prec == 32) {
66  vout.detailed(m_vl, "ildg-format: single precision. extend to double\n");
67  } else {
68  vout.detailed(m_vl, "ildg-format: double precision\n");
69  }
70 }
71 
72 
73 //================================================================
74 void FieldIO_LIME::load_metadata(LimeReader *reader, ILDG_Format::Params *params)
75 {
76  off_t nbytes = limeReaderBytes(reader);
77 
78  vout.detailed(m_vl, "limeReaderBytes: %lu bytes to read.\n", nbytes);
79 
80  off_t nbytes_alloc = nbytes + ((nbytes % block_size) ? (block_size - (nbytes % block_size)) : 0);
81 
82  char *buf = (char *)malloc(nbytes_alloc);
83  if (buf == 0) {
84  vout.crucial(m_vl, "Error at %s: malloc failed.\n", class_name.c_str());
85  exit(EXIT_FAILURE);
86  }
87  vout.detailed(m_vl, "allocated %lu bytes\n", nbytes_alloc);
88 
89  int status = limeReaderReadData(buf, &nbytes, reader);
90  if (status != LIME_SUCCESS) {
91  vout.crucial(m_vl, "Error at %s: limeReaderReadData failed.\n", class_name.c_str());
92  exit(EXIT_FAILURE);
93  }
94 
95  // dump Metadata
96  vout.detailed(m_vl, "%s\n", buf);
97 
98  // process ILDG Metadata
99  ILDG_Format::Metadata md;
100  md.read_from_buffer(buf).extract(params);
101 
102  free(buf);
103 }
104 
105 
106 // endif of #if USE_ILDG_METADATA
107 #endif
108 
109 //================================================================
110 void FieldIO_LIME::load_lfn(LimeReader *reader)
111 {
112  off_t nbytes = limeReaderBytes(reader);
113 
114  vout.detailed(m_vl, "limeReaderBytes: %lu bytes to read.\n", nbytes);
115 
116  off_t nbytes_alloc = (nbytes + 1) + (block_size - ((nbytes + 1) % block_size));
117 
118  char *buf = (char *)malloc(nbytes_alloc);
119  if (!buf) {
120  vout.crucial(m_vl, "Error at %s: malloc failed.\n", class_name.c_str());
121  exit(EXIT_FAILURE);
122  }
123 
124  vout.detailed(m_vl, "allocated %lu bytes\n", nbytes_alloc);
125 
126  int status = limeReaderReadData(buf, &nbytes, reader);
127  if (status != LIME_SUCCESS) {
128  vout.crucial(m_vl, "Error at %s: limeReaderReadData failed.\n", class_name.c_str());
129  exit(EXIT_FAILURE);
130  }
131 
132  vout.detailed(m_vl, "limeReaderReadData: %lu bytes read.\n", nbytes);
133 
134  buf[nbytes] = '\0';
135 
136  vout.detailed(m_vl, "ildg-data-lfn: %s\n", buf);
137 
138  free(buf);
139 }
140 
141 
142 //====================================================================
143 void FieldIO_LIME::load_data(LimeReader *reader, Field *v)
144 {
145  off_t word_size = 8; //XXX assume 64bit precision
146 
147  off_t nbytes = limeReaderBytes(reader);
148 
149  vout.detailed(m_vl, "ildg-binary-data: limeReaderBytes: %lu bytes to read.\n", nbytes);
150 
151  // allocate memory for whole config data.
152  char *buf = (char *)malloc(nbytes);
153  if (!buf) {
154  vout.crucial(m_vl, "Error at %s: malloc failed.", __func__);
155  exit(EXIT_FAILURE);
156  }
157 
158  int status = limeReaderReadData(buf, &nbytes, reader);
159  if (status != LIME_SUCCESS) {
160  vout.crucial(m_vl, "Error at %s: malloc failed.", __func__);
161  exit(EXIT_FAILURE);
162  }
163 
164  // adjust byteorder
165  if (!FieldIO::is_bigendian()) {
166  byte_swap(buf, word_size, nbytes / word_size);
167  }
168 
169  // reorder and store
170  int nin_file = m_format->nin();
171  int nex_file = m_format->nex();
172 
173  if ((nin_file == 0) || (nex_file == 0)) {
174  nin_file = v->nin();
175  nex_file = v->nex();
176  }
177 
178  const int nvol = v->nvol();
179 
180  double *p = (double *)buf;
181 
182  for (int j = 0; j < nex_file; ++j) {
183  for (int isite = 0; isite < nvol; ++isite) {
184  for (int i = 0; i < nin_file; ++i) {
185  int s, t;
186  m_format->file_to_field(s, t, i, j);
187  v->set(s, isite, t, *p++);
188  }
189  }
190  }
191 
192  free(buf);
193 }
194 
195 
196 //====================================================================
197 void FieldIO_LIME::process_file(Field *v, const std::string filename)
198 {
199  FILE *fp = fopen(filename.c_str(), "r");
200 
201  if (!fp) {
202  vout.crucial(m_vl, "Error at %s: fopen failed.", __func__);
203  exit(EXIT_FAILURE);
204  }
205 
206  LimeReader *reader = limeCreateReader(fp);
207  if (!reader) {
208  vout.crucial(m_vl, "Error at %s: limeCreateReader failed.", __func__);
209  exit(EXIT_FAILURE);
210  }
211 
212 #if USE_ILDG_METADATA
213  ILDG_Format::Params params;
214 #endif
215 
216  // scan file for records.
217  for ( ; ;) {
218  int status = limeReaderNextRecord(reader);
219  if (status != LIME_SUCCESS) break;
220 
221  const char *t = limeReaderType(reader);
222 
223  if (strcmp(t, "ildg-format") == 0) {
224 #if USE_ILDG_METADATA
225  load_metadata(reader, &params);
226  check_metadata(&params);
227 #endif
228  } else if (strcmp(t, "ildg-binary-data") == 0) {
229  load_data(reader, v);
230  } else if (strcmp(t, "ildg-data-lfn") == 0) {
231  load_lfn(reader);
232  } else {
233  vout.detailed(m_vl, "%s: known record %s.\n", __func__, t);
234  }
235  } // end loop over records.
236 
237  limeDestroyReader(reader);
238 
239  fclose(fp);
240 }
241 
242 
243 //====================================================================
244 void FieldIO_LIME::read_file(Field *v, const std::string filename)
245 {
246  const int nin_field = v->nin();
247  const int nex_field = v->nex();
248 
249  const long_t Lvol = CommonParameters::Lvol();
250 
251  Field vtmp;
252 
253  if (Communicator::is_primary()) {
254  vout.detailed(m_vl, "reading gauge configuration from %s", filename.c_str());
255 
256  vtmp.reset(nin_field, Lvol, nex_field);
257 
258  process_file(&vtmp, filename);
259  }
260 
261  FieldIO::deliver(v, &vtmp);
262 
263  vout.detailed(m_vl, "read successful\n");
264 }
265 
266 
267 //================================================================
268 #if USE_ILDG_METADATA
269 
270 void FieldIO_LIME::store_metadata(LimeWriter *writer)
271 {
272  // first, write metadata record.
273  vout.detailed(m_vl, "%s: write metadata.\n", __func__);
274 
275  ILDG_Format::Params params;
276  params.Lx = CommonParameters::Lx();
277  params.Ly = CommonParameters::Ly();
278  params.Lz = CommonParameters::Lz();
279  params.Lt = CommonParameters::Lt();
280  params.type = 0;
281  params.prec = 8 * sizeof(double);
282 
283  ILDG_Format::Metadata md;
284  md.store(&params);
285 
286  const int buf_size = 4 * 1024;
287  char *buf = (char *)malloc(buf_size);
288 
289  md.write_to_buffer(buf, buf_size);
290 
291  off_t nbytes = strlen(buf);
292 
293  LimeRecordHeader *h = limeCreateHeader(1 /* MB */, 0 /* ME */, const_cast<char *>("ildg-format"), nbytes);
294  limeWriteRecordHeader(h, writer);
295  limeDestroyHeader(h);
296 
297  limeWriteRecordData(buf, &nbytes, writer);
298 
299  free(buf);
300 }
301 
302 
303 #endif
304 
305 //====================================================================
306 void FieldIO_LIME::store_data(LimeWriter *writer, Field *v,
307  bool mark_begin, bool mark_end)
308 {
309  // second, write binary data.
310  vout.detailed(m_vl, "%s: write binary data.\n", __func__);
311 
312 // off_t nbytes = sizeof(Field::element_type) * u->size();
313  off_t nbytes = sizeof(double) * v->size();
314 
315  LimeRecordHeader *h = limeCreateHeader(
316  mark_begin ? 1 : 0 /* MB */,
317  mark_end ? 1 : 0 /* ME */,
318  const_cast<char *>("ildg-binary-data"), nbytes);
319  limeWriteRecordHeader(h, writer);
320  limeDestroyHeader(h);
321 
322  char *buf = (char *)malloc(nbytes);
323  if (!buf) {
324  vout.crucial(m_vl, "Error at %s: malloc failed.\n", __func__);
325  exit(EXIT_FAILURE);
326  }
327 
328  // reorder and pack to buffer
329  const int nin_field = v->nin();
330  const int nex_field = v->nex();
331 
332  int nin_file = m_format->nin();
333  int nex_file = m_format->nex();
334 
335  if ((nin_file == 0) || (nex_file == 0)) {
336  nin_file = nin_field;
337  nex_file = nex_field;
338  }
339 
340  const long_t Lvol = CommonParameters::Lvol();
341 
342  // Field::element_type *p = (Field::element_type *)buf;
343  double *p = (double *)buf;
344 
345  for (int j = 0; j < nex_file; ++j) {
346  for (long_t isite = 0; isite < Lvol; ++isite) {
347  for (int i = 0; i < nin_file; ++i) {
348  int s, t;
349  m_format->file_to_field(s, t, i, j);
350  *p++ = v->cmp(s, isite, t);
351  }
352  }
353  }
354 
355  // adjust byteorder
356  if (!FieldIO::is_bigendian()) {
357  // byte_swap(buf, sizeof(Field::element_type), u->size());
358  byte_swap(buf, sizeof(double), v->size());
359  }
360 
361  // store
362  limeWriteRecordData(buf, &nbytes, writer);
363 
364  vout.detailed(m_vl, "write succeeded.\n");
365 
366  free(buf); // added by s.motoki[12.06.05].
367 }
368 
369 
370 //====================================================================
371 void FieldIO_LIME::store_lfn(LimeWriter *writer, const std::string lfn_string)
372 {
373  off_t nbytes = lfn_string.size();
374 
375  LimeRecordHeader *h = limeCreateHeader(1 /* MB */, 1 /* ME */, const_cast<char *>("ildg-data-lfn"), nbytes);
376 
377  limeWriteRecordHeader(h, writer);
378  limeDestroyHeader(h);
379 
380  // store
381  limeWriteRecordData(const_cast<char *>(lfn_string.c_str()), &nbytes, writer);
382 
383  vout.detailed(m_vl, "write succeeded.\n");
384 }
385 
386 
387 //====================================================================
388 void FieldIO_LIME::write_file(Field *v, const std::string filename)
389 {
390  const int nin_field = v->nin();
391  const int nex_field = v->nex();
392 
393  int nin_file = m_format->nin();
394  int nex_file = m_format->nex();
395 
396  if ((nin_file == 0) || (nex_file == 0)) {
397  nin_file = nin_field;
398  nex_file = nex_field;
399  }
400 
401  const long_t Lvol = CommonParameters::Lvol();
402 
403  Field vtmp;
404  if (Communicator::is_primary()) {
405  vtmp.reset(nin_field, Lvol, nex_field);
406  }
407 
408  // gather data
409  FieldIO::gather(&vtmp, v);
410 
411  // reorder
412 
413  // dump to file.
414  if (Communicator::is_primary()) {
415  vout.detailed(m_vl, "writing gauge configuration to %s\n", filename.c_str());
416 
417  FILE *fp = fopen(filename.c_str(), "w");
418  if (!fp) {
419  vout.crucial(m_vl, "Error at %s: cannot open file for write\n", class_name.c_str());
420  exit(EXIT_FAILURE);
421  }
422 
423  LimeWriter *writer = limeCreateWriter(fp);
424  if (!writer) {
425  vout.crucial(m_vl, "Error at %s: cannot create limeWriter\n", class_name.c_str());
426  exit(EXIT_FAILURE);
427  }
428 
429  // first, write metadata
430 #ifdef USE_ILDG_METADATA
431  store_metadata(writer);
432 #endif
433 
434  // second, write binary data.
435  store_data(writer, &vtmp, true, true);
436 
437  // if any, write lfn.
438 // store_lfn(writer, "lfn://");
439 
440  limeDestroyWriter(writer);
441 
442  fclose(fp);
443  }
444 
445  vout.detailed(m_vl, "write succeeded.\n");
446 
447  // cleanup.
448 }
449 
450 
451 //====================================================================
452 void FieldIO_LIME::read_file(std::vector<Field *>& vv, const std::string& filename)
453 {
454  if (vv.size() == 0) return;
455 
456  const int nin_field = vv[0]->nin();
457  const int nex_field = vv[0]->nex();
458 
459  const long_t Lvol = CommonParameters::Lvol();
460 
461  Field vtmp;
462 
463  if (Communicator::is_primary()) {
464  vout.detailed(m_vl, "reading gauge configuration from %s", filename.c_str());
465 
466  vtmp.reset(nin_field, Lvol, nex_field);
467 
468  FILE *fp = fopen(filename.c_str(), "r");
469  if (!fp) {
470  vout.crucial(m_vl, "Error at %s: fopen failed.", __func__);
471  exit(EXIT_FAILURE);
472  }
473 
474  LimeReader *reader = limeCreateReader(fp);
475  if (!reader) {
476  vout.crucial(m_vl, "Error at %s: limeCreateReader failed.", __func__);
477  exit(EXIT_FAILURE);
478  }
479 
480  // primary node reads file and deliver data to the other nodes.
481 
482  int idx = 0; // idx-th field
483 
484  // scan file for records.
485  for ( ; ;) {
486  int status = limeReaderNextRecord(reader);
487  if (status != LIME_SUCCESS) break;
488 
489  const char *t = limeReaderType(reader);
490 
491  if (strcmp(t, "ildg-format") == 0) {
492 #if USE_ILDG_METADATA
493  ILDG_Format::Params params;
494  load_metadata(reader, &params);
495  check_metadata(&params);
496 #endif
497  } else if (strcmp(t, "ildg-binary-data") == 0) {
498  load_data(reader, &vtmp);
499 
500  FieldIO::deliver(vv[idx++], &vtmp);
501  } else if (strcmp(t, "ildg-data-lfn") == 0) {
502  load_lfn(reader);
503  } else {
504  vout.detailed(m_vl, "%s: known record %s.\n", __func__, t);
505  }
506  } // end loop over records.
507 
508  limeDestroyReader(reader);
509 
510  fclose(fp);
511  } else {
512  // other nodes wait for data to be delivered from primary node.
513  for (int i = 0, n = vv.size(); i < n; ++i) {
514  FieldIO::deliver(vv[i], &vtmp);
515  }
516  }
517 
518  vout.detailed(m_vl, "read successful\n");
519 }
520 
521 
522 //====================================================================
523 void FieldIO_LIME::write_file(std::vector<Field *>& vv, const std::string& filename)
524 {
525  if (vv.size() == 0) return;
526 
527  const int nin_field = vv[0]->nin();
528  const int nex_field = vv[0]->nex();
529 
530  int nin_file = m_format->nin();
531  int nex_file = m_format->nex();
532 
533  if ((nin_file == 0) || (nex_file == 0)) {
534  nin_file = nin_field;
535  nex_file = nex_field;
536  }
537 
538  const long_t Lvol = CommonParameters::Lvol();
539 
540  Field vtmp;
541  if (Communicator::is_primary()) {
542  vtmp.reset(nin_field, Lvol, nex_field);
543  }
544 
545  FILE *fp = NULL; // only on primary node.
546  LimeWriter *writer = NULL;
547 
548  // dump to file.
549  if (Communicator::is_primary()) {
550  vout.detailed(m_vl, "writing gauge configuration to %s\n", filename.c_str());
551 
552  fp = fopen(filename.c_str(), "w");
553  if (!fp) {
554  vout.crucial(m_vl, "Error at %s: cannot open file for write\n", class_name.c_str());
555  exit(EXIT_FAILURE);
556  }
557 
558  writer = limeCreateWriter(fp);
559  if (!writer) {
560  vout.crucial(m_vl, "Error at %s: cannot create limeWriter\n", class_name.c_str());
561  exit(EXIT_FAILURE);
562  }
563 
564  // first, write metadata
565 #ifdef USE_ILDG_METADATA
566  store_metadata(writer);
567 #endif
568  }
569 
570  for (int i = 0, n = vv.size(); i < n; ++i) {
571  // gather data
572  FieldIO::gather(&vtmp, vv[i]);
573 
574  if (Communicator::is_primary()) {
575  // reorder
576 
577  // second, write binary data.
578  store_data(writer, &vtmp, (i == 0), (i == n - 1));
579  }
580  }
581 
582  if (Communicator::is_primary()) {
583  // if any, write lfn.
584 // store_lfn(writer, "lfn://");
585 
586  limeDestroyWriter(writer);
587 
588  fclose(fp);
589  }
590 
591  vout.detailed(m_vl, "write succeeded.\n");
592 
593  // cleanup.
594 }
595 
596 
597 #else /* USE_LIMELIB */
598 
599 void FieldIO_LIME::read_file(Field *v, const std::string filename) {}
600 void FieldIO_LIME::write_file(Field *v, const std::string filename) {}
601 #endif /* USE_LIMELIB */
602 
603 //====================================================================
604 //============================================================END=====
Index_lex idx
Definition: fieldIO.h:59
BridgeIO vout
Definition: bridgeIO.cpp:503
static void byte_swap(void *buf, size_t size, size_t nmemb)
< convert byte order. alternative interface.
Definition: fieldIO.h:91
static const std::string class_name
Definition: fieldIO.h:56
void detailed(const char *format,...)
Definition: bridgeIO.cpp:216
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
virtual void file_to_field(int &s, int &t, const int i, const int j) const =0
void deliver(Field *vlocal, Field *vglobal)
distribute data on primary node over parallel nodes.
Definition: fieldIO.cpp:30
virtual int nex() const =0
Container of Field-type object.
Definition: field.h:45
virtual int nin() const =0
int nvol() const
Definition: field.h:127
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:143
void write_file(Field *v, const std::string filename)
write data to file.
static bool is_bigendian()
Definition: fieldIO.cpp:206
int nin() const
Definition: field.h:126
int nex() const
Definition: field.h:128
const IO_Format::Format * m_format
Definition: fieldIO.h:62
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=Element_type::COMPLEX)
Definition: field.h:95
long long_t
definition of long for Bridge++
Definition: bridge_long.h:46
void crucial(const char *format,...)
Definition: bridgeIO.cpp:178
void read_file(Field *v, const std::string filename)
read data from file.
static bool is_primary()
check if the present node is primary in small communicator.
void gather(Field *vglobal, Field *vlocal)
gather data on parallel nodes to primary node.
Definition: fieldIO.cpp:119
static long_t Lvol()
Bridge::VerboseLevel m_vl
Definition: fieldIO.h:64
int size() const
Definition: field.h:132