28 #include "ildg_metadata.h"
40 #define off_t n_uint64_t
42 static const off_t block_size = 64;
49 void FieldIO_LIME::check_metadata(
const ILDG_Format::Params *params)
57 if (!((params->Lx == Lx) &&
60 (params->Lt == Lt))) {
61 vout.
crucial(
m_vl,
"lattice size mismatch. config=(%d,%d,%d,%d), expected=(%d,%d,%d,%d).\n",
62 params->Lx, params->Ly, params->Lz, params->Lt,
67 if (params->prec == 32) {
75 void FieldIO_LIME::load_metadata(LimeReader *reader, ILDG_Format::Params *params)
77 off_t nbytes = limeReaderBytes(reader);
81 off_t nbytes_alloc = nbytes + ((nbytes % block_size) ? (block_size - (nbytes % block_size)) : 0);
83 char *buf = (
char *)malloc(nbytes_alloc);
90 int status = limeReaderReadData(buf, &nbytes, reader);
91 if (status != LIME_SUCCESS) {
100 ILDG_Format::Metadata md;
101 md.read_from_buffer(buf).extract(params);
107 void FieldIO_LIME::load_lfn(LimeReader *reader)
109 off_t nbytes = limeReaderBytes(reader);
113 off_t nbytes_alloc = (nbytes + 1) + (block_size - ((nbytes + 1) % block_size));
115 char *buf = (
char *)malloc(nbytes_alloc);
123 int status = limeReaderReadData(buf, &nbytes, reader);
124 if (status != LIME_SUCCESS) {
139 void FieldIO_LIME::load_data(LimeReader *reader,
Field *v)
143 off_t nbytes = limeReaderBytes(reader);
145 vout.
detailed(
m_vl,
"ildg-binary-data: limeReaderBytes: %lu bytes to read.\n", nbytes);
148 char *buf = (
char *)malloc(nbytes);
154 int status = limeReaderReadData(buf, &nbytes, reader);
155 if (status != LIME_SUCCESS) {
162 byte_swap(buf, word_size, nbytes / word_size);
169 if ((nin_file == 0) || (nex_file == 0)) {
174 int lvol = v->
nvol();
176 double *p = (
double *)buf;
178 for (
int j = 0; j < nex_file; ++j) {
179 for (
int isite = 0; isite < lvol; ++isite) {
180 for (
int i = 0; i < nin_file; ++i) {
183 v->
set(s, isite, t, *p++);
192 void FieldIO_LIME::process_file(
Field *v,
string filename)
194 FILE *fp = fopen(filename.c_str(),
"r");
200 LimeReader *reader = limeCreateReader(fp);
205 #if USE_ILDG_METADATA
206 ILDG_Format::Params params;
211 int status = limeReaderNextRecord(reader);
212 if (status != LIME_SUCCESS)
break;
214 const char *t = limeReaderType(reader);
216 if (strcmp(t,
"ildg-format") == 0) {
217 #if USE_ILDG_METADATA
218 load_metadata(reader, ¶ms);
219 check_metadata(¶ms);
221 }
else if (strcmp(t,
"ildg-binary-data") == 0) {
222 load_data(reader, v);
223 }
else if (strcmp(t,
"ildg-data-lfn") == 0) {
230 limeDestroyReader(reader);
238 int nin_field = v->
nin();
239 int nex_field = v->
nex();
246 vout.
detailed(
m_vl,
"reading gauge configuration from %s", filename.c_str());
248 vtmp.
reset(nin_field, Lvol, nex_field);
250 process_file(&vtmp, filename);
260 #if USE_ILDG_METADATA
262 void FieldIO_LIME::store_metadata(LimeWriter *writer)
267 ILDG_Format::Params params;
273 params.prec = 8 *
sizeof(double);
275 ILDG_Format::Metadata md;
278 const int buf_size = 4 * 1024;
279 char *buf = (
char *)malloc(buf_size);
281 md.write_to_buffer(buf, buf_size);
283 off_t nbytes = strlen(buf);
285 LimeRecordHeader *h = limeCreateHeader(1 , 0 , const_cast<char *>(
"ildg-format"), nbytes);
286 limeWriteRecordHeader(h, writer);
287 limeDestroyHeader(h);
289 limeWriteRecordData(buf, &nbytes, writer);
295 void FieldIO_LIME::store_data(LimeWriter *writer,
Field *v,
296 bool mark_begin,
bool mark_end)
302 off_t nbytes =
sizeof(double) * v->
size();
304 LimeRecordHeader *h = limeCreateHeader(
307 const_cast<char *>(
"ildg-binary-data"), nbytes);
308 limeWriteRecordHeader(h, writer);
309 limeDestroyHeader(h);
311 char *buf = (
char *)malloc(nbytes);
319 int nin_field = v->
nin();
320 int nex_field = v->
nex();
325 if ((nin_file == 0) || (nex_file == 0)) {
326 nin_file = nin_field;
327 nex_file = nex_field;
333 double *p = (
double *)buf;
335 for (
int j = 0; j < nex_file; ++j) {
336 for (
int isite = 0; isite < lvol; ++isite) {
337 for (
int i = 0; i < nin_file; ++i) {
340 *p++ = v->
cmp(s, isite, t);
352 limeWriteRecordData(buf, &nbytes, writer);
360 void FieldIO_LIME::store_lfn(LimeWriter *writer,
string lfn_string)
362 off_t nbytes = lfn_string.size();
364 LimeRecordHeader *h = limeCreateHeader(1 , 1 , const_cast<char *>(
"ildg-data-lfn"), nbytes);
366 limeWriteRecordHeader(h, writer);
367 limeDestroyHeader(h);
370 limeWriteRecordData(const_cast<char *>(lfn_string.c_str()), &nbytes, writer);
378 int nin_field = v->
nin();
379 int nex_field = v->
nex();
384 if ((nin_file == 0) || (nex_file == 0)) {
385 nin_file = nin_field;
386 nex_file = nex_field;
393 vtmp.
reset(nin_field, Lvol, nex_field);
403 vout.
detailed(
m_vl,
"writing gauge configuration to %s\n", filename.c_str());
405 FILE *fp = fopen(filename.c_str(),
"w");
411 LimeWriter *writer = limeCreateWriter(fp);
418 #ifdef USE_ILDG_METADATA
419 store_metadata(writer);
423 store_data(writer, &vtmp,
true,
true);
428 limeDestroyWriter(writer);
441 if (vv.size() == 0)
return;
443 int nin_field = vv[0]->nin();
444 int nex_field = vv[0]->nex();
451 vout.
detailed(
m_vl,
"reading gauge configuration from %s", filename.c_str());
453 vtmp.
reset(nin_field, Lvol, nex_field);
455 FILE *fp = fopen(filename.c_str(),
"r");
460 LimeReader *reader = limeCreateReader(fp);
471 int status = limeReaderNextRecord(reader);
472 if (status != LIME_SUCCESS)
break;
474 const char *t = limeReaderType(reader);
476 if (strcmp(t,
"ildg-format") == 0) {
477 #if USE_ILDG_METADATA
478 ILDG_Format::Params params;
479 load_metadata(reader, ¶ms);
480 check_metadata(¶ms);
482 }
else if (strcmp(t,
"ildg-binary-data") == 0) {
483 load_data(reader, &vtmp);
486 }
else if (strcmp(t,
"ildg-data-lfn") == 0) {
493 limeDestroyReader(reader);
498 for (
int i = 0, n = vv.size(); i < n; ++i) {
509 if (vv.size() == 0)
return;
511 int nin_field = vv[0]->nin();
512 int nex_field = vv[0]->nex();
517 if ((nin_file == 0) || (nex_file == 0)) {
518 nin_file = nin_field;
519 nex_file = nex_field;
526 vtmp.
reset(nin_field, Lvol, nex_field);
530 LimeWriter *writer = NULL;
534 vout.
detailed(
m_vl,
"writing gauge configuration to %s\n", filename.c_str());
536 fp = fopen(filename.c_str(),
"w");
542 writer = limeCreateWriter(fp);
549 #ifdef USE_ILDG_METADATA
550 store_metadata(writer);
554 for (
int i = 0, n = vv.size(); i < n; ++i) {
562 store_data(writer, &vtmp, (i == 0), (i == n - 1));
570 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