Bridge++  Ver. 2.0.2
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 #include "bridgeIO.h"
21 using Bridge::vout;
22 
23 
24 #ifdef USE_LIMELIB
25 
26 //typedef unsigned short int n_uint16_t;
27 //typedef unsigned int n_uint32_t;
28 //typedef unsigned long int n_uint64_t;
29 
30 #ifdef off_t
31 #undef off_t
32 #endif
33 #define off_t n_uint64_t
34 
35 static const off_t block_size = 64;
36 
37 const std::string FieldIO_LIME::class_name = "FieldIO_LIME";
38 
39 //================================================================
40 void FieldIO_LIME::load_lfn(LimeReader *reader)
41 {
42  off_t nbytes = limeReaderBytes(reader);
43 
44  vout.detailed(m_vl, "limeReaderBytes: %lu bytes to read.\n", nbytes);
45 
46  off_t nbytes_alloc = (nbytes + 1) + (block_size - ((nbytes + 1) % block_size));
47 
48  char *buf = (char *)malloc(nbytes_alloc);
49  if (!buf) {
50  vout.crucial(m_vl, "Error at %s: malloc failed.\n", class_name.c_str());
51  exit(EXIT_FAILURE);
52  }
53 
54  vout.detailed(m_vl, "allocated %lu bytes\n", nbytes_alloc);
55 
56  int status = limeReaderReadData(buf, &nbytes, reader);
57  if (status != LIME_SUCCESS) {
58  vout.crucial(m_vl, "Error at %s: limeReaderReadData failed.\n", class_name.c_str());
59  exit(EXIT_FAILURE);
60  }
61 
62  vout.detailed(m_vl, "limeReaderReadData: %lu bytes read.\n", nbytes);
63 
64  buf[nbytes] = '\0';
65 
66  vout.detailed(m_vl, "ildg-data-lfn: %s\n", buf);
67 
68  free(buf);
69 }
70 
71 
72 //====================================================================
73 void FieldIO_LIME::load_data(LimeReader *reader, Field *v)
74 {
75  off_t word_size = 8; //XXX assume 64bit precision
76 
77  off_t nbytes = limeReaderBytes(reader);
78 
79  vout.detailed(m_vl, "ildg-binary-data: limeReaderBytes: %lu bytes to read.\n", nbytes);
80 
81  // allocate memory for whole config data.
82  char *buf = (char *)malloc(nbytes);
83  if (!buf) {
84  vout.crucial(m_vl, "Error at %s: malloc failed.", __func__);
85  exit(EXIT_FAILURE);
86  }
87 
88  int status = limeReaderReadData(buf, &nbytes, reader);
89  if (status != LIME_SUCCESS) {
90  vout.crucial(m_vl, "Error at %s: malloc failed.", __func__);
91  exit(EXIT_FAILURE);
92  }
93 
94  // adjust byteorder
95  if (!FieldIO::is_bigendian()) {
96  byte_swap(buf, word_size, nbytes / word_size);
97  }
98 
99  // reorder and store
100  int nin_file = m_format->nin();
101  int nex_file = m_format->nex();
102 
103  if ((nin_file == 0) || (nex_file == 0)) {
104  nin_file = v->nin();
105  nex_file = v->nex();
106  }
107 
108  const int nvol = v->nvol();
109 
110  double *p = (double *)buf;
111 
112  for (int j = 0; j < nex_file; ++j) {
113  for (int isite = 0; isite < nvol; ++isite) {
114  for (int i = 0; i < nin_file; ++i) {
115  int s, t;
116  m_format->file_to_field(s, t, i, j);
117  v->set(s, isite, t, *p++);
118  }
119  }
120  }
121 
122  free(buf);
123 }
124 
125 
126 //====================================================================
127 void FieldIO_LIME::process_file(Field *v, const std::string filename)
128 {
129  FILE *fp = fopen(filename.c_str(), "r");
130 
131  if (!fp) {
132  vout.crucial(m_vl, "Error at %s: fopen failed.", __func__);
133  exit(EXIT_FAILURE);
134  }
135 
136  LimeReader *reader = limeCreateReader(fp);
137  if (!reader) {
138  vout.crucial(m_vl, "Error at %s: limeCreateReader failed.", __func__);
139  exit(EXIT_FAILURE);
140  }
141 
142 #if USE_ILDG_METADATA
143  ILDG_Format::Params params;
144 #endif
145 
146  // scan file for records.
147  for ( ; ;) {
148  int status = limeReaderNextRecord(reader);
149  if (status != LIME_SUCCESS) break;
150 
151  const char *t = limeReaderType(reader);
152 
153  if (strcmp(t, "ildg-format") == 0) {
154 #if USE_ILDG_METADATA
155  load_metadata(reader, &params);
156  check_metadata(&params);
157 #endif
158  } else if (strcmp(t, "ildg-binary-data") == 0) {
159  load_data(reader, v);
160  } else if (strcmp(t, "ildg-data-lfn") == 0) {
161  load_lfn(reader);
162  } else {
163  vout.detailed(m_vl, "%s: known record %s.\n", __func__, t);
164  }
165  } // end loop over records.
166 
167  limeDestroyReader(reader);
168 
169  fclose(fp);
170 }
171 
172 
173 //====================================================================
174 void FieldIO_LIME::read_file(Field& v, const std::string& filename)
175 {
176  const int nin_field = v.nin();
177  const int nex_field = v.nex();
178 
179  const long_t Lvol = CommonParameters::Lvol();
180 
181  Field vtmp;
182 
183  if (Communicator::is_primary()) {
184  vout.detailed(m_vl, "reading gauge configuration from %s", filename.c_str());
185 
186  vtmp.reset(nin_field, Lvol, nex_field);
187 
188  process_file(&vtmp, filename);
189  }
190 
191  FieldIO::deliver(&v, &vtmp);
192 
193  vout.detailed(m_vl, "read successful\n");
194 }
195 
196 
197 //====================================================================
198 void FieldIO_LIME::store_data(LimeWriter *writer, Field *v,
199  bool mark_begin, bool mark_end)
200 {
201  // second, write binary data.
202  vout.detailed(m_vl, "%s: write binary data.\n", __func__);
203 
204 // off_t nbytes = sizeof(Field::element_type) * u->size();
205  off_t nbytes = sizeof(double) * v->size();
206 
207  LimeRecordHeader *h = limeCreateHeader(
208  mark_begin ? 1 : 0 /* MB */,
209  mark_end ? 1 : 0 /* ME */,
210  const_cast<char *>("ildg-binary-data"), nbytes);
211  limeWriteRecordHeader(h, writer);
212  limeDestroyHeader(h);
213 
214  char *buf = (char *)malloc(nbytes);
215  if (!buf) {
216  vout.crucial(m_vl, "Error at %s: malloc failed.\n", __func__);
217  exit(EXIT_FAILURE);
218  }
219 
220  // reorder and pack to buffer
221  const int nin_field = v->nin();
222  const int nex_field = v->nex();
223 
224  int nin_file = m_format->nin();
225  int nex_file = m_format->nex();
226 
227  if ((nin_file == 0) || (nex_file == 0)) {
228  nin_file = nin_field;
229  nex_file = nex_field;
230  }
231 
232  const long_t Lvol = CommonParameters::Lvol();
233 
234  // Field::element_type *p = (Field::element_type *)buf;
235  double *p = (double *)buf;
236 
237  for (int j = 0; j < nex_file; ++j) {
238  for (long_t isite = 0; isite < Lvol; ++isite) {
239  for (int i = 0; i < nin_file; ++i) {
240  int s, t;
241  m_format->file_to_field(s, t, i, j);
242  *p++ = v->cmp(s, isite, t);
243  }
244  }
245  }
246 
247  // adjust byteorder
248  if (!FieldIO::is_bigendian()) {
249  // byte_swap(buf, sizeof(Field::element_type), u->size());
250  byte_swap(buf, sizeof(double), v->size());
251  }
252 
253  // store
254  limeWriteRecordData(buf, &nbytes, writer);
255 
256  vout.detailed(m_vl, "write succeeded.\n");
257 
258  free(buf); // added by s.motoki[12.06.05].
259 }
260 
261 
262 //====================================================================
263 void FieldIO_LIME::store_lfn(LimeWriter *writer, const std::string lfn_string)
264 {
265  off_t nbytes = lfn_string.size();
266 
267  LimeRecordHeader *h = limeCreateHeader(1 /* MB */, 1 /* ME */, const_cast<char *>("ildg-data-lfn"), nbytes);
268 
269  limeWriteRecordHeader(h, writer);
270  limeDestroyHeader(h);
271 
272  // store
273  limeWriteRecordData(const_cast<char *>(lfn_string.c_str()), &nbytes, writer);
274 
275  vout.detailed(m_vl, "write succeeded.\n");
276 }
277 
278 
279 //====================================================================
280 void FieldIO_LIME::write_file(Field& v, const std::string& filename)
281 {
282  const int nin_field = v.nin();
283  const int nex_field = v.nex();
284 
285  int nin_file = m_format->nin();
286  int nex_file = m_format->nex();
287 
288  if ((nin_file == 0) || (nex_file == 0)) {
289  nin_file = nin_field;
290  nex_file = nex_field;
291  }
292 
293  const long_t Lvol = CommonParameters::Lvol();
294 
295  Field vtmp;
296  if (Communicator::is_primary()) {
297  vtmp.reset(nin_field, Lvol, nex_field);
298  }
299 
300  // gather data
301  FieldIO::gather(&vtmp, &v);
302 
303  // reorder
304 
305  // dump to file.
306  if (Communicator::is_primary()) {
307  vout.detailed(m_vl, "writing gauge configuration to %s\n", filename.c_str());
308 
309  FILE *fp = fopen(filename.c_str(), "w");
310  if (!fp) {
311  vout.crucial(m_vl, "Error at %s: cannot open file for write\n", class_name.c_str());
312  exit(EXIT_FAILURE);
313  }
314 
315  LimeWriter *writer = limeCreateWriter(fp);
316  if (!writer) {
317  vout.crucial(m_vl, "Error at %s: cannot create limeWriter\n", class_name.c_str());
318  exit(EXIT_FAILURE);
319  }
320 
321  // first, write metadata if any.
322 
323  // second, write binary data.
324  store_data(writer, &vtmp, true, true);
325 
326  // third, write lfn if any.
327  // store_lfn(writer, "lfn://");
328 
329  limeDestroyWriter(writer);
330 
331  fclose(fp);
332  }
333 
334  vout.detailed(m_vl, "write succeeded.\n");
335 
336  // cleanup.
337 }
338 
339 
340 //====================================================================
341 #else /* USE_LIMELIB */
342 
343 void FieldIO_LIME::read_file(Field& v, const std::string& filename)
344 {
345  vout.crucial("FieldIO_LIME::read_file unsupported.\n");
346  exit(EXIT_FAILURE);
347 }
348 
349 
350 void FieldIO_LIME::write_file(Field& v, const std::string& filename)
351 {
352  vout.crucial("FieldIO_LIME::write_file unsupported.\n");
353  exit(EXIT_FAILURE);
354 }
355 
356 
357 #endif /* USE_LIMELIB */
358 
359 //====================================================================
360 //============================================================END=====
bridgeIO.h
CommonParameters::Lvol
static long_t Lvol()
Definition: commonParameters.h:95
Field::set
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
IO_Format::Format::nin
virtual int nin() const =0
Bridge::BridgeIO::detailed
void detailed(const char *format,...)
Definition: bridgeIO.cpp:219
Field::nex
int nex() const
Definition: field.h:128
fieldIO_LIME.h
io_format_gauge.h
FieldIO::is_bigendian
static bool is_bigendian()
Definition: fieldIO.cpp:210
FieldIO::m_vl
Bridge::VerboseLevel m_vl
Definition: fieldIO.h:64
FieldIO_LIME::write_file
void write_file(Field &v, const std::string &filename)
write data to file. (‘const’ is added [18 Mar 2021])
Definition: fieldIO_LIME.cpp:350
FieldIO::gather
void gather(Field *vglobal, Field *vlocal)
gather data on parallel nodes to primary node.
Definition: fieldIO.cpp:121
Field::nin
int nin() const
Definition: field.h:126
FieldIO::class_name
static const std::string class_name
Definition: fieldIO.h:56
FieldIO::deliver
void deliver(Field *vlocal, Field *vglobal)
distribute data on primary node over parallel nodes.
Definition: fieldIO.cpp:30
Field::size
int size() const
Definition: field.h:132
Field::nvol
int nvol() const
Definition: field.h:127
Field::reset
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=Element_type::COMPLEX)
Definition: field.h:95
Field::cmp
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:143
Communicator::is_primary
static bool is_primary()
check if the present node is primary in small communicator.
Definition: communicator.cpp:60
FieldIO_LIME::read_file
void read_file(Field &v, const std::string &filename)
read data from file. (‘const’ is added [18 Mar 2021])
Definition: fieldIO_LIME.cpp:343
IO_Format::Format::file_to_field
virtual void file_to_field(int &s, int &t, const int i, const int j) const =0
Bridge::BridgeIO::crucial
void crucial(const char *format,...)
Definition: bridgeIO.cpp:180
Field
Container of Field-type object.
Definition: field.h:46
IO_Format::Format::nex
virtual int nex() const =0
FieldIO::m_format
const IO_Format::Format * m_format
Definition: fieldIO.h:62
Bridge::vout
BridgeIO vout
Definition: bridgeIO.cpp:512