Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fieldIO_Binary_Distributed.cpp
Go to the documentation of this file.
1 
15 #include "Tools/file_utils.h"
16 
17 const std::string FieldIO_Binary_Distributed::class_name = "FieldIO_Binary_Distributed";
18 
19 //====================================================================
20 void FieldIO_Binary_Distributed::read_file(Field *u, const std::string filename_base)
21 {
22  static const char _function_name[] = "FieldIO_Binary_Distributed::read_file";
23  const std::string filename
24  = FileUtils::generate_filename("%s.x%dy%dz%dt%d",
25  filename_base.c_str(),
30 
31  const int nin_field = u->nin();
32  const int nex_field = u->nex();
33 
34  int nin_file = m_format->nin();
35  int nex_file = m_format->nex();
36 
37  if ((nin_file == 0) || (nex_file == 0)) {
38  nin_file = nin_field;
39  nex_file = nex_field;
40  }
41 
42  const int nvol = CommonParameters::Nvol();
43 
44  std::ifstream infile(filename.c_str(), std::ios::in | std::ios::binary);
45  if (!infile) {
46  vout.crucial(m_vl, "%s: file open failed.\n", _function_name);
47  exit(EXIT_FAILURE);
48  }
49 
50  const bool do_swap = (is_bigendian() == false);
51  if (do_swap) {
52  vout.detailed(m_vl, "host endian is little: byte swap performed.\n");
53  }
54 
55  // read block size
56  const int block_size = nin_file;
57  char buf[sizeof(double) * block_size];
58 
59  for (int j = 0; j < nex_file; ++j) {
60  for (int isite = 0; isite < nvol; ++isite) {
61  // read 1 block
62  infile.read(buf, sizeof(double) * block_size);
63 
64  if (!infile) {
65  if (infile.eof()) { // short file
66  vout.crucial(m_vl, "%s: file size too small.\n", _function_name);
67  } else {
68  vout.crucial(m_vl, "%s: io error.\n", _function_name);
69  }
70 
71  exit(EXIT_FAILURE);
72  }
73 
74  if (do_swap) {
75  byte_swap(buf, sizeof(double), block_size);
76  }
77 
78  double *ptr = (double *)buf;
79 
80  for (int i = 0; i < nin_file; ++i) {
81  int s, t;
82  m_format->file_to_field(s, t, i, j);
83 
84  u->set(s, isite, t, ptr[i]);
85  }
86  }
87  }
88 
89  infile.close();
90  vout.detailed(m_vl, "read succeeded.\n");
91 }
92 
93 
94 //====================================================================
95 void FieldIO_Binary_Distributed::write_file(Field *u, const std::string filename_base)
96 {
97  static const char _function_name[] = "FieldIO_Binary_Distributed::write_file";
98  const std::string filename
99  = FileUtils::generate_filename("%s.x%dy%dz%dt%d",
100  filename_base.c_str(),
104  Communicator::ipe(3));
105 
106  const int nin_field = u->nin();
107  const int nex_field = u->nex();
108 
109  int nin_file = m_format->nin();
110  int nex_file = m_format->nex();
111 
112  if ((nin_file == 0) || (nex_file == 0)) {
113  nin_file = nin_field;
114  nex_file = nex_field;
115  }
116 
117  const int nvol = CommonParameters::Nvol();
118 
119  const bool do_swap = (is_bigendian() == false);
120  if (do_swap) {
121  vout.detailed(m_vl, "host endian is little: byte swap performed.\n");
122  }
123 
124  // write block size
125  const int block_size = nin_file;
126  char buf[sizeof(double) * block_size];
127 
128  std::ofstream outfile(filename.c_str(), std::ios::out | std::ios::binary);
129  if (!outfile) {
130  vout.crucial(m_vl, "%s: file open failed\n", _function_name);
131  exit(EXIT_FAILURE);
132  }
133 
134  for (int j = 0; j < nex_file; ++j) {
135  for (int isite = 0; isite < nvol; ++isite) {
136  double *ptr = (double *)buf;
137 
138  for (int i = 0; i < nin_file; ++i) {
139  int s, t;
140  m_format->file_to_field(s, t, i, j);
141 
142  ptr[i] = u->cmp(s, isite, t);
143  }
144 
145  if (do_swap) {
146  byte_swap(buf, sizeof(double), block_size);
147  }
148 
149  outfile.write(buf, sizeof(double) * block_size);
150 
151  if (!outfile) { // error
152  vout.crucial(m_vl, "%s: io error.\n", _function_name);
153  exit(EXIT_FAILURE);
154  }
155  }
156  }
157 
158  outfile.close();
159  vout.detailed(m_vl, "write succeeded.\n");
160 }
161 
162 
163 //====================================================================
164 //============================================================END=====
BridgeIO vout
Definition: bridgeIO.cpp:503
static void byte_swap(void *buf, size_t size, size_t nmemb)
< convert byte order. alternative interface.
Definition: fieldIO.h:91
void detailed(const char *format,...)
Definition: bridgeIO.cpp:216
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
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:45
virtual int nin() const =0
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:143
static int ipe(const int dir)
logical coordinate of current proc.
static bool is_bigendian()
Definition: fieldIO.cpp:206
void read_file(Field *v, const std::string filename)
read data from file.
int nin() const
Definition: field.h:126
std::string generate_filename(const char *fmt,...)
Definition: file_utils.cpp:17
int nex() const
Definition: field.h:128
const IO_Format::Format * m_format
Definition: fieldIO.h:62
void crucial(const char *format,...)
Definition: bridgeIO.cpp:178
void write_file(Field *v, const std::string filename)
write data to file.
static const std::string class_name
Bridge::VerboseLevel m_vl
Definition: fieldIO.h:64