OpenLB 1.7
Loading...
Searching...
No Matches
column.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 CPU_SISD_COLUMN_H
26#define CPU_SISD_COLUMN_H
27
28#include <memory>
29#include <array>
30#include <stdexcept>
31
33#include "core/serializer.h"
35
36namespace olb {
37
38namespace cpu {
39
40namespace sisd {
41
43template<typename T>
44class Column final : public AbstractColumn<T>
45 , public Serializable {
46private:
47 std::size_t _count;
48 std::unique_ptr<T[]> _data;
49
50public:
51 using value_t = T;
52
53 Column(std::size_t count):
54 _count(count),
55 _data(new T[count] {})
56 { }
57
59 Column(0)
60 { }
61
63 _count(rhs._count),
64 _data(rhs._data.release())
65 { }
66
67 Column(const Column<T>& rhs):
68 _count(rhs._count),
69 _data(new T[_count] {})
70 {
71 std::copy(rhs._data.get(),
72 rhs._data.get() + _count,
73 _data.get());
74 }
75
76 virtual ~Column() = default;
77
78 void resize(std::size_t count)
79 {
80 std::unique_ptr<T[]> data = std::unique_ptr<T[]>(new T[count] { });
81 std::copy(_data.get(), _data.get() + std::min(_count, count), data.get());
82 _data.swap(data);
83 _count = count;
84 }
85
86 const T& operator[](std::size_t i) const override
87 {
88 return _data[i];
89 }
90
91 T& operator[](std::size_t i) override
92 {
93 return _data[i];
94 }
95
96 std::size_t size() const
97 {
98 return _count;
99 }
100
101 const T* data() const
102 {
103 return _data.get();
104 }
105
106 T* data()
107 {
108 return _data.get();
109 }
110
112
114 std::size_t getNblock() const override;
116 std::size_t getSerializableSize() const override;
118 bool* getBlock(std::size_t iBlock, std::size_t& sizeBlock, bool loadingMode) override;
119
120};
121
122
124
128template<typename T>
129class CyclicColumn final : public AbstractCyclicColumn<T>
130 , public Serializable {
131private:
132 const std::size_t _count;
133 std::unique_ptr<T[]> _data;
134
135 std::ptrdiff_t _shift;
136 std::size_t _remainder;
137 std::array<T*,2> _start;
138
139public:
140 using value_t = T;
141
142 CyclicColumn(std::size_t count):
143 _count(count),
144 _data(new T[count] {}),
145 _shift(0),
146 _remainder(count)
147 {
148 refresh();
149 }
150
152 _count(rhs._count),
153 _data(rhs._data.release()),
154 _shift(rhs._shift),
155 _remainder(rhs._remainder)
156 {
157 refresh();
158 }
159
160 ~CyclicColumn() = default;
161
162 const T& operator[](std::size_t i) const override
163 {
164 return (i > _remainder ? _start[1] : _start[0])[i];
165 }
166
167 T& operator[](std::size_t i) override
168 {
169 return (i > _remainder ? _start[1] : _start[0])[i];
170 }
171
172 std::size_t size() const
173 {
174 return _count;
175 }
176
177 void refresh()
178 {
179 const std::ptrdiff_t n = size();
180 T* const base = _data.get();
181 if (_shift >= 0) {
182 _remainder = n - _shift - 1;
183 _start[0] = base + _shift;
184 _start[1] = base - (n - _shift);
185 }
186 else {
187 _remainder = -_shift - 1;
188 _start[0] = base + (n + _shift);
189 _start[1] = base + _shift;
190 }
191 }
192
193 void rotate(std::ptrdiff_t offset)
194 {
195 const std::ptrdiff_t n = size();
196 _shift -= offset;
197 if (_shift >= n) {
198 _shift -= n;
199 }
200 else if (_shift <= -n) {
201 _shift += n;
202 }
203 refresh();
204 }
205
206 void resize(std::size_t count)
207 {
208 throw std::logic_error("Cyclic column can not be resized");
209 }
210
212
214 std::size_t getNblock() const override;
216 std::size_t getSerializableSize() const override;
218 bool* getBlock(std::size_t iBlock, std::size_t& sizeBlock, bool loadingMode) override;
219 void postLoad() override { refresh(); }
220
221};
222
223}
224
225}
226
228template <typename T>
232
234template <typename T>
238
240template <typename T>
242
244template <typename T>
246
247}
248
249#endif
250
251#include "column.hh"
Base class for serializable objects of constant size. For dynamic size use BufferSerializable.
Definition serializer.h:145
Plain column for SISD CPU targets (default)
Definition column.h:45
void setProcessingContext(ProcessingContext)
Definition column.h:111
const T * data() const
Definition column.h:101
void resize(std::size_t count)
Definition column.h:78
const T & operator[](std::size_t i) const override
Definition column.h:86
Column(std::size_t count)
Definition column.h:53
std::size_t size() const
Definition column.h:96
Column(Column< T > &&rhs)
Definition column.h:62
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.
Definition column.hh:51
Column(const Column< T > &rhs)
Definition column.h:67
std::size_t getSerializableSize() const override
Binary size for the serializer.
Definition column.hh:45
std::size_t getNblock() const override
Number of data blocks for the serializable interface.
Definition column.hh:39
T & operator[](std::size_t i) override
Definition column.h:91
virtual ~Column()=default
Cyclic column for usage in ColumnVector.
Definition column.h:130
std::size_t getNblock() const override
Number of data blocks for the serializable interface.
Definition column.hh:67
CyclicColumn(std::size_t count)
Definition column.h:142
const T & operator[](std::size_t i) const override
Definition column.h:162
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.
Definition column.hh:79
void postLoad() override
Definition column.h:219
std::size_t getSerializableSize() const override
Binary size for the serializer.
Definition column.hh:73
void setProcessingContext(ProcessingContext)
Definition column.h:211
T & operator[](std::size_t i) override
Definition column.h:167
void rotate(std::ptrdiff_t offset)
Definition column.h:193
CyclicColumn(CyclicColumn< T > &&rhs)
Definition column.h:151
std::size_t size() const
Definition column.h:172
void resize(std::size_t count)
Definition column.h:206
Top level namespace for all of OpenLB.
ProcessingContext
OpenLB processing contexts.
Definition platform.h:55
Platform
OpenLB execution targets.
Definition platform.h:36
Abstract declarator of Column-like storage.
Definition column.h:34
Abstract declarator of cyclic Column-like storage.
Definition column.h:43
Specializable declarator for concrete implementations of abstract storage types.
Definition column.h:52