21 #include "ildg_metadata.h"
37 #define off_t n_uint64_t
39 static const off_t block_size = 64;
46 void FieldIO_LIME::check_metadata(
const ILDG_Format::Params *params)
54 if (!((params->Lx == Lx) &&
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",
60 params->Lx, params->Ly, params->Lz, params->Lt,
65 if (params->prec == 32) {
74 void FieldIO_LIME::load_metadata(LimeReader *reader, ILDG_Format::Params *params)
76 off_t nbytes = limeReaderBytes(reader);
80 off_t nbytes_alloc = nbytes + ((nbytes % block_size) ? (block_size - (nbytes % block_size)) : 0);
82 char *buf = (
char *)malloc(nbytes_alloc);
89 int status = limeReaderReadData(buf, &nbytes, reader);
90 if (status != LIME_SUCCESS) {
99 ILDG_Format::Metadata md;
100 md.read_from_buffer(buf).extract(params);
110 void FieldIO_LIME::load_lfn(LimeReader *reader)
112 off_t nbytes = limeReaderBytes(reader);
116 off_t nbytes_alloc = (nbytes + 1) + (block_size - ((nbytes + 1) % block_size));
118 char *buf = (
char *)malloc(nbytes_alloc);
126 int status = limeReaderReadData(buf, &nbytes, reader);
127 if (status != LIME_SUCCESS) {
143 void FieldIO_LIME::load_data(LimeReader *reader,
Field *v)
147 off_t nbytes = limeReaderBytes(reader);
149 vout.
detailed(
m_vl,
"ildg-binary-data: limeReaderBytes: %lu bytes to read.\n", nbytes);
152 char *buf = (
char *)malloc(nbytes);
158 int status = limeReaderReadData(buf, &nbytes, reader);
159 if (status != LIME_SUCCESS) {
166 byte_swap(buf, word_size, nbytes / word_size);
173 if ((nin_file == 0) || (nex_file == 0)) {
178 const int nvol = v->
nvol();
180 double *p = (
double *)buf;
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) {
187 v->
set(s, isite, t, *p++);
197 void FieldIO_LIME::process_file(
Field *v,
const std::string filename)
199 FILE *fp = fopen(filename.c_str(),
"r");
206 LimeReader *reader = limeCreateReader(fp);
212 #if USE_ILDG_METADATA
213 ILDG_Format::Params params;
218 int status = limeReaderNextRecord(reader);
219 if (status != LIME_SUCCESS)
break;
221 const char *t = limeReaderType(reader);
223 if (strcmp(t,
"ildg-format") == 0) {
224 #if USE_ILDG_METADATA
225 load_metadata(reader, ¶ms);
226 check_metadata(¶ms);
228 }
else if (strcmp(t,
"ildg-binary-data") == 0) {
229 load_data(reader, v);
230 }
else if (strcmp(t,
"ildg-data-lfn") == 0) {
237 limeDestroyReader(reader);
246 const int nin_field = v->
nin();
247 const int nex_field = v->
nex();
254 vout.
detailed(
m_vl,
"reading gauge configuration from %s", filename.c_str());
256 vtmp.
reset(nin_field, Lvol, nex_field);
258 process_file(&vtmp, filename);
268 #if USE_ILDG_METADATA
270 void FieldIO_LIME::store_metadata(LimeWriter *writer)
275 ILDG_Format::Params params;
281 params.prec = 8 *
sizeof(double);
283 ILDG_Format::Metadata md;
286 const int buf_size = 4 * 1024;
287 char *buf = (
char *)malloc(buf_size);
289 md.write_to_buffer(buf, buf_size);
291 off_t nbytes = strlen(buf);
293 LimeRecordHeader *h = limeCreateHeader(1 , 0 , const_cast<char *>(
"ildg-format"), nbytes);
294 limeWriteRecordHeader(h, writer);
295 limeDestroyHeader(h);
297 limeWriteRecordData(buf, &nbytes, writer);
306 void FieldIO_LIME::store_data(LimeWriter *writer,
Field *v,
307 bool mark_begin,
bool mark_end)
313 off_t nbytes =
sizeof(double) * v->
size();
315 LimeRecordHeader *h = limeCreateHeader(
318 const_cast<char *>(
"ildg-binary-data"), nbytes);
319 limeWriteRecordHeader(h, writer);
320 limeDestroyHeader(h);
322 char *buf = (
char *)malloc(nbytes);
329 const int nin_field = v->
nin();
330 const int nex_field = v->
nex();
335 if ((nin_file == 0) || (nex_file == 0)) {
336 nin_file = nin_field;
337 nex_file = nex_field;
343 double *p = (
double *)buf;
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) {
350 *p++ = v->
cmp(s, isite, t);
362 limeWriteRecordData(buf, &nbytes, writer);
371 void FieldIO_LIME::store_lfn(LimeWriter *writer,
const std::string lfn_string)
373 off_t nbytes = lfn_string.size();
375 LimeRecordHeader *h = limeCreateHeader(1 , 1 , const_cast<char *>(
"ildg-data-lfn"), nbytes);
377 limeWriteRecordHeader(h, writer);
378 limeDestroyHeader(h);
381 limeWriteRecordData(const_cast<char *>(lfn_string.c_str()), &nbytes, writer);
390 const int nin_field = v->
nin();
391 const int nex_field = v->
nex();
396 if ((nin_file == 0) || (nex_file == 0)) {
397 nin_file = nin_field;
398 nex_file = nex_field;
405 vtmp.
reset(nin_field, Lvol, nex_field);
415 vout.
detailed(
m_vl,
"writing gauge configuration to %s\n", filename.c_str());
417 FILE *fp = fopen(filename.c_str(),
"w");
423 LimeWriter *writer = limeCreateWriter(fp);
430 #ifdef USE_ILDG_METADATA
431 store_metadata(writer);
435 store_data(writer, &vtmp,
true,
true);
440 limeDestroyWriter(writer);
454 if (vv.size() == 0)
return;
456 const int nin_field = vv[0]->nin();
457 const int nex_field = vv[0]->nex();
464 vout.
detailed(
m_vl,
"reading gauge configuration from %s", filename.c_str());
466 vtmp.
reset(nin_field, Lvol, nex_field);
468 FILE *fp = fopen(filename.c_str(),
"r");
474 LimeReader *reader = limeCreateReader(fp);
486 int status = limeReaderNextRecord(reader);
487 if (status != LIME_SUCCESS)
break;
489 const char *t = limeReaderType(reader);
491 if (strcmp(t,
"ildg-format") == 0) {
492 #if USE_ILDG_METADATA
493 ILDG_Format::Params params;
494 load_metadata(reader, ¶ms);
495 check_metadata(¶ms);
497 }
else if (strcmp(t,
"ildg-binary-data") == 0) {
498 load_data(reader, &vtmp);
501 }
else if (strcmp(t,
"ildg-data-lfn") == 0) {
508 limeDestroyReader(reader);
513 for (
int i = 0, n = vv.size(); i < n; ++i) {
525 if (vv.size() == 0)
return;
527 const int nin_field = vv[0]->nin();
528 const int nex_field = vv[0]->nex();
533 if ((nin_file == 0) || (nex_file == 0)) {
534 nin_file = nin_field;
535 nex_file = nex_field;
542 vtmp.
reset(nin_field, Lvol, nex_field);
546 LimeWriter *writer = NULL;
550 vout.
detailed(
m_vl,
"writing gauge configuration to %s\n", filename.c_str());
552 fp = fopen(filename.c_str(),
"w");
558 writer = limeCreateWriter(fp);
565 #ifdef USE_ILDG_METADATA
566 store_metadata(writer);
570 for (
int i = 0, n = vv.size(); i < n; ++i) {
578 store_data(writer, &vtmp, (i == 0), (i == n - 1));
586 limeDestroyWriter(writer);
static void byte_swap(void *buf, size_t size, size_t nmemb)
< convert byte order. alternative interface.
static const std::string class_name
void detailed(const char *format,...)
void set(const int jin, const int site, const int jex, double v)
void deliver(Field *vlocal, Field *vglobal)
distribute data on primary node over parallel nodes.
Container of Field-type object.
double cmp(const int jin, const int site, const int jex) const
void write_file(Field *v, const std::string filename)
write data to file.
static bool is_bigendian()
const IO_Format::Format * m_format
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=Element_type::COMPLEX)
long long_t
definition of long for Bridge++
void crucial(const char *format,...)
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.
Bridge::VerboseLevel m_vl