33 #include "ildg_metadata.h"
45 #define off_t n_uint64_t
47 static const off_t block_size = 64;
54 void FieldIO_LIME::check_metadata(
const ILDG_Format::Params *params)
62 if (!((params->Lx == Lx) &&
65 (params->Lt == Lt))) {
66 vout.
crucial(
m_vl,
"lattice size mismatch. config=(%d,%d,%d,%d), expected=(%d,%d,%d,%d).\n",
67 params->Lx, params->Ly, params->Lz, params->Lt,
72 if (params->prec == 32) {
80 void FieldIO_LIME::load_metadata(LimeReader *reader, ILDG_Format::Params *params)
82 off_t nbytes = limeReaderBytes(reader);
86 off_t nbytes_alloc = nbytes + ((nbytes % block_size) ? (block_size - (nbytes % block_size)) : 0);
88 char *buf = (
char *)malloc(nbytes_alloc);
95 int status = limeReaderReadData(buf, &nbytes, reader);
96 if (status != LIME_SUCCESS) {
105 ILDG_Format::Metadata md;
106 md.read_from_buffer(buf).extract(params);
114 void FieldIO_LIME::load_lfn(LimeReader *reader)
116 off_t nbytes = limeReaderBytes(reader);
120 off_t nbytes_alloc = (nbytes + 1) + (block_size - ((nbytes + 1) % block_size));
122 char *buf = (
char *)malloc(nbytes_alloc);
130 int status = limeReaderReadData(buf, &nbytes, reader);
131 if (status != LIME_SUCCESS) {
146 void FieldIO_LIME::load_data(LimeReader *reader,
Field *v)
150 off_t nbytes = limeReaderBytes(reader);
152 vout.
detailed(
m_vl,
"ildg-binary-data: limeReaderBytes: %lu bytes to read.\n", nbytes);
155 char *buf = (
char *)malloc(nbytes);
161 int status = limeReaderReadData(buf, &nbytes, reader);
162 if (status != LIME_SUCCESS) {
169 byte_swap(buf, word_size, nbytes / word_size);
176 if ((nin_file == 0) || (nex_file == 0)) {
181 int lvol = v->
nvol();
183 double *p = (
double *)buf;
185 for (
int j = 0; j < nex_file; ++j) {
186 for (
int isite = 0; isite < lvol; ++isite) {
187 for (
int i = 0; i < nin_file; ++i) {
190 v->
set(s, isite, t, *p++);
199 void FieldIO_LIME::process_file(
Field *v,
string filename)
201 FILE *fp = fopen(filename.c_str(),
"r");
207 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);
245 int nin_field = v->
nin();
246 int nex_field = v->
nex();
253 vout.
detailed(
m_vl,
"reading gauge configuration from %s", filename.c_str());
255 vtmp.
reset(nin_field, Lvol, nex_field);
257 process_file(&vtmp, filename);
267 #if USE_ILDG_METADATA
269 void FieldIO_LIME::store_metadata(LimeWriter *writer)
274 ILDG_Format::Params params;
280 params.prec = 8 *
sizeof(double);
282 ILDG_Format::Metadata md;
285 const int buf_size = 4 * 1024;
286 char *buf = (
char *)malloc(buf_size);
288 md.write_to_buffer(buf, buf_size);
290 off_t nbytes = strlen(buf);
292 LimeRecordHeader *h = limeCreateHeader(1 , 0 , const_cast<char *>(
"ildg-format"), nbytes);
293 limeWriteRecordHeader(h, writer);
294 limeDestroyHeader(h);
296 limeWriteRecordData(buf, &nbytes, writer);
304 void FieldIO_LIME::store_data(LimeWriter *writer,
Field *v)
310 off_t nbytes =
sizeof(double) * v->
size();
312 LimeRecordHeader *h = limeCreateHeader(0 , 1 , const_cast<char *>(
"ildg-binary-data"), nbytes);
313 limeWriteRecordHeader(h, writer);
314 limeDestroyHeader(h);
316 char *buf = (
char *)malloc(nbytes);
324 int nin_field = v->
nin();
325 int nex_field = v->
nex();
330 if ((nin_file == 0) || (nex_file == 0)) {
331 nin_file = nin_field;
332 nex_file = nex_field;
338 double *p = (
double *)buf;
340 for (
int j = 0; j < nex_file; ++j) {
341 for (
int isite = 0; isite < lvol; ++isite) {
342 for (
int i = 0; i < nin_file; ++i) {
345 *p++ = v->
cmp(s, isite, t);
357 limeWriteRecordData(buf, &nbytes, writer);
365 void FieldIO_LIME::store_lfn(LimeWriter *writer,
string lfn_string)
367 off_t nbytes = lfn_string.size();
369 LimeRecordHeader *h = limeCreateHeader(1 , 1 , const_cast<char *>(
"ildg-data-lfn"), nbytes);
371 limeWriteRecordHeader(h, writer);
372 limeDestroyHeader(h);
375 limeWriteRecordData(const_cast<char *>(lfn_string.c_str()), &nbytes, writer);
383 int nin_field = v->
nin();
384 int nex_field = v->
nex();
389 if ((nin_file == 0) || (nex_file == 0)) {
390 nin_file = nin_field;
391 nex_file = nex_field;
398 vtmp.
reset(nin_field, Lvol, nex_field);
408 vout.
detailed(
m_vl,
"writing gauge configuration to %s\n", filename.c_str());
410 FILE *fp = fopen(filename.c_str(),
"w");
416 LimeWriter *writer = limeCreateWriter(fp);
423 #ifdef USE_ILDG_METADATA
424 store_metadata(writer);
428 store_data(writer, &vtmp);
433 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