Newer
Older
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
#pragma once
// [bin-named-stamp.h]: binNamedStamp data structure for file I/O
//
// -------------------------------------------------------------------------------------------- //
#include "grid-id.h"
#include <stdio.h>
#include <string>
namespace nse
{
template< typename T >
class binNamedStamp {
public:
binNamedStamp();
~binNamedStamp();
bool get(const int idx, T* out) const;
bool get(const std::string& name, T* out) const;
int get_size() const { return size; }
int get_record_size() const;
bool update(const int idx, T* out) const;
bool update(const std::string& name, T* out) const;
void push(const std::string& name, const T in);
int fwrite(FILE *ptr) const;
int fread(FILE* ptr);
void mpi_broadcast(const int host, const MPI_Comm comm);
void debug_print() const;
private:
int mem_size, size;
T *value;
std::string *name;
static const int c_alloc_step = 16;
void allocate(const int msize);
void resize(const int msize);
};
}
// -------------------------------------------------------------------------------------------- //
// Implementation
// -------------------------------------------------------------------------------------------- //
template< typename T >
nse::binNamedStamp< T >::binNamedStamp() : mem_size(0), size(0) {}
template< typename T >
nse::binNamedStamp< T >::~binNamedStamp()
{
if (mem_size > 0) delete[] value;
mem_size = 0;
size = 0;
}
// -------------------------------------------------------------------------------------------- //
template< typename T >
bool nse::binNamedStamp< T >::get(const int idx, T* out) const
{
if ((idx < 0) || (idx >= size)) return false;
(*out) = value[idx];
return true;
}
template< typename T >
bool nse::binNamedStamp< T >::get(const std::string& _name, T* out) const
{
for (int k = 0; k < size; k++) {
if (name[k] == _name) {
(*out) = value[k];
return true;
}
}
return false;
}
// -------------------------------------------------------------------------------------------- //
template< typename T >
bool nse::binNamedStamp< T >::update(const int idx, T* out) const
{
if ((idx < 0) || (idx >= size)) return false;
(*out) += value[idx];
return true;
}
template< typename T >
bool nse::binNamedStamp< T >::update(const std::string& _name, T* out) const
{
for (int k = 0; k < size; k++) {
if (name[k] == _name) {
(*out) += value[k];
return true;
}
}
return false;
}
// -------------------------------------------------------------------------------------------- //
template< typename T >
void nse::binNamedStamp< T >::push(const std::string& _name, const T in)
{
if (_name.empty()) return;
for (int k = 0; k < size; k++) {
if (name[k] == _name) {
value[k] = in;
return;
}
}
resize(size + 1);
value[size] = in;
name[size] = _name;
size++;
}
// -------------------------------------------------------------------------------------------- //
template< typename T >
void nse::binNamedStamp< T >::allocate(const int req_size)
{
if (req_size > mem_size)
{
int alloc_size = (req_size > mem_size + c_alloc_step) ?
req_size : mem_size + c_alloc_step;
if (mem_size > 0) {
delete[] value;
delete[] name;
}
value = new T[alloc_size];
name = new std::string[alloc_size];
mem_size = alloc_size;
}
}
// -------------------------------------------------------------------------------------------- //
template< typename T >
void nse::binNamedStamp< T >::resize(const int req_size)
{
if (req_size > mem_size)
{
int alloc_size = (req_size > mem_size + c_alloc_step) ?
req_size : mem_size + c_alloc_step;
T *cpval = new T[alloc_size];
std::string *cpname = new std::string[alloc_size];
if (size > 0)
memcpy(cpval, value, size * sizeof(T));
for (int k = 0; k < size; k++)
cpname[k] = name[k];
if (mem_size > 0) {
delete[] value;
delete[] name;
}
value = cpval;
name = cpname;
mem_size = alloc_size;
}
}
// -------------------------------------------------------------------------------------------- //
template< typename T >
int nse::binNamedStamp< T >::fwrite(FILE *ptr) const
{
int status = 0;
status += ::fwrite(&size, sizeof(int), 1, ptr);
if (size > 0) {
status += ::fwrite(value, sizeof(T), size, ptr);
}
for (int k = 0; k < size; k++) {
int c_length = strlen(name[k].c_str());
status += ::fwrite(&c_length, sizeof(int), 1, ptr);
status += ::fwrite(name[k].c_str(), sizeof(char), c_length, ptr);
}
return status;
}
// -------------------------------------------------------------------------------------------- //
template< typename T >
int nse::binNamedStamp< T >::fread(FILE* ptr)
{
int status = 0;
status += ::fread(&size, sizeof(int), 1, ptr);
allocate(size);
if (size > 0) {
status += ::fread(value, sizeof(T), size, ptr);
}
for (int k = 0; k < size; k++)
{
int c_length;
status += ::fread(&c_length, sizeof(int), 1, ptr);
char *c_buf = new char[c_length + 1];
status += ::fread(c_buf, sizeof(char), c_length, ptr);
c_buf[c_length] = '\0';
name[k] = std::string(c_buf);
delete[] c_buf;
}
return status;
}
// -------------------------------------------------------------------------------------------- //
template< typename T >
int nse::binNamedStamp< T >::get_record_size() const
{
int recsize = size + 1;
for (int k = 0; k < size; k++) {
int name_length = strlen(name[k].c_str());
recsize += name_length + 1;
}
return recsize;
}
// -------------------------------------------------------------------------------------------- //
template< typename T >
void nse::binNamedStamp< T >::mpi_broadcast(
const int host, const MPI_Comm comm)
{
int mpi_rank;
MPI_Comm_rank(comm, &mpi_rank);
MPI_Bcast(&size, 1, MPI_INT, host, comm);
if (mpi_rank != host) allocate(size);
if (size > 0) {
MPI_Bcast(value, size, mpi_type< T >(), host, comm);
}
for (int k = 0; k < size; k++) {
int c_length = 0;
if (mpi_rank == host) c_length = strlen(name[k].c_str());
MPI_Bcast(&c_length, 1, MPI_INT, host, comm);
char *c_buf = new char[c_length + 1];
strcpy(c_buf, name[k].c_str());
MPI_Bcast(c_buf, c_length + 1, MPI_CHAR, host, comm);
if (mpi_rank != host) {
name[k] = std::string(c_buf);
}
delete[] c_buf;
}
}
// -------------------------------------------------------------------------------------------- //
template< typename T >
void nse::binNamedStamp< T >::debug_print() const
{
for (int k = 0; k < size; k++) {
printf(" idx = %i, name = %s, value = %.5f\n", k, name[k].c_str(), value[k]);
}
}
// -------------------------------------------------------------------------------------------- //