OpenLB 1.8.1
Loading...
Searching...
No Matches
columnVector.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2020 Adrian Kummerlaender
4 *
5 * E-mail contact: info@openlb.net
6 * The most recent release of OpenLB can be downloaded at
7 * <http://www.openlb.net/>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23*/
24
25#ifndef COLUMN_VECTOR_H
26#define COLUMN_VECTOR_H
27
28#include <type_traits>
29#include <memory>
30#include <array>
31#include <vector>
32
33#include "serializer.h"
34#include "meta.h"
36
37#include "genericVector.h"
38#include "scalarVector.h"
39
40namespace olb {
41
42
44
48 virtual ~ColumnVectorBase() { };
49
50 virtual void resize(std::size_t newCount) = 0;
51};
52
54
57template<typename COLUMN, unsigned D>
59protected:
61 std::size_t _count;
62 std::array<COLUMN,D> _column;
63
64public:
65 class const_ptr;
66 class ptr;
67
68 static constexpr unsigned d = D;
69
70 ColumnVector(std::size_t count):
71 _count(count),
72 _column(meta::make_array_f<COLUMN,D>([count](unsigned iDim) -> std::size_t {
73 return count;
74 }))
75 { }
76
78 _count(rhs._count),
79 _column(std::move(rhs._column)) { }
80
81 std::size_t getSize() const
82 {
83 return _count;
84 }
85
86 const COLUMN& operator[](unsigned iDim) const
87 {
88 return _column[iDim];
89 }
90
91 COLUMN& operator[](unsigned iDim)
92 {
93 return _column[iDim];
94 }
95
97 auto getRow(std::size_t i) const
98 {
99 if constexpr (D == 1) {
100 return operator[](0)[i];
101 } else {
102 return Vector<typename COLUMN::value_t,D>([this,i](unsigned iDim) {
103 return operator[](iDim)[i];
104 });
105 }
106 __builtin_unreachable();
107 }
108
109 auto setRow(std::size_t i, const Vector<typename COLUMN::value_t,D>& value)
110 {
111 for (unsigned iDim=0; iDim < D; ++iDim) {
112 operator[](iDim)[i] = value[iDim];
113 }
114 }
115
116 ptr getRowPointer(std::size_t i)
117 {
118 return ptr(*this, i);
119 }
120
121 const_ptr getRowPointer(std::size_t i) const
122 {
123 return const_ptr(*this, i);
124 }
125
127
130 void swap(std::size_t i, std::size_t j)
131 {
132 for (unsigned iDim=0; iDim < D; ++iDim) {
133 std::swap(_column[iDim][i], _column[iDim][j]);
134 }
135 }
136
137 void resize(std::size_t newCount) override {
138 for (unsigned iDim=0; iDim < D; ++iDim) {
139 operator[](iDim).resize(newCount);
140 }
141 _count = newCount;
142 }
143
145 std::size_t getNblock() const override;
147 std::size_t getSerializableSize() const override;
149 bool* getBlock(std::size_t iBlock, std::size_t& sizeBlock, bool loadingMode) override;
150 void postLoad() override;
151
152};
153
154template<typename COLUMN, unsigned D>
156{
157 return D * _column[0].getNblock();
158}
159
160template<typename COLUMN, unsigned D>
162{
163 return D * _column[0].getSerializableSize();
164}
165
166template<typename COLUMN, unsigned D>
167bool* ColumnVector<COLUMN,D>::getBlock(std::size_t iBlock, std::size_t& sizeBlock, bool loadingMode)
168{
169 std::size_t currentBlock = 0;
170 bool* dataPtr = nullptr;
171
172 for (unsigned iDim=0; iDim < D; ++iDim) {
173 this->registerSerializableOfConstSize(iBlock, sizeBlock, currentBlock, dataPtr, _column[iDim], loadingMode);
174 }
175
176 return dataPtr;
177}
178
179template<typename COLUMN, unsigned D>
181{
182 for (unsigned iD=0; iD < D; ++iD) {
183 this->operator[](iD).postLoad();
184 }
185}
186
187
189template<typename COLUMN, unsigned D>
190class ColumnVector<COLUMN,D>::const_ptr : public ScalarVector<const typename COLUMN::value_t,D,const_ptr> {
191private:
192 const ColumnVector<COLUMN,D>& _data;
193 std::size_t _index;
194
196
197protected:
198
199public:
200 const_ptr(const ColumnVector<COLUMN,D>& columns, std::size_t index):
201 _data(columns),
202 _index(index) { }
203
205 _data(rhs._data),
206 _index(rhs._index) { }
207
208 const typename COLUMN::value_t* getComponentPointer(unsigned iDim) const
209 {
210 return &_data[iDim][_index];
211 }
212
213 std::size_t getIndex() const
214 {
215 return _index;
216 }
217
218 void setIndex(std::size_t index)
219 {
220 _index = index;
221 }
222
223};
224
226template<typename COLUMN, unsigned D>
227class ColumnVector<COLUMN,D>::ptr : public ScalarVector<typename COLUMN::value_t,D,ptr> {
228private:
230 std::size_t _index;
231
233
234protected:
235
236public:
237 ptr(ColumnVector<COLUMN,D>& columns, std::size_t index):
238 _data(columns),
239 _index(index) { }
240
241 ptr(ptr&& rhs):
242 _data(rhs._data),
243 _index(rhs._index) { }
244
245 const typename COLUMN::value_t* getComponentPointer(unsigned iDim) const
246 {
247 return &_data[iDim][_index];
248 }
249
250 typename COLUMN::value_t* getComponentPointer(unsigned iDim)
251 {
252 return &_data[iDim][_index];
253 }
254
255 template <typename U, typename IMPL>
257 {
258 for (unsigned iDim=0; iDim < D; ++iDim) {
259 this->operator[](iDim) = rhs[iDim];
260 }
261 return *this;
262 }
263
264 std::size_t getIndex() const
265 {
266 return _index;
267 }
268
269 void setIndex(std::size_t index)
270 {
271 _index = index;
272 }
273
274 unsigned getSize() const
275 {
276 return D;
277 }
278
279};
280
281
282template <typename COLUMN, unsigned D>
283class ConcreteCommunicatable<ColumnVector<COLUMN,D>> final : public Communicatable {
284private:
285 ColumnVector<COLUMN,D>& _vector;
286
287public:
289 _vector{vector} { }
290
292 std::size_t size(ConstSpan<CellID> indices) const
293 {
294 std::size_t size = 0;
295 for (unsigned iD=0; iD < D; ++iD) {
296 size += ConcreteCommunicatable<COLUMN>(_vector[iD]).size(indices);
297 }
298 return size;
299 }
300
302 std::size_t serialize(ConstSpan<CellID> indices,
303 std::uint8_t* buffer) const
304 {
305 std::size_t size = ConcreteCommunicatable<COLUMN>(_vector[0]).size(indices);
306 std::uint8_t* curr = buffer;
307 #ifdef PARALLEL_MODE_OMP
308 #pragma omp parallel for schedule(static,1)
309 #endif
310 for (unsigned iD=0; iD < D; ++iD) {
311 ConcreteCommunicatable<COLUMN>(_vector[iD]).serialize(indices, curr + iD*size);
312 }
313 return D * size;
314 }
315
317 std::size_t deserialize(ConstSpan<CellID> indices,
318 const std::uint8_t* buffer)
319 {
320 std::size_t size = ConcreteCommunicatable<COLUMN>(_vector[0]).size(indices);
321 const std::uint8_t* curr = buffer;
322 #ifdef PARALLEL_MODE_OMP
323 #pragma omp parallel for schedule(static,1)
324 #endif
325 for (unsigned iD=0; iD < D; ++iD) {
326 ConcreteCommunicatable<COLUMN>(_vector[iD]).deserialize(indices, curr + iD*size);
327 }
328 return D * size;
329 }
330
331};
332
333
334}
335
336#endif
Read-only proxy for accessing a column vector entry.
const COLUMN::value_t * getComponentPointer(unsigned iDim) const
void setIndex(std::size_t index)
const_ptr(const ColumnVector< COLUMN, D > &columns, std::size_t index)
std::size_t getIndex() const
Proxy for accessing a column vector entry.
std::size_t getIndex() const
ptr(ColumnVector< COLUMN, D > &columns, std::size_t index)
COLUMN::value_t * getComponentPointer(unsigned iDim)
const COLUMN::value_t * getComponentPointer(unsigned iDim) const
void setIndex(std::size_t index)
unsigned getSize() const
ptr & operator=(const GenericVector< U, D, IMPL > &rhs)
Vector of columns.
COLUMN & operator[](unsigned iDim)
std::size_t getNblock() const override
Number of data blocks for the serializable interface.
void resize(std::size_t newCount) override
std::size_t getSize() const
void swap(std::size_t i, std::size_t j)
Swap contents of row i and row j.
bool * getBlock(std::size_t iBlock, std::size_t &sizeBlock, bool loadingMode) override
Return a pointer to the memory of the current block and its size for the serializable interface.
const_ptr getRowPointer(std::size_t i) const
ColumnVector(ColumnVector &&rhs)
static constexpr unsigned d
auto setRow(std::size_t i, const Vector< typename COLUMN::value_t, D > &value)
std::size_t _count
Number of rows.
auto getRow(std::size_t i) const
Return copy of data at index i.
std::array< COLUMN, D > _column
ptr getRowPointer(std::size_t i)
std::size_t getSerializableSize() const override
Binary size for the serializer.
void postLoad() override
const COLUMN & operator[](unsigned iDim) const
ColumnVector(std::size_t count)
std::size_t deserialize(ConstSpan< CellID > indices, const std::uint8_t *buffer)
Deserialize data at locations indices to buffer
std::size_t serialize(ConstSpan< CellID > indices, std::uint8_t *buffer) const
Serialize data at locations indices to buffer
ConcreteCommunicatable(ColumnVector< COLUMN, D > &vector)
std::size_t size(ConstSpan< CellID > indices) const
Get serialized size for data at locations indices
std::size_t deserialize(ConstSpan< CellID > indices, const std::uint8_t *buffer) override
Deserialize data at locations indices to buffer
std::size_t serialize(ConstSpan< CellID > indices, std::uint8_t *buffer) const override
Serialize data at locations indices to buffer
std::size_t size(ConstSpan< CellID > indices) const override
Get serialized size for data at locations indices
Base class for serializable objects of constant size. For dynamic size use BufferSerializable.
Definition serializer.h:145
Plain old scalar vector.
Top level namespace for all of OpenLB.
Vector(T &&t, Ts &&... ts) -> Vector< std::remove_cvref_t< T >, 1+sizeof...(Ts)>
Base of all ColumnVector specializations.
virtual void resize(std::size_t newCount)=0
virtual ~ColumnVectorBase()
Generic vector of values supporting basic arithmetic.
Vector of scalars.