24 m_is_initialized(false),
25 m_nvol(0), m_nin_file(0), m_nex_file(0)
29 FieldIO_Binary_Parallel::~FieldIO_Binary_Parallel()
38 static const char _function_name[] =
"FieldIO_Binary_Parallel::read_file";
51 double *buf =
new double [m_nin_file * m_nvol * m_nex_file];
53 vout.
crucial(
m_vl,
"Error at %s: allocate buffer failed.\n", _function_name);
57 ret = MPI_File_open(
Communicator_impl::world(), const_cast<char *>(filename.c_str()), MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
59 vout.
crucial(
m_vl,
"Error at %s: MPI_File_open failed.\n", _function_name);
63 ret = MPI_File_set_view(fh, 0, m_type_vector, m_type_tiled, const_cast<char *>(
"native"), MPI_INFO_NULL);
65 vout.
crucial(
m_vl,
"Error at %s: MPI_File_set_view failed.\n", _function_name);
69 ret = MPI_File_read_all(fh, (
void *)buf, m_nvol * m_nex_file, m_type_vector, MPI_STATUS_IGNORE);
71 vout.
crucial(
m_vl,
"Error at %s: MPI_File_read_all failed.\n", _function_name);
75 ret = MPI_File_close(&fh);
77 vout.
crucial(
m_vl,
"Error at %s: MPI_File_close failed.\n", _function_name);
82 convert_endian(buf,
sizeof(
double), m_nvol * m_nin_file * m_nex_file);
88 for (
int j = 0; j < m_nex_file; ++j) {
89 for (
int isite = 0; isite < m_nvol; ++isite) {
90 for (
int i = 0; i < m_nin_file; ++i) {
94 u->
set(s, isite, t, *p++);
108 static const char _function_name[] =
"FieldIO_Binary_Parallel::write_file";
116 double *buf =
new double [m_nin_file * m_nvol * m_nex_file];
118 vout.
crucial(
m_vl,
"Error at %s: allocate buffer failed.\n", _function_name);
125 for (
int j = 0; j < m_nex_file; ++j) {
126 for (
int isite = 0; isite < m_nvol; ++isite) {
127 for (
int i = 0; i < m_nin_file; ++i) {
131 *p++ = u->
cmp(s, isite, t);
137 convert_endian(buf,
sizeof(
double), m_nin_file * m_nvol * m_nex_file);
143 ret = MPI_File_open(
Communicator_impl::world(), const_cast<char *>(filename.c_str()), MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
145 vout.
crucial(
m_vl,
"Error at %s: MPI_File_open failed.\n", _function_name);
149 ret = MPI_File_set_view(fh, 0, m_type_vector, m_type_tiled, const_cast<char *>(
"native"), MPI_INFO_NULL);
151 vout.
crucial(
m_vl,
"Error at %s: MPI_File_set_view failed.\n", _function_name);
155 ret = MPI_File_write_all(fh, (
void *)buf, m_nvol * m_nex_file, m_type_vector, MPI_STATUS_IGNORE);
157 vout.
crucial(
m_vl,
"Error at %s: MPI_File_write_all failed.\n", _function_name);
161 ret = MPI_File_close(&fh);
163 vout.
crucial(
m_vl,
"Error at %s: MPI_File_close failed.\n", _function_name);
174 int FieldIO_Binary_Parallel::initialize(
const Field *v)
176 static const char _function_name[] =
"FieldIO_Binary_Parallel::initialize";
181 const int nvol = v->
nvol();
184 if (m_is_initialized &&
185 (nin_file == m_nin_file) &&
186 (nex_file == m_nex_file) &&
197 m_nin_file = nin_file;
198 m_nex_file = nex_file;
202 int *global_dims =
new int[ndim];
208 int *local_dims =
new int[ndim];
215 for (
int i = 0; i < ndim; ++i) {
216 m_nvol *= local_dims[i];
219 int *grid_pos =
new int[ndim];
220 for (
int i = 0; i < ndim; ++i) {
224 int *starts =
new int[ndim];
225 for (
int i = 0; i < ndim; ++i) {
226 starts[i] = local_dims[i] * grid_pos[i];
232 ret = MPI_Type_contiguous(
sizeof(
double) * m_nin_file, MPI_BYTE, &m_type_vector);
234 vout.
crucial(
m_vl,
"%s: MPI_Type_Contiguous failed.\n", _function_name);
238 ret = MPI_Type_commit(&m_type_vector);
245 ret = MPI_Type_create_subarray(ndim, global_dims, local_dims, starts, MPI_ORDER_FORTRAN, m_type_vector, &m_type_tiled);
247 vout.
crucial(
m_vl,
"%s: MPI_Type_create_subarray failed.\n", _function_name);
251 ret = MPI_Type_commit(&m_type_tiled);
259 delete [] local_dims;
260 delete [] global_dims;
263 m_is_initialized =
true;
272 int FieldIO_Binary_Parallel::clear_layout()
274 const char _function_name[] =
"FieldIO_Binary_Parallel::clear_layout";
276 if (m_is_initialized) {
283 ret = MPI_Type_free(&m_type_vector);
285 vout.
crucial(
m_vl,
"%s: MPI_Type_free for type_vector failed.\n", _function_name);
289 ret = MPI_Type_free(&m_type_tiled);
291 vout.
crucial(
m_vl,
"%s: MPI_Type_free for type_tiled failed.\n", _function_name);
295 m_is_initialized =
false;
303 int FieldIO_Binary_Parallel::finalize()
305 static const char _function_name[] =
"FieldIO_Binary_Parallel::finalize";
void read_file(Field *v, const std::string filename)
read data from file.
void detailed(const char *format,...)
void set(const int jin, const int site, const int jex, double v)
Container of Field-type object.
double cmp(const int jin, const int site, const int jex) const
static int ipe(const int dir)
logical coordinate of current proc.
static bool is_bigendian()
void write_file(Field *v, const std::string filename)
write data to file.
FieldIO_Binary_Parallel(const IO_Format::Format *format)
static MPI_Comm & world()
retrieves current communicator.
const IO_Format::Format * m_format
void crucial(const char *format,...)
static const std::string class_name
static void convert_endian(void *buf, size_t size, size_t nmemb)
check if machine byte order is big-endian.
FieldIO class for file I/O of space-time distributed data.
Bridge::VerboseLevel m_vl