forked from gentryx/libgeodecomp
-
Notifications
You must be signed in to change notification settings - Fork 10
/
displacedgrid.h
368 lines (315 loc) · 10.8 KB
/
displacedgrid.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
#ifndef LIBGEODECOMP_STORAGE_DISPLACEDGRID_H
#define LIBGEODECOMP_STORAGE_DISPLACEDGRID_H
#include <libgeodecomp/geometry/coordbox.h>
#include <libgeodecomp/geometry/region.h>
#include <libgeodecomp/storage/grid.h>
#include <libgeodecomp/config.h>
#ifdef LIBGEODECOMP_WITH_BOOST_SERIALIZATION
#include <libgeodecomp/misc/cudaboostworkaround.h>
#include <libgeodecomp/communication/boostserialization.h>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#endif
#ifdef LIBGEODECOMP_WITH_HPX
#include <libgeodecomp/misc/cudaboostworkaround.h>
#include <libgeodecomp/communication/hpxserializationwrapper.h>
#include <hpx/runtime/serialization/input_archive.hpp>
#include <hpx/runtime/serialization/output_archive.hpp>
#endif
namespace LibGeoDecomp {
#ifdef _MSC_BUILD
#pragma warning( push )
#pragma warning( disable : 4820 )
#endif
namespace DisplacedGridHelpers {
template<class TOPOLOGY, bool TOPOLOGICALLY_CORRECT>
class NormalizingIterator : public RegionStreakIterator<TOPOLOGY::DIM, Region<TOPOLOGY::DIM> >
{
public:
static const int DIM = TOPOLOGY::DIM;
inline NormalizingIterator(
const RegionStreakIterator<DIM, Region<DIM> >& iter,
const Coord<DIM>& origin,
const Coord<DIM>& topoDimensions) :
RegionStreakIterator<DIM, Region<DIM> >(iter),
topoDimensions(topoDimensions)
{
this->offset -= origin;
this->streak.origin -= origin;
this->streak.endX -= origin.x();
normalize();
}
inline NormalizingIterator(
const RegionStreakIterator<DIM, Region<DIM> >& iter,
const Coord<DIM>& topoDimensions) :
RegionStreakIterator<DIM, Region<DIM> >(iter),
topoDimensions(topoDimensions)
{
normalize();
}
inline void operator++()
{
RegionStreakIterator<DIM, Region<DIM> >::operator++();
normalize();
}
private:
// fixme: get rid of this, too?
Streak<DIM> normalizedStreak;
const Coord<DIM>& topoDimensions;
inline void normalize()
{
if (TOPOLOGICALLY_CORRECT) {
int length = this->streak.length();
this->streak.origin = TOPOLOGY::normalize(this->streak.origin, topoDimensions);
this->streak.endX = this->streak.origin.x() + length;
}
}
};
}
/**
* A grid whose origin has been shiftet by a certain offset. If
* TOPOLOGICALLY_CORRECT is set to true, the coordinates will be
* normalized according to the given topology and some superordinate
* dimension (see topologicalDimensions()) before using them for
* access. Useful for writing topology agnostic code that should work
* an a torus, too.
*/
template<typename CELL_TYPE,
typename TOPOLOGY=Topologies::Cube<2>::Topology,
bool TOPOLOGICALLY_CORRECT=false>
class DisplacedGrid : public GridBase<CELL_TYPE, TOPOLOGY::DIM>
{
public:
const static int DIM = TOPOLOGY::DIM;
typedef CELL_TYPE Cell;
typedef TOPOLOGY Topology;
typedef Grid<CELL_TYPE, TOPOLOGY> Delegate;
typedef CoordMap<CELL_TYPE, Delegate> CoordMapType;
using GridBase<CELL_TYPE, TOPOLOGY::DIM>::loadRegion;
using GridBase<CELL_TYPE, TOPOLOGY::DIM>::saveRegion;
using GridBase<CELL_TYPE, TOPOLOGY::DIM>::topoDimensions;
explicit DisplacedGrid(
const CoordBox<DIM>& box = CoordBox<DIM>(),
const CELL_TYPE& defaultCell = CELL_TYPE(),
const CELL_TYPE& edgeCell = CELL_TYPE(),
const Coord<DIM>& topologicalDimensions = Coord<DIM>()) :
GridBase<CELL_TYPE, TOPOLOGY::DIM>(topologicalDimensions),
delegate(box.dimensions, defaultCell, edgeCell),
origin(box.origin)
{}
explicit DisplacedGrid(
const Region<DIM>& region,
const CELL_TYPE& defaultCell = CELL_TYPE(),
const CELL_TYPE& edgeCell = CELL_TYPE(),
const Coord<DIM>& topologicalDimensions = Coord<DIM>()) :
GridBase<CELL_TYPE, TOPOLOGY::DIM>(topologicalDimensions),
delegate(region.boundingBox().dimensions, defaultCell, edgeCell),
origin(region.boundingBox().origin)
{}
explicit DisplacedGrid(
const Delegate& grid,
const Coord<DIM>& origin=Coord<DIM>()) :
delegate(grid),
origin(origin)
{}
/**
* Return a pointer to the underlying data storage. Use with care!
*/
inline
CELL_TYPE *data()
{
return delegate.data();
}
/**
* Return a const pointer to the underlying data storage. Use with
* care!
*/
inline
const CELL_TYPE *data() const
{
return delegate.data();
}
inline const Coord<DIM>& getOrigin() const
{
return origin;
}
inline const CELL_TYPE& getEdgeCell() const
{
return delegate.getEdgeCell();
}
inline CELL_TYPE& getEdgeCell()
{
return delegate.getEdgeCell();
}
inline void setOrigin(const Coord<DIM>& newOrigin)
{
origin = newOrigin;
}
inline void resize(const CoordBox<DIM>& box)
{
delegate.resize(box.dimensions);
origin = box.origin;
}
inline CELL_TYPE& operator[](const Coord<DIM>& absoluteCoord)
{
Coord<DIM> relativeCoord = absoluteCoord - origin;
if (TOPOLOGICALLY_CORRECT) {
relativeCoord = Topology::normalize(relativeCoord, topoDimensions);
}
return delegate[relativeCoord];
}
inline const CELL_TYPE& operator[](const Coord<DIM>& absoluteCoord) const
{
return (const_cast<DisplacedGrid&>(*this))[absoluteCoord];
}
virtual void set(const Coord<DIM>& coord, const CELL_TYPE& cell)
{
(*this)[coord] = cell;
}
virtual void set(const Streak<DIM>& streak, const CELL_TYPE *cells)
{
delegate.set(Streak<DIM>(streak.origin - origin,
streak.endX - origin.x()),
cells);
}
virtual CELL_TYPE get(const Coord<DIM>& coord) const
{
return (*this)[coord];
}
virtual void get(const Streak<DIM>& streak, CELL_TYPE *cells) const
{
delegate.get(Streak<DIM>(streak.origin - origin,
streak.endX - origin.x()),
cells);
}
virtual void setEdge(const CELL_TYPE& cell)
{
getEdgeCell() = cell;
}
virtual const CELL_TYPE& getEdge() const
{
return getEdgeCell();
}
inline const Coord<DIM>& getDimensions() const
{
return delegate.getDimensions();
}
virtual CoordBox<DIM> boundingBox() const
{
return CoordBox<DIM>(origin, delegate.getDimensions());
}
void saveRegion(
std::vector<CELL_TYPE> *buffer,
const Region<DIM>& region,
const Coord<DIM>& offset = Coord<DIM>()) const
{
typedef DisplacedGridHelpers::NormalizingIterator<TOPOLOGY, TOPOLOGICALLY_CORRECT> NormalizingIterator;
NormalizingIterator iter(region.beginStreak(offset - origin), topoDimensions);
delegate.saveRegionImplementation(buffer, iter, region.endStreak(offset - origin));
}
#ifdef LIBGEODECOMP_WITH_BOOST_SERIALIZATION
void saveRegion(
std::vector<char> *buffer,
const Region<DIM>& region,
const Coord<DIM>& offset = Coord<DIM>()) const
{
typedef typename APITraits::SelectBoostSerialization<CELL_TYPE>::Value Trait;
delegate.saveRegionImplementation(buffer, region.beginStreak(offset - origin), region.endStreak(offset - origin), Trait());
}
#endif
void loadRegion(
const std::vector<CELL_TYPE>& buffer,
const Region<DIM>& region,
const Coord<DIM>& offset = Coord<DIM>())
{
typedef DisplacedGridHelpers::NormalizingIterator<TOPOLOGY, TOPOLOGICALLY_CORRECT> NormalizingIterator;
NormalizingIterator iter(region.beginStreak(offset - origin), topoDimensions);
delegate.loadRegionImplementation(buffer, iter, region.endStreak(offset - origin));
}
#ifdef LIBGEODECOMP_WITH_BOOST_SERIALIZATION
void loadRegion(
const std::vector<char>& buffer,
const Region<DIM>& region,
const Coord<DIM>& offset = Coord<DIM>())
{
typedef typename APITraits::SelectBoostSerialization<CELL_TYPE>::Value Trait;
delegate.loadRegionImplementation(buffer, region.beginStreak(offset - origin), region.endStreak(offset - origin), Trait());
}
#endif
inline CoordMapType getNeighborhood(const Coord<DIM>& center) const
{
Coord<DIM> relativeCoord = center - origin;
if (TOPOLOGICALLY_CORRECT) {
relativeCoord = Topology::normalize(relativeCoord, topoDimensions);
}
return CoordMapType(relativeCoord, &delegate);
}
inline const Delegate *vanillaGrid() const
{
return &delegate;
}
inline Delegate *vanillaGrid()
{
return &delegate;
}
inline std::string toString() const
{
std::ostringstream message;
message << "DisplacedGrid<" << DIM << ">(\n"
<< " origin: " << origin << "\n"
<< " delegate:\n"
<< delegate
<< ")";
return message.str();
}
protected:
void saveMemberImplementation(
char *target,
MemoryLocation::Location targetLocation,
const Selector<CELL_TYPE>& selector,
const typename Region<DIM>::StreakIterator& begin,
const typename Region<DIM>::StreakIterator& end) const
{
typedef DisplacedGridHelpers::NormalizingIterator<TOPOLOGY, TOPOLOGICALLY_CORRECT> NormalizingIterator;
delegate.saveMemberImplementationTemplated(
target,
targetLocation,
selector,
NormalizingIterator(begin, origin, topoDimensions),
end);
}
void loadMemberImplementation(
const char *source,
MemoryLocation::Location sourceLocation,
const Selector<CELL_TYPE>& selector,
const typename Region<DIM>::StreakIterator& begin,
const typename Region<DIM>::StreakIterator& end)
{
typedef DisplacedGridHelpers::NormalizingIterator<TOPOLOGY, TOPOLOGICALLY_CORRECT> NormalizingIterator;
delegate.loadMemberImplementationTemplated(
source,
sourceLocation,
selector,
NormalizingIterator(begin, origin, topoDimensions),
end);
}
private:
Delegate delegate;
Coord<DIM> origin;
};
#ifdef _MSC_BUILD
#pragma warning( pop )
#endif
}
template<typename _CharT, typename _Traits, typename _CellT, typename _Topology, bool _Correctness>
std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const LibGeoDecomp::DisplacedGrid<_CellT, _Topology, _Correctness>& grid)
{
__os << grid.toString();
return __os;
}
#endif