Bridge++  Ver. 1.1.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 //================================================================
50 #if USE_ILDG_METADATA
51 
52 void FieldIO_LIME::check_metadata(const ILDG_Format::Params *params)
53 {
54  // check if config is as expected.
55  int Lx = CommonParameters::Lx();
56  int Ly = CommonParameters::Ly();
57  int Lz = CommonParameters::Lz();
58  int Lt = CommonParameters::Lt();
59 
60  if (!((params->Lx == Lx) &&
61  (params->Ly == Ly) &&
62  (params->Lz == Lz) &&
63  (params->Lt == Lt))) {
64  vout.crucial(m_vl, "lattice size mismatch. config=(%d,%d,%d,%d), expected=(%d,%d,%d,%d).\n",
65  params->Lx, params->Ly, params->Lz, params->Lt,
66  Lx, Ly, Lz, Lt);
67  return;
68  }
69 
70  if (params->prec == 32) {
71  vout.detailed(m_vl, "ildg-format: single precision: extend to double\n");
72  } else {
73  vout.detailed(m_vl, "ildg-format: double precision\n");
74  }
75 }
76 
77 
78 void FieldIO_LIME::load_metadata(LimeReader *reader, ILDG_Format::Params *params)
79 {
80  off_t nbytes = limeReaderBytes(reader);
81 
82  vout.detailed(m_vl, "limeReaderBytes: %lu bytes to read.\n", nbytes);
83 
84  off_t nbytes_alloc = nbytes + ((nbytes % block_size) ? (block_size - (nbytes % block_size)) : 0);
85 
86  char *buf = (char *)malloc(nbytes_alloc);
87  if (buf == 0) {
88  vout.crucial(m_vl, "malloc failed.");
89  return;
90  }
91  vout.detailed(m_vl, "allocated %lu bytes\n", nbytes_alloc);
92 
93  int status = limeReaderReadData(buf, &nbytes, reader);
94  if (status != LIME_SUCCESS) {
95  vout.crucial(m_vl, "limeReaderReadData failed.");
96  return;
97  }
98 
99  // dump Metadata
100  vout.detailed(m_vl, "%s\n", buf);
101 
102  // process ILDG Metadata
103  ILDG_Format::Metadata md;
104  md.read_from_buffer(buf).extract(params);
105 
106  free(buf);
107 }
108 #endif
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, "malloc failed.");
121  return;
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, "limeReaderReadData failed.");
129  return;
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 void FieldIO_LIME::load_data(LimeReader *reader, Field *v)
143 {
144  off_t word_size = 8; //XXX assume 64bit precision
145 
146  off_t nbytes = limeReaderBytes(reader);
147 
148  vout.detailed(m_vl, "ildg-binary-data: limeReaderBytes: %lu bytes to read.\n", nbytes);
149 
150  // allocate memory for whole config data.
151  char *buf = (char *)malloc(nbytes);
152  if (!buf) {
153  vout.crucial(m_vl, "%s: malloc failed.", __func__);
154  return;
155  }
156 
157  int status = limeReaderReadData(buf, &nbytes, reader);
158  if (status != LIME_SUCCESS) {
159  vout.crucial(m_vl, "%s: malloc failed.", __func__);
160  return;
161  }
162 
163  // adjust byteorder
164  if (!FieldIO::is_bigendian()) {
165  byte_swap(buf, word_size, nbytes / word_size);
166  }
167 
168  // reorder and store
169  int nin_file = m_format->nin();
170  int nex_file = m_format->nex();
171 
172  if ((nin_file == 0) || (nex_file == 0)) {
173  nin_file = v->nin();
174  nex_file = v->nex();
175  }
176 
177  int lvol = v->nvol();
178 
179  double *p = (double *)buf;
180 
181  for (int j = 0; j < nex_file; ++j) {
182  for (int isite = 0; isite < lvol; ++isite) {
183  for (int i = 0; i < nin_file; ++i) {
184  int s, t;
185  m_format->file_to_field(s, t, i, j);
186  v->set(s, isite, t, *p++);
187  }
188  }
189  }
190 
191  free(buf);
192 }
193 
194 
195 void FieldIO_LIME::process_file(Field *v, string filename)
196 {
197  FILE *fp = fopen(filename.c_str(), "r");
198 
199  if (!fp) {
200  vout.crucial(m_vl, "%s: fopen failed.", __func__);
201  }
202 
203  LimeReader *reader = limeCreateReader(fp);
204  if (!reader) {
205  vout.crucial(m_vl, "%s: limeCreateReader failed.", __func__);
206  }
207 
208 #if USE_ILDG_METADATA
209  ILDG_Format::Params params;
210 #endif
211 
212  // scan file for records.
213  for ( ; ; ) {
214  int status = limeReaderNextRecord(reader);
215  if (status != LIME_SUCCESS) break;
216 
217  const char *t = limeReaderType(reader);
218 
219  if (strcmp(t, "ildg-format") == 0) {
220 #if USE_ILDG_METADATA
221  load_metadata(reader, &params);
222  check_metadata(&params);
223 #endif
224  } else if (strcmp(t, "ildg-binary-data") == 0) {
225  load_data(reader, v);
226  } else if (strcmp(t, "ildg-data-lfn") == 0) {
227  load_lfn(reader);
228  } else {
229  vout.crucial(m_vl, "%s: known record %s.\n", __func__, t);
230  }
231  } // end loop over records.
232 
233  limeDestroyReader(reader);
234 
235  fclose(fp);
236 }
237 
238 
239 void FieldIO_LIME::read_file(Field *v, string filename)
240 {
241  int nin_field = v->nin();
242  int nex_field = v->nex();
243 
244  int Lvol = CommonParameters::Lvol();
245 
246  Field vtmp;
247 
248  if (Communicator::is_primary()) {
249  vout.detailed(m_vl, "reading gauge configuration from %s", filename.c_str());
250 
251  vtmp.reset(nin_field, Lvol, nex_field);
252 
253  process_file(&vtmp, filename);
254  }
255 
256  FieldIO::deliver(v, &vtmp);
257 
258  vout.detailed(m_vl, "read successful\n");
259 }
260 
261 
262 //================================================================
263 #if USE_ILDG_METADATA
264 
265 void FieldIO_LIME::store_metadata(LimeWriter *writer)
266 {
267  // first, write metadata record.
268  vout.detailed(m_vl, "%s: write metadata.\n", __func__);
269 
270  ILDG_Format::Params params;
271  params.Lx = CommonParameters::Lx();
272  params.Ly = CommonParameters::Ly();
273  params.Lz = CommonParameters::Lz();
274  params.Lt = CommonParameters::Lt();
275  params.type = 0;
276  params.prec = 8 * sizeof(double);
277 
278  ILDG_Format::Metadata md;
279  md.store(&params);
280 
281  const int buf_size = 4 * 1024;
282  char *buf = (char *)malloc(buf_size);
283 
284  md.write_to_buffer(buf, buf_size);
285 
286  off_t nbytes = strlen(buf);
287 
288  LimeRecordHeader *h = limeCreateHeader(1 /* MB */, 0 /* ME */, const_cast<char *>("ildg-format"), nbytes);
289  limeWriteRecordHeader(h, writer);
290  limeDestroyHeader(h);
291 
292  limeWriteRecordData(buf, &nbytes, writer);
293 
294  free(buf);
295 }
296 #endif
297 
298 void FieldIO_LIME::store_data(LimeWriter *writer, Field *v)
299 {
300  // second, write binary data.
301  vout.detailed(m_vl, "%s: write binary data.\n", __func__);
302 
303 // off_t nbytes = sizeof(Field::element_type) * u->size();
304  off_t nbytes = sizeof(double) * v->size();
305 
306  LimeRecordHeader *h = limeCreateHeader(0 /* MB */, 1 /* ME */, const_cast<char *>("ildg-binary-data"), nbytes);
307  limeWriteRecordHeader(h, writer);
308  limeDestroyHeader(h);
309 
310  char *buf = (char *)malloc(nbytes);
311  if (!buf) {
312  vout.crucial(m_vl, "%s: malloc failed.\n", __func__);
313  return;
314  }
315 
316  // reorder and pack to buffer
317 
318  int nin_field = v->nin();
319  int nex_field = v->nex();
320 
321  int nin_file = m_format->nin();
322  int nex_file = m_format->nex();
323 
324  if ((nin_file == 0) || (nex_file == 0)) {
325  nin_file = nin_field;
326  nex_file = nex_field;
327  }
328 
329  int lvol = CommonParameters::Lvol();
330 
331 // Field::element_type *p = (Field::element_type *)buf;
332  double *p = (double *)buf;
333 
334  for (int j = 0; j < nex_file; ++j) {
335  for (int isite = 0; isite < lvol; ++isite) {
336  for (int i = 0; i < nin_file; ++i) {
337  int s, t;
338  m_format->file_to_field(s, t, i, j);
339  *p++ = v->cmp(s, isite, t);
340  }
341  }
342  }
343 
344  // adjust byteorder
345  if (!FieldIO::is_bigendian()) {
346 // byte_swap(buf, sizeof(Field::element_type), u->size());
347  byte_swap(buf, sizeof(double), v->size());
348  }
349 
350  // store
351  limeWriteRecordData(buf, &nbytes, writer);
352 
353  vout.detailed(m_vl, "write succeeded.\n");
354 
355  free(buf); // added by s.motoki[12.06.05].
356 }
357 
358 
359 void FieldIO_LIME::store_lfn(LimeWriter *writer, string lfn_string)
360 {
361  off_t nbytes = lfn_string.size();
362 
363  LimeRecordHeader *h = limeCreateHeader(1 /* MB */, 1 /* ME */, const_cast<char *>("ildg-data-lfn"), nbytes);
364 
365  limeWriteRecordHeader(h, writer);
366  limeDestroyHeader(h);
367 
368  // store
369  limeWriteRecordData(const_cast<char *>(lfn_string.c_str()), &nbytes, writer);
370 
371  vout.detailed(m_vl, "write succeeded.\n");
372 }
373 
374 
375 void FieldIO_LIME::write_file(Field *v, string filename)
376 {
377  int nin_field = v->nin();
378  int nex_field = v->nex();
379 
380  int nin_file = m_format->nin();
381  int nex_file = m_format->nex();
382 
383  if ((nin_file == 0) || (nex_file == 0)) {
384  nin_file = nin_field;
385  nex_file = nex_field;
386  }
387 
388  int Lvol = CommonParameters::Lvol();
389 
390  Field vtmp;
391  if (Communicator::is_primary()) {
392  vtmp.reset(nin_field, Lvol, nex_field);
393  }
394 
395  // gather data
396  FieldIO::gather(&vtmp, v);
397 
398  // reorder
399 
400  // dump to file.
401  if (Communicator::is_primary()) {
402  vout.detailed(m_vl, "writing gauge configuration to %s\n", filename.c_str());
403 
404  FILE *fp = fopen(filename.c_str(), "w");
405  if (!fp) {
406  vout.crucial(m_vl, "cannot open file for write\n");
407  abort();
408  }
409 
410  LimeWriter *writer = limeCreateWriter(fp);
411  if (!writer) {
412  vout.crucial(m_vl, "cannot create limeWriter\n");
413  abort();
414  }
415 
416  // first, write metadata
417 #ifdef USE_ILDG_METADATA
418  store_metadata(writer);
419 #endif
420 
421  // second, write binary data.
422  store_data(writer, &vtmp);
423 
424  // if any, write lfn.
425 // store_lfn(writer, "lfn://");
426 
427  limeDestroyWriter(writer);
428 
429  fclose(fp);
430  }
431 
432  vout.detailed(m_vl, "write succeeded.\n");
433 
434  // cleanup.
435 }
436 
437 
438 #else /* USE_LIMELIB */
439 
440 void FieldIO_LIME::read_file(Field *v, string filename) {}
441 void FieldIO_LIME::write_file(Field *v, string filename) {}
442 #endif /* USE_LIMELIB */
443 
444 //================================================================