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