Bridge++  Ver. 1.1.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 
33 //====================================================================
34 void FieldIO_Binary::read_file(Field *v, std::string filename)
35 {
36  int nin_field = v->nin();
37  int nex_field = v->nex();
38 
39  int nin_file = m_format->nin();
40  int nex_file = m_format->nex();
41 
42  if ((nin_file == 0) || (nex_file == 0)) {
43  nin_file = nin_field;
44  nex_file = nex_field;
45  }
46 
47  // temporary buffer: allocated only at I/O node.
48  Field vtmp;
49 
51  vout.detailed(m_vl, "reading field data from %s\n", filename.c_str());
52 
53  int Lvol = CommonParameters::Lvol();
54  vtmp.reset(nin_field, Lvol, nex_field);
55 
56  bool do_swap = (is_bigendian() == false);
57  if (do_swap) {
58  vout.detailed(m_vl, "host endian is little: byte swap performed.\n");
59  }
60 
61  const int block_size = nin_file;
62  char buf[sizeof(double) * block_size];
63 
64  std::ifstream infile(filename.c_str(), std::ios::in | std::ios::binary);
65  if (!infile) {
66  vout.crucial(m_vl, "read file failed\n");
67  return;
68  }
69 
70  for (int j = 0; j < nex_file; ++j) {
71  for (int isite = 0; isite < Lvol; ++isite) {
72  // read 1 block
73  infile.read(buf, sizeof(double) * block_size);
74 
75  if (do_swap) {
76  byte_swap(buf, sizeof(double), block_size);
77  }
78 
79  double *ptr = (double *)buf;
80 
81  for (int i = 0; i < nin_file; ++i) {
82  int s, t;
83  m_format->file_to_field(s, t, i, j);
84 
85  vtmp.set(s, isite, t, ptr[i]);
86  }
87  }
88  }
89 
90  infile.close();
91  }
92 
93  FieldIO::deliver(v, &vtmp);
94 
95  vout.detailed(m_vl, "read successful\n");
96 }
97 
98 
99 //====================================================================
100 void FieldIO_Binary::write_file(Field *v, std::string filename)
101 {
102  int nin_field = v->nin();
103  int nex_field = v->nex();
104 
105  int nin_file = m_format->nin();
106  int nex_file = m_format->nex();
107 
108  if ((nin_file == 0) || (nex_file == 0)) {
109  nin_file = nin_field;
110  nex_file = nex_field;
111  }
112 
113  int Lvol = CommonParameters::Lvol();
114 
115  Field vtmp;
116  if (Communicator::is_primary()) {
117  vtmp.reset(nin_field, Lvol, nex_field);
118  }
119 
120  FieldIO::gather(&vtmp, v);
121 
122  if (Communicator::is_primary()) {
123  vout.detailed(m_vl, "writing field data to %s\n", filename.c_str());
124 
125  bool do_swap = (is_bigendian() == false);
126  if (do_swap) {
127  vout.detailed(m_vl, "host endian is little: byte swap performed.\n");
128  }
129 
130  const int block_size = nin_file;
131  char buf[sizeof(double) * block_size];
132 
133  std::ofstream outfile(filename.c_str(), std::ios::out | std::ios::binary);
134  if (!outfile) {
135  vout.general(m_vl, "file = %s open failed\n", filename.c_str());
136  return;
137  }
138 
139  for (int j = 0; j < nex_file; ++j) {
140  for (int isite = 0; isite < Lvol; ++isite) {
141  double *ptr = (double *)buf;
142 
143  for (int i = 0; i < nin_file; ++i) {
144  int s, t;
145  m_format->file_to_field(s, t, i, j);
146 
147  ptr[i] = vtmp.cmp(s, isite, t);
148  }
149 
150  if (do_swap) {
151  byte_swap(buf, sizeof(double), block_size);
152  }
153 
154  outfile.write(buf, sizeof(double) * block_size);
155  }
156  }
157 
158  outfile.close();
159  }
160 
161  vout.detailed(m_vl, "write succeeded.\n");
162 }
163 
164 
165 //====================================================================
166 //============================================================END=====