Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fieldIO_Binary.cpp
Go to the documentation of this file.
1 
14 #include "fieldIO_Binary.h"
15 
16 #include <fstream>
17 #include <strings.h>
18 
19 #include "bridgeIO.h"
20 using Bridge::vout;
21 
22 //typedef unsigned short int n_uint16_t;
23 //typedef unsigned int n_uint32_t;
24 //typedef unsigned long int n_uint64_t;
25 
26 const std::string FieldIO_Binary::class_name = "FieldIO_Binary";
27 
28 //====================================================================
29 void FieldIO_Binary::read_file(Field *v, const std::string filename)
30 {
31  const int nin_field = v->nin();
32  const int nex_field = v->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  // temporary buffer: allocated only at I/O node.
43  Field vtmp;
44 
46  vout.detailed(m_vl, "reading field data from %s\n", filename.c_str());
47 
48  const long_t Lvol = CommonParameters::Lvol();
49  vtmp.reset(nin_field, Lvol, nex_field);
50 
51  const bool do_swap = (is_bigendian() == false);
52  if (do_swap) {
53  vout.detailed(m_vl, "host endian is little: byte swap performed.\n");
54  }
55 
56  const int block_size = nin_file;
57  char buf[sizeof(double) * block_size];
58 
59  std::ifstream infile(filename.c_str(), std::ios::in | std::ios::binary);
60  if (!infile) {
61  vout.crucial(m_vl, "Error at %s: file open failed, %s may not exist.\n", class_name.c_str(), filename.c_str());
62  exit(EXIT_FAILURE);
63  }
64 
65  for (int j = 0; j < nex_file; ++j) {
66  for (long_t isite = 0; isite < Lvol; ++isite) {
67  // read 1 block
68  infile.read(buf, sizeof(double) * block_size);
69 
70  if (!infile) {
71  if (infile.eof()) { // short file
72  vout.crucial(m_vl, "Error at %s: file size of %s is too small.\n", class_name.c_str(), __func__);
73  } else {
74  vout.crucial(m_vl, "Error at %s: io error of %s.\n", class_name.c_str(), __func__);
75  }
76 
77  exit(EXIT_FAILURE);
78  }
79 
80  if (do_swap) {
81  byte_swap(buf, sizeof(double), block_size);
82  }
83 
84  double *ptr = (double *)buf;
85 
86  for (int i = 0; i < nin_file; ++i) {
87  int s, t;
88  m_format->file_to_field(s, t, i, j);
89 
90  vtmp.set(s, isite, t, ptr[i]);
91  }
92  }
93  }
94 
95  infile.close();
96  }
97 
98  FieldIO::deliver(v, &vtmp);
99 
100  vout.detailed(m_vl, "read successful\n");
101 }
102 
103 
104 //====================================================================
105 void FieldIO_Binary::write_file(Field *v, const std::string filename)
106 {
107  const int nin_field = v->nin();
108  const int nex_field = v->nex();
109 
110  int nin_file = m_format->nin();
111  int nex_file = m_format->nex();
112 
113  if ((nin_file == 0) || (nex_file == 0)) {
114  nin_file = nin_field;
115  nex_file = nex_field;
116  }
117 
118  const long_t Lvol = CommonParameters::Lvol();
119 
120  Field vtmp;
121  if (Communicator::is_primary()) {
122  vtmp.reset(nin_field, Lvol, nex_field);
123  }
124 
125  FieldIO::gather(&vtmp, v);
126 
127  if (Communicator::is_primary()) {
128  vout.detailed(m_vl, "writing field data to %s\n", filename.c_str());
129 
130  const bool do_swap = (is_bigendian() == false);
131  if (do_swap) {
132  vout.detailed(m_vl, "host endian is little: byte swap performed.\n");
133  }
134 
135  const int block_size = nin_file;
136  char buf[sizeof(double) * block_size];
137 
138  std::ofstream outfile(filename.c_str(), std::ios::out | std::ios::binary);
139  if (!outfile) {
140  vout.crucial(m_vl, "Error at %s: file open of %s failed\n", class_name.c_str(), filename.c_str());
141  exit(EXIT_FAILURE);
142  }
143 
144  for (int j = 0; j < nex_file; ++j) {
145  for (long_t isite = 0; isite < Lvol; ++isite) {
146  double *ptr = (double *)buf;
147 
148  for (int i = 0; i < nin_file; ++i) {
149  int s, t;
150  m_format->file_to_field(s, t, i, j);
151 
152  ptr[i] = vtmp.cmp(s, isite, t);
153  }
154 
155  if (do_swap) {
156  byte_swap(buf, sizeof(double), block_size);
157  }
158 
159  outfile.write(buf, sizeof(double) * block_size);
160 
161  if (!outfile) { // error
162  vout.crucial(m_vl, "Error at %s: io error of %s.\n", class_name.c_str(), __func__);
163  exit(EXIT_FAILURE);
164  }
165  }
166  }
167 
168  outfile.close();
169  }
170 
171  vout.detailed(m_vl, "write succeeded.\n");
172 }
173 
174 
175 //====================================================================
176 //============================================================END=====
void read_file(Field *v, const std::string filename)
read data from file.
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
void deliver(Field *vlocal, Field *vglobal)
distribute data on primary node over parallel nodes.
Definition: fieldIO.cpp:30
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 bool is_bigendian()
Definition: fieldIO.cpp:206
void write_file(Field *v, const std::string filename)
write data to file.
int nin() const
Definition: field.h:126
int nex() const
Definition: field.h:128
const IO_Format::Format * m_format
Definition: fieldIO.h:62
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=Element_type::COMPLEX)
Definition: field.h:95
long long_t
definition of long for Bridge++
Definition: bridge_long.h:46
void crucial(const char *format,...)
Definition: bridgeIO.cpp:178
static const std::string class_name
static bool is_primary()
check if the present node is primary in small communicator.
void gather(Field *vglobal, Field *vlocal)
gather data on parallel nodes to primary node.
Definition: fieldIO.cpp:119
static long_t Lvol()
Bridge::VerboseLevel m_vl
Definition: fieldIO.h:64