-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpargrid_datawrapper.h
262 lines (237 loc) · 10.5 KB
/
pargrid_datawrapper.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
/** This file is part of ParGrid parallel grid.
*
* Copyright 2011-2014 Finnish Meteorological Institute
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PARGRID_DATAWRAPPER_H
#define PARGRID_DATAWRAPPER_H
#include <cstdlib>
#include <iostream>
#include "pargrid_userdata_dynamic.h"
namespace pargrid {
/** Wrapper for class pargrid::UserDataDynamic. This class defines
* the interface through which end user can read and modify the
* contents of dynamic data arrays in ParGrid.*/
template<typename T>
class DataWrapper {
public:
DataWrapper(char** arrays,ArraySizetype* capacities,CellID N_cells,ArraySizetype* sizes,uint64_t elementByteSize);
const ArraySizetype* capacity() const;
ArraySizetype capacity(CellID cell) const;
T** data() const;
void push_back(CellID cell,const T& element);
void recapacitate(CellID cell,ArraySizetype newCapacity);
void reserve(CellID cell,ArraySizetype newCapacity);
void resize(CellID cell,ArraySizetype newSize);
const ArraySizetype* size() const;
ArraySizetype size(CellID cell) const;
bool valid() const;
private:
T** arrays; /**< Pointers to arrays containing user data for each cell.*/
ArraySizetype* capacities; /**< Current capacity of each cell, counted in array elements having byte size elementByteSize.*/
uint64_t elementByteSize; /**< Byte size of an array element.*/
CellID N_cells; /**< Number of cells in arrays, capacities, and sizes.*/
ArraySizetype* sizes; /**< Current size of each cell, counted in array elements having byte size elementByteSiz.*/
/** Private default constructor to prevent the creation
* of DataWrappers that point to an invalid ParGrid dynamic data array.*/
DataWrapper();
};
/** Create a new DataWrapper for given ParGrid dynamic data array.
* @param arrays Arrays containing the data for each cell.
* @param capacities Array containing current capacity for each cell, counted in array elements having byte size elementByteSize.
* @param N_cells Number of cells in the mesh local to this process.
* @param sizes Array containing current size for each cell, counted in array elements having byte size elementByteSize.
* @param elementByteSize Byte size of an array element.*/
template<typename T> inline
DataWrapper<T>::DataWrapper(char** arrays,ArraySizetype* capacities,CellID N_cells,ArraySizetype* sizes,uint64_t elementByteSize) {
this->arrays = reinterpret_cast<T**>(arrays);
this->capacities = capacities;
this->N_cells = N_cells;
this->sizes = sizes;
this->elementByteSize = elementByteSize;
}
/** Get the array containing current capacities of cells.
* @return Pointer to capacity array.*/
template<typename T> inline
const ArraySizetype* DataWrapper<T>::capacity() const {return capacities;}
/** Get the current capacity of the given cell.
* @param cell Local ID of the cell.
* @return Current capacity of the cell.*/
template<typename T> inline
ArraySizetype DataWrapper<T>::capacity(CellID cell) const {
#ifndef NDEBUG
if (cell >= N_cells) {
std::cerr << "(PARGRID DATAWRAPPER) ERROR: cell #" << cell << " out of bounds in capacity!" << std::endl;
exit(1);
}
#endif
return capacities[cell];
}
/** Get arrays containg user data. Number of elements in each cell
* can be obtained from the array returned by size() member function.
* @return Pointers to arrays containing dynamic user data.*/
template<typename T> inline
//T** DataWrapper<T>::data() const {return reinterpret_cast<T**>(arrays);}
T** DataWrapper<T>::data() const {return arrays;}
/** Insert a new element to the given cell. This function increases the
* capacity of the cell by a factor of two if the current capacity is too
* small to hold the new element.
* @param cell Local ID of the cell.
* @param element Element to be inserted.*/
template<typename T> inline
void DataWrapper<T>::push_back(CellID cell,const T& element) {
#ifndef NDEBUG
if (cell >= N_cells) {
std::cerr << "(PARGRID DATAWRAPPER) ERROR: Given cell ID #" << cell << " out of bounds in push_back!" << std::endl;
exit(1);
}
#endif
// If array does not have sufficient capacity for a new element,
// increase the capacity, create a new array, and copy contents
// from the old array before inserting the new element:
if (sizes[cell]+1 > capacities[cell]) {
capacities[cell] = 2*(sizes[cell]+1);
T* tmp = new T[capacities[cell]];
for (ArraySizetype i=0; i<sizes[cell]; ++i) tmp[i] = arrays[cell][i];
delete [] arrays[cell];
arrays[cell] = tmp;
}
#ifndef NDEBUG
if (arrays[cell] == NULL) {
std::cerr << "(PARGRID) ERROR: DataWrapper array[" << cell << "] is NULL!" << std::endl;
exit(1);
}
#endif
// Insert given element and increase array size:
arrays[cell][sizes[cell]] = element;
++sizes[cell];
}
/** Force the capacity of the given cell to be newCapacity. If newCapacity is
* less than the current capacity, a new array with smaller capacity is allocated
* and elements [0,newCapacity-1] are copied from the old array into the new one.
* The difference between this function and reserve is that this function will
* also reduce array capacity. Note that this function will not call constructors
* or destructors for inserted or removed data elements.
* @param cell Local ID of the cell.
* @param newCapacity New capacity for the cell.*/
template<typename T> inline
void DataWrapper<T>::recapacitate(CellID cell,ArraySizetype newCapacity) {
#ifndef NDEBUG
if (cell >= N_cells) {
std::cerr << "(PARGRID DATAWRAPPER) ERROR: cell #" << cell << " out of bounds in reserve!" << std::endl;
exit(1);
}
#endif
if (newCapacity < capacities[cell]) {
// If requested capacity is smaller than the current one,
// create a new array with reduces capacity and copy
// some of the contents from the old array:
capacities[cell] = newCapacity;
T* tmp = new T[newCapacity];
for (ArraySizetype i=0; i<newCapacity; ++i) tmp[i] = arrays[cell][i];
delete [] arrays[cell];
arrays[cell] = tmp;
sizes[cell] = newCapacity;
} else {
// If requested capacity is larger than the current one,
// just call reserve:
reserve(cell,newCapacity);
}
}
/** Make a request that given cell is able to hold at least
* newCapacity number of elements. This function will increase
* the current capacity of the given cell if the requested capacity
* is larger than the current one. Note that this function will not
* reduce current capacity.
* @param cell Local ID of the cell.
* @param newCapacity Requested new capacity.
* @see recapacitate.*/
template<typename T> inline
void DataWrapper<T>::reserve(CellID cell,ArraySizetype newCapacity) {
#ifndef NDEBUG
if (cell >= N_cells) {
std::cerr << "(PARGRID DATAWRAPPER) ERROR: cell #" << cell << " out of bounds in reserve!" << std::endl;
exit(1);
}
#endif
// If requested capacity is larger than the current capacity,
// create a new array with requested capacity and copy
// contents from the old one:
if (newCapacity > capacities[cell]) {
capacities[cell] = newCapacity;
T* tmp = new T[newCapacity];
for (ArraySizetype i=0; i<sizes[cell]; ++i) tmp[i] = arrays[cell][i];
delete [] arrays[cell];
arrays[cell] = tmp;
}
}
/** Resize, i.e. insert or remove elements to/from, the given cell. If
* newSize is larger than the current capacity, a new array with capacity equal
* to newSize is allocated. Note that this function will not call
* constructors or destructors for the inserted or removed elements.
* @param cell Local ID of the cell.
* @param newSize New size of the cell.*/
template<typename T> inline
void DataWrapper<T>::resize(CellID cell,ArraySizetype newSize) {
#ifndef NDEBUG
if (cell >= N_cells) {
std::cerr << "(PARGRID DATAWRAPPER) ERROR: Given cell ID #" << cell << " out of bounds in resize!" << std::endl;
exit(1);
}
#endif
if (newSize <= capacities[cell]) {
// Just reduce array size:
sizes[cell] = newSize;
} else {
// Old capacity is too small, increase it and
// copy contents from old array into a new one:
capacities[cell] = newSize;
char* ptr = reinterpret_cast<char*>(arrays[cell]);
char* tmp = new char[newSize*elementByteSize];
for (uint64_t i=0; i<sizes[cell]*elementByteSize; ++i) tmp[i] = ptr[i];
delete [] arrays[cell];
arrays[cell] = reinterpret_cast<T*>(tmp);
// Resize array:
sizes[cell] = newSize;
}
}
/** Get array containing the current size of each cell.
* @return Pointer to array containing the size of each cell.*/
template<typename T> inline
const ArraySizetype* DataWrapper<T>::size() const {return sizes;}
/** Get current size of given cell.
* @param cell Local ID of the cell.
* @return Current number of elements in the cell.*/
template<typename T> inline
ArraySizetype DataWrapper<T>::size(CellID cell) const {
#ifndef NDEBUG
if (cell >= N_cells) {
std::cerr << "(PARGRID DATAWRAPPER) ERROR: Cell #" << cell << " out of bounds in size!" << std::endl;
exit(1);
}
#endif
return sizes[cell];
}
/** If true, this DataWrapper points to a valid dynamic ParGrid data array.
* DataWrappers become invalid, e.g. after repartitioning.
* @return If true, this DataWrapper points to a valid dynamic
* data array and can be used safely.*/
template<typename T> inline
bool DataWrapper<T>::valid() const {
if (arrays == NULL) return false;
return true;
}
} // namespace pargrid
#endif