33 #include "ildg_metadata.h"
45 #define off_t n_uint64_t
47 static const off_t block_size = 64;
52 void FieldIO_LIME::check_metadata(
const ILDG_Format::Params *params)
60 if (!((params->Lx == Lx) &&
63 (params->Lt == Lt))) {
64 vout.
crucial(
m_vl,
"lattice size mismatch. config=(%d,%d,%d,%d), expected=(%d,%d,%d,%d).\n",
65 params->Lx, params->Ly, params->Lz, params->Lt,
70 if (params->prec == 32) {
78 void FieldIO_LIME::load_metadata(LimeReader *reader, ILDG_Format::Params *params)
80 off_t nbytes = limeReaderBytes(reader);
84 off_t nbytes_alloc = nbytes + ((nbytes % block_size) ? (block_size - (nbytes % block_size)) : 0);
86 char *buf = (
char *)malloc(nbytes_alloc);
93 int status = limeReaderReadData(buf, &nbytes, reader);
94 if (status != LIME_SUCCESS) {
103 ILDG_Format::Metadata md;
104 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) {
142 void FieldIO_LIME::load_data(LimeReader *reader,
Field *v)
146 off_t nbytes = limeReaderBytes(reader);
148 vout.
detailed(
m_vl,
"ildg-binary-data: limeReaderBytes: %lu bytes to read.\n", nbytes);
151 char *buf = (
char *)malloc(nbytes);
157 int status = limeReaderReadData(buf, &nbytes, reader);
158 if (status != LIME_SUCCESS) {
165 byte_swap(buf, word_size, nbytes / word_size);
172 if ((nin_file == 0) || (nex_file == 0)) {
177 int lvol = v->
nvol();
179 double *p = (
double *)buf;
181 for (
int j = 0; j < nex_file; ++j) {
182 for (
int isite = 0; isite < lvol; ++isite) {
183 for (
int i = 0; i < nin_file; ++i) {
186 v->
set(s, isite, t, *p++);
195 void FieldIO_LIME::process_file(
Field *v,
string filename)
197 FILE *fp = fopen(filename.c_str(),
"r");
203 LimeReader *reader = limeCreateReader(fp);
208 #if USE_ILDG_METADATA
209 ILDG_Format::Params params;
214 int status = limeReaderNextRecord(reader);
215 if (status != LIME_SUCCESS)
break;
217 const char *t = limeReaderType(reader);
219 if (strcmp(t,
"ildg-format") == 0) {
220 #if USE_ILDG_METADATA
221 load_metadata(reader, ¶ms);
222 check_metadata(¶ms);
224 }
else if (strcmp(t,
"ildg-binary-data") == 0) {
225 load_data(reader, v);
226 }
else if (strcmp(t,
"ildg-data-lfn") == 0) {
233 limeDestroyReader(reader);
241 int nin_field = v->
nin();
242 int nex_field = v->
nex();
249 vout.
detailed(
m_vl,
"reading gauge configuration from %s", filename.c_str());
251 vtmp.
reset(nin_field, Lvol, nex_field);
253 process_file(&vtmp, filename);
263 #if USE_ILDG_METADATA
265 void FieldIO_LIME::store_metadata(LimeWriter *writer)
270 ILDG_Format::Params params;
276 params.prec = 8 *
sizeof(double);
278 ILDG_Format::Metadata md;
281 const int buf_size = 4 * 1024;
282 char *buf = (
char *)malloc(buf_size);
284 md.write_to_buffer(buf, buf_size);
286 off_t nbytes = strlen(buf);
288 LimeRecordHeader *h = limeCreateHeader(1 , 0 , const_cast<char *>(
"ildg-format"), nbytes);
289 limeWriteRecordHeader(h, writer);
290 limeDestroyHeader(h);
292 limeWriteRecordData(buf, &nbytes, writer);
298 void FieldIO_LIME::store_data(LimeWriter *writer,
Field *v)
304 off_t nbytes =
sizeof(double) * v->
size();
306 LimeRecordHeader *h = limeCreateHeader(0 , 1 , const_cast<char *>(
"ildg-binary-data"), nbytes);
307 limeWriteRecordHeader(h, writer);
308 limeDestroyHeader(h);
310 char *buf = (
char *)malloc(nbytes);
318 int nin_field = v->
nin();
319 int nex_field = v->
nex();
324 if ((nin_file == 0) || (nex_file == 0)) {
325 nin_file = nin_field;
326 nex_file = nex_field;
332 double *p = (
double *)buf;
334 for (
int j = 0; j < nex_file; ++j) {
335 for (
int isite = 0; isite < lvol; ++isite) {
336 for (
int i = 0; i < nin_file; ++i) {
339 *p++ = v->
cmp(s, isite, t);
351 limeWriteRecordData(buf, &nbytes, writer);
359 void FieldIO_LIME::store_lfn(LimeWriter *writer,
string lfn_string)
361 off_t nbytes = lfn_string.size();
363 LimeRecordHeader *h = limeCreateHeader(1 , 1 , const_cast<char *>(
"ildg-data-lfn"), nbytes);
365 limeWriteRecordHeader(h, writer);
366 limeDestroyHeader(h);
369 limeWriteRecordData(const_cast<char *>(lfn_string.c_str()), &nbytes, writer);
377 int nin_field = v->
nin();
378 int nex_field = v->
nex();
383 if ((nin_file == 0) || (nex_file == 0)) {
384 nin_file = nin_field;
385 nex_file = nex_field;
392 vtmp.
reset(nin_field, Lvol, nex_field);
402 vout.
detailed(
m_vl,
"writing gauge configuration to %s\n", filename.c_str());
404 FILE *fp = fopen(filename.c_str(),
"w");
410 LimeWriter *writer = limeCreateWriter(fp);
417 #ifdef USE_ILDG_METADATA
418 store_metadata(writer);
422 store_data(writer, &vtmp);
427 limeDestroyWriter(writer);