forked from fmihpc/vlasiator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
velocity_block_container.h
387 lines (330 loc) · 14.4 KB
/
velocity_block_container.h
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* This file is part of Vlasiator.
* Copyright 2010-2016 Finnish Meteorological Institute
*
* For details of usage, see the COPYING file and read the "Rules of the Road"
* at http://www.physics.helsinki.fi/vlasiator/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef VELOCITY_BLOCK_CONTAINER_H
#define VELOCITY_BLOCK_CONTAINER_H
#include <vector>
#include "common.h"
#include "unistd.h"
#ifdef DEBUG_VBC
#include <sstream>
#endif
namespace vmesh {
static const double BLOCK_ALLOCATION_FACTOR = 1.1;
template<typename LID>
class VelocityBlockContainer {
public:
VelocityBlockContainer();
LID capacity() const;
size_t capacityInBytes() const;
void clear();
void copy(const LID& source,const LID& target);
static double getBlockAllocationFactor();
Realf* getData();
const Realf* getData() const;
Realf* getData(const LID& blockLID);
const Realf* getData(const LID& blockLID) const;
Realf* getNullData();
Real* getParameters();
const Real* getParameters() const;
Real* getParameters(const LID& blockLID);
const Real* getParameters(const LID& blockLID) const;
void pop();
LID push_back();
LID push_back(const uint32_t& N_blocks);
bool recapacitate(const LID& capacity);
bool setSize(const LID& newSize);
LID size() const;
size_t sizeInBytes() const;
void swap(VelocityBlockContainer& vbc);
#ifdef DEBUG_VBC
const Realf& getData(const LID& blockLID,const unsigned int& cell) const;
const Real& getParameters(const LID& blockLID,const unsigned int& i) const;
void setData(const LID& blockLID,const unsigned int& cell,const Realf& value);
#endif
private:
void exitInvalidLocalID(const LID& localID,const std::string& funcName) const;
void resize();
std::vector<Realf,aligned_allocator<Realf,WID3> > block_data;
Realf null_block_data[WID3];
LID currentCapacity;
LID numberOfBlocks;
std::vector<Real,aligned_allocator<Real,BlockParams::N_VELOCITY_BLOCK_PARAMS> > parameters;
};
template<typename LID> inline
VelocityBlockContainer<LID>::VelocityBlockContainer() {
currentCapacity = 0;
numberOfBlocks = 0;
}
template<typename LID> inline
LID VelocityBlockContainer<LID>::capacity() const {
return currentCapacity;
}
template<typename LID> inline
size_t VelocityBlockContainer<LID>::capacityInBytes() const {
return (block_data.capacity())*sizeof(Realf) + parameters.capacity()*sizeof(Real);
}
/** Clears VelocityBlockContainer data and deallocates all memory
* reserved for velocity blocks.*/
template<typename LID> inline
void VelocityBlockContainer<LID>::clear() {
std::vector<Realf,aligned_allocator<Realf,WID3> > dummy_data;
std::vector<Real,aligned_allocator<Real,BlockParams::N_VELOCITY_BLOCK_PARAMS> > dummy_parameters;
block_data.swap(dummy_data);
parameters.swap(dummy_parameters);
currentCapacity = 0;
numberOfBlocks = 0;
}
template<typename LID> inline
void VelocityBlockContainer<LID>::copy(const LID& source,const LID& target) {
#ifdef DEBUG_VBC
bool ok = true;
if (source >= numberOfBlocks) ok = false;
if (source >= currentCapacity) ok = false;
if (target >= numberOfBlocks) ok = false;
if (target >= currentCapacity) ok = false;
if (numberOfBlocks >= currentCapacity) ok = false;
if (source != numberOfBlocks-1) ok = false;
if (source*WID3+WID3-1 >= block_data.size()) ok = false;
if (source*BlockParams::N_VELOCITY_BLOCK_PARAMS+BlockParams::N_VELOCITY_BLOCK_PARAMS-1 >= parameters.size()) ok = false;
if (target*BlockParams::N_VELOCITY_BLOCK_PARAMS+BlockParams::N_VELOCITY_BLOCK_PARAMS-1 >= parameters.size()) ok = false;
if (parameters.size()/BlockParams::N_VELOCITY_BLOCK_PARAMS != block_data.size()/WID3) ok = false;
if (ok == false) {
std::stringstream ss;
ss << "VBC ERROR: invalid source LID=" << source << " in copy, target=" << target << " #blocks=" << numberOfBlocks << " capacity=" << currentCapacity << std::endl;
ss << "or sizes are wrong, data.size()=" << block_data.size() << " parameters.size()=" << parameters.size() << std::endl;
std::cerr << ss.str();
sleep(1);
exit(1);
}
#endif
for (unsigned int i=0; i<WID3; ++i) block_data[target*WID3+i] = block_data[source*WID3+i];
for (int i=0; i<BlockParams::N_VELOCITY_BLOCK_PARAMS; ++i) {
parameters[target*BlockParams::N_VELOCITY_BLOCK_PARAMS+i] = parameters[source*BlockParams::N_VELOCITY_BLOCK_PARAMS+i];
}
}
template<typename LID> inline
void VelocityBlockContainer<LID>::exitInvalidLocalID(const LID& localID,const std::string& funcName) const {
int rank;
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
std::stringstream ss;
ss << "Process " << rank << ' ';
ss << "Invalid localID " << localID << " used in function '" << funcName << "' max allowed value is " << numberOfBlocks << std::endl;
std::cerr << ss.str();
sleep(1);
exit(1);
}
template<typename LID> inline
double VelocityBlockContainer<LID>::getBlockAllocationFactor() {
return BLOCK_ALLOCATION_FACTOR;
}
template<typename LID> inline
Realf* VelocityBlockContainer<LID>::getData() {
return block_data.data();
}
template<typename LID> inline
const Realf* VelocityBlockContainer<LID>::getData() const {
return block_data.data();
}
template<typename LID> inline
Realf* VelocityBlockContainer<LID>::getData(const LID& blockLID) {
#ifdef DEBUG_VBC
if (blockLID >= numberOfBlocks) exitInvalidLocalID(blockLID,"getData");
if (blockLID >= block_data.size()/WID3) exitInvalidLocalID(blockLID,"const getData const");
#endif
return block_data.data() + blockLID*WID3;
}
template<typename LID> inline
const Realf* VelocityBlockContainer<LID>::getData(const LID& blockLID) const {
#ifdef DEBUG_VBC
if (blockLID >= numberOfBlocks) exitInvalidLocalID(blockLID,"const getData const");
if (blockLID >= block_data.size()/WID3) exitInvalidLocalID(blockLID,"const getData const");
#endif
return block_data.data() + blockLID*WID3;
}
template<typename LID> inline
Realf* VelocityBlockContainer<LID>::getNullData() {
return null_block_data;
}
template<typename LID> inline
Real* VelocityBlockContainer<LID>::getParameters() {
return parameters.data();
}
template<typename LID> inline
const Real* VelocityBlockContainer<LID>::getParameters() const {
return parameters.data();
}
template<typename LID> inline
Real* VelocityBlockContainer<LID>::getParameters(const LID& blockLID) {
#ifdef DEBUG_VBC
if (blockLID >= numberOfBlocks) exitInvalidLocalID(blockLID,"getParameters");
if (blockLID >= parameters.size()/BlockParams::N_VELOCITY_BLOCK_PARAMS) exitInvalidLocalID(blockLID,"getParameters");
#endif
return parameters.data() + blockLID*BlockParams::N_VELOCITY_BLOCK_PARAMS;
}
template<typename LID> inline
const Real* VelocityBlockContainer<LID>::getParameters(const LID& blockLID) const {
#ifdef DEBUG_VBC
if (blockLID >= numberOfBlocks) exitInvalidLocalID(blockLID,"const getParameters const");
if (blockLID >= parameters.size()/BlockParams::N_VELOCITY_BLOCK_PARAMS) exitInvalidLocalID(blockLID,"getParameters");
#endif
return parameters.data() + blockLID*BlockParams::N_VELOCITY_BLOCK_PARAMS;
}
template<typename LID> inline
void VelocityBlockContainer<LID>::pop() {
if (numberOfBlocks == 0) return;
--numberOfBlocks;
}
template<typename LID> inline
LID VelocityBlockContainer<LID>::push_back() {
LID newIndex = numberOfBlocks;
if (newIndex >= currentCapacity) resize();
#ifdef DEBUG_VBC
if (newIndex >= block_data.size()/WID3 || newIndex >= parameters.size()/BlockParams::N_VELOCITY_BLOCK_PARAMS) {
std::stringstream ss;
ss << "VBC ERROR in push_back, LID=" << newIndex << " for new block is out of bounds" << std::endl;
ss << "\t data.size()=" << block_data.size() << " parameters.size()=" << parameters.size() << std::endl;
std::cerr << ss.str();
sleep(1);
exit(1);
}
#endif
// Clear velocity block data to zero values
for (size_t i=0; i<WID3; ++i) block_data[newIndex*WID3+i] = 0.0;
for (size_t i=0; i<BlockParams::N_VELOCITY_BLOCK_PARAMS; ++i)
parameters[newIndex*BlockParams::N_VELOCITY_BLOCK_PARAMS+i] = 0.0;
++numberOfBlocks;
return newIndex;
}
template<typename LID> inline
LID VelocityBlockContainer<LID>::push_back(const uint32_t& N_blocks) {
const LID newIndex = numberOfBlocks;
numberOfBlocks += N_blocks;
resize();
// Clear velocity block data to zero values
for (size_t i=0; i<WID3*N_blocks; ++i) block_data[newIndex*WID3+i] = 0.0;
for (size_t i=0; i<BlockParams::N_VELOCITY_BLOCK_PARAMS*N_blocks; ++i)
parameters[newIndex*BlockParams::N_VELOCITY_BLOCK_PARAMS+i] = 0.0;
return newIndex;
}
template<typename LID> inline
bool VelocityBlockContainer<LID>::recapacitate(const LID& newCapacity) {
if (newCapacity < numberOfBlocks) return false;
{
std::vector<Realf,aligned_allocator<Realf,WID3> > dummy_data(newCapacity*WID3);
for (size_t i=0; i<numberOfBlocks*WID3; ++i) dummy_data[i] = block_data[i];
dummy_data.swap(block_data);
}
{
std::vector<Real,aligned_allocator<Real,BlockParams::N_VELOCITY_BLOCK_PARAMS> > dummy_parameters(newCapacity*BlockParams::N_VELOCITY_BLOCK_PARAMS);
for (size_t i=0; i<numberOfBlocks*BlockParams::N_VELOCITY_BLOCK_PARAMS; ++i) dummy_parameters[i] = parameters[i];
dummy_parameters.swap(parameters);
}
currentCapacity = newCapacity;
return true;
}
template<typename LID> inline
void VelocityBlockContainer<LID>::resize() {
if ((numberOfBlocks+1) >= currentCapacity) {
// Resize so that free space is block_allocation_chunk blocks,
// and at least two in case of having zero blocks.
// The order of velocity blocks is unaltered.
currentCapacity = 2 + numberOfBlocks * BLOCK_ALLOCATION_FACTOR;
block_data.resize(currentCapacity*WID3);
parameters.resize(currentCapacity*BlockParams::N_VELOCITY_BLOCK_PARAMS);
}
}
template<typename LID> inline
bool VelocityBlockContainer<LID>::setSize(const LID& newSize) {
numberOfBlocks = newSize;
if (newSize > currentCapacity) resize();
return true;
}
/** Return the number of existing velocity blocks.
* @return Number of existing velocity blocks.*/
template<typename LID> inline
LID VelocityBlockContainer<LID>::size() const {
return numberOfBlocks;
}
template<typename LID> inline
size_t VelocityBlockContainer<LID>::sizeInBytes() const {
return block_data.size()*sizeof(Realf) + parameters.size()*sizeof(Real);
}
template<typename LID> inline
void VelocityBlockContainer<LID>::swap(VelocityBlockContainer& vbc) {
block_data.swap(vbc.block_data);
parameters.swap(vbc.parameters);
LID dummy = currentCapacity;
currentCapacity = vbc.currentCapacity;
vbc.currentCapacity = dummy;
dummy = numberOfBlocks;
numberOfBlocks = vbc.numberOfBlocks;
vbc.numberOfBlocks = dummy;
}
#ifdef DEBUG_VBC
template<typename LID> inline
const Realf& VelocityBlockContainer<LID>::getData(const LID& blockLID,const unsigned int& cell) const {
bool ok = true;
if (cell >= WID3) ok = false;
if (blockLID >= numberOfBlocks) ok = false;
if (blockLID*WID3+cell >= block_data.size()) ok = false;
if (ok == false) {
std::stringstream ss;
ss << "VBC ERROR: out of bounds in getData, LID=" << blockLID << " cell=" << cell << " #blocks=" << numberOfBlocks << " data.size()=" << block_data.size() << std::endl;
std::cerr << ss.str();
sleep(1);
exit(1);
}
return block_data[blockLID*WID3+cell];
}
template<typename LID> inline
const Real& VelocityBlockContainer<LID>::getParameters(const LID& blockLID,const unsigned int& cell) const {
bool ok = true;
if (cell >= BlockParams::N_VELOCITY_BLOCK_PARAMS) ok = false;
if (blockLID >= numberOfBlocks) ok = false;
if (blockLID*BlockParams::N_VELOCITY_BLOCK_PARAMS+cell >= parameters.size()) ok = false;
if (ok == false) {
std::stringstream ss;
ss << "VBC ERROR: out of bounds in getParameters, LID=" << blockLID << " cell=" << cell << " #blocks=" << numberOfBlocks << " parameters.size()=" << parameters.size() << std::endl;
std::cerr << ss.str();
sleep(1);
exit(1);
}
return parameters[blockLID*BlockParams::N_VELOCITY_BLOCK_PARAMS+cell];
}
template<typename LID> inline
void VelocityBlockContainer<LID>::setData(const LID& blockLID,const unsigned int& cell,const Realf& value) {
bool ok = true;
if (cell >= WID3) ok = false;
if (blockLID >= numberOfBlocks) ok = false;
if (blockLID*WID3+cell >= block_data.size()) ok = false;
if (ok == false) {
std::stringstream ss;
ss << "VBC ERROR: out of bounds in setData, LID=" << blockLID << " cell=" << cell << " #blocks=" << numberOfBlocks << " data.size()=" << block_data.size() << std::endl;
std::cerr << ss.str();
sleep(1);
exit(1);
}
block_data[blockLID*WID3+cell] = value;
}
#endif
} // namespace block_cont
#endif