AutoPas  3.0.0
Loading...
Searching...
No Matches
SoA.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <algorithm>
10#include <initializer_list>
11#include <map>
12#include <tuple>
13#include <vector>
14
20
21namespace autopas {
22
27template <class SoAArraysType>
28class SoA {
29 public:
33 SoA() = default;
34
39 SoA(const SoA &soa) = default;
40
45 void resizeArrays(size_t length) {
46 soaStorage.apply([=](auto &list) { list.resize(length); });
47 }
48
54 template <std::size_t attribute>
55 void push(const double value) {
56 soaStorage.template get<attribute>().push_back(value);
57 }
58
66 template <int attribute, class ValueType>
67 void write(size_t particleId, const ValueType &value) {
68 soaStorage.template get<attribute>().at(particleId) = value;
69 }
70
75 void append(const SoA<SoAArraysType> &other) {
76 if (other.size() > 0) {
77 append_impl(other.soaStorage, std::make_index_sequence<std::tuple_size<SoAArraysType>::value>{});
78 }
79 }
80
85 void append(const SoAView<SoAArraysType> &other) {
86 if (other.size() > 0) {
87 append_impl(other, std::make_index_sequence<std::tuple_size<SoAArraysType>::value>{});
88 }
89 }
90
96 template <int... attributes>
97 void append(const SoA<SoAArraysType> &other) {
98 if (other.size() > 0) {
99 const auto newSize = size() + other.size();
100 append_impl(other.soaStorage, std::index_sequence<attributes...>{});
101 resizeArrays(newSize);
102 }
103 }
104
112 template <int... attributes, class ValueArrayType>
113 void writeMultiple(size_t particleId, const ValueArrayType &values) {
114 write_impl<attributes...>(particleId, values);
115 }
116
124 template <int... attributes, size_t N = sizeof...(attributes)>
125 inline void writeMultiple(size_t particleId, const std::array<double, N> &values) {
126 write_impl<attributes...>(particleId, values);
127 }
128
137 template <int... attributes>
138 std::array<double, sizeof...(attributes)> readMultiple(size_t particleId) const {
139 std::array<double, sizeof...(attributes)> retArray;
140 if (particleId >= size()) {
142 "SoA::read: requested particle id ({}) is bigger than number of particles ({})", particleId, size());
143 return retArray;
144 }
145 read_impl<attributes...>(particleId, retArray);
146 return retArray;
147 }
148
155 template <std::size_t attribute>
156 auto read(size_t particleId) const {
157 return soaStorage.template get<attribute>().at(particleId);
158 }
159
165 template <std::size_t attribute>
166 auto begin() {
167 return soaStorage.template get<attribute>().data();
168 }
169
175 template <std::size_t attribute>
176 auto begin() const {
177 return soaStorage.template get<attribute>().data();
178 }
179
187 inline size_t size() const {
188 size_t maxLength = 0;
189 utils::TupleUtils::for_each(soaStorage.getTuple(), [&](auto &v) { maxLength = std::max(maxLength, v.size()); });
190 return maxLength;
191 }
192
196 void clear() {
197 soaStorage.apply([](auto &list) { list.clear(); });
198 }
199
205 void swap(std::size_t a, std::size_t b) {
206 soaStorage.apply([=](auto &list) { std::swap(list[a], list[b]); });
207 }
208
212 void pop_back() {
213 soaStorage.apply([](auto &list) { list.pop_back(); });
214 }
215
220 SoAView<SoAArraysType> constructView() { return {this, 0, size()}; }
221
231 SoAView<SoAArraysType> constructView(size_t startIndex, size_t endIndex) { return {this, startIndex, endIndex}; }
232
233 private:
234 // actual implementation of read
235 template <int attribute, int... attributes, class ValueArrayType>
236 void read_impl(size_t particleId, ValueArrayType &values, int _current = 0) const {
237 values[_current] = soaStorage.template get<attribute>().at(particleId);
238 read_impl<attributes...>(particleId, values, _current + 1);
239 }
240
241 // stop of recursive read call
242 template <class ValueArrayType>
243 void read_impl(size_t particleId, ValueArrayType &values, int _current = 0) const {}
244
245 // actual implementation of the write function.
246 // uses a recursive call.
247 template <int attribute, int... attributes, class ValueArrayType>
248 void write_impl(size_t particleId, const ValueArrayType &values, int _current = 0) {
249 soaStorage.template get<attribute>().at(particleId) = values[_current];
250 write_impl<attributes...>(particleId, values, _current + 1);
251 }
252
253 // Stop of the recursive write_impl call
254 template <class ValueArrayType>
255 void write_impl(size_t particleId, const ValueArrayType &values, int _current = 0) {}
256
257 // helper function to append a single array
258 template <std::size_t attribute>
259 void appendSingleArray(const utils::SoAStorage<SoAArraysType> &valArrays) {
260 auto &currentVector = soaStorage.template get<attribute>();
261 auto &otherVector = valArrays.template get<attribute>();
262 currentVector.insert(currentVector.end(), otherVector.begin(), otherVector.end());
263 }
264
265 // helper function to append a single array
266 template <std::size_t attribute>
267 void appendSingleArray(const SoAView<SoAArraysType> &valArrays) {
268 auto &currentVector = soaStorage.template get<attribute>();
269 auto otherVectorIterator = valArrays.template begin<attribute>();
270 currentVector.insert(currentVector.end(), otherVectorIterator, otherVectorIterator + valArrays.size());
271 }
272
273 // actual implementation of append
274 template <std::size_t... Is>
275 void append_impl(const utils::SoAStorage<SoAArraysType> &valArrays, std::index_sequence<Is...>) {
276 // fold expression
277 (appendSingleArray<Is>(valArrays), ...);
278 }
279
280 // actual implementation of append
281 template <std::size_t... Is>
282 void append_impl(const SoAView<SoAArraysType> &valArrays, std::index_sequence<Is...>) {
283 // fold expression
284 (appendSingleArray<Is>(valArrays), ...);
285 }
286
287 // ------------- members ---------------
288
289 // storage container for the SoA's
290 utils::SoAStorage<SoAArraysType> soaStorage;
291};
292} // namespace autopas
Provides a struct to easily generate the desired SoAType.
View on a fixed part of a SoA between a start index and an end index.
Definition: SoAView.h:23
size_t size() const
Returns the number of particles in the view.
Definition: SoAView.h:83
Structur of the array class.
Definition: SoA.h:28
void writeMultiple(size_t particleId, const ValueArrayType &values)
Writes or updates values of attributes for a specific particle.
Definition: SoA.h:113
void append(const SoAView< SoAArraysType > &other)
Appends the other SoA buffer to this.
Definition: SoA.h:85
void append(const SoA< SoAArraysType > &other)
Appends the specified attributes from the other SoA buffer to this.
Definition: SoA.h:97
void resizeArrays(size_t length)
Resizes all Vectors to the given length.
Definition: SoA.h:45
void pop_back()
Delete the last particle in the SoA.
Definition: SoA.h:212
void write(size_t particleId, const ValueType &value)
Writes / updates the value of an attribute for a specific particle.
Definition: SoA.h:67
void clear()
delete all particles in the soa
Definition: SoA.h:196
SoAView< SoAArraysType > constructView(size_t startIndex, size_t endIndex)
Constructs a view that starts at startIndex (inclusive) and ends at endIndex (exclusive).
Definition: SoA.h:231
SoA(const SoA &soa)=default
Copy constructor.
std::array< double, sizeof...(attributes)> readMultiple(size_t particleId) const
Reads from all given attribute arrays at position particleId.
Definition: SoA.h:138
void writeMultiple(size_t particleId, const std::array< double, N > &values)
Specialized version to pass arrays without specifying it directly.
Definition: SoA.h:125
SoAView< SoAArraysType > constructView()
Constructs a SoAView for the whole SoA and returns it.
Definition: SoA.h:220
void push(const double value)
Pushes a given value to the desired attribute array.
Definition: SoA.h:55
size_t size() const
Returns the number of particles.
Definition: SoA.h:187
SoA()=default
Default constructor.
void swap(std::size_t a, std::size_t b)
swap the position of two particles in the soa
Definition: SoA.h:205
auto begin()
Returns a pointer to the given attribute vector.
Definition: SoA.h:166
auto read(size_t particleId) const
Reads the value of a given attribute of a given particle.
Definition: SoA.h:156
void append(const SoA< SoAArraysType > &other)
Appends the other SoA buffer to this.
Definition: SoA.h:75
auto begin() const
Returns a const pointer to the given attribute vector.
Definition: SoA.h:176
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:64
void for_each(T &&tuple, F &&f)
Applies a function f on every element of the tuple.
Definition: TupleUtils.h:22
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:34