30 const char ildg_metadata_template[] =
31 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
32 "<ildgFormat xmlns=\"http://www.lqcd.org/ildg\"\n"
33 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
34 " xsi:schemaLocation=\"http://www.lqcd.org/ildg http://www.lqcd.org/ildg/filefmt.xsd\">\n"
35 " <version> 1.0 </version>\n"
36 " <field> su3gauge </field>\n"
37 " <precision> %zu </precision>\n"
38 " <lx> %d </lx> <ly> %d </ly> <lz> %d </lz> <lt> %d </lt>\n"
43 #define LIME_MAGIC ((uint32_t)0x456789ab)
44 #define MB_MASK ((uint16_t)0x8000)
45 #define ME_MASK ((uint16_t)0x4000)
57 struct LIME_record_info
64 typedef std::list<LIME_record_info> LIME_message_info;
65 typedef std::list<LIME_message_info> LIME_file_info;
67 int traverse(MPI_File& fh, LIME_file_info& file_info);
68 int read_lime_header(MPI_File& fh, LIME_header& header);
69 int read_lime_content(MPI_File& fh,
const MPI_Offset offset,
char *buf,
const size_t length);
70 int find_record_offset(
const LIME_file_info& file_info,
const char *type, MPI_Offset& pos);
72 int report_file_info(
const LIME_file_info& file_info);
74 size_t write_lime_header(MPI_File& fh,
const char *type,
const size_t length,
const uint16_t flag);
75 size_t write_lime_record(MPI_File& fh,
const char *type,
const char *content,
const size_t length,
const uint16_t flag);
83 static const char _function_name[] =
"FieldIO_LIME_MPIIO::read_file";
94 double *buf =
new double [nin_file * nex_file * m_nvol];
101 ret = MPI_File_open(MPI_COMM_WORLD, const_cast<char *>(filename.c_str()), MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
103 ret = MPI_File_open(
Communicator_impl::world(), const_cast<char *>(filename.c_str()), MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
114 LIME_file_info file_info;
116 traverse(fh, file_info);
118 report_file_info(file_info);
120 if (!find_record_offset(file_info,
"ildg-binary-data", pos)) {
129 MPI_Bcast(&pos,
sizeof(off_t), MPI_BYTE, 0, MPI_COMM_WORLD);
134 ret = MPI_File_set_view(fh, pos, m_type_vector, m_type_tiled, const_cast<char *>(
"native"), MPI_INFO_NULL);
140 ret = MPI_File_read_all(fh, (
void *)buf, m_nvol * nex_file, m_type_vector, MPI_STATUS_IGNORE);
146 ret = MPI_File_close(&fh);
154 convert_endian(buf,
sizeof(
double), m_nvol * nin_file * nex_file);
160 for (
int j = 0; j < nex_file; ++j) {
161 for (
int isite = 0; isite < m_nvol; ++isite) {
162 for (
int i = 0; i < nin_file; ++i) {
166 u->
set(s, isite, t, *p++);
180 static const char _function_name[] =
"FieldIO_LIME_MPIIO::write_file";
188 double *buf =
new double [nin_file * nex_file * m_nvol];
199 for (
int j = 0; j < nex_file; ++j) {
200 for (
int isite = 0; isite < m_nvol; ++isite) {
201 for (
int i = 0; i < nin_file; ++i) {
205 *p++ = u->
cmp(s, isite, t);
212 convert_endian(buf,
sizeof(
double), nin_file * nex_file * m_nvol);
219 ret = MPI_File_open(MPI_COMM_WORLD, const_cast<char *>(filename.c_str()), MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
221 ret = MPI_File_open(
Communicator_impl::world(), const_cast<char *>(filename.c_str()), MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
233 sprintf(metadata, ildg_metadata_template,
240 pos += write_lime_record(fh,
"ildg-format", metadata, strlen(metadata), MB_MASK);
243 pos += write_lime_header(fh,
"ildg-binary-data", data_length, ME_MASK);
249 MPI_Bcast(&pos,
sizeof(off_t), MPI_BYTE, 0, MPI_COMM_WORLD);
256 ret = MPI_File_set_view(fh, pos, m_type_vector, m_type_tiled, const_cast<char *>(
"native"), MPI_INFO_NULL);
262 ret = MPI_File_write_all(fh, (
void *)buf, m_nvol * nex_file, m_type_vector, MPI_STATUS_IGNORE);
264 vout.
crucial(
m_vl,
"%s: MPI_File_write_all failed.\n", _function_name);
269 if (data_length % 8 > 0) {
270 size_t padding_size = (8 - data_length % 8) % 8;
272 const char blank[8] =
"";
273 ret = MPI_File_write_at(fh, pos + data_length, const_cast<char *>(blank), padding_size, MPI_BYTE, MPI_STATUS_IGNORE);
275 vout.
crucial(
vl,
"%s: write padding failed.\n", _function_name);
279 vout.
general(
m_vl,
"%s: padding %lu bytes added.\n", _function_name, padding_size);
282 ret = MPI_File_close(&fh);
295 int FieldIO_LIME_MPIIO::initialize()
297 static const char _function_name[] =
"FieldIO_LIME_MPIIO::initialize";
302 if (m_is_initialized)
return EXIT_SUCCESS;
309 int *global_dims =
new int[ndim];
315 int *local_dims =
new int[ndim];
322 for (
int i = 0; i < ndim; ++i) {
323 m_nvol *= local_dims[i];
326 int *grid_pos =
new int[ndim];
327 for (
int i = 0; i < ndim; ++i) {
331 int *starts =
new int[ndim];
332 for (
int i = 0; i < ndim; ++i) {
333 starts[i] = local_dims[i] * grid_pos[i];
340 ret = MPI_Type_contiguous(
sizeof(
double) * nin_file, MPI_BYTE, &m_type_vector);
342 vout.
general(
m_vl,
"%s: MPI_Type_Contiguous failed.\n", _function_name);
346 ret = MPI_Type_commit(&m_type_vector);
353 ret = MPI_Type_create_subarray(ndim, global_dims, local_dims, starts, MPI_ORDER_FORTRAN, m_type_vector, &m_type_tiled);
355 vout.
general(
m_vl,
"%s: MPI_Type_create_subarray failed.\n", _function_name);
359 ret = MPI_Type_commit(&m_type_tiled);
365 m_is_initialized =
true;
369 delete [] local_dims;
370 delete [] global_dims;
379 int FieldIO_LIME_MPIIO::finalize()
381 static const char _function_name[] =
"FieldIO_LIME_MPIIO::finalize";
383 if (!m_is_initialized)
return EXIT_SUCCESS;
387 ret = MPI_Type_free(&m_type_tiled);
393 ret = MPI_Type_free(&m_type_vector);
399 m_is_initialized =
false;
410 int read_lime_header(MPI_File& fh, LIME_header& header)
414 int ret = MPI_File_read(fh, (
void *)&header,
sizeof(LIME_header), MPI_BYTE, &status);
418 MPI_Get_count(&status, MPI_BYTE, &count);
419 if (count !=
sizeof(LIME_header)) {
435 if (header.magic != LIME_MAGIC) {
445 int traverse(MPI_File& fh, LIME_file_info& file_info)
448 MPI_File_seek(fh, 0, MPI_SEEK_SET);
452 LIME_message_info message_info;
457 int stat = read_lime_header(fh, header);
475 pos +=
sizeof(LIME_header);
477 LIME_record_info record_info;
479 memcpy((
void *)&record_info, (
void *)&header,
sizeof(LIME_record_info));
480 record_info.offset = pos;
483 size_t padding_size = (8 - header.length % 8) % 8;
486 MPI_File_seek(fh, header.length + padding_size, MPI_SEEK_CUR);
487 pos += header.length + padding_size;
491 if ((header.bitfield & MB_MASK) == MB_MASK) {
492 message_info.clear();
495 message_info.push_back(record_info);
497 if ((header.bitfield & ME_MASK) == ME_MASK) {
498 file_info.push_back(message_info);
508 int find_record_offset(
const LIME_file_info& file_info,
const char *type, MPI_Offset& pos)
510 bool is_found =
false;
512 for (LIME_file_info::const_iterator p = file_info.begin(); p != file_info.end(); ++p) {
513 for (LIME_message_info::const_iterator q = p->begin(); q != p->end(); ++q) {
514 if (strncmp(q->type, type, strlen(type)) == 0) {
522 return is_found ? 1 : 0;
527 int read_record_content(MPI_File& fh,
const LIME_file_info& file_info,
const char *type, std::string& content)
529 bool is_found =
false;
530 LIME_record_info info;
532 for (LIME_file_info::const_iterator p = file_info.begin(); p != file_info.end(); ++p) {
533 for (LIME_message_info::const_iterator q = p->begin(); q != p->end(); ++q) {
534 if (strncmp(q->type, type, strlen(type)) == 0) {
547 char *buf =
new char [info.length + 1];
548 MPI_File_read_at(fh, info.offset, buf, info.length, MPI_BYTE, &status);
551 MPI_Get_count(&status, MPI_BYTE, &count);
553 if (count != info.length) {
554 vout.
crucial(
vl,
"%s: read error. content length mismatch.\n", __func__);
558 content = std::string(buf);
565 int report_file_info(
const LIME_file_info& file_info)
571 for (LIME_file_info::const_iterator p = file_info.begin(); p != file_info.end(); ++p) {
574 for (LIME_message_info::const_iterator q = p->begin(); q != p->end(); ++q) {
589 size_t write_lime_header(MPI_File& fh,
const char *type,
const size_t length,
const uint16_t flag)
593 memset(&header, 0,
sizeof(LIME_header));
595 header.magic = LIME_MAGIC;
596 header.version = (uint16_t)1;
597 header.bitfield = flag;
598 strncpy(header.type, type, 128);
599 header.length = length;
609 int ret = MPI_File_write(fh, (
void *)&header,
sizeof(LIME_header), MPI_BYTE, &status);
616 return sizeof(LIME_header);
621 size_t write_lime_record(MPI_File& fh,
const char *type,
const char *content,
const size_t length,
const uint16_t flag)
623 const char blank[8] =
"";
625 if (write_lime_header(fh, type, length, flag) == 0) {
629 size_t padding_size = (8 - length % 8) % 8;
632 int ret = MPI_File_write(fh, const_cast<char *>(content), length, MPI_BYTE, &status);
638 if (padding_size > 0) {
639 ret = MPI_File_write(fh, const_cast<char *>(blank), padding_size, MPI_BYTE, &status);
646 return sizeof(LIME_header) + length + padding_size;
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 general(const char *format,...)
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 int broadcast(size_t size, void *data, int sender)
static int ipe(const int dir)
logical coordinate of current proc.
static bool is_bigendian()
void write_file(Field *v, std::string filename)
write data to file.
const IO_Format::Format * m_format
void crucial(const char *format,...)
static void convert_endian(void *buf, size_t size, size_t nmemb)
check if machine byte order is big-endian.
static bool is_primary()
check if the present node is primary in small communicator.
Bridge::VerboseLevel m_vl