Bridge++  Ver. 1.2.x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fieldIO_Binary_Parallel.cpp
Go to the documentation of this file.
1 
9 // static const char rcsid[] = "$Id: fieldIO_Binary_Parallel.cpp 1147 2014-09-16 13:41:54Z matufuru $";
10 
11 // this code only makes sense in MPI environment.
12 #ifdef USE_MPI
13 
15 
16 const std::string FieldIO_Binary_Parallel::class_name = "FieldIO_Binary_Parallel";
17 
18 //====================================================================
19 void FieldIO_Binary_Parallel::read_file(Field *u, string filename)
20 {
21  static const char _function_name[] = "FieldIO_Binary_Parallel::read_file";
22 
23  initialize();
24 
25  MPI_File fh;
26  int ret;
27 
28  int nin_file = m_format->nin();
29  int nex_file = m_format->nex();
30 
31 // Field::element_type *buf = new Field::element_type [m_nvol*m_nvector];
32  double *buf = new double [nin_file * nex_file * m_nvol];
33  if (!buf) {
34  vout.crucial(m_vl, "%s: allocate buffer failed.\n", _function_name);
35  abort();
36  }
37 
38 #ifdef USE_BGNET
39  ret = MPI_File_open(MPI_COMM_WORLD, const_cast<char *>(filename.c_str()), MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
40 #else
41  ret = MPI_File_open(Communicator_impl::world(), const_cast<char *>(filename.c_str()), MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
42 #endif
43  if (ret) {
44  vout.crucial(m_vl, "%s: MPI_File_open failed.\n", _function_name);
45  abort();
46  }
47 
48  ret = MPI_File_set_view(fh, 0, m_type_vector, m_type_tiled, const_cast<char *>("native"), MPI_INFO_NULL);
49  if (ret) {
50  vout.crucial(m_vl, "%s: MPI_File_set_view failed.\n", _function_name);
51  abort();
52  }
53 
54  ret = MPI_File_read_all(fh, (void *)buf, m_nvol * nex_file, m_type_vector, MPI_STATUS_IGNORE);
55  if (ret) {
56  vout.crucial(m_vl, "%s: MPI_File_read_all failed.\n", _function_name);
57  abort();
58  }
59 
60  ret = MPI_File_close(&fh);
61  if (ret) {
62  vout.crucial(m_vl, "%s: MPI_File_close failed.\n", _function_name);
63  abort();
64  }
65 
66  if (!is_bigendian()) {
67 // convert_endian(buf, sizeof(Field::element_type), m_nvol*m_nvector);
68  convert_endian(buf, sizeof(double), m_nvol * nin_file * nex_file);
69  }
70 
71  // unpack buffer
72  double *p = buf;
73 
74  for (int j = 0; j < nex_file; ++j) {
75  for (int isite = 0; isite < m_nvol; ++isite) {
76  for (int i = 0; i < nin_file; ++i) {
77  int s, t;
78  m_format->file_to_field(s, t, i, j);
79 
80  u->set(s, isite, t, *p++);
81  }
82  }
83  }
84 
85  delete [] buf;
86 
87  finalize();
88 }
89 
90 
91 //====================================================================
92 void FieldIO_Binary_Parallel::write_file(Field *u, string filename)
93 {
94  static const char _function_name[] = "FieldIO_Binary_Parallel::write_file";
95 
96  initialize();
97 
98  int nin_file = m_format->nin();
99  int nex_file = m_format->nex();
100 
101  // Field::element_type *buf = new Field::element_type [m_nvol*m_nvector];
102  double *buf = new double [nin_file * nex_file * m_nvol];
103  if (!buf) {
104  vout.crucial(m_vl, "%s: allocate buffer failed.\n", _function_name);
105  abort();
106  }
107 
108  // pack buffer
109  double *p = buf;
110 
111  for (int j = 0; j < nex_file; ++j) {
112  for (int isite = 0; isite < m_nvol; ++isite) {
113  for (int i = 0; i < nin_file; ++i) {
114  int s, t;
115  m_format->file_to_field(s, t, i, j);
116 
117  *p++ = u->cmp(s, isite, t);
118  }
119  }
120  }
121 
122  if (!is_bigendian()) {
123  // convert_endian(buf, sizeof(Field::element_type), m_nvol*m_nvector);
124  convert_endian(buf, sizeof(double), nin_file * nex_file * m_nvol);
125  }
126 
127  MPI_File fh;
128  int ret;
129 
130 #ifdef USE_BGNET
131  ret = MPI_File_open(MPI_COMM_WORLD, const_cast<char *>(filename.c_str()), MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
132 #else
133  ret = MPI_File_open(Communicator_impl::world(), const_cast<char *>(filename.c_str()), MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
134 #endif
135  if (ret) {
136  vout.crucial(m_vl, "%s: MPI_File_open failed.\n", _function_name);
137  abort();
138  }
139 
140  ret = MPI_File_set_view(fh, 0, m_type_vector, m_type_tiled, const_cast<char *>("native"), MPI_INFO_NULL);
141  if (ret) {
142  vout.crucial(m_vl, "%s: MPI_File_set_view failed.\n", _function_name);
143  abort();
144  }
145 
146  ret = MPI_File_write_all(fh, (void *)buf, m_nvol * nex_file, m_type_vector, MPI_STATUS_IGNORE);
147  if (ret) {
148  vout.crucial(m_vl, "%s: MPI_File_write_all failed.\n", _function_name);
149  abort();
150  }
151 
152  ret = MPI_File_close(&fh);
153  if (ret) {
154  vout.crucial(m_vl, "%s: MPI_File_close failed.\n", _function_name);
155  abort();
156  }
157 
158  delete [] buf;
159 
160  finalize();
161 }
162 
163 
164 //====================================================================
165 int FieldIO_Binary_Parallel::initialize()
166 {
167  static const char _function_name[] = "FieldIO_Binary_Parallel::initialize";
168 
169  if (m_is_initialized) return EXIT_SUCCESS;
170 
171  int nin_file = m_format->nin();
172  int nex_file = m_format->nex();
173 
174  const int ndim = CommonParameters::Ndim();
175 
176  int *global_dims = new int[ndim];
177  global_dims[0] = CommonParameters::Lx();
178  global_dims[1] = CommonParameters::Ly();
179  global_dims[2] = CommonParameters::Lz();
180  global_dims[3] = CommonParameters::Lt();
181 
182  int *local_dims = new int[ndim];
183  local_dims[0] = CommonParameters::Nx();
184  local_dims[1] = CommonParameters::Ny();
185  local_dims[2] = CommonParameters::Nz();
186  local_dims[3] = CommonParameters::Nt();
187 
188  m_nvol = 1;
189  for (int i = 0; i < ndim; ++i) {
190  m_nvol *= local_dims[i];
191  }
192 
193  int *grid_pos = new int[ndim];
194  for (int i = 0; i < ndim; ++i) {
195  grid_pos[i] = Communicator::ipe(i);
196  }
197 
198  int *starts = new int[ndim];
199  for (int i = 0; i < ndim; ++i) {
200  starts[i] = local_dims[i] * grid_pos[i];
201  }
202 
203  int ret = 0;
204 
205 // MPI_Datatype m_type_vector;
206 // ret = MPI_Type_contiguous(sizeof(Field::element_type)*nin_file, MPI_BYTE, &m_type_vector);
207  ret = MPI_Type_contiguous(sizeof(double) * nin_file, MPI_BYTE, &m_type_vector);
208  if (ret) {
209  vout.general(m_vl, "%s: MPI_Type_Contiguous failed.\n", _function_name);
210  return EXIT_FAILURE;
211  }
212 
213  ret = MPI_Type_commit(&m_type_vector);
214  if (ret) {
215  vout.general(m_vl, "%s: MPI_Type_commit failed.\n", _function_name);
216  return EXIT_FAILURE;
217  }
218 
219 // MPI_Datatype m_type_tiled;
220  ret = MPI_Type_create_subarray(ndim, global_dims, local_dims, starts, MPI_ORDER_FORTRAN, m_type_vector, &m_type_tiled);
221  if (ret) {
222  vout.general(m_vl, "%s: MPI_Type_create_subarray failed.\n", _function_name);
223  return EXIT_FAILURE;
224  }
225 
226  ret = MPI_Type_commit(&m_type_tiled);
227  if (ret) {
228  vout.general(m_vl, "%s: MPI_Type_commit failed.\n", _function_name);
229  return EXIT_FAILURE;
230  }
231 
232  m_is_initialized = true;
233 
234  delete [] starts;
235  delete [] grid_pos;
236  delete [] local_dims;
237  delete [] global_dims;
238 
239  vout.detailed(m_vl, "FieldIO_Binary_Parallel via MPI I/O initialize done.\n");
240 
241  return EXIT_SUCCESS;
242 }
243 
244 
245 //====================================================================
246 int FieldIO_Binary_Parallel::finalize()
247 {
248  static const char _function_name[] = "FieldIO_Binary_Parallel::finalize";
249 
250  if (!m_is_initialized) return EXIT_SUCCESS;
251 
252  int ret;
253 
254  ret = MPI_Type_free(&m_type_tiled);
255  if (ret) {
256  vout.general(m_vl, "%s: MPI_Type_free failed.\n", _function_name);
257  return EXIT_FAILURE;
258  }
259 
260  ret = MPI_Type_free(&m_type_vector);
261  if (ret) {
262  vout.general(m_vl, "%s: MPI_Type_free failed.\n", _function_name);
263  return EXIT_FAILURE;
264  }
265 
266  m_is_initialized = false;
267 
268  vout.detailed(m_vl, "%s via MPI I/O finalize done.\n", class_name.c_str());
269 
270  return EXIT_SUCCESS;
271 }
272 
273 
274 //====================================================================
275 #endif
BridgeIO vout
Definition: bridgeIO.cpp:207
void detailed(const char *format,...)
Definition: bridgeIO.cpp:50
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:128
void general(const char *format,...)
Definition: bridgeIO.cpp:38
virtual void file_to_field(int &s, int &t, const int i, const int j) const =0
virtual int nex() const =0
Container of Field-type object.
Definition: field.h:37
virtual int nin() const =0
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:108
static int ipe(const int dir)
logical coordinate of current proc.
static bool is_bigendian()
Definition: fieldIO.cpp:202
void write_file(Field *v, string filename)
const IO_Format::Format * m_format
Definition: fieldIO.h:57
void crucial(const char *format,...)
Definition: bridgeIO.cpp:26
void read_file(Field *v, string filename)
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.
Definition: fieldIO.cpp:224
Bridge::VerboseLevel m_vl
Definition: fieldIO.h:59