forked from rclam/DES_singleEMT_inProgress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryio.cxx
298 lines (248 loc) · 8.55 KB
/
binaryio.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#ifdef USE_NPROF
#include <nvToolsExt.h>
#endif
#include <cstdio>
#include <cstring>
#include <iostream>
#include "constants.hpp"
#include "parameters.hpp"
#include "binaryio.hpp"
#ifdef WIN32
#ifdef _MSC_VER
#define snprintf _snprintf
#endif // _MSC_VER
namespace std { using ::snprintf; }
#endif // WIN32
/*****************************************************************************
* The format of the binary file:
* 1 The first 'headerlen' bytes are ASCII text.
* 1.1 The 1st line in the header is the revision string. Starting with
* "# DynEarthSol ndims=%1 revision=%2", with %1 equal to 2 or 3
* (indicating 2D or 3D simulation) and %2 an integer.
* 1.2 The following lines are 'name', 'position' pairs, separated by a
* TAB character. This line tells the name of the data and the
* starting position (in bytes) of the data in this file.
* 2 The rests are binary data.
****************************************************************************/
namespace {
const std::size_t headerlen = 4096;
const char revision_str[] = "# DynEarthSol ndims="
#ifdef THREED
"3"
#else
"2"
#endif
" revision=3\n";
}
/* Not using C++ stream IO for bulk file io since it can be much slower than C stdio. */
BinaryOutput::BinaryOutput(const char *filename)
{
f = std::fopen(filename, "wb");
if (f == NULL) {
std::cerr << "Error: cannot open file: " << filename << '\n';
std::exit(2);
}
header = new char[headerlen]();
hd_pos = std::strcat(header, revision_str);
eof_pos = headerlen;
std::fseek(f, eof_pos, SEEK_SET);
}
BinaryOutput::~BinaryOutput()
{
close();
}
void BinaryOutput::close()
{
#ifdef USE_NPROF
nvtxRangePush(__FUNCTION__);
#endif
if (f) {
/* write header buffer to the beginning of file */
std::fseek(f, 0, SEEK_SET);
std::fwrite(header, sizeof(char), headerlen, f);
std::fclose(f);
f = NULL;
}
delete [] header;
header = NULL;
#ifdef USE_NPROF
nvtxRangePop();
#endif
}
void BinaryOutput::write_header(const char *name)
{
/* write to header buffer */
const std::size_t bsize = 256;
char buffer[bsize];
std::size_t len = std::snprintf(buffer, bsize, "%s\t%ld\n", name, eof_pos);
if (len >= bsize) {
std::cerr << "Error: exceeding buffer length at Output::write_array, name=" << name
<< " eof_position=" << eof_pos << '\n';
std::exit(12);
}
if (len >= headerlen - (hd_pos - header)*sizeof(char)) {
std::cerr << "Error: exceeding header length at Output::write_array, name=" << name
<< " eof_position=" << eof_pos << '\n';
std::exit(12);
}
hd_pos = std::strncat(hd_pos, buffer, len);
}
// XXX: when A is *var.bcflag, i.e. T is uint, g++ cannot instantiate the template
template <typename T>
void BinaryOutput::write_array(const std::vector<T>& A, const char *name, std::size_t size)
{
write_header(name);
std::size_t n = std::fwrite(A.data(), sizeof(T), size, f);
eof_pos += n * sizeof(T);
}
// specialize for uint
void BinaryOutput::write_array(const std::vector<uint>& A, const char *name, std::size_t size)
{
write_header(name);
std::size_t n = std::fwrite(A.data(), sizeof(uint), size, f);
eof_pos += n * sizeof(uint);
}
template <typename T, int N>
void BinaryOutput::write_array(const Array2D<T,N>& A, const char *name, std::size_t size)
{
write_header(name);
std::size_t n = std::fwrite(A.data(), sizeof(T), size*N, f);
eof_pos += n * sizeof(T);
}
// explicit instantiation
template
void BinaryOutput::write_array<int>(const std::vector<int>& A, const char *name, std::size_t);
template
void BinaryOutput::write_array<double>(const std::vector<double>& A, const char *name, std::size_t);
template
void BinaryOutput::write_array<double,NDIMS>(const Array2D<double,NDIMS>& A, const char *name, std::size_t);
template
void BinaryOutput::write_array<double,NSTR>(const Array2D<double,NSTR>& A, const char *name, std::size_t);
#ifdef THREED // when 2d, NSTR == NODES_PER_ELEM == 3
template
void BinaryOutput::write_array<double,NODES_PER_ELEM>(const Array2D<double,NODES_PER_ELEM>& A, const char *name, std::size_t);
#endif
template
void BinaryOutput::write_array<double,1>(const Array2D<double,1>& A, const char *name, std::size_t);
template
void BinaryOutput::write_array<int,NODES_PER_ELEM>(const Array2D<int,NODES_PER_ELEM>& A, const char *name, std::size_t);
template
void BinaryOutput::write_array<int,NDIMS>(const Array2D<int,NDIMS>& A, const char *name, std::size_t);
template
void BinaryOutput::write_array<int,1>(const Array2D<int,1>& A, const char *name, std::size_t);
//////////////////////////////////////////////////////////////////////////////
BinaryInput::BinaryInput(const char *filename)
{
f = std::fopen(filename, "r");
if (f == NULL) {
std::cerr << "Error: cannot open file: " << filename << '\n';
std::exit(2);
}
read_header();
}
BinaryInput::~BinaryInput()
{
std::fclose(f);
}
void BinaryInput::read_header()
{
/* Read into header buffer */
std::fseek(f, 0, SEEK_SET);
char *header = new char[headerlen]();
std::size_t n = std::fread(header, sizeof(char), headerlen, f);
if (n != headerlen) {
std::cerr << "Error: error reading file header\n";
std::exit(2);
}
/* Parse the content of header buffer */
char *line = header;
// Compare revision string (excluding the trailing new line)
line = std::strtok(header, "\n");
if (strncmp(line, revision_str, strlen(revision_str)-1) != 0) {
std::cerr << "Error: mismatching revision string in header\n"
<< " Expect: " << revision_str
<< " Got: "<< line << '\n';
std::exit(1);
}
line = std::strtok(NULL, "\n");
while (line != NULL) {
/* Each line is a string (might contain space), a tab, and an integer */
char *tab = std::strchr(line, '\t');
if (tab == NULL) {
std::cerr << "Error: error parsing file header\n"
<< " Line is:" << line << '\n';
std::exit(1);
}
std::string name(line, tab-line);
std::size_t loc;
std::sscanf(tab, "%zu", &loc);
offset[name] = loc;
line = std::strtok(NULL, "\n");
}
delete [] header;
}
void BinaryInput::seek_to_array(const char *name)
{
std::string name2(name);
auto it = offset.find(name);
if (it == offset.end()) {
std::cerr << "Error: no array with a name: " << name << '\n';
std::exit(1);
}
std::size_t loc = it->second;
//std::cout << name << ' ' << loc << '\n';
std::fseek(f, loc, SEEK_SET);
}
template <typename T>
void BinaryInput::read_array(std::vector<T>& A, const char *name)
{
/* The caller must ensure A is of right size to hold the array */
int size = A.size();
if (A.size() == 0) {
std::cerr << "Error: array size is 0: " << name << '\n';
std::exit(1);
}
seek_to_array(name);
int n = std::fread(A.data(), sizeof(T), size, f);
if (n != size) {
std::cerr << "Error: cannot read array: " << name << '\n';
std::exit(1);
}
}
template <typename T, int N>
void BinaryInput::read_array(Array2D<T,N>& A, const char *name)
{
/* The caller must ensure A is of right size to hold the array */
int size = A.size();
if (A.size() == 0) {
std::cerr << "Error: array size is 0: " << name << '\n';
std::exit(1);
}
seek_to_array(name);
int n = std::fread(A.data(), sizeof(T), size*N, f);
if (n != N*size) {
std::cerr << "Error: cannot read array: " << name << '\n';
std::exit(1);
}
}
// explicit instantiation
template
void BinaryInput::read_array<double>(std::vector<double>& A, const char *name);
template
void BinaryInput::read_array<int>(std::vector<int>& A, const char *name);
template
void BinaryInput::read_array<double,NDIMS>(Array2D<double,NDIMS>& A, const char *name);
template
void BinaryInput::read_array<double,NSTR>(Array2D<double,NSTR>& A, const char *name);
#ifdef THREED // when 2d, NSTR == NODES_PER_ELEM == 3
template
void BinaryInput::read_array<double,NODES_PER_ELEM>(Array2D<double,NODES_PER_ELEM>& A, const char *name);
#endif
template
void BinaryInput::read_array<double,1>(Array2D<double,1>& A, const char *name);
template
void BinaryInput::read_array<int,NDIMS>(Array2D<int,NDIMS>& A, const char *name);
template
void BinaryInput::read_array<int,NODES_PER_ELEM>(Array2D<int,NODES_PER_ELEM>& A, const char *name);
template
void BinaryInput::read_array<int,1>(Array2D<int,1>& A, const char *name);