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 int lvol = v->
nvol();
180 double *p = (
double *)buf;
182 for (
int j = 0; j < nex_file; ++j) {
183 for (
int isite = 0; isite < lvol; ++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, 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 int nin_field = v->
nin();
247 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);
304 void FieldIO_LIME::store_data(LimeWriter *writer,
Field *v,
305 bool mark_begin,
bool mark_end)
311 off_t nbytes =
sizeof(double) * v->
size();
313 LimeRecordHeader *h = limeCreateHeader(
316 const_cast<char *>(
"ildg-binary-data"), nbytes);
317 limeWriteRecordHeader(h, writer);
318 limeDestroyHeader(h);
320 char *buf = (
char *)malloc(nbytes);
327 int nin_field = v->
nin();
328 int nex_field = v->
nex();
333 if ((nin_file == 0) || (nex_file == 0)) {
334 nin_file = nin_field;
335 nex_file = nex_field;
341 double *p = (
double *)buf;
343 for (
int j = 0; j < nex_file; ++j) {
344 for (
int isite = 0; isite < lvol; ++isite) {
345 for (
int i = 0; i < nin_file; ++i) {
348 *p++ = v->
cmp(s, isite, t);
360 limeWriteRecordData(buf, &nbytes, writer);
369 void FieldIO_LIME::store_lfn(LimeWriter *writer, std::string lfn_string)
371 off_t nbytes = lfn_string.size();
373 LimeRecordHeader *h = limeCreateHeader(1 , 1 , const_cast<char *>(
"ildg-data-lfn"), nbytes);
375 limeWriteRecordHeader(h, writer);
376 limeDestroyHeader(h);
379 limeWriteRecordData(const_cast<char *>(lfn_string.c_str()), &nbytes, writer);
388 int nin_field = v->
nin();
389 int nex_field = v->
nex();
394 if ((nin_file == 0) || (nex_file == 0)) {
395 nin_file = nin_field;
396 nex_file = nex_field;
403 vtmp.
reset(nin_field, Lvol, nex_field);
413 vout.
detailed(
m_vl,
"writing gauge configuration to %s\n", filename.c_str());
415 FILE *fp = fopen(filename.c_str(),
"w");
421 LimeWriter *writer = limeCreateWriter(fp);
428 #ifdef USE_ILDG_METADATA
429 store_metadata(writer);
433 store_data(writer, &vtmp,
true,
true);
438 limeDestroyWriter(writer);
452 if (vv.size() == 0)
return;
454 int nin_field = vv[0]->nin();
455 int nex_field = vv[0]->nex();
462 vout.
detailed(
m_vl,
"reading gauge configuration from %s", filename.c_str());
464 vtmp.
reset(nin_field, Lvol, nex_field);
466 FILE *fp = fopen(filename.c_str(),
"r");
472 LimeReader *reader = limeCreateReader(fp);
484 int status = limeReaderNextRecord(reader);
485 if (status != LIME_SUCCESS)
break;
487 const char *t = limeReaderType(reader);
489 if (strcmp(t,
"ildg-format") == 0) {
490 #if USE_ILDG_METADATA
491 ILDG_Format::Params params;
492 load_metadata(reader, ¶ms);
493 check_metadata(¶ms);
495 }
else if (strcmp(t,
"ildg-binary-data") == 0) {
496 load_data(reader, &vtmp);
499 }
else if (strcmp(t,
"ildg-data-lfn") == 0) {
506 limeDestroyReader(reader);
511 for (
int i = 0, n = vv.size(); i < n; ++i) {
523 if (vv.size() == 0)
return;
525 int nin_field = vv[0]->nin();
526 int nex_field = vv[0]->nex();
531 if ((nin_file == 0) || (nex_file == 0)) {
532 nin_file = nin_field;
533 nex_file = nex_field;
540 vtmp.
reset(nin_field, Lvol, nex_field);
544 LimeWriter *writer = NULL;
548 vout.
detailed(
m_vl,
"writing gauge configuration to %s\n", filename.c_str());
550 fp = fopen(filename.c_str(),
"w");
556 writer = limeCreateWriter(fp);
563 #ifdef USE_ILDG_METADATA
564 store_metadata(writer);
568 for (
int i = 0, n = vv.size(); i < n; ++i) {
576 store_data(writer, &vtmp, (i == 0), (i == n - 1));
584 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 read_file(Field *v, std::string filename)
read data from file.
static bool is_bigendian()
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)
const IO_Format::Format * m_format
void crucial(const char *format,...)
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