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