Bridge++  Ver. 1.2.x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fieldIO_LIME.cpp
Go to the documentation of this file.
1 
9 #include <string>
10 using std::string;
11 
12 #include <iostream>
13 #include <fstream>
14 #include <string>
15 #include <string.h>
16 
17 #ifdef USE_LIMELIB
18 extern "C" {
19 #include <lime.h>
20 }
21 #endif
22 
23 #include "commonParameters.h"
24 #include "communicator.h"
25 #include "bridgeIO.h"
26 using Bridge::vout;
27 
28 #include "field.h"
29 #include "fieldIO_LIME.h"
30 #include "io_format_gauge.h"
31 
32 #if USE_ILDG_METADATA
33 #include "ildg_metadata.h"
34 #endif
35 
36 #ifdef USE_LIMELIB
37 
38 //typedef unsigned short int n_uint16_t;
39 //typedef unsigned int n_uint32_t;
40 //typedef unsigned long int n_uint64_t;
41 
42 #ifdef off_t
43 #undef off_t
44 #endif
45 #define off_t n_uint64_t
46 
47 static const off_t block_size = 64;
48 
49 const std::string FieldIO_LIME::class_name = "FieldIO_LIME";
50 
51 //================================================================
52 #if USE_ILDG_METADATA
53 
54 void FieldIO_LIME::check_metadata(const ILDG_Format::Params *params)
55 {
56  // check if config is as expected.
57  int Lx = CommonParameters::Lx();
58  int Ly = CommonParameters::Ly();
59  int Lz = CommonParameters::Lz();
60  int Lt = CommonParameters::Lt();
61 
62  if (!((params->Lx == Lx) &&
63  (params->Ly == Ly) &&
64  (params->Lz == Lz) &&
65  (params->Lt == Lt))) {
66  vout.crucial(m_vl, "lattice size mismatch. config=(%d,%d,%d,%d), expected=(%d,%d,%d,%d).\n",
67  params->Lx, params->Ly, params->Lz, params->Lt,
68  Lx, Ly, Lz, Lt);
69  abort();
70  }
71 
72  if (params->prec == 32) {
73  vout.detailed(m_vl, "ildg-format: single precision: extend to double\n");
74  } else {
75  vout.detailed(m_vl, "ildg-format: double precision\n");
76  }
77 }
78 
79 
80 void FieldIO_LIME::load_metadata(LimeReader *reader, ILDG_Format::Params *params)
81 {
82  off_t nbytes = limeReaderBytes(reader);
83 
84  vout.detailed(m_vl, "limeReaderBytes: %lu bytes to read.\n", nbytes);
85 
86  off_t nbytes_alloc = nbytes + ((nbytes % block_size) ? (block_size - (nbytes % block_size)) : 0);
87 
88  char *buf = (char *)malloc(nbytes_alloc);
89  if (buf == 0) {
90  vout.crucial(m_vl, "malloc failed.");
91  abort();
92  }
93  vout.detailed(m_vl, "allocated %lu bytes\n", nbytes_alloc);
94 
95  int status = limeReaderReadData(buf, &nbytes, reader);
96  if (status != LIME_SUCCESS) {
97  vout.crucial(m_vl, "limeReaderReadData failed.");
98  abort();
99  }
100 
101  // dump Metadata
102  vout.detailed(m_vl, "%s\n", buf);
103 
104  // process ILDG Metadata
105  ILDG_Format::Metadata md;
106  md.read_from_buffer(buf).extract(params);
107 
108  free(buf);
109 }
110 
111 
112 #endif
113 
114 void FieldIO_LIME::load_lfn(LimeReader *reader)
115 {
116  off_t nbytes = limeReaderBytes(reader);
117 
118  vout.detailed(m_vl, "limeReaderBytes: %lu bytes to read.\n", nbytes);
119 
120  off_t nbytes_alloc = (nbytes + 1) + (block_size - ((nbytes + 1) % block_size));
121 
122  char *buf = (char *)malloc(nbytes_alloc);
123  if (!buf) {
124  vout.crucial(m_vl, "malloc failed.");
125  abort();
126  }
127 
128  vout.detailed(m_vl, "allocated %lu bytes\n", nbytes_alloc);
129 
130  int status = limeReaderReadData(buf, &nbytes, reader);
131  if (status != LIME_SUCCESS) {
132  vout.crucial(m_vl, "limeReaderReadData failed.");
133  abort();
134  }
135 
136  vout.detailed(m_vl, "limeReaderReadData: %lu bytes read.\n", nbytes);
137 
138  buf[nbytes] = '\0';
139 
140  vout.detailed(m_vl, "ildg-data-lfn: %s\n", buf);
141 
142  free(buf);
143 }
144 
145 
146 void FieldIO_LIME::load_data(LimeReader *reader, Field *v)
147 {
148  off_t word_size = 8; //XXX assume 64bit precision
149 
150  off_t nbytes = limeReaderBytes(reader);
151 
152  vout.detailed(m_vl, "ildg-binary-data: limeReaderBytes: %lu bytes to read.\n", nbytes);
153 
154  // allocate memory for whole config data.
155  char *buf = (char *)malloc(nbytes);
156  if (!buf) {
157  vout.crucial(m_vl, "%s: malloc failed.", __func__);
158  abort();
159  }
160 
161  int status = limeReaderReadData(buf, &nbytes, reader);
162  if (status != LIME_SUCCESS) {
163  vout.crucial(m_vl, "%s: malloc failed.", __func__);
164  abort();
165  }
166 
167  // adjust byteorder
168  if (!FieldIO::is_bigendian()) {
169  byte_swap(buf, word_size, nbytes / word_size);
170  }
171 
172  // reorder and store
173  int nin_file = m_format->nin();
174  int nex_file = m_format->nex();
175 
176  if ((nin_file == 0) || (nex_file == 0)) {
177  nin_file = v->nin();
178  nex_file = v->nex();
179  }
180 
181  int lvol = v->nvol();
182 
183  double *p = (double *)buf;
184 
185  for (int j = 0; j < nex_file; ++j) {
186  for (int isite = 0; isite < lvol; ++isite) {
187  for (int i = 0; i < nin_file; ++i) {
188  int s, t;
189  m_format->file_to_field(s, t, i, j);
190  v->set(s, isite, t, *p++);
191  }
192  }
193  }
194 
195  free(buf);
196 }
197 
198 
199 void FieldIO_LIME::process_file(Field *v, string filename)
200 {
201  FILE *fp = fopen(filename.c_str(), "r");
202 
203  if (!fp) {
204  vout.crucial(m_vl, "%s: fopen failed.", __func__);
205  }
206 
207  LimeReader *reader = limeCreateReader(fp);
208  if (!reader) {
209  vout.crucial(m_vl, "%s: limeCreateReader failed.", __func__);
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.crucial(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 void FieldIO_LIME::read_file(Field *v, string filename)
244 {
245  int nin_field = v->nin();
246  int nex_field = v->nex();
247 
248  int Lvol = CommonParameters::Lvol();
249 
250  Field vtmp;
251 
252  if (Communicator::is_primary()) {
253  vout.detailed(m_vl, "reading gauge configuration from %s", filename.c_str());
254 
255  vtmp.reset(nin_field, Lvol, nex_field);
256 
257  process_file(&vtmp, filename);
258  }
259 
260  FieldIO::deliver(v, &vtmp);
261 
262  vout.detailed(m_vl, "read successful\n");
263 }
264 
265 
266 //================================================================
267 #if USE_ILDG_METADATA
268 
269 void FieldIO_LIME::store_metadata(LimeWriter *writer)
270 {
271  // first, write metadata record.
272  vout.detailed(m_vl, "%s: write metadata.\n", __func__);
273 
274  ILDG_Format::Params params;
275  params.Lx = CommonParameters::Lx();
276  params.Ly = CommonParameters::Ly();
277  params.Lz = CommonParameters::Lz();
278  params.Lt = CommonParameters::Lt();
279  params.type = 0;
280  params.prec = 8 * sizeof(double);
281 
282  ILDG_Format::Metadata md;
283  md.store(&params);
284 
285  const int buf_size = 4 * 1024;
286  char *buf = (char *)malloc(buf_size);
287 
288  md.write_to_buffer(buf, buf_size);
289 
290  off_t nbytes = strlen(buf);
291 
292  LimeRecordHeader *h = limeCreateHeader(1 /* MB */, 0 /* ME */, const_cast<char *>("ildg-format"), nbytes);
293  limeWriteRecordHeader(h, writer);
294  limeDestroyHeader(h);
295 
296  limeWriteRecordData(buf, &nbytes, writer);
297 
298  free(buf);
299 }
300 
301 
302 #endif
303 
304 void FieldIO_LIME::store_data(LimeWriter *writer, Field *v)
305 {
306  // second, write binary data.
307  vout.detailed(m_vl, "%s: write binary data.\n", __func__);
308 
309 // off_t nbytes = sizeof(Field::element_type) * u->size();
310  off_t nbytes = sizeof(double) * v->size();
311 
312  LimeRecordHeader *h = limeCreateHeader(0 /* MB */, 1 /* ME */, const_cast<char *>("ildg-binary-data"), nbytes);
313  limeWriteRecordHeader(h, writer);
314  limeDestroyHeader(h);
315 
316  char *buf = (char *)malloc(nbytes);
317  if (!buf) {
318  vout.crucial(m_vl, "%s: malloc failed.\n", __func__);
319  abort();
320  }
321 
322  // reorder and pack to buffer
323 
324  int nin_field = v->nin();
325  int nex_field = v->nex();
326 
327  int nin_file = m_format->nin();
328  int nex_file = m_format->nex();
329 
330  if ((nin_file == 0) || (nex_file == 0)) {
331  nin_file = nin_field;
332  nex_file = nex_field;
333  }
334 
335  int lvol = CommonParameters::Lvol();
336 
337 // Field::element_type *p = (Field::element_type *)buf;
338  double *p = (double *)buf;
339 
340  for (int j = 0; j < nex_file; ++j) {
341  for (int isite = 0; isite < lvol; ++isite) {
342  for (int i = 0; i < nin_file; ++i) {
343  int s, t;
344  m_format->file_to_field(s, t, i, j);
345  *p++ = v->cmp(s, isite, t);
346  }
347  }
348  }
349 
350  // adjust byteorder
351  if (!FieldIO::is_bigendian()) {
352 // byte_swap(buf, sizeof(Field::element_type), u->size());
353  byte_swap(buf, sizeof(double), v->size());
354  }
355 
356  // store
357  limeWriteRecordData(buf, &nbytes, writer);
358 
359  vout.detailed(m_vl, "write succeeded.\n");
360 
361  free(buf); // added by s.motoki[12.06.05].
362 }
363 
364 
365 void FieldIO_LIME::store_lfn(LimeWriter *writer, string lfn_string)
366 {
367  off_t nbytes = lfn_string.size();
368 
369  LimeRecordHeader *h = limeCreateHeader(1 /* MB */, 1 /* ME */, const_cast<char *>("ildg-data-lfn"), nbytes);
370 
371  limeWriteRecordHeader(h, writer);
372  limeDestroyHeader(h);
373 
374  // store
375  limeWriteRecordData(const_cast<char *>(lfn_string.c_str()), &nbytes, writer);
376 
377  vout.detailed(m_vl, "write succeeded.\n");
378 }
379 
380 
381 void FieldIO_LIME::write_file(Field *v, string filename)
382 {
383  int nin_field = v->nin();
384  int nex_field = v->nex();
385 
386  int nin_file = m_format->nin();
387  int nex_file = m_format->nex();
388 
389  if ((nin_file == 0) || (nex_file == 0)) {
390  nin_file = nin_field;
391  nex_file = nex_field;
392  }
393 
394  int Lvol = CommonParameters::Lvol();
395 
396  Field vtmp;
397  if (Communicator::is_primary()) {
398  vtmp.reset(nin_field, Lvol, nex_field);
399  }
400 
401  // gather data
402  FieldIO::gather(&vtmp, v);
403 
404  // reorder
405 
406  // dump to file.
407  if (Communicator::is_primary()) {
408  vout.detailed(m_vl, "writing gauge configuration to %s\n", filename.c_str());
409 
410  FILE *fp = fopen(filename.c_str(), "w");
411  if (!fp) {
412  vout.crucial(m_vl, "cannot open file for write\n");
413  abort();
414  }
415 
416  LimeWriter *writer = limeCreateWriter(fp);
417  if (!writer) {
418  vout.crucial(m_vl, "cannot create limeWriter\n");
419  abort();
420  }
421 
422  // first, write metadata
423 #ifdef USE_ILDG_METADATA
424  store_metadata(writer);
425 #endif
426 
427  // second, write binary data.
428  store_data(writer, &vtmp);
429 
430  // if any, write lfn.
431 // store_lfn(writer, "lfn://");
432 
433  limeDestroyWriter(writer);
434 
435  fclose(fp);
436  }
437 
438  vout.detailed(m_vl, "write succeeded.\n");
439 
440  // cleanup.
441 }
442 
443 
444 #else /* USE_LIMELIB */
445 
446 void FieldIO_LIME::read_file(Field *v, string filename) {}
447 void FieldIO_LIME::write_file(Field *v, string filename) {}
448 #endif /* USE_LIMELIB */
449 
450 //================================================================
BridgeIO vout
Definition: bridgeIO.cpp:207
static void byte_swap(void *buf, size_t size, size_t nmemb)
&lt; convert byte order. alternative interface.
Definition: fieldIO.h:76
static const std::string class_name
Definition: fieldIO.h:51
void detailed(const char *format,...)
Definition: bridgeIO.cpp:50
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:128
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:29
virtual int nex() const =0
Container of Field-type object.
Definition: field.h:37
virtual int nin() const =0
int nvol() const
Definition: field.h:101
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:108
void read_file(Field *v, std::string filename)
read data from file.
static int Lvol()
static bool is_bigendian()
Definition: fieldIO.cpp:202
int nin() const
Definition: field.h:100
void write_file(Field *v, std::string filename)
write data to file.
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=COMPLEX)
Definition: field.h:82
int nex() const
Definition: field.h:102
const IO_Format::Format * m_format
Definition: fieldIO.h:57
void crucial(const char *format,...)
Definition: bridgeIO.cpp:26
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:115
Bridge::VerboseLevel m_vl
Definition: fieldIO.h:59
int size() const
Definition: field.h:106