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
177 inline size_t size() const {
178 size_t maxLength = 0;
179 utils::TupleUtils::for_each(soaStorage.getTuple(), [&](auto &v) { maxLength = std::max(maxLength, v.size()); });
180 return maxLength;
181 }
182
186 void clear() {
187 soaStorage.apply([](auto &list) { list.clear(); });
188 }
189
195 void swap(std::size_t a, std::size_t b) {
196 soaStorage.apply([=](auto &list) { std::swap(list[a], list[b]); });
197 }
198
202 void pop_back() {
203 soaStorage.apply([](auto &list) { list.pop_back(); });
204 }
205
210 SoAView<SoAArraysType> constructView() { return {this, 0, size()}; }
211
221 SoAView<SoAArraysType> constructView(size_t startIndex, size_t endIndex) { return {this, startIndex, endIndex}; }
222
223 private:
224 // actual implementation of read
225 template <int attribute, int... attributes, class ValueArrayType>
226 void read_impl(size_t particleId, ValueArrayType &values, int _current = 0) const {
227 values[_current] = soaStorage.template get<attribute>().at(particleId);
228 read_impl<attributes...>(particleId, values, _current + 1);
229 }
230
231 // stop of recursive read call
232 template <class ValueArrayType>
233 void read_impl(size_t particleId, ValueArrayType &values, int _current = 0) const {}
234
235 // actual implementation of the write function.
236 // uses a recursive call.
237 template <int attribute, int... attributes, class ValueArrayType>
238 void write_impl(size_t particleId, const ValueArrayType &values, int _current = 0) {
239 soaStorage.template get<attribute>().at(particleId) = values[_current];
240 write_impl<attributes...>(particleId, values, _current + 1);
241 }
242
243 // Stop of the recursive write_impl call
244 template <class ValueArrayType>
245 void write_impl(size_t particleId, const ValueArrayType &values, int _current = 0) {}
246
247 // helper function to append a single array
248 template <std::size_t attribute>
249 void appendSingleArray(const utils::SoAStorage<SoAArraysType> &valArrays) {
250 auto &currentVector = soaStorage.template get<attribute>();
251 auto &otherVector = valArrays.template get<attribute>();
252 currentVector.insert(currentVector.end(), otherVector.begin(), otherVector.end());
253 }
254
255 // helper function to append a single array
256 template <std::size_t attribute>
257 void appendSingleArray(const SoAView<SoAArraysType> &valArrays) {
258 auto &currentVector = soaStorage.template get<attribute>();
259 auto otherVectorIterator = valArrays.template begin<attribute>();
260 currentVector.insert(currentVector.end(), otherVectorIterator, otherVectorIterator + valArrays.size());
261 }
262
263 // actual implementation of append
264 template <std::size_t... Is>
265 void append_impl(const utils::SoAStorage<SoAArraysType> &valArrays, std::index_sequence<Is...>) {
266 // fold expression
267 (appendSingleArray<Is>(valArrays), ...);
268 }
269
270 // actual implementation of append
271 template <std::size_t... Is>
272 void append_impl(const SoAView<SoAArraysType> &valArrays, std::index_sequence<Is...>) {
273 // fold expression
274 (appendSingleArray<Is>(valArrays), ...);
275 }
276
277 // ------------- members ---------------
278
279 // storage container for the SoA's
280 utils::SoAStorage<SoAArraysType> soaStorage;
281};
282} // 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:202
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:186
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:221
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:210
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:177
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:195
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
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:63
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:32