AutoPas  3.0.0
Loading...
Searching...
No Matches
ContainerIterator.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <array>
11#include <functional>
12#include <limits>
13#include <optional>
14#include <tuple>
15#include <type_traits>
16#include <vector>
17
22#include "autopas/utils/inBox.h"
24
25namespace autopas {
26
27namespace internal {
37template <class ParticleIterator>
38void deleteParticle(ParticleIterator &iterator) {
39 iterator.deleteCurrentParticle();
40}
41} // namespace internal
42
46namespace containerIteratorUtils {
59template <bool regionIter, class Particle_T, class Arr>
60[[nodiscard]] bool particleFulfillsIteratorRequirements(const Particle_T &p, IteratorBehavior behavior,
61 const Arr &regionMin, const Arr &regionMax) {
62 // If this is a region iterator check box condition first, otherwise race conditions can occur
63 // if multiple region iterator overlap and ownership state is touched.
64 // (e.g. during collection of leaving particles)
65 if constexpr (regionIter) {
66 if (not utils::inBox(p.getR(), regionMin, regionMax)) {
67 return false;
68 }
69 }
70
71 // Check ownership
72 // Dummy is not in sync with iterator behavior because it needs to be 0.
73 // `a & b == b` idiom is to check a == b disregarding any bits beyond b. This is needed due to e.g. forceSequential.
74 if (((behavior & IteratorBehavior::ownedOrHaloOrDummy) == IteratorBehavior::ownedOrHaloOrDummy) or
75 (behavior & IteratorBehavior::dummy and p.isDummy())) {
76 return true;
77 } else {
78 // relies on both enums having the same encoding. This should be guaranteed statically in IteratorBehaviorTest!
79 return static_cast<unsigned int>(p.getOwnershipState()) & static_cast<unsigned int>(behavior);
80 }
81}
82} // namespace containerIteratorUtils
83
95template <class Particle_T, bool modifiable, bool regionIter>
97 template <class T>
98 friend void internal::deleteParticle(T &);
99
100 public:
104 using ParticleType = std::conditional_t<modifiable, Particle_T, const Particle_T>;
108 using ParticleVecType = std::conditional_t<modifiable, std::vector<std::vector<Particle_T> *>,
109 std::vector<std::vector<Particle_T> const *>>;
113 using ContainerType = std::conditional_t<modifiable, ParticleContainerInterface<Particle_T>,
115
119 ContainerIterator() : _currentParticle(nullptr){};
120
130 ContainerIterator(ContainerType &container, IteratorBehavior behavior,
131 utils::optRef<ParticleVecType> additionalVectorsToIterate, const std::array<double, 3> &regionMin,
132 const std::array<double, 3> &regionMax)
133 : ContainerIterator(nullptr, container, behavior, additionalVectorsToIterate, regionMin, regionMax) {
134 // sanity check
135 static_assert(regionIter == true,
136 "Constructor for Region iterator called but template argument regionIter is false");
137 }
138
146 ContainerIterator(ContainerType &container, IteratorBehavior behavior,
147 utils::optRef<ParticleVecType> additionalVectorsToIterate)
148 : ContainerIterator(nullptr, container, behavior, additionalVectorsToIterate, {}, {}) {
149 static_assert(regionIter == false,
150 "Constructor for non-Region iterator called but template argument regionIter is true");
151 }
152
158 : _container(other._container),
159 _currentParticleIndex(other._currentParticleIndex),
160 _currentVectorIndex(other._currentVectorIndex),
161 _currentParticle(other._currentParticle),
162 _additionalVectors(other._additionalVectors),
163 _behavior(other._behavior),
164 _iteratingAdditionalVectors(other._iteratingAdditionalVectors),
165 _vectorIndexOffset(other._vectorIndexOffset),
166 _regionMin(other._regionMin),
167 _regionMax(other._regionMax) {}
168
176 if (this != other) {
177 _container = other._container;
178 _currentParticleIndex = other._currentParticleIndex;
179 _currentVectorIndex = other._currentVectorIndex;
180 _currentParticle = other._currentParticle;
181 _additionalVectors = other._additionalVectors;
182 _behavior = other._behavior;
183 _iteratingAdditionalVectors = other._iteratingAdditionalVectors;
184 _vectorIndexOffset = other._vectorIndexOffset;
185 if constexpr (regionIter) {
186 _regionMin = other._regionMin;
187 _regionMax = other._regionMax;
188 }
189 }
190 return *this;
191 }
192
198 : _container(other._container),
199 _currentParticleIndex(other._currentParticleIndex),
200 _currentVectorIndex(other._currentVectorIndex),
201 _currentParticle(other._currentParticle),
202 _additionalVectors(std::move(other._additionalVectors)),
203 _behavior(other._behavior),
204 _iteratingAdditionalVectors(other._iteratingAdditionalVectors),
205 _vectorIndexOffset(other._vectorIndexOffset),
206 _regionMin(std::move(other._regionMin)),
207 _regionMax(std::move(other._regionMax)) {}
208
216 if (this != &other) {
217 _container = other._container;
218 _currentParticleIndex = other._currentParticleIndex;
219 _currentVectorIndex = other._currentVectorIndex;
220 _currentParticle = other._currentParticle;
221 _additionalVectors = std::move(other._additionalVectors);
222 _behavior = other._behavior;
223 _iteratingAdditionalVectors = other._iteratingAdditionalVectors;
224 _vectorIndexOffset = other._vectorIndexOffset;
225 if constexpr (regionIter) {
226 _regionMin = std::move(other._regionMin);
227 _regionMax = std::move(other._regionMax);
228 }
229 }
230 return *this;
231 }
232
233 private:
245 ContainerIterator(void * /*dummy*/, ContainerType &container, IteratorBehavior behavior,
246 utils::optRef<ParticleVecType> additionalVectorsToIterate, const std::array<double, 3> &regionMin,
247 const std::array<double, 3> &regionMax)
248 : _container(&container),
249 _behavior(behavior),
250 _vectorIndexOffset((behavior & IteratorBehavior::forceSequential) ? 1 : autopas_get_num_threads()) {
251 if (additionalVectorsToIterate.has_value()) {
252 // store pointers to all additional vectors
253 _additionalVectors.insert(_additionalVectors.end(), additionalVectorsToIterate->get().begin(),
254 additionalVectorsToIterate->get().end());
255 }
256
257 if constexpr (regionIter) {
258 // clamp region to the container, either with or without halo
259 const auto boxMaxWithHalo = utils::ArrayMath::addScalar(container.getBoxMax(), container.getInteractionLength());
260 const auto boxMinWithHalo = utils::ArrayMath::subScalar(container.getBoxMin(), container.getInteractionLength());
261 _regionMax = utils::ArrayMath::min(regionMax, boxMaxWithHalo);
262 _regionMin = utils::ArrayMath::max(regionMin, boxMinWithHalo);
263 }
264 // fetches the next (=first) valid particle or sets _currentParticle = nullptr which marks the iterator as invalid.
265 fetchParticleAtCurrentIndex();
266 }
267
268 public:
278 // bump the index. If it is invalid now, the container will give us the proper one.
279 ++_currentParticleIndex;
280 fetchParticleAtCurrentIndex();
281 return *this;
282 }
283
288 inline ParticleType &operator*() const { return *_currentParticle; }
289
294 inline ParticleType *operator->() const { return _currentParticle; }
295
300 [[nodiscard]] bool isValid() const { return _currentParticle != nullptr; }
301
311 bool operator==(const bool input) const { return isValid() == input; }
312
318 bool operator!=(const bool input) const { return not(*this == input); }
319
320 private:
325 void fetchParticleAtCurrentIndex() {
326 if (not _iteratingAdditionalVectors) {
327 // getParticle either gives us a particle with the desired properties or a nullptr
328 if constexpr (regionIter) {
329 std::tie(_currentParticle, _currentVectorIndex, _currentParticleIndex) =
330 _container->getParticle(_currentVectorIndex, _currentParticleIndex, _behavior, _regionMin, _regionMax);
331 } else {
332 std::tie(_currentParticle, _currentVectorIndex, _currentParticleIndex) =
333 _container->getParticle(_currentVectorIndex, _currentParticleIndex, _behavior);
334 }
335 // if getParticle told us that the container doesn't have a particle in the first vector for our thread...
336 if (_currentParticle == nullptr and not _additionalVectors.empty()) {
337 // determine which additional vector this thread should start to iterate
338 _currentVectorIndex = (_behavior & IteratorBehavior::forceSequential) ? 0 : autopas_get_thread_num();
339 _currentParticleIndex = 0;
340 _iteratingAdditionalVectors = true;
341 } else {
342 // Case: nothing left in the container and no additional vectors to iterate
343 return;
344 }
345 }
346 // no "else" here because the first case might trigger the second if we reached the end of the container
347 if (_iteratingAdditionalVectors) {
348 // check all vectors ...
349 for (; _currentVectorIndex < _additionalVectors.size(); _currentVectorIndex += _vectorIndexOffset) {
350 // ... and all particles within the vector ...
351 for (; _currentParticleIndex < _additionalVectors[_currentVectorIndex]->size(); ++_currentParticleIndex) {
352 _currentParticle = &(_additionalVectors[_currentVectorIndex]->operator[](_currentParticleIndex));
353 // ... until we find a particle that satisfies our requirements.
354 if (containerIteratorUtils::particleFulfillsIteratorRequirements<regionIter>(*_currentParticle, _behavior,
355 _regionMin, _regionMax)) {
356 return;
357 }
358 }
359 _currentParticleIndex = 0;
360 }
361 }
362 // if we reach this point there is no satisfying particle left neither in the container nor the additional vectors.
363 _currentParticle = nullptr;
364 _currentParticleIndex = std::numeric_limits<decltype(_currentParticleIndex)>::max();
365 _currentVectorIndex = std::numeric_limits<decltype(_currentVectorIndex)>::max();
366 }
367
374 void deleteCurrentParticle() {
375 if (_iteratingAdditionalVectors) {
376 internal::markParticleAsDeleted(*_currentParticle);
377 } else {
378 _container->deleteParticle(_currentVectorIndex, _currentParticleIndex);
379 }
380 // move on to the next particle, as the deleted one stays in place.
381 this->operator++();
382 // move the current index back so that the next ++ points again to the same particle.
383 --_currentParticleIndex;
384 }
385
390 ContainerType *_container = nullptr;
391
396 size_t _currentParticleIndex{0};
397
403 size_t _currentVectorIndex{};
404
409 ParticleType *_currentParticle = nullptr;
410
414 ParticleVecType _additionalVectors;
415
419 IteratorBehavior _behavior;
420
425 bool _iteratingAdditionalVectors{false};
426
430 size_t _vectorIndexOffset{};
431
435 struct empty {};
439 using RegionCornerT = std::conditional_t<regionIter, std::array<double, 3>, empty>;
445 [[no_unique_address]] RegionCornerT _regionMin{};
451 [[no_unique_address]] RegionCornerT _regionMax{};
452};
453} // namespace autopas
Public iterator class that iterates over a particle container and additional vectors (which are typic...
Definition: ContainerIterator.h:96
std::conditional_t< modifiable, Particle_T, const Particle_T > ParticleType
Type of the particle this iterator points to.
Definition: ContainerIterator.h:104
ContainerIterator(ContainerType &container, IteratorBehavior behavior, utils::optRef< ParticleVecType > additionalVectorsToIterate, const std::array< double, 3 > &regionMin, const std::array< double, 3 > &regionMax)
Region Iterator constructor meant to be called from the logic handler.
Definition: ContainerIterator.h:130
ContainerIterator(const ContainerIterator< Particle_T, modifiable, regionIter > &other)
Copy constructor.
Definition: ContainerIterator.h:157
ContainerIterator< Particle_T, modifiable, regionIter > & operator=(ContainerIterator< Particle_T, modifiable, regionIter > &&other) noexcept
Move assignment operator.
Definition: ContainerIterator.h:214
std::conditional_t< modifiable, ParticleContainerInterface< Particle_T >, const ParticleContainerInterface< Particle_T > > ContainerType
Type of the Particle Container type.
Definition: ContainerIterator.h:114
ContainerIterator< Particle_T, modifiable, regionIter > & operator=(const ContainerIterator< Particle_T, modifiable, regionIter > &other)
Copy assignment operator.
Definition: ContainerIterator.h:174
ParticleType * operator->() const
Dereference operator.
Definition: ContainerIterator.h:294
bool operator==(const bool input) const
Checks if the current iterator has a given validity.
Definition: ContainerIterator.h:311
ContainerIterator(ContainerType &container, IteratorBehavior behavior, utils::optRef< ParticleVecType > additionalVectorsToIterate)
Regular Iterator constructor meant to be called from the logic handler.
Definition: ContainerIterator.h:146
bool operator!=(const bool input) const
Checks if the current iterator does not have a given validity.
Definition: ContainerIterator.h:318
ContainerIterator< Particle_T, modifiable, regionIter > & operator++()
Increments the iterator.
Definition: ContainerIterator.h:277
std::conditional_t< modifiable, std::vector< std::vector< Particle_T > * >, std::vector< std::vector< Particle_T > const * > > ParticleVecType
Type of the additional vector collection.
Definition: ContainerIterator.h:109
ContainerIterator(ContainerIterator< Particle_T, modifiable, regionIter > &&other) noexcept
Move constructor.
Definition: ContainerIterator.h:197
ContainerIterator()
Default constructor that guarantees an invalid iterator.
Definition: ContainerIterator.h:119
ParticleType & operator*() const
Dereference operator.
Definition: ContainerIterator.h:288
bool isValid() const
Check whether the iterator currently points to a valid particle.
Definition: ContainerIterator.h:300
The ParticleContainerInterface class provides a basic interface for all Containers within AutoPas.
Definition: ParticleContainerInterface.h:40
bool particleFulfillsIteratorRequirements(const Particle_T &p, IteratorBehavior behavior, const Arr &regionMin, const Arr &regionMax)
Indicates whether the particle has the correct ownership state and if this is a region iterator is in...
Definition: ContainerIterator.h:60
void markParticleAsDeleted(Particle_T &p)
Marks a particle as deleted.
Definition: markParticleAsDeleted.h:23
void deleteParticle(ParticleIterator &iterator)
Function to access private iterator.deleteCurrentParticle() via friend.
Definition: ContainerIterator.h:38
constexpr std::array< T, SIZE > max(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Takes elementwise maximum and returns the result.
Definition: ArrayMath.h:96
constexpr std::array< T, SIZE > subScalar(const std::array< T, SIZE > &a, T s)
Subtracts a scalar s from each element of array a and returns the result.
Definition: ArrayMath.h:164
constexpr std::array< T, SIZE > addScalar(const std::array< T, SIZE > &a, T s)
Adds a scalar s to each element of array a and returns the result.
Definition: ArrayMath.h:147
constexpr std::array< T, SIZE > min(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Takes elementwise minimum, returns the result.
Definition: ArrayMath.h:62
bool inBox(const std::array< T, 3 > &position, const std::array< T, 3 > &low, const std::array< T, 3 > &high)
Checks if position is inside of a box defined by low and high.
Definition: inBox.h:26
std::optional< std::reference_wrapper< T > > optRef
Short alias for std::optional<std::reference_wrapper<T>>
Definition: optRef.h:16
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:34
int autopas_get_num_threads()
Dummy for omp_get_num_threads() when no OpenMP is available.
Definition: WrapOpenMP.h:138
int autopas_get_thread_num()
Dummy for omp_set_lock() when no OpenMP is available.
Definition: WrapOpenMP.h:132