OpenLB 1.7
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
52
55template<typename COLUMN, unsigned D>
57protected:
59 std::size_t _count;
60 std::array<COLUMN,D> _column;
61
62public:
63 class const_ptr;
64 class ptr;
65
66 static constexpr unsigned d = D;
67
68 ColumnVector(std::size_t count):
69 _count(count),
70 _column(meta::make_array_f<COLUMN,D>([count](unsigned iDim) -> std::size_t {
71 return count;
72 }))
73 { }
74
76 _count(rhs._count),
77 _column(std::move(rhs._column)) { }
78
79 std::size_t getSize() const
80 {
81 return _count;
82 }
83
84 const COLUMN& operator[](unsigned iDim) const
85 {
86 return _column[iDim];
87 }
88
89 COLUMN& operator[](unsigned iDim)
90 {
91 return _column[iDim];
92 }
93
95 auto getRow(std::size_t i) const
96 {
97 if constexpr (D == 1) {
98 return operator[](0)[i];
99 } else {
100 return Vector<typename COLUMN::value_t,D>([this,i](unsigned iDim) {
101 return operator[](iDim)[i];
102 });
103 }
104 __builtin_unreachable();
105 }
106
107 auto setRow(std::size_t i, const Vector<typename COLUMN::value_t,D>& value)
108 {
109 for (unsigned iDim=0; iDim < D; ++iDim) {
110 operator[](iDim)[i] = value[iDim];
111 }
112 }
113
114 ptr getRowPointer(std::size_t i)
115 {
116 return ptr(*this, i);
117 }
118
119 const_ptr getRowPointer(std::size_t i) const
120 {
121 return const_ptr(*this, i);
122 }
123
125 void resize(std::size_t newCount)
126 {
127 for (unsigned iDim=0; iDim < D; ++iDim) {
128 _column[iDim].resize(newCount);
129 }
130 _count = newCount;
131 }
132
134
137 void swap(std::size_t i, std::size_t j)
138 {
139 for (unsigned iDim=0; iDim < D; ++iDim) {
140 std::swap(_column[iDim][i], _column[iDim][j]);
141 }
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 const typename COLUMN::value_t* getComponentPointer(unsigned iDim) const
199 {
200 return &_data[iDim][_index];
201 }
202
203public:
204 const_ptr(const ColumnVector<COLUMN,D>& columns, std::size_t index):
205 _data(columns),
206 _index(index) { }
207
209 _data(rhs._data),
210 _index(rhs._index) { }
211
212 std::size_t getIndex() const
213 {
214 return _index;
215 }
216
217 void setIndex(std::size_t index)
218 {
219 _index = index;
220 }
221
222};
223
225template<typename COLUMN, unsigned D>
226class ColumnVector<COLUMN,D>::ptr : public ScalarVector<typename COLUMN::value_t,D,ptr> {
227private:
229 std::size_t _index;
230
232
233protected:
234 const typename COLUMN::value_t* getComponentPointer(unsigned iDim) const
235 {
236 return &_data[iDim][_index];
237 }
238 typename COLUMN::value_t* getComponentPointer(unsigned iDim)
239 {
240 return &_data[iDim][_index];
241 }
242
243public:
244 ptr(ColumnVector<COLUMN,D>& columns, std::size_t index):
245 _data(columns),
246 _index(index) { }
247
248 ptr(ptr&& rhs):
249 _data(rhs._data),
250 _index(rhs._index) { }
251
252 template <typename U, typename IMPL>
254 {
255 for (unsigned iDim=0; iDim < D; ++iDim) {
256 this->operator[](iDim) = rhs[iDim];
257 }
258 return *this;
259 }
260
261 std::size_t getIndex() const
262 {
263 return _index;
264 }
265
266 void setIndex(std::size_t index)
267 {
268 _index = index;
269 }
270
271 unsigned getSize() const
272 {
273 return D;
274 }
275
276};
277
278
279template <typename COLUMN, unsigned D>
280class ConcreteCommunicatable<ColumnVector<COLUMN,D>> final : public Communicatable {
281private:
282 ColumnVector<COLUMN,D>& _vector;
283
284public:
286 _vector{vector} { }
287
289 std::size_t size(ConstSpan<CellID> indices) const
290 {
291 std::size_t size = 0;
292 for (unsigned iD=0; iD < D; ++iD) {
293 size += ConcreteCommunicatable<COLUMN>(_vector[iD]).size(indices);
294 }
295 return size;
296 }
297
299 std::size_t serialize(ConstSpan<CellID> indices,
300 std::uint8_t* buffer) const
301 {
302 std::size_t size = ConcreteCommunicatable<COLUMN>(_vector[0]).size(indices);
303 std::uint8_t* curr = buffer;
304 #ifdef PARALLEL_MODE_OMP
305 #pragma omp parallel for schedule(static,1)
306 #endif
307 for (unsigned iD=0; iD < D; ++iD) {
308 ConcreteCommunicatable<COLUMN>(_vector[iD]).serialize(indices, curr + iD*size);
309 }
310 return D * size;
311 }
312
314 std::size_t deserialize(ConstSpan<CellID> indices,
315 const std::uint8_t* buffer)
316 {
317 std::size_t size = ConcreteCommunicatable<COLUMN>(_vector[0]).size(indices);
318 const std::uint8_t* curr = buffer;
319 #ifdef PARALLEL_MODE_OMP
320 #pragma omp parallel for schedule(static,1)
321 #endif
322 for (unsigned iD=0; iD < D; ++iD) {
323 ConcreteCommunicatable<COLUMN>(_vector[iD]).deserialize(indices, curr + iD*size);
324 }
325 return D * size;
326 }
327
328};
329
330
331}
332
333#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.
std::size_t getSize() const
void resize(std::size_t newCount)
Resize columns, potentially invalidates any inbound pointers.
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.
Definition vector.h:47
Top level namespace for all of OpenLB.
Base of all ColumnVector specializations.
virtual ~ColumnVectorBase()
Generic vector of values supporting basic arithmetic.
Vector of scalars.