29 const char ildg_metadata_template[] =
30 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
31 "<ildgFormat xmlns=\"http://www.lqcd.org/ildg\"\n"
32 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
33 " xsi:schemaLocation=\"http://www.lqcd.org/ildg http://www.lqcd.org/ildg/filefmt.xsd\">\n"
34 " <version> 1.0 </version>\n"
35 " <field> su3gauge </field>\n"
36 " <precision> %zu </precision>\n"
37 " <lx> %d </lx> <ly> %d </ly> <lz> %d </lz> <lt> %d </lt>\n"
42 #define LIME_MAGIC ((uint32_t)0x456789ab)
43 #define MB_MASK ((uint16_t)0x8000)
44 #define ME_MASK ((uint16_t)0x4000)
56 struct LIME_record_info
63 typedef std::list<LIME_record_info> LIME_message_info;
64 typedef std::list<LIME_message_info> LIME_file_info;
66 int traverse(MPI_File& fh, LIME_file_info& file_info);
67 int read_lime_header(MPI_File& fh, LIME_header& header);
68 int read_lime_content(MPI_File& fh,
const MPI_Offset offset,
char *buf,
const size_t length);
69 int find_record_offset(
const LIME_file_info& file_info,
const char *type, MPI_Offset& pos);
71 int report_file_info(
const LIME_file_info& file_info);
73 size_t write_lime_header(MPI_File& fh,
const char *type,
const size_t length,
const uint16_t flag);
74 size_t write_lime_record(MPI_File& fh,
const char *type,
const char *content,
const size_t length,
const uint16_t flag);
82 static const char _function_name[] =
"FieldIO_LIME_MPIIO::read_file";
93 double *buf =
new double [nin_file * nex_file * m_nvol];
100 ret = MPI_File_open(MPI_COMM_WORLD, const_cast<char *>(filename.c_str()), MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
102 ret = MPI_File_open(
Communicator_impl::world(), const_cast<char *>(filename.c_str()), MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
113 LIME_file_info file_info;
115 traverse(fh, file_info);
117 report_file_info(file_info);
119 if (!find_record_offset(file_info,
"ildg-binary-data", pos)) {
128 MPI_Bcast(&pos,
sizeof(off_t), MPI_BYTE, 0, MPI_COMM_WORLD);
133 ret = MPI_File_set_view(fh, pos, m_type_vector, m_type_tiled, const_cast<char *>(
"native"), MPI_INFO_NULL);
139 ret = MPI_File_read_all(fh, (
void *)buf, m_nvol * nex_file, m_type_vector, MPI_STATUS_IGNORE);
145 ret = MPI_File_close(&fh);
153 convert_endian(buf,
sizeof(
double), m_nvol * nin_file * nex_file);
159 for (
int j = 0; j < nex_file; ++j) {
160 for (
int isite = 0; isite < m_nvol; ++isite) {
161 for (
int i = 0; i < nin_file; ++i) {
165 u->
set(s, isite, t, *p++);
179 static const char _function_name[] =
"FieldIO_LIME_MPIIO::write_file";
187 double *buf =
new double [nin_file * nex_file * m_nvol];
198 for (
int j = 0; j < nex_file; ++j) {
199 for (
int isite = 0; isite < m_nvol; ++isite) {
200 for (
int i = 0; i < nin_file; ++i) {
204 *p++ = u->
cmp(s, isite, t);
211 convert_endian(buf,
sizeof(
double), nin_file * nex_file * m_nvol);
218 ret = MPI_File_open(MPI_COMM_WORLD, const_cast<char *>(filename.c_str()), MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
220 ret = MPI_File_open(
Communicator_impl::world(), const_cast<char *>(filename.c_str()), MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
232 sprintf(metadata, ildg_metadata_template,
239 pos += write_lime_record(fh,
"ildg-format", metadata, strlen(metadata), MB_MASK);
242 pos += write_lime_header(fh,
"ildg-binary-data", data_length, ME_MASK);
248 MPI_Bcast(&pos,
sizeof(off_t), MPI_BYTE, 0, MPI_COMM_WORLD);
255 ret = MPI_File_set_view(fh, pos, m_type_vector, m_type_tiled, const_cast<char *>(
"native"), MPI_INFO_NULL);
261 ret = MPI_File_write_all(fh, (
void *)buf, m_nvol * nex_file, m_type_vector, MPI_STATUS_IGNORE);
263 vout.
crucial(
m_vl,
"%s: MPI_File_write_all failed.\n", _function_name);
268 if (data_length % 8 > 0) {
269 size_t padding_size = (8 - data_length % 8) % 8;
271 const char blank[8] =
"";
272 ret = MPI_File_write_at(fh, pos + data_length, const_cast<char *>(blank), padding_size, MPI_BYTE, MPI_STATUS_IGNORE);
274 vout.
crucial(
vl,
"%s: write padding failed.\n", _function_name);
278 vout.
general(
m_vl,
"%s: padding %lu bytes added.\n", _function_name, padding_size);
281 ret = MPI_File_close(&fh);
294 int FieldIO_LIME_MPIIO::initialize()
296 static const char _function_name[] =
"FieldIO_LIME_MPIIO::initialize";
301 if (m_is_initialized)
return EXIT_SUCCESS;
308 int *global_dims =
new int[ndim];
314 int *local_dims =
new int[ndim];
321 for (
int i = 0; i < ndim; ++i) {
322 m_nvol *= local_dims[i];
325 int *grid_pos =
new int[ndim];
326 for (
int i = 0; i < ndim; ++i) {
330 int *starts =
new int[ndim];
331 for (
int i = 0; i < ndim; ++i) {
332 starts[i] = local_dims[i] * grid_pos[i];
339 ret = MPI_Type_contiguous(
sizeof(
double) * nin_file, MPI_BYTE, &m_type_vector);
341 vout.
general(
m_vl,
"%s: MPI_Type_Contiguous failed.\n", _function_name);
345 ret = MPI_Type_commit(&m_type_vector);
352 ret = MPI_Type_create_subarray(ndim, global_dims, local_dims, starts, MPI_ORDER_FORTRAN, m_type_vector, &m_type_tiled);
354 vout.
general(
m_vl,
"%s: MPI_Type_create_subarray failed.\n", _function_name);
358 ret = MPI_Type_commit(&m_type_tiled);
364 m_is_initialized =
true;
368 delete [] local_dims;
369 delete [] global_dims;
378 int FieldIO_LIME_MPIIO::finalize()
380 static const char _function_name[] =
"FieldIO_LIME_MPIIO::finalize";
382 if (!m_is_initialized)
return EXIT_SUCCESS;
386 ret = MPI_Type_free(&m_type_tiled);
392 ret = MPI_Type_free(&m_type_vector);
398 m_is_initialized =
false;
409 int read_lime_header(MPI_File& fh, LIME_header& header)
413 int ret = MPI_File_read(fh, (
void *)&header,
sizeof(LIME_header), MPI_BYTE, &status);
417 MPI_Get_count(&status, MPI_BYTE, &count);
418 if (count !=
sizeof(LIME_header)) {
434 if (header.magic != LIME_MAGIC) {
444 int traverse(MPI_File& fh, LIME_file_info& file_info)
447 MPI_File_seek(fh, 0, MPI_SEEK_SET);
451 LIME_message_info message_info;
456 int stat = read_lime_header(fh, header);
474 pos +=
sizeof(LIME_header);
476 LIME_record_info record_info;
478 memcpy((
void *)&record_info, (
void *)&header,
sizeof(LIME_record_info));
479 record_info.offset = pos;
482 size_t padding_size = (8 - header.length % 8) % 8;
485 MPI_File_seek(fh, header.length + padding_size, MPI_SEEK_CUR);
486 pos += header.length + padding_size;
490 if ((header.bitfield & MB_MASK) == MB_MASK) {
491 message_info.clear();
494 message_info.push_back(record_info);
496 if ((header.bitfield & ME_MASK) == ME_MASK) {
497 file_info.push_back(message_info);
507 int find_record_offset(
const LIME_file_info& file_info,
const char *type, MPI_Offset& pos)
509 bool is_found =
false;
511 for (LIME_file_info::const_iterator p = file_info.begin(); p != file_info.end(); ++p) {
512 for (LIME_message_info::const_iterator q = p->begin(); q != p->end(); ++q) {
513 if (strncmp(q->type, type, strlen(type)) == 0) {
521 return is_found ? 1 : 0;
526 int read_record_content(MPI_File& fh,
const LIME_file_info& file_info,
const char *type, std::string& content)
528 bool is_found =
false;
529 LIME_record_info info;
531 for (LIME_file_info::const_iterator p = file_info.begin(); p != file_info.end(); ++p) {
532 for (LIME_message_info::const_iterator q = p->begin(); q != p->end(); ++q) {
533 if (strncmp(q->type, type, strlen(type)) == 0) {
546 char *buf =
new char [info.length + 1];
547 MPI_File_read_at(fh, info.offset, buf, info.length, MPI_BYTE, &status);
550 MPI_Get_count(&status, MPI_BYTE, &count);
552 if (count != info.length) {
553 vout.
crucial(
vl,
"%s: read error. content length mismatch.\n", __func__);
557 content = std::string(buf);
564 int report_file_info(
const LIME_file_info& file_info)
570 for (LIME_file_info::const_iterator p = file_info.begin(); p != file_info.end(); ++p) {
573 for (LIME_message_info::const_iterator q = p->begin(); q != p->end(); ++q) {
588 size_t write_lime_header(MPI_File& fh,
const char *type,
const size_t length,
const uint16_t flag)
592 memset(&header, 0,
sizeof(LIME_header));
594 header.magic = LIME_MAGIC;
595 header.version = (uint16_t)1;
596 header.bitfield = flag;
597 strncpy(header.type, type, 128);
598 header.length = length;
608 int ret = MPI_File_write(fh, (
void *)&header,
sizeof(LIME_header), MPI_BYTE, &status);
615 return sizeof(LIME_header);
620 size_t write_lime_record(MPI_File& fh,
const char *type,
const char *content,
const size_t length,
const uint16_t flag)
622 const char blank[8] =
"";
624 if (write_lime_header(fh, type, length, flag) == 0) {
628 size_t padding_size = (8 - length % 8) % 8;
631 int ret = MPI_File_write(fh, const_cast<char *>(content), length, MPI_BYTE, &status);
637 if (padding_size > 0) {
638 ret = MPI_File_write(fh, const_cast<char *>(blank), padding_size, MPI_BYTE, &status);
645 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