AutoPas  3.0.0
Loading...
Searching...
No Matches
LogicHandler.h
Go to the documentation of this file.
1
7#pragma once
8#include <atomic>
9#include <limits>
10#include <memory>
11#include <optional>
12#include <tuple>
13#include <type_traits>
14#include <vector>
15
31#include "autopas/utils/Timer.h"
40
41namespace autopas {
42
48template <typename Particle_T>
50 public:
58 LogicHandler(const std::shared_ptr<TuningManager> &tunerManager, const LogicHandlerInfo &logicHandlerInfo,
59 unsigned int rebuildFrequency, const std::string &outputSuffix)
60 : _tuningManager(tunerManager),
61 _logicHandlerInfo(logicHandlerInfo),
62 _neighborListRebuildFrequency{rebuildFrequency},
63 _particleBuffer(autopas_get_max_threads()),
64 _haloParticleBuffer(autopas_get_max_threads()),
65 _remainderPairwiseInteractionHandler(_spatialLocks),
66 _remainderTriwiseInteractionHandler(_spatialLocks),
67 _verletClusterSize(logicHandlerInfo.verletClusterSize),
68 _sortingThreshold(logicHandlerInfo.sortingThreshold),
69 _iterationLogger(outputSuffix,
70 std::any_of(tunerManager->getAutoTuners().begin(), tunerManager->getAutoTuners().end(),
71 [](const auto &tuner) { return tuner.second->canMeasureEnergy(); })),
72 _flopLogger(outputSuffix),
73 _liveInfoLogger(outputSuffix) {
74 using namespace autopas::utils::ArrayMath::literals;
75 // Initialize AutoPas with tuners for given interaction types
76 for (const auto &[interactionType, tuner] : tunerManager->getAutoTuners()) {
77 _interactionTypes.insert(interactionType);
78
79 const auto configuration = tuner->getCurrentConfig();
80 // initialize the container and make sure it is valid
81 _currentContainerSelectorInfo = ContainerSelectorInfo{_logicHandlerInfo.boxMin,
82 _logicHandlerInfo.boxMax,
83 _logicHandlerInfo.cutoff,
84 configuration.cellSizeFactor,
85 _logicHandlerInfo.verletSkin,
86 _verletClusterSize,
87 _sortingThreshold,
88 configuration.loadEstimator};
89 _currentContainer =
90 ContainerSelector<Particle_T>::generateContainer(configuration.container, _currentContainerSelectorInfo);
91 checkMinimalSize();
92 }
93
94 // initialize locks needed for remainder traversal
95 const auto interactionLength = logicHandlerInfo.cutoff + logicHandlerInfo.verletSkin;
96 const auto interactionLengthInv = 1. / interactionLength;
97 const auto boxLengthWithHalo = logicHandlerInfo.boxMax - logicHandlerInfo.boxMin + (2 * interactionLength);
98 initSpatialLocks(boxLengthWithHalo, interactionLengthInv);
99 }
100
105 ParticleContainerInterface<Particle_T> &getContainer() { return *_currentContainer; }
106
112 [[nodiscard]] std::vector<Particle_T> collectLeavingParticlesFromBuffer(bool insertOwnedParticlesToContainer) {
113 const auto &boxMin = _currentContainer->getBoxMin();
114 const auto &boxMax = _currentContainer->getBoxMax();
115 std::vector<Particle_T> leavingBufferParticles{};
116 for (auto &cell : _particleBuffer) {
117 auto &buffer = cell._particles;
118 if (insertOwnedParticlesToContainer) {
119 // Can't be const because we potentially modify ownership before re-adding
120 for (auto &p : buffer) {
121 if (p.isDummy()) {
122 continue;
123 }
124 if (utils::inBox(p.getR(), boxMin, boxMax)) {
125 p.setOwnershipState(OwnershipState::owned);
126 _currentContainer->addParticle(p);
127 } else {
128 leavingBufferParticles.push_back(p);
129 }
130 }
131 buffer.clear();
132 } else {
133 for (auto iter = buffer.begin(); iter < buffer.end();) {
134 auto &p = *iter;
135
136 auto fastRemoveP = [&]() {
137 // Fast remove of particle, i.e., swap with last entry && pop.
138 std::swap(p, buffer.back());
139 buffer.pop_back();
140 // Do not increment the iter afterward!
141 };
142 if (p.isDummy()) {
143 // We remove dummies!
144 fastRemoveP();
145 // In case we swapped a dummy here, don't increment the iterator and do another iteration to check again.
146 continue;
147 }
148 // if p was a dummy a new particle might now be at the memory location of p so we need to check that.
149 // We also just might have deleted the last particle in the buffer in that case the inBox check is meaningless
150 if (not buffer.empty() and utils::notInBox(p.getR(), boxMin, boxMax)) {
151 leavingBufferParticles.push_back(p);
152 fastRemoveP();
153 } else {
154 ++iter;
155 }
156 }
157 }
158 }
159 return leavingBufferParticles;
160 }
161
165 std::vector<Particle_T> updateContainer() {
166 ++_iteration;
167
168#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
170
171 if (_tuningManager->isStartOfTuningPhase(_iteration)) {
172 _numRebuildsInNonTuningPhase = 0;
173 }
174
175 // Rebuild frequency estimation should be triggered early in the tuning phase.
176 // This is necessary because runtime prediction for each trial configuration
177 // depends on the rebuild frequency.
178 // To avoid the influence of poorly initialized velocities at the start of the simulation,
179 // the rebuild frequency is estimated at iteration corresponding to the last sample of the first configuration.
180 // The rebuild frequency estimated here is then reused for the remainder of the tuning phase.
181 if (_tuningManager->inFirstConfigurationLastSample(_iteration)) {
182 // Fetch the needed information for estimating the rebuild frequency from _logicHandlerInfo
183 // and estimate the current rebuild frequency using the velocity method.
184 double rebuildFrequencyEstimate =
185 getVelocityMethodRFEstimate(_logicHandlerInfo.verletSkin, _logicHandlerInfo.deltaT);
186 double userProvidedRF = static_cast<double>(_neighborListRebuildFrequency);
187 // The user defined rebuild frequency is considered as the upper bound.
188 // If velocity method estimate exceeds upper bound, set the rebuild frequency to the user defined value.
189 // This is done because we currently use the user defined rebuild frequency as the upper bound to avoid expensive
190 // buffer interactions.
191 _tuningManager->setRebuildFrequency(std::min(userProvidedRF, rebuildFrequencyEstimate));
192 }
193#endif
194 bool doDataStructureUpdate = not neighborListsAreValid();
195
196 if (_tuningManager->tuningPhaseJustFinished()) {
197 _iterationAfterLastTuningPhase = _iteration;
198 }
199 // We will do a rebuild in this timestep
200 if (not _neighborListsAreValid.load(std::memory_order_relaxed)) {
201 _stepsSinceLastListRebuild = 0;
202 }
203 ++_stepsSinceLastListRebuild;
204
205 // The next call also adds particles to the container if doDataStructureUpdate is true.
206 auto leavingBufferParticles = collectLeavingParticlesFromBuffer(doDataStructureUpdate);
207
208 AutoPasLog(TRACE, "Initiating container update.");
209 auto leavingParticles = _currentContainer->updateContainer(not doDataStructureUpdate);
210 leavingParticles.insert(leavingParticles.end(), leavingBufferParticles.begin(), leavingBufferParticles.end());
211
212 // Subtract the amount of leaving particles from the number of owned particles.
213 _numParticlesOwned.fetch_sub(leavingParticles.size(), std::memory_order_relaxed);
214 // updateContainer deletes all halo particles.
215 std::for_each(_haloParticleBuffer.begin(), _haloParticleBuffer.end(), [](auto &buffer) { buffer.clear(); });
216 _numParticlesHalo.store(0, std::memory_order_relaxed);
217 return leavingParticles;
218 }
219
226 std::vector<Particle_T> resizeBox(const std::array<double, 3> &boxMin, const std::array<double, 3> &boxMax) {
227 using namespace autopas::utils::ArrayMath::literals;
228 const auto &oldMin = _currentContainer->getBoxMin();
229 const auto &oldMax = _currentContainer->getBoxMax();
230
231 // if nothing changed, do nothing
232 if (oldMin == boxMin and oldMax == boxMax) {
233 return {};
234 }
235
236 // sanity check that new size is actually positive
237 for (size_t i = 0; i < boxMin.size(); ++i) {
238 if (boxMin[i] >= boxMax[i]) {
240 "New box size in dimension {} is not positive!\nboxMin[{}] = {}\nboxMax[{}] = {}", i, i, boxMin[i], i,
241 boxMax[i]);
242 }
243 }
244
245 // warn if domain changes too drastically
246 const auto newLength = boxMax - boxMin;
247 const auto oldLength = oldMax - oldMin;
248 const auto relDiffLength = newLength / oldLength;
249 for (size_t i = 0; i < newLength.size(); ++i) {
250 // warning threshold is set arbitrary and up for change if needed
251 if (relDiffLength[i] > 1.3 or relDiffLength[i] < 0.7) {
252 AutoPasLog(WARN,
253 "LogicHandler.resize(): Domain size changed drastically in dimension {}! Gathered AutoTuning "
254 "information might not be applicable anymore!\n"
255 "Size old box : {}\n"
256 "Size new box : {}\n"
257 "Relative diff: {}",
259 utils::ArrayUtils::to_string(relDiffLength));
260 }
261 }
262
263 // The new box size is valid, so update the current container info.
264 _currentContainerSelectorInfo.boxMin = boxMin;
265 _currentContainerSelectorInfo.boxMax = boxMax;
266
267 // check all particles
268 std::vector<Particle_T> particlesNowOutside;
269 for (auto pIter = _currentContainer->begin(); pIter.isValid(); ++pIter) {
270 // make sure only owned ones are present
271 if (not pIter->isOwned()) {
273 "LogicHandler::resizeBox() encountered non owned particle. "
274 "When calling resizeBox() these should be already deleted. "
275 "This could be solved by calling updateContainer() before resizeBox().");
276 }
277 // owned particles that are now outside are removed from the container and returned
278 if (not utils::inBox(pIter->getR(), boxMin, boxMax)) {
279 particlesNowOutside.push_back(*pIter);
282 }
283 }
284
285 // Resize by generating a new container with the new box size and moving all particles to it.
286 auto newContainer = ContainerSelector<Particle_T>::generateContainer(_currentContainer->getContainerType(),
287 _currentContainerSelectorInfo);
288 setCurrentContainer(std::move(newContainer));
289 // The container might have changed sufficiently that we would need a different number of spatial locks.
290 const auto boxLength = boxMax - boxMin;
291 const auto interactionLengthInv = 1. / _currentContainer->getInteractionLength();
292 initSpatialLocks(boxLength, interactionLengthInv);
293
294 // Set this flag, s.t., the container is rebuilt!
295 _neighborListsAreValid.store(false, std::memory_order_relaxed);
296
297 return particlesNowOutside;
298 }
299
306 void reserve(size_t numParticles) {
307 const auto numParticlesHaloEstimate = autopas::utils::NumParticlesEstimator::estimateNumHalosUniform(
308 numParticles, _currentContainer->getBoxMin(), _currentContainer->getBoxMax(),
309 _currentContainer->getInteractionLength());
310 reserve(numParticles, numParticlesHaloEstimate);
311 }
312
319 void reserve(size_t numParticles, size_t numHaloParticles) {
320 const auto numHaloParticlesPerBuffer = numHaloParticles / _haloParticleBuffer.size();
321 for (auto &buffer : _haloParticleBuffer) {
322 buffer.reserve(numHaloParticlesPerBuffer);
323 }
324 // there is currently no good heuristic for this buffer, so reuse the one for halos.
325 for (auto &buffer : _particleBuffer) {
326 buffer.reserve(numHaloParticlesPerBuffer);
327 }
328
329 // reserve is called for the container only in the rebuild iterations.
330 // during non-rebuild iterations, particles are not added in the container but in buffer.
331 if (not _neighborListsAreValid.load(std::memory_order_relaxed)) {
332 _currentContainer->reserve(numParticles, numHaloParticles);
333 }
334 }
335
339 void addParticle(const Particle_T &p) {
340 // first check that the particle actually belongs in the container
341 const auto &boxMin = _currentContainer->getBoxMin();
342 const auto &boxMax = _currentContainer->getBoxMax();
343 if (utils::notInBox(p.getR(), boxMin, boxMax)) {
345 "LogicHandler: Trying to add a particle that is not in the bounding box.\n"
346 "Box Min {}\n"
347 "Box Max {}\n"
348 "{}",
349 boxMin, boxMax, p.toString());
350 }
351 Particle_T particleCopy = p;
352 particleCopy.setOwnershipState(OwnershipState::owned);
353 if (not _neighborListsAreValid.load(std::memory_order_relaxed)) {
354 // Container has to (about to) be invalid to be able to add Particles!
355 _currentContainer->template addParticle<false>(particleCopy);
356 } else {
357 // If the container is valid, we add it to the particle buffer.
358 _particleBuffer[autopas_get_thread_num()].addParticle(particleCopy);
359 }
360 _numParticlesOwned.fetch_add(1, std::memory_order_relaxed);
361 }
362
366 void addHaloParticle(const Particle_T &haloParticle) {
367 const auto &boxMin = _currentContainer->getBoxMin();
368 const auto &boxMax = _currentContainer->getBoxMax();
369 Particle_T haloParticleCopy = haloParticle;
370 if (utils::inBox(haloParticleCopy.getR(), boxMin, boxMax)) {
372 "LogicHandler: Trying to add a halo particle that is not outside the box of the container.\n"
373 "Box Min {}\n"
374 "Box Max {}\n"
375 "{}",
376 utils::ArrayUtils::to_string(boxMin), utils::ArrayUtils::to_string(boxMax), haloParticleCopy.toString());
377 }
378 haloParticleCopy.setOwnershipState(OwnershipState::halo);
379 if (not _neighborListsAreValid.load(std::memory_order_relaxed)) {
380 // If the neighbor lists are not valid, we can add the particle.
381 _currentContainer->template addHaloParticle</* checkInBox */ false>(haloParticleCopy);
382 } else {
383 // Check if we can update an existing halo(dummy) particle.
384 bool updated = _currentContainer->updateHaloParticle(haloParticleCopy);
385 if (not updated) {
386 // If we couldn't find an existing particle, add it to the halo particle buffer.
387 _haloParticleBuffer[autopas_get_thread_num()].addParticle(haloParticleCopy);
388 }
389 }
390 _numParticlesHalo.fetch_add(1, std::memory_order_relaxed);
391 }
392
397 _neighborListsAreValid.store(false, std::memory_order_relaxed);
398 _currentContainer->deleteAllParticles();
399 std::for_each(_particleBuffer.begin(), _particleBuffer.end(), [](auto &buffer) { buffer.clear(); });
400 std::for_each(_haloParticleBuffer.begin(), _haloParticleBuffer.end(), [](auto &buffer) { buffer.clear(); });
401 // all particles are gone -> reset counters.
402 _numParticlesOwned.store(0, std::memory_order_relaxed);
403 _numParticlesHalo.store(0, std::memory_order_relaxed);
404 }
405
412 std::tuple<bool, bool> deleteParticleFromBuffers(Particle_T &particle) {
413 // find the buffer the particle belongs to
414 auto &bufferCollection = particle.isOwned() ? _particleBuffer : _haloParticleBuffer;
415 for (auto &cell : bufferCollection) {
416 auto &buffer = cell._particles;
417 // if the address of the particle is between start and end of the buffer it is in this buffer
418 if (not buffer.empty() and &(buffer.front()) <= &particle and &particle <= &(buffer.back())) {
419 const bool isRearParticle = &particle == &buffer.back();
420 // swap-delete
421 particle = buffer.back();
422 buffer.pop_back();
423 return {true, not isRearParticle};
424 }
425 }
426 return {false, true};
427 }
428
434 void decreaseParticleCounter(Particle_T &particle) {
435 if (particle.isOwned()) {
436 _numParticlesOwned.fetch_sub(1, std::memory_order_relaxed);
437 } else {
438 _numParticlesHalo.fetch_sub(1, std::memory_order_relaxed);
439 }
440 }
441
462 template <class Functor>
463 bool computeInteractionsPipeline(Functor *functor, const InteractionTypeOption &interactionType);
464
471 template <class Iterator>
472 typename Iterator::ParticleVecType gatherAdditionalVectors(IteratorBehavior behavior) {
473 typename Iterator::ParticleVecType additionalVectors;
474 if (not(behavior & IteratorBehavior::containerOnly)) {
475 additionalVectors.reserve(static_cast<bool>(behavior & IteratorBehavior::owned) * _particleBuffer.size() +
476 static_cast<bool>(behavior & IteratorBehavior::halo) * _haloParticleBuffer.size());
477 if (behavior & IteratorBehavior::owned) {
478 for (auto &buffer : _particleBuffer) {
479 // Don't insert empty buffers. This also means that we won't pick up particles added during iterating if they
480 // go to the buffers. But since we wouldn't pick them up if they go into the container to a cell that the
481 // iterators already passed this is unsupported anyways.
482 if (not buffer.isEmpty()) {
483 additionalVectors.push_back(&(buffer._particles));
484 }
485 }
486 }
487 if (behavior & IteratorBehavior::halo) {
488 for (auto &buffer : _haloParticleBuffer) {
489 if (not buffer.isEmpty()) {
490 additionalVectors.push_back(&(buffer._particles));
491 }
492 }
493 }
494 }
495 return additionalVectors;
496 }
497
502 auto additionalVectors = gatherAdditionalVectors<ContainerIterator<Particle_T, true, false>>(behavior);
503 return _currentContainer->begin(behavior, std::ref(additionalVectors));
504 }
505
509 ContainerIterator<Particle_T, false, false> begin(IteratorBehavior behavior) const {
510 auto additionalVectors =
512 behavior);
513 return _currentContainer->begin(behavior, std::ref(additionalVectors));
514 }
515
519 ContainerIterator<Particle_T, true, true> getRegionIterator(const std::array<double, 3> &lowerCorner,
520 const std::array<double, 3> &higherCorner,
521 IteratorBehavior behavior) {
522 // sanity check: Most of our stuff depends on `inBox`, which does not handle lowerCorner > higherCorner well.
523 for (size_t d = 0; d < 3; ++d) {
524 if (lowerCorner[d] > higherCorner[d]) {
526 "Requesting region Iterator where the upper corner is lower than the lower corner!\n"
527 "Lower corner: {}\n"
528 "Upper corner: {}",
529 lowerCorner, higherCorner);
530 }
531 }
532
533 auto additionalVectors = gatherAdditionalVectors<ContainerIterator<Particle_T, true, true>>(behavior);
534 return _currentContainer->getRegionIterator(lowerCorner, higherCorner, behavior, std::ref(additionalVectors));
535 }
536
540 ContainerIterator<Particle_T, false, true> getRegionIterator(const std::array<double, 3> &lowerCorner,
541 const std::array<double, 3> &higherCorner,
542 IteratorBehavior behavior) const {
543 // sanity check: Most of our stuff depends on `inBox`, which does not handle lowerCorner > higherCorner well.
544 for (size_t d = 0; d < 3; ++d) {
545 if (lowerCorner[d] > higherCorner[d]) {
547 "Requesting region Iterator where the upper corner is lower than the lower corner!\n"
548 "Lower corner: {}\n"
549 "Upper corner: {}",
550 lowerCorner, higherCorner);
551 }
552 }
553
554 auto additionalVectors =
556 return std::as_const(_currentContainer)
557 ->getRegionIterator(lowerCorner, higherCorner, behavior, std::ref(additionalVectors));
558 }
559
564 [[nodiscard]] unsigned long getNumberOfParticlesOwned() const { return _numParticlesOwned; }
565
570 [[nodiscard]] unsigned long getNumberOfParticlesHalo() const { return _numParticlesHalo; }
571
584 template <class Functor>
585 [[nodiscard]] std::tuple<std::unique_ptr<TraversalInterface>, bool> isConfigurationApplicable(
586 const Configuration &config, Functor &functor);
587
598 void setParticleBuffers(const std::vector<FullParticleCell<Particle_T>> &particleBuffers,
599 const std::vector<FullParticleCell<Particle_T>> &haloParticleBuffers);
600
608 std::tuple<const std::vector<FullParticleCell<Particle_T>> &, const std::vector<FullParticleCell<Particle_T>> &>
609 getParticleBuffers() const;
610
622 [[nodiscard]] double getMeanRebuildFrequency(bool considerOnlyLastNonTuningPhase = false) const {
623#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
624 const auto numRebuilds = considerOnlyLastNonTuningPhase ? _numRebuildsInNonTuningPhase : _numRebuilds;
625 // The total number of iterations is iteration + 1
626 const auto iterationCount =
627 considerOnlyLastNonTuningPhase ? _iteration - _iterationAfterLastTuningPhase : _iteration + 1;
628 if (numRebuilds == 0) {
629 return static_cast<double>(_neighborListRebuildFrequency);
630 } else {
631 return static_cast<double>(iterationCount) / numRebuilds;
632 }
633#else
634 return static_cast<double>(_neighborListRebuildFrequency);
635#endif
636 }
637
645 double getVelocityMethodRFEstimate(const double skin, const double deltaT) const {
647 // Initialize the maximum velocity to zero
648 double maxVelocity = 0;
649 // Iterate over the owned particles in container to determine maximum velocity
650 AUTOPAS_OPENMP(parallel reduction(max : maxVelocity))
651 for (auto iter = this->begin(IteratorBehavior::owned | IteratorBehavior::containerOnly); iter.isValid(); ++iter) {
652 std::array<double, 3> tempVel = iter->getV();
653 double tempVelAbs = sqrt(dot(tempVel, tempVel));
654 maxVelocity = std::max(tempVelAbs, maxVelocity);
655 }
656 // return the rebuild frequency estimate
657 return skin / maxVelocity / deltaT / 2;
658 }
664
670
676
686
687 private:
700 void initSpatialLocks(const std::array<double, 3> &boxLength, double interactionLengthInv) {
701 using namespace autopas::utils::ArrayMath::literals;
704
705 // The maximum number of spatial locks is capped at 1e6.
706 // This limit is chosen more or less arbitrary. It is big enough so that our regular MD simulations
707 // fall well within it and small enough so that no memory issues arise.
708 // There were no rigorous tests for an optimal number of locks.
709 // Without this cap, very large domains (or tiny cutoffs) would generate an insane number of locks,
710 // that could blow up the memory.
711 constexpr size_t maxNumSpacialLocks{1000000};
712
713 // One lock per interaction length or less if this would generate too many.
714 const std::array<size_t, 3> locksPerDim = [&]() {
715 // First naively calculate the number of locks if we simply take the desired cell length.
716 // Ceil because both decisions are possible, and we are generous gods.
717 const std::array<size_t, 3> locksPerDimNaive =
718 static_cast_copy_array<size_t>(ceil(boxLength * interactionLengthInv));
719 const auto totalLocksNaive =
720 std::accumulate(locksPerDimNaive.begin(), locksPerDimNaive.end(), 1ul, std::multiplies<>());
721 // If the number of locks is within the limits everything is fine and we can return.
722 if (totalLocksNaive <= maxNumSpacialLocks) {
723 return locksPerDimNaive;
724 } else {
725 // If the number of locks grows too large, calculate the locks per dimension proportionally to the side lengths.
726 // Calculate side length relative to dimension 0.
727 const std::array<double, 3> boxSideProportions = {
728 1.,
729 boxLength[0] / boxLength[1],
730 boxLength[0] / boxLength[2],
731 };
732 // With this, calculate the number of locks the first dimension should receive.
733 const auto prodProportions =
734 std::accumulate(boxSideProportions.begin(), boxSideProportions.end(), 1., std::multiplies<>());
735 // Needs floor, otherwise we exceed the limit.
736 const auto locksInFirstDimFloat = std::floor(std::cbrt(maxNumSpacialLocks * prodProportions));
737 // From this and the proportions relative to the first dimension, we can calculate the remaining number of locks
738 const std::array<size_t, 3> locksPerDimLimited = {
739 static_cast<size_t>(locksInFirstDimFloat), // omitted div by 1
740 static_cast<size_t>(locksInFirstDimFloat / boxSideProportions[1]),
741 static_cast<size_t>(locksInFirstDimFloat / boxSideProportions[2]),
742 };
743 return locksPerDimLimited;
744 }
745 }();
746 _spatialLocks.resize(locksPerDim[0]);
747 for (auto &lockVecVec : _spatialLocks) {
748 lockVecVec.resize(locksPerDim[1]);
749 for (auto &lockVec : lockVecVec) {
750 lockVec.resize(locksPerDim[2]);
751 for (auto &lockPtr : lockVec) {
752 if (not lockPtr) {
753 lockPtr = std::make_unique<std::mutex>();
754 }
755 }
756 }
757 }
758 }
759
767 template <class Functor>
768 std::tuple<Configuration, std::unique_ptr<TraversalInterface>, bool> selectConfiguration(
769 Functor &functor, const InteractionTypeOption &interactionType);
770
775 void setCurrentContainer(std::unique_ptr<ParticleContainerInterface<Particle_T>> newContainer);
776
791 template <class Functor>
792 IterationMeasurements computeInteractions(Functor &functor, TraversalInterface &traversal);
793
804 template <class Functor>
805 void computeRemainderInteractions(Functor &functor, bool newton3, bool useSoA);
806
812 void checkMinimalSize() const;
813
814 const LogicHandlerInfo _logicHandlerInfo;
818 unsigned int _neighborListRebuildFrequency;
819
823 unsigned int _verletClusterSize;
824
828 size_t _numRebuilds{0};
829
834 size_t _numRebuildsInNonTuningPhase{0};
835
839 size_t _sortingThreshold;
840
841 std::shared_ptr<TuningManager> _tuningManager;
842
846 std::unique_ptr<ParticleContainerInterface<Particle_T>> _currentContainer{nullptr};
847
851 ContainerSelectorInfo _currentContainerSelectorInfo;
852
856 RemainderPairwiseInteractionHandler<Particle_T> _remainderPairwiseInteractionHandler;
857
861 RemainderTriwiseInteractionHandler<Particle_T> _remainderTriwiseInteractionHandler;
862
866 std::set<InteractionTypeOption> _interactionTypes{};
867
871 std::atomic<bool> _neighborListsAreValid{false};
872
876 size_t _stepsSinceLastListRebuild{0};
877
882 size_t _iteration{std::numeric_limits<size_t>::max()};
883
887 size_t _iterationAfterLastTuningPhase{0};
888
892 std::atomic<size_t> _numParticlesOwned{0ul};
893
897 std::atomic<size_t> _numParticlesHalo{0ul};
898
902 std::vector<FullParticleCell<Particle_T>> _particleBuffer;
903
907 std::vector<FullParticleCell<Particle_T>> _haloParticleBuffer;
908
914 std::vector<std::vector<std::vector<std::unique_ptr<std::mutex>>>> _spatialLocks;
915
919 IterationLogger _iterationLogger;
920
925 bool _neighborListInvalidDoDynamicRebuild{false};
926
930 void updateRebuildPositions();
931
935 LiveInfoLogger _liveInfoLogger;
936
940 FLOPLogger _flopLogger;
941};
942
943template <typename Particle_T>
945#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
946 // The owned particles in buffer are ignored because they do not rely on the structure of the particle containers,
947 // e.g. neighbour list, and these are iterated over using the region iterator. Movement of particles in buffer doesn't
948 // require a rebuild of neighbor lists.
949 AUTOPAS_OPENMP(parallel)
950 for (auto iter = this->begin(IteratorBehavior::owned | IteratorBehavior::containerOnly); iter.isValid(); ++iter) {
951 iter->resetRAtRebuild();
952 }
953#endif
954}
955
956template <typename Particle_T>
958 // check boxSize at least cutoff + skin
959 for (unsigned int dim = 0; dim < 3; ++dim) {
960 if (_currentContainer->getBoxMax()[dim] - _currentContainer->getBoxMin()[dim] <
961 _currentContainer->getInteractionLength()) {
963 "Box (boxMin[{}]={} and boxMax[{}]={}) is too small.\nHas to be at least cutoff({}) + skin({}) = {}.", dim,
964 _currentContainer->getBoxMin()[dim], dim, _currentContainer->getBoxMax()[dim], _currentContainer->getCutoff(),
965 _currentContainer->getVerletSkin(), _currentContainer->getCutoff() + _currentContainer->getVerletSkin());
966 }
967 }
968}
969
970template <typename Particle_T>
972 return _neighborListInvalidDoDynamicRebuild;
973}
974
975template <typename Particle_T>
977 if (_stepsSinceLastListRebuild >= _neighborListRebuildFrequency
978#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
979 or getNeighborListsInvalidDoDynamicRebuild()
980#endif
981 or _tuningManager->requiresRebuilding(_iteration)) {
982 _neighborListsAreValid.store(false, std::memory_order_relaxed);
983 }
984
985 return _neighborListsAreValid.load(std::memory_order_relaxed);
986}
987
988template <typename Particle_T>
990#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
991 const auto skin = getContainer().getVerletSkin();
992 // (skin/2)^2
993 const auto halfSkinSquare = skin * skin * 0.25;
994 // The owned particles in buffer are ignored because they do not rely on the structure of the particle containers,
995 // e.g. neighbour list, and these are iterated over using the region iterator. Movement of particles in buffer doesn't
996 // require a rebuild of neighbor lists.
997 AUTOPAS_OPENMP(parallel reduction(or : _neighborListInvalidDoDynamicRebuild))
998 for (auto iter = this->begin(IteratorBehavior::owned | IteratorBehavior::containerOnly); iter.isValid(); ++iter) {
999 const auto distance = iter->calculateDisplacementSinceRebuild();
1000 const double distanceSquare = utils::ArrayMath::dot(distance, distance);
1001
1002 _neighborListInvalidDoDynamicRebuild |= distanceSquare >= halfSkinSquare;
1003 }
1004#endif
1005}
1006
1007template <typename Particle_T>
1009 _neighborListInvalidDoDynamicRebuild = false;
1010}
1011
1012template <typename Particle_T>
1014 const std::vector<FullParticleCell<Particle_T>> &particleBuffers,
1015 const std::vector<FullParticleCell<Particle_T>> &haloParticleBuffers) {
1016 auto exchangeBuffer = [](const auto &newBuffers, auto &oldBuffers, auto &particleCounter) {
1017 // sanity check
1018 if (oldBuffers.size() < newBuffers.size()) {
1020 "The number of new buffers ({}) is larger than number of existing buffers ({})!", newBuffers.size(),
1021 oldBuffers.size());
1022 }
1023
1024 // we will clear the old buffers so subtract the particles from the counters.
1025 const auto numParticlesInOldBuffers =
1026 std::transform_reduce(oldBuffers.begin(), std::next(oldBuffers.begin(), newBuffers.size()), 0, std::plus<>(),
1027 [](const auto &cell) { return cell.size(); });
1028 particleCounter.fetch_sub(numParticlesInOldBuffers, std::memory_order_relaxed);
1029
1030 // clear the old buffers and copy the content of the new buffers over.
1031 size_t numParticlesInNewBuffers = 0;
1032 for (size_t i = 0; i < newBuffers.size(); ++i) {
1033 oldBuffers[i].clear();
1034 for (const auto &p : newBuffers[i]) {
1035 ++numParticlesInNewBuffers;
1036 oldBuffers[i].addParticle(p);
1037 }
1038 }
1039 // update the counters.
1040 particleCounter.fetch_add(numParticlesInNewBuffers, std::memory_order_relaxed);
1041 };
1042
1043 exchangeBuffer(particleBuffers, _particleBuffer, _numParticlesOwned);
1044 exchangeBuffer(haloParticleBuffers, _haloParticleBuffer, _numParticlesHalo);
1045}
1046
1047template <typename Particle_T>
1048std::tuple<const std::vector<FullParticleCell<Particle_T>> &, const std::vector<FullParticleCell<Particle_T>> &>
1050 return {_particleBuffer, _haloParticleBuffer};
1051}
1052
1053template <typename Particle_T>
1054template <class Functor>
1056 // Helper to derive the Functor type at compile time
1057 constexpr auto interactionType = [] {
1059 return InteractionTypeOption::pairwise;
1060 } else if (utils::isTriwiseFunctor<Functor>()) {
1061 return InteractionTypeOption::triwise;
1062 } else {
1064 "LogicHandler::computeInteractions(): Functor is not valid. Only pairwise and triwise functors are "
1065 "supported. "
1066 "Please use a functor derived from "
1067 "PairwiseFunctor or TriwiseFunctor.");
1068 }
1069 }();
1070
1071 auto &autoTuner = *_tuningManager->getAutoTuners()[interactionType];
1072 utils::Timer timerTotal;
1073 utils::Timer timerRebuild;
1074 utils::Timer timerComputeInteractions;
1075 utils::Timer timerComputeRemainder;
1076 long energyTotalRebuild;
1077
1078 const bool energyMeasurementsPossible = autoTuner.resetEnergy();
1079 timerTotal.start();
1080 timerRebuild.start();
1081 functor.initTraversal();
1082
1083 // if lists are not valid -> rebuild;
1084 if (not _neighborListsAreValid.load(std::memory_order_relaxed)) {
1085#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
1086 this->updateRebuildPositions();
1087#endif
1088 _currentContainer->rebuildNeighborLists(&traversal);
1089#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
1090 this->resetNeighborListsInvalidDoDynamicRebuild();
1091 _numRebuilds++;
1092 if (not autoTuner.inTuningPhase()) {
1093 _numRebuildsInNonTuningPhase++;
1094 }
1095#endif
1096 _neighborListsAreValid.store(true, std::memory_order_relaxed);
1097 }
1098 timerRebuild.stop();
1099 std::tie(std::ignore, std::ignore, std::ignore, energyTotalRebuild) = autoTuner.sampleEnergy();
1100
1101 timerComputeInteractions.start();
1102 _currentContainer->computeInteractions(&traversal);
1103 timerComputeInteractions.stop();
1104
1105 timerComputeRemainder.start();
1106 const bool newton3 = autoTuner.getCurrentConfig().newton3;
1107 const auto dataLayout = autoTuner.getCurrentConfig().dataLayout;
1108 computeRemainderInteractions(functor, newton3, dataLayout);
1109 timerComputeRemainder.stop();
1110
1111 functor.endTraversal(newton3);
1112
1113 const auto [energyWatts, energyJoules, energyDeltaT, energyTotal] = autoTuner.sampleEnergy();
1114 timerTotal.stop();
1115
1116 constexpr auto nanD = std::numeric_limits<double>::quiet_NaN();
1117 constexpr auto nanL = std::numeric_limits<long>::quiet_NaN();
1118 return {timerComputeInteractions.getTotalTime(),
1119 timerComputeRemainder.getTotalTime(),
1120 timerRebuild.getTotalTime(),
1121 timerTotal.getTotalTime(),
1122 energyMeasurementsPossible,
1123 energyMeasurementsPossible ? energyWatts : nanD,
1124 energyMeasurementsPossible ? energyJoules : nanD,
1125 energyMeasurementsPossible ? energyDeltaT : nanD,
1126 energyMeasurementsPossible ? energyTotalRebuild : nanL,
1127 energyMeasurementsPossible ? energyTotal - energyTotalRebuild
1128 : nanL, // ComputeInteractions + Remainder Traversal energy consumption
1129 energyMeasurementsPossible ? energyTotal : nanL};
1130}
1131
1132template <typename Particle_T>
1133template <class Functor>
1134void LogicHandler<Particle_T>::computeRemainderInteractions(Functor &functor, bool newton3, bool useSoA) {
1135 withStaticContainerType(*_currentContainer, [&](auto &actualContainerType) {
1136 if constexpr (utils::isPairwiseFunctor<Functor>()) {
1137 if (newton3) {
1138 _remainderPairwiseInteractionHandler.template computeRemainderInteractions<true>(
1139 &functor, actualContainerType, _particleBuffer, _haloParticleBuffer, useSoA);
1140 } else {
1141 _remainderPairwiseInteractionHandler.template computeRemainderInteractions<false>(
1142 &functor, actualContainerType, _particleBuffer, _haloParticleBuffer, useSoA);
1143 }
1144 } else if constexpr (utils::isTriwiseFunctor<Functor>()) {
1145 if (newton3) {
1146 _remainderTriwiseInteractionHandler.template computeRemainderInteractions<true>(
1147 &functor, actualContainerType, _particleBuffer, _haloParticleBuffer);
1148 } else {
1149 _remainderTriwiseInteractionHandler.template computeRemainderInteractions<false>(
1150 &functor, actualContainerType, _particleBuffer, _haloParticleBuffer);
1151 }
1152 }
1153 });
1154}
1155
1156template <typename Particle_T>
1157template <class Functor>
1158std::tuple<Configuration, std::unique_ptr<TraversalInterface>, bool> LogicHandler<Particle_T>::selectConfiguration(
1159 Functor &functor, const InteractionTypeOption &interactionType) {
1160 // Todo: Make LiveInfo persistent between multiple functor calls in the same timestep (e.g. 2B + 3B)
1161 // https://github.com/AutoPas/AutoPas/issues/916
1162 LiveInfo info{};
1163#ifdef AUTOPAS_LOG_LIVEINFO
1164 auto particleIter = this->begin(IteratorBehavior::ownedOrHalo);
1165 info.gather(particleIter, _neighborListRebuildFrequency, getNumberOfParticlesOwned(), _logicHandlerInfo.boxMin,
1166 _logicHandlerInfo.boxMax, _logicHandlerInfo.cutoff, _logicHandlerInfo.verletSkin);
1167 _liveInfoLogger.logLiveInfo(info, _iteration);
1168#endif
1169
1170 // if this iteration is not relevant, take the same algorithm config as before.
1171 if (not functor.isRelevantForTuning()) {
1172 auto configuration = _tuningManager->getCurrentConfig(interactionType);
1173 auto [traversalPtr, _] = isConfigurationApplicable(configuration, functor);
1174
1175 if (not traversalPtr) {
1176 // TODO: Can we handle this case gracefully?
1178 "LogicHandler: Functor {} is not relevant for tuning but the given configuration is not applicable!",
1179 functor.getName());
1180 }
1181 functor.setVecPattern(configuration.vecPattern);
1182 return {configuration, std::move(traversalPtr), false};
1183 }
1184
1185 if (_tuningManager->needsLiveInfo(_iteration)) {
1186 // If live info has not been gathered yet, gather it now and send it to the tuner.
1187 if (info.get().empty()) {
1188 auto particleIter = this->begin(IteratorBehavior::ownedOrHalo);
1189 info.gather(particleIter, _neighborListRebuildFrequency, getNumberOfParticlesOwned(), _logicHandlerInfo.boxMin,
1190 _logicHandlerInfo.boxMax, _logicHandlerInfo.cutoff, _logicHandlerInfo.verletSkin);
1191 }
1192 }
1193
1194 size_t numRejectedConfigs = 0;
1195 utils::TraceTimer selectConfigurationTimer;
1196 selectConfigurationTimer.start();
1197
1198 auto stillTuning = _tuningManager->tune(_iteration, info);
1199
1200 auto configuration = _tuningManager->getCurrentConfig(interactionType);
1201
1202 // loop as long as we don't get a valid configuration
1203 do {
1204 // applicability check also sets the container
1205 auto [traversalPtr, rejectIndefinitely] = isConfigurationApplicable(configuration, functor);
1206 if (traversalPtr) {
1207 functor.setVecPattern(configuration.vecPattern);
1208 selectConfigurationTimer.stop();
1209 AutoPasLog(TRACE, "Select Configuration took {} ms. A total of {} configurations were rejected.",
1210 selectConfigurationTimer.getTotalTime(), numRejectedConfigs);
1211 return {configuration, std::move(traversalPtr), stillTuning};
1212 }
1213 numRejectedConfigs++;
1214 // if no config is left after rejecting this one, an exception is thrown here.
1215 configuration = _tuningManager->rejectConfiguration(configuration, rejectIndefinitely, interactionType);
1216 } while (true);
1217}
1218
1219template <typename Particle_T>
1221 std::unique_ptr<ParticleContainerInterface<Particle_T>> newContainer) {
1222 // copy particles so they do not get lost when the container is switched
1223 if (_currentContainer != nullptr and newContainer != nullptr) {
1224 // with these assumptions slightly more space is reserved as numParticlesTotal already includes halos
1225 const auto numParticlesTotal = _currentContainer->size();
1226 const auto numParticlesHalo = utils::NumParticlesEstimator::estimateNumHalosUniform(
1227 numParticlesTotal, _currentContainer->getBoxMin(), _currentContainer->getBoxMax(),
1228 _currentContainer->getInteractionLength());
1229
1230 newContainer->reserve(numParticlesTotal, numParticlesHalo);
1231 for (auto particleIter = _currentContainer->begin(IteratorBehavior::ownedOrHalo); particleIter.isValid();
1232 ++particleIter) {
1233 // add a particle as inner if it is owned
1234 if (particleIter->isOwned()) {
1235 newContainer->addParticle(*particleIter);
1236 } else {
1237 newContainer->addHaloParticle(*particleIter);
1238 }
1239 }
1240 }
1241
1242 _currentContainer = std::move(newContainer);
1243}
1244
1245template <typename Particle_T>
1246template <class Functor>
1248 const InteractionTypeOption &interactionType) {
1249 if (not _interactionTypes.contains(interactionType)) {
1251 "LogicHandler::computeInteractionsPipeline(): AutPas was not initialized for the Functor's interactions type: "
1252 "{}.",
1253 interactionType);
1254 }
1256 utils::Timer tuningTimer;
1257 tuningTimer.start();
1258 const auto [configuration, traversalPtr, stillTuning] = selectConfiguration(*functor, interactionType);
1259 tuningTimer.stop();
1260 _tuningManager->logTuningResult(tuningTimer.getTotalTime(), _iteration, interactionType);
1261
1262 // Retrieve rebuild info before calling `computeInteractions()` to get the correct value.
1263 const auto rebuildIteration = not _neighborListsAreValid.load(std::memory_order_relaxed);
1264
1266 AutoPasLog(DEBUG, "Iterating with configuration: {} tuning: {}", configuration.toString(), stillTuning);
1267 const IterationMeasurements measurements = computeInteractions(*functor, *traversalPtr);
1268
1270 auto bufferSizeListing = [](const auto &buffers) -> std::string {
1271 std::stringstream ss;
1272 size_t sum = 0;
1273 for (const auto &buffer : buffers) {
1274 ss << buffer.size() << ", ";
1275 sum += buffer.size();
1276 }
1277 ss << " Total: " << sum;
1278 return ss.str();
1279 };
1280 AutoPasLog(TRACE, "particleBuffer size : {}", bufferSizeListing(_particleBuffer));
1281 AutoPasLog(TRACE, "haloParticleBuffer size : {}", bufferSizeListing(_haloParticleBuffer));
1282 AutoPasLog(DEBUG, "Type of interaction : {}", interactionType.to_string());
1283 AutoPasLog(DEBUG, "Container::computeInteractions took {} ns", measurements.timeComputeInteractions);
1284 AutoPasLog(DEBUG, "RemainderTraversal took {} ns", measurements.timeRemainderTraversal);
1285 AutoPasLog(DEBUG, "RebuildNeighborLists took {} ns", measurements.timeRebuild);
1286 AutoPasLog(DEBUG, "AutoPas::computeInteractions took {} ns", measurements.timeTotal);
1287 if (measurements.energyMeasurementsPossible) {
1288 AutoPasLog(DEBUG, "Energy Consumption: Watts: {} Joules: {} Seconds: {}", measurements.energyWatts,
1289 measurements.energyJoules, measurements.energyDeltaT);
1290 }
1291 _iterationLogger.logIteration(configuration, _iteration, functor->getName(), stillTuning, tuningTimer.getTotalTime(),
1292 measurements);
1293
1294 _flopLogger.logIteration(_iteration, functor->getNumFLOPs(), functor->getHitRate());
1295
1297 // if this was a major iteration add measurements
1298 if (functor->isRelevantForTuning()) {
1299 if (stillTuning) {
1300 // choose the metric of interest
1301 const auto measurement = [&]() {
1302 switch (_tuningManager->getTuningMetric(interactionType)) {
1304 return std::make_pair(measurements.timeRebuild,
1305 measurements.timeComputeInteractions + measurements.timeRemainderTraversal);
1307 return std::make_pair(measurements.energyTotalRebuild, measurements.energyTotalNonRebuild);
1308 default:
1309 utils::ExceptionHandler::exception("LogicHandler::computeInteractionsPipeline(): Unknown tuning metric.");
1310 return std::make_pair(0l, 0l);
1311 }
1312 }();
1313 _tuningManager->addMeasurement(measurement.first, measurement.second, rebuildIteration, _iteration,
1314 interactionType);
1315 }
1316 } else {
1317 AutoPasLog(TRACE, "Skipping adding of sample because functor is not marked relevant.");
1318 }
1319 return stillTuning;
1320}
1321
1322template <typename Particle_T>
1323template <class Functor>
1324std::tuple<std::unique_ptr<TraversalInterface>, bool> LogicHandler<Particle_T>::isConfigurationApplicable(
1325 const Configuration &config, Functor &functor) {
1326 // Check if the container supports the traversal
1327 const auto allContainerTraversals =
1328 compatibleTraversals::allCompatibleTraversals(config.container, config.interactionType);
1329 if (allContainerTraversals.find(config.traversal) == allContainerTraversals.end()) {
1330 AutoPasLog(WARN, "Configuration rejected: Container {} does not support the traversal {}.", config.container,
1331 config.traversal);
1332 return {nullptr, /*rejectIndefinitely*/ true};
1333 }
1334
1335 // Check if the functor supports the required Newton 3 mode
1336 if ((config.newton3 == Newton3Option::enabled and not functor.allowsNewton3()) or
1337 (config.newton3 == Newton3Option::disabled and not functor.allowsNonNewton3())) {
1338 AutoPasLog(DEBUG, "Configuration rejected: The functor doesn't support Newton 3 {}!", config.newton3);
1339 return {nullptr, /*rejectIndefinitely*/ true};
1340 }
1341
1342 // Check if the VectorizationPattern is supported by the functor
1343 if (not functor.isVecPatternAllowed(config.vecPattern)) {
1344 AutoPasLog(DEBUG, "Configuration rejected: The functor doesn't support the Vectorization Pattern {}!",
1345 config.vecPattern);
1346 return {nullptr, /*rejectIndefinitely*/ true};
1347 }
1348
1349 std::unique_ptr<ParticleContainerInterface<Particle_T>> containerPtr{nullptr};
1350 auto containerInfo =
1351 ContainerSelectorInfo(_currentContainer->getBoxMin(), _currentContainer->getBoxMax(),
1352 _currentContainer->getCutoff(), config.cellSizeFactor, _currentContainer->getVerletSkin(),
1353 _verletClusterSize, _sortingThreshold, config.loadEstimator);
1354
1355 // If we have no current container or needs to be updated to the new config.container, we need to generate a new
1356 // container.
1357 const bool generateNewContainer = _currentContainer == nullptr or
1358 _currentContainer->getContainerType() != config.container or
1359 containerInfo != _currentContainerSelectorInfo;
1360
1361 if (generateNewContainer) {
1362 // For now, set the local containerPtr to the new container. We do not copy the particles over and set the member
1363 // _currentContainer until after we know that the traversal is applicable to the domain.
1364 containerPtr = ContainerSelector<Particle_T>::generateContainer(config.container, containerInfo);
1365 }
1366
1367 const auto traversalInfo =
1368 generateNewContainer ? containerPtr->getTraversalSelectorInfo() : _currentContainer->getTraversalSelectorInfo();
1369
1370 // Generates a traversal if applicable, otherwise returns a nullptr
1371 auto traversalPtr =
1372 TraversalSelector::generateTraversalFromConfig<Particle_T, Functor>(config, functor, traversalInfo);
1373
1374 // If the traversal is applicable to the domain, and the configuration requires generating a new container,
1375 // update the member _currentContainer with setCurrentContainer, copying the particle data over, and update
1376 // _currentContainerSelectorInfo.
1377 if (traversalPtr and generateNewContainer) {
1378 _currentContainerSelectorInfo = containerInfo;
1379 setCurrentContainer(std::move(containerPtr));
1380 }
1381
1382 return {std::move(traversalPtr), /*rejectIndefinitely*/ false};
1383}
1384
1385} // namespace autopas
#define AutoPasLog(lvl, fmt,...)
Macro for logging providing common meta information without filename.
Definition: Logger.h:24
#define AUTOPAS_OPENMP(args)
Empty macro to throw away any arguments.
Definition: WrapOpenMP.h:126
Class containing multiple options that form an algorithm configuration for the pairwise iteration.
Definition: Configuration.h:26
LoadEstimatorOption loadEstimator
Load Estimator option.
Definition: Configuration.h:135
TraversalOption traversal
Traversal option.
Definition: Configuration.h:127
double cellSizeFactor
CellSizeFactor.
Definition: Configuration.h:147
InteractionTypeOption interactionType
Interaction type of the configuration.
Definition: Configuration.h:151
ContainerOption container
Container option.
Definition: Configuration.h:123
Newton3Option newton3
Newton 3 option.
Definition: Configuration.h:143
VectorizationPatternOption vecPattern
Vectorization Pattern option.
Definition: Configuration.h:131
Public iterator class that iterates over a particle container and additional vectors (which are typic...
Definition: ContainerIterator.h:95
Info to generate a container.
Definition: ContainerSelectorInfo.h:17
std::array< double, 3 > boxMin
Lower corner of the container.
Definition: ContainerSelectorInfo.h:93
std::array< double, 3 > boxMax
Upper corner of the container.
Definition: ContainerSelectorInfo.h:98
static std::unique_ptr< ParticleContainerInterface< Particle_T > > generateContainer(ContainerOption containerChoice, const ContainerSelectorInfo &containerInfo)
Container factory method.
Definition: ContainerSelector.h:44
Helper to log FLOP count and HitRate for AutoPas::iteratePairwise() calls with the functors in the mo...
Definition: FLOPLogger.h:30
This class handles the storage of particles in their full form.
Definition: FullParticleCell.h:26
Functor base class.
Definition: Functor.h:41
virtual size_t getNumFLOPs() const
Get the number of FLOPs.
Definition: Functor.h:192
virtual bool isVecPatternAllowed(const VectorizationPatternOption::Value vecPattern)=0
Specifies whether the functor is capable of using the specified Vectorization Pattern in the SoA func...
virtual bool allowsNewton3()=0
Specifies whether the functor is capable of Newton3-like functors.
virtual void setVecPattern(const VectorizationPatternOption::Value vecPattern)
Setter for the vectorization pattern to be used.
Definition: Functor.h:183
virtual void initTraversal()
This function is called at the start of each traversal.
Definition: Functor.h:64
virtual bool allowsNonNewton3()=0
Specifies whether the functor is capable of non-Newton3-like functors.
virtual void endTraversal(bool newton3)
This function is called at the end of each traversal.
Definition: Functor.h:71
virtual bool isRelevantForTuning()=0
Specifies whether the functor should be considered for the auto-tuning process.
virtual std::string getName()=0
Returns name of functor.
virtual double getHitRate() const
Get the hit rate.
Definition: Functor.h:202
Helper to log performance data of AutoPas::computeInteractions() to a csv file for easier analysis.
Definition: IterationLogger.h:24
Helper to log the collected LiveInfo data during tuning to a csv file for easier analysis.
Definition: LiveInfoLogger.h:23
This class is able to gather and store important information for a tuning phase from a container and ...
Definition: LiveInfo.h:33
Class that wraps all arguments for the logic handler to provide a more stable API.
Definition: LogicHandlerInfo.h:16
double verletSkin
Length added to the cutoff for the Verlet lists' skin.
Definition: LogicHandlerInfo.h:33
std::array< double, 3 > boxMax
Upper corner of the container without halo.
Definition: LogicHandlerInfo.h:25
double deltaT
Time step used in the simulation.
Definition: LogicHandlerInfo.h:46
double cutoff
Cutoff radius to be used in this simulation.
Definition: LogicHandlerInfo.h:29
std::array< double, 3 > boxMin
Lower corner of the container without halo.
Definition: LogicHandlerInfo.h:21
The LogicHandler takes care of the containers s.t.
Definition: LogicHandler.h:49
void setParticleBuffers(const std::vector< FullParticleCell< Particle_T > > &particleBuffers, const std::vector< FullParticleCell< Particle_T > > &haloParticleBuffers)
Directly exchange the internal particle and halo buffers with the given vectors and update particle c...
Definition: LogicHandler.h:1013
void reserve(size_t numParticles, size_t numHaloParticles)
Reserves space in the particle buffers and the container.
Definition: LogicHandler.h:319
std::tuple< std::unique_ptr< TraversalInterface >, bool > isConfigurationApplicable(const Configuration &config, Functor &functor)
Checks if the given configuration can be used with the given functor and the current state of the sim...
Definition: LogicHandler.h:1324
ContainerIterator< Particle_T, true, false > begin(IteratorBehavior behavior)
Iterate over all particles by using for(auto iter = autoPas.begin(); iter.isValid(); ++iter)
Definition: LogicHandler.h:501
std::vector< Particle_T > updateContainer()
Updates the container.
Definition: LogicHandler.h:165
unsigned long getNumberOfParticlesOwned() const
Get the number of owned particles.
Definition: LogicHandler.h:564
bool computeInteractionsPipeline(Functor *functor, const InteractionTypeOption &interactionType)
This function covers the full pipeline of all mechanics happening during the computation of particle ...
Definition: LogicHandler.h:1247
std::tuple< const std::vector< FullParticleCell< Particle_T > > &, const std::vector< FullParticleCell< Particle_T > > & > getParticleBuffers() const
Getter for the particle buffers.
Definition: LogicHandler.h:1049
void addParticle(const Particle_T &p)
Adds a particle to the container.
Definition: LogicHandler.h:339
void decreaseParticleCounter(Particle_T &particle)
Decrease the correct internal particle counters.
Definition: LogicHandler.h:434
std::vector< Particle_T > collectLeavingParticlesFromBuffer(bool insertOwnedParticlesToContainer)
Collects leaving particles from buffer and potentially inserts owned particles to the container.
Definition: LogicHandler.h:112
bool neighborListsAreValid()
Checks if in the next iteration the neighbor lists have to be rebuilt.
Definition: LogicHandler.h:976
unsigned long getNumberOfParticlesHalo() const
Get the number of halo particles.
Definition: LogicHandler.h:570
std::tuple< bool, bool > deleteParticleFromBuffers(Particle_T &particle)
Takes a particle, checks if it is in any of the particle buffers, and deletes it from them if found.
Definition: LogicHandler.h:412
LogicHandler(const std::shared_ptr< TuningManager > &tunerManager, const LogicHandlerInfo &logicHandlerInfo, unsigned int rebuildFrequency, const std::string &outputSuffix)
Constructor of the LogicHandler.
Definition: LogicHandler.h:58
double getVelocityMethodRFEstimate(const double skin, const double deltaT) const
Estimates the rebuild frequency based on the current maximum velocity in the container Using the form...
Definition: LogicHandler.h:645
void checkNeighborListsInvalidDoDynamicRebuild()
Checks if any particle has moved more than skin/2.
Definition: LogicHandler.h:989
Iterator::ParticleVecType gatherAdditionalVectors(IteratorBehavior behavior)
Create the additional vectors vector for a given iterator behavior.
Definition: LogicHandler.h:472
ParticleContainerInterface< Particle_T > & getContainer()
Returns a non-const reference to the currently selected particle container.
Definition: LogicHandler.h:105
void addHaloParticle(const Particle_T &haloParticle)
Adds a particle to the container that lies in the halo region of the container.
Definition: LogicHandler.h:366
ContainerIterator< Particle_T, false, true > getRegionIterator(const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner, IteratorBehavior behavior) const
Iterate over all particles in a specified region.
Definition: LogicHandler.h:540
void deleteAllParticles()
Deletes all particles.
Definition: LogicHandler.h:396
ContainerIterator< Particle_T, false, false > begin(IteratorBehavior behavior) const
Iterate over all particles by using for(auto iter = autoPas.begin(); iter.isValid(); ++iter)
Definition: LogicHandler.h:509
bool getNeighborListsInvalidDoDynamicRebuild()
getter function for _neighborListInvalidDoDynamicRebuild
Definition: LogicHandler.h:971
void resetNeighborListsInvalidDoDynamicRebuild()
Checks if any particle has moved more than skin/2.
Definition: LogicHandler.h:1008
double getMeanRebuildFrequency(bool considerOnlyLastNonTuningPhase=false) const
Getter for the mean rebuild frequency.
Definition: LogicHandler.h:622
std::vector< Particle_T > resizeBox(const std::array< double, 3 > &boxMin, const std::array< double, 3 > &boxMax)
Pass values to the actual container.
Definition: LogicHandler.h:226
void reserve(size_t numParticles)
Estimates the number of halo particles via autopas::utils::NumParticlesEstimator::estimateNumHalosUni...
Definition: LogicHandler.h:306
ContainerIterator< Particle_T, true, true > getRegionIterator(const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner, IteratorBehavior behavior)
Iterate over all particles in a specified region.
Definition: LogicHandler.h:519
The ParticleContainerInterface class provides a basic interface for all Containers within AutoPas.
Definition: ParticleContainerInterface.h:38
Handles pairwise interactions involving particle buffers (particles not yet inserted into the main co...
Definition: RemainderPairwiseInteractionHandler.h:31
Handles triwise interactions involving particle buffers (particles not yet inserted into the main con...
Definition: RemainderTriwiseInteractionHandler.h:30
This interface serves as a common parent class for all traversals.
Definition: TraversalInterface.h:18
@ energy
Optimize for least energy usage.
Definition: TuningMetricOption.h:31
@ time
Optimize for shortest simulation time.
Definition: TuningMetricOption.h:27
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:64
Timer class to stop times.
Definition: Timer.h:27
void start()
start the timer.
Definition: Timer.cpp:17
long getTotalTime() const
Get total accumulated time.
Definition: Timer.h:60
long stop()
Stops the timer and returns the time elapsed in nanoseconds since the last call to start.
Definition: Timer.cpp:25
A wrapper around autopas::utils::Timer that only compiles implementation logic if the SPDLOG_ACTIVE_L...
Definition: TraceTimer.h:20
long getTotalTime() const
Get total accumulated time.
Definition: TraceTimer.h:63
void start()
start the timer.
Definition: TraceTimer.h:25
long stop()
Stops the timer and returns the time elapsed in nanoseconds since the last call to start.
Definition: TraceTimer.h:34
void markParticleAsDeleted(Particle_T &p)
Marks a particle as deleted.
Definition: markParticleAsDeleted.h:23
constexpr T dot(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Generates the dot product of two arrays.
Definition: ArrayMath.h:233
constexpr std::array< T, SIZE > ceil(const std::array< T, SIZE > &a)
For each element in a, computes the smallest integer value not less than the element.
Definition: ArrayMath.h:316
constexpr std::array< output_t, SIZE > static_cast_copy_array(const std::array< input_t, SIZE > &a)
Creates a new array by performing an element-wise static_cast<>.
Definition: ArrayUtils.h:33
void to_string(std::ostream &os, const Container &container, const std::string &delimiter, const std::array< std::string, 2 > &surround, Fun elemToString)
Generates a string representation of a container which fulfills the Container requirement (provide cb...
Definition: ArrayUtils.h:54
size_t estimateNumHalosUniform(size_t numParticles, const std::array< double, 3 > &boxMin, const std::array< double, 3 > &boxMax, double haloWidth)
Given a number of particles and the dimensions of a box, estimate the number of halo particles.
Definition: NumParticlesEstimator.cpp:9
decltype(isTriwiseFunctorImpl(std::declval< FunctorT >())) isTriwiseFunctor
Check whether a Functor Type is inheriting from TriwiseFunctor.
Definition: checkFunctorType.h:56
bool notInBox(const std::array< T, 3 > &position, const std::array< T, 3 > &low, const std::array< T, 3 > &high)
Checks if position is not inside of a box defined by low and high.
Definition: inBox.h:50
decltype(isPairwiseFunctorImpl(std::declval< FunctorT >())) isPairwiseFunctor
Check whether a Functor Type is inheriting from PairwiseFunctor.
Definition: checkFunctorType.h:49
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
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:34
int autopas_get_max_threads()
Dummy for omp_get_max_threads() when no OpenMP is available.
Definition: WrapOpenMP.h:144
@ halo
Halo state, a particle with this state is an actual particle, but not owned by the current AutoPas ob...
@ owned
Owned state, a particle with this state is an actual particle and owned by the current AutoPas object...
decltype(auto) withStaticContainerType(ParticleContainerInterface< Particle_T > &container, FunctionType &&function)
Will execute the passed function body with the static container type of container.
Definition: StaticContainerSelector.h:35
int autopas_get_thread_num()
Dummy for omp_set_lock() when no OpenMP is available.
Definition: WrapOpenMP.h:132
Struct to collect all sorts of measurements taken during a computeInteractions iteration.
Definition: IterationMeasurements.h:13
double energyWatts
Average energy consumed per time in Watts.
Definition: IterationMeasurements.h:42
double energyDeltaT
Time in seconds during which energy was consumed.
Definition: IterationMeasurements.h:52
long timeRebuild
Time it takes for rebuilding neighbor lists.
Definition: IterationMeasurements.h:27
long timeTotal
Time it takes for the complete iteratePairwise pipeline.
Definition: IterationMeasurements.h:32
long energyTotalRebuild
Total energy consumed during rebuilding.
Definition: IterationMeasurements.h:57
long timeRemainderTraversal
Time it takes for the Remainder Traversal.
Definition: IterationMeasurements.h:22
long energyTotalNonRebuild
Total energy consumed during compute interactions and remainder traversal.
Definition: IterationMeasurements.h:62
long timeComputeInteractions
Time it takes for the LogicHandler's computeInteractions() function.
Definition: IterationMeasurements.h:17
bool energyMeasurementsPossible
Bool whether energy measurements are currently possible.
Definition: IterationMeasurements.h:37
double energyJoules
Total energy consumed in Joules.
Definition: IterationMeasurements.h:47