47template <
typename Particle_T>
57 LogicHandler(std::unordered_map<InteractionTypeOption::Value, std::unique_ptr<AutoTuner>> &autotuners,
58 const LogicHandlerInfo &logicHandlerInfo,
unsigned int rebuildFrequency,
const std::string &outputSuffix)
59 : _autoTunerRefs(autotuners),
60 _logicHandlerInfo(logicHandlerInfo),
61 _neighborListRebuildFrequency{rebuildFrequency},
64 _remainderPairwiseInteractionHandler(_spatialLocks),
65 _remainderTriwiseInteractionHandler(_spatialLocks),
66 _verletClusterSize(logicHandlerInfo.verletClusterSize),
67 _sortingThreshold(logicHandlerInfo.sortingThreshold),
68 _iterationLogger(outputSuffix, std::any_of(autotuners.
begin(), autotuners.end(),
69 [](const auto &tuner) {
return tuner.second->canMeasureEnergy(); })),
70 _flopLogger(outputSuffix),
71 _liveInfoLogger(outputSuffix) {
72 using namespace autopas::utils::ArrayMath::literals;
74 for (
const auto &[interactionType, tuner] : autotuners) {
75 _interactionTypes.insert(interactionType);
77 const auto configuration = tuner->getCurrentConfig();
82 configuration.cellSizeFactor,
86 configuration.loadEstimator};
93 const auto interactionLength = logicHandlerInfo.cutoff + logicHandlerInfo.verletSkin;
94 const auto interactionLengthInv = 1. / interactionLength;
95 const auto boxLengthWithHalo = logicHandlerInfo.boxMax - logicHandlerInfo.boxMin + (2 * interactionLength);
96 initSpatialLocks(boxLengthWithHalo, interactionLengthInv);
111 const auto &boxMin = _currentContainer->getBoxMin();
112 const auto &boxMax = _currentContainer->getBoxMax();
113 std::vector<Particle_T> leavingBufferParticles{};
114 for (
auto &cell : _particleBuffer) {
115 auto &buffer = cell._particles;
116 if (insertOwnedParticlesToContainer) {
118 for (
auto &p : buffer) {
124 _currentContainer->addParticle(p);
126 leavingBufferParticles.push_back(p);
131 for (
auto iter = buffer.begin(); iter < buffer.end();) {
134 auto fastRemoveP = [&]() {
136 std::swap(p, buffer.back());
148 if (not buffer.empty() and
utils::notInBox(p.getR(), boxMin, boxMax)) {
149 leavingBufferParticles.push_back(p);
157 return leavingBufferParticles;
164#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
169 if (_functorCalls > 0) {
171 for (
const auto &[interactionType, autoTuner] : _autoTunerRefs) {
174 if (autoTuner->inLastTuningIteration()) {
175 _iterationAtEndOfLastTuningPhase = _iteration;
177 autoTuner->bumpIterationCounters(needsToWait);
181 if (not _neighborListsAreValid.load(std::memory_order_relaxed)) {
182 _stepsSinceLastListRebuild = 0;
184 ++_stepsSinceLastListRebuild;
185 _currentContainer->setStepsSinceLastRebuild(_stepsSinceLastListRebuild);
192 AutoPasLog(TRACE,
"Initiating container update.");
193 auto leavingParticles = _currentContainer->updateContainer(not doDataStructureUpdate);
194 leavingParticles.insert(leavingParticles.end(), leavingBufferParticles.begin(), leavingBufferParticles.end());
197 _numParticlesOwned.fetch_sub(leavingParticles.size(), std::memory_order_relaxed);
199 std::for_each(_haloParticleBuffer.begin(), _haloParticleBuffer.end(), [](
auto &buffer) { buffer.clear(); });
200 _numParticlesHalo.store(0, std::memory_order_relaxed);
201 return leavingParticles;
210 std::vector<Particle_T>
resizeBox(
const std::array<double, 3> &boxMin,
const std::array<double, 3> &boxMax) {
211 using namespace autopas::utils::ArrayMath::literals;
212 const auto &oldMin = _currentContainer->getBoxMin();
213 const auto &oldMax = _currentContainer->getBoxMax();
216 if (oldMin == boxMin and oldMax == boxMax) {
221 for (
size_t i = 0; i < boxMin.size(); ++i) {
222 if (boxMin[i] >= boxMax[i]) {
224 "New box size in dimension {} is not positive!\nboxMin[{}] = {}\nboxMax[{}] = {}", i, i, boxMin[i], i,
230 const auto newLength = boxMax - boxMin;
231 const auto oldLength = oldMax - oldMin;
232 const auto relDiffLength = newLength / oldLength;
233 for (
size_t i = 0; i < newLength.size(); ++i) {
235 if (relDiffLength[i] > 1.3 or relDiffLength[i] < 0.7) {
237 "LogicHandler.resize(): Domain size changed drastically in dimension {}! Gathered AutoTuning "
238 "information might not be applicable anymore!\n"
239 "Size old box : {}\n"
240 "Size new box : {}\n"
248 _currentContainerSelectorInfo.
boxMin = boxMin;
249 _currentContainerSelectorInfo.
boxMax = boxMax;
252 std::vector<Particle_T> particlesNowOutside;
253 for (
auto pIter = _currentContainer->begin(); pIter.isValid(); ++pIter) {
255 if (not pIter->isOwned()) {
257 "LogicHandler::resizeBox() encountered non owned particle. "
258 "When calling resizeBox() these should be already deleted. "
259 "This could be solved by calling updateContainer() before resizeBox().");
263 particlesNowOutside.push_back(*pIter);
271 _currentContainerSelectorInfo);
272 setCurrentContainer(std::move(newContainer));
274 const auto boxLength = boxMax - boxMin;
275 const auto interactionLengthInv = 1. / _currentContainer->getInteractionLength();
276 initSpatialLocks(boxLength, interactionLengthInv);
279 _neighborListsAreValid.store(
false, std::memory_order_relaxed);
281 return particlesNowOutside;
292 numParticles, _currentContainer->getBoxMin(), _currentContainer->getBoxMax(),
293 _currentContainer->getInteractionLength());
294 reserve(numParticles, numParticlesHaloEstimate);
303 void reserve(
size_t numParticles,
size_t numHaloParticles) {
304 const auto numHaloParticlesPerBuffer = numHaloParticles / _haloParticleBuffer.size();
305 for (
auto &buffer : _haloParticleBuffer) {
306 buffer.reserve(numHaloParticlesPerBuffer);
309 for (
auto &buffer : _particleBuffer) {
310 buffer.reserve(numHaloParticlesPerBuffer);
315 if (not _neighborListsAreValid.load(std::memory_order_relaxed)) {
316 _currentContainer->reserve(numParticles, numHaloParticles);
325 const auto &boxMin = _currentContainer->getBoxMin();
326 const auto &boxMax = _currentContainer->getBoxMax();
329 "LogicHandler: Trying to add a particle that is not in the bounding box.\n"
333 boxMin, boxMax, p.toString());
335 Particle_T particleCopy = p;
337 if (not _neighborListsAreValid.load(std::memory_order_relaxed)) {
339 _currentContainer->template addParticle<false>(particleCopy);
344 _numParticlesOwned.fetch_add(1, std::memory_order_relaxed);
351 const auto &boxMin = _currentContainer->getBoxMin();
352 const auto &boxMax = _currentContainer->getBoxMax();
353 Particle_T haloParticleCopy = haloParticle;
354 if (
utils::inBox(haloParticleCopy.getR(), boxMin, boxMax)) {
356 "LogicHandler: Trying to add a halo particle that is not outside the box of the container.\n"
363 if (not _neighborListsAreValid.load(std::memory_order_relaxed)) {
368 bool updated = _currentContainer->updateHaloParticle(haloParticleCopy);
374 _numParticlesHalo.fetch_add(1, std::memory_order_relaxed);
381 _neighborListsAreValid.store(
false, std::memory_order_relaxed);
382 _currentContainer->deleteAllParticles();
383 std::for_each(_particleBuffer.begin(), _particleBuffer.end(), [](
auto &buffer) { buffer.clear(); });
384 std::for_each(_haloParticleBuffer.begin(), _haloParticleBuffer.end(), [](
auto &buffer) { buffer.clear(); });
386 _numParticlesOwned.store(0, std::memory_order_relaxed);
387 _numParticlesHalo.store(0, std::memory_order_relaxed);
398 auto &bufferCollection = particle.isOwned() ? _particleBuffer : _haloParticleBuffer;
399 for (
auto &cell : bufferCollection) {
400 auto &buffer = cell._particles;
402 if (not buffer.empty() and &(buffer.front()) <= &particle and &particle <= &(buffer.back())) {
403 const bool isRearParticle = &particle == &buffer.back();
405 particle = buffer.back();
407 return {
true, not isRearParticle};
410 return {
false,
true};
419 if (particle.isOwned()) {
420 _numParticlesOwned.fetch_sub(1, std::memory_order_relaxed);
422 _numParticlesHalo.fetch_sub(1, std::memory_order_relaxed);
446 template <
class Functor>
455 template <
class Iterator>
457 typename Iterator::ParticleVecType additionalVectors;
458 if (not(behavior & IteratorBehavior::containerOnly)) {
459 additionalVectors.reserve(
static_cast<bool>(behavior & IteratorBehavior::owned) * _particleBuffer.size() +
460 static_cast<bool>(behavior & IteratorBehavior::halo) * _haloParticleBuffer.size());
461 if (behavior & IteratorBehavior::owned) {
462 for (
auto &buffer : _particleBuffer) {
466 if (not buffer.isEmpty()) {
467 additionalVectors.push_back(&(buffer._particles));
471 if (behavior & IteratorBehavior::halo) {
472 for (
auto &buffer : _haloParticleBuffer) {
473 if (not buffer.isEmpty()) {
474 additionalVectors.push_back(&(buffer._particles));
479 return additionalVectors;
486 auto additionalVectors = gatherAdditionalVectors<ContainerIterator<Particle_T, true, false>>(behavior);
487 return _currentContainer->begin(behavior, std::ref(additionalVectors));
494 auto additionalVectors =
497 return _currentContainer->begin(behavior, std::ref(additionalVectors));
504 const std::array<double, 3> &higherCorner,
505 IteratorBehavior behavior) {
507 for (
size_t d = 0; d < 3; ++d) {
508 if (lowerCorner[d] > higherCorner[d]) {
510 "Requesting region Iterator where the upper corner is lower than the lower corner!\n"
513 lowerCorner, higherCorner);
517 auto additionalVectors = gatherAdditionalVectors<ContainerIterator<Particle_T, true, true>>(behavior);
518 return _currentContainer->getRegionIterator(lowerCorner, higherCorner, behavior, std::ref(additionalVectors));
525 const std::array<double, 3> &higherCorner,
526 IteratorBehavior behavior)
const {
528 for (
size_t d = 0; d < 3; ++d) {
529 if (lowerCorner[d] > higherCorner[d]) {
531 "Requesting region Iterator where the upper corner is lower than the lower corner!\n"
534 lowerCorner, higherCorner);
538 auto additionalVectors =
540 return std::as_const(_currentContainer)
541 ->getRegionIterator(lowerCorner, higherCorner, behavior, std::ref(additionalVectors));
564 return std::any_of(std::begin(_autoTunerRefs), std::end(_autoTunerRefs), [&](
const auto &entry) {
565 return not(entry.first == interactionType) and entry.second->inTuningPhase();
581 template <
class Functor>
605 std::tuple<const std::vector<FullParticleCell<Particle_T>> &,
const std::vector<FullParticleCell<Particle_T>> &>
620#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
621 const auto numRebuilds = considerOnlyLastNonTuningPhase ? _numRebuildsInNonTuningPhase : _numRebuilds;
623 const auto iterationCount =
624 considerOnlyLastNonTuningPhase ? _iteration - _iterationAtEndOfLastTuningPhase : _iteration + 1;
625 if (numRebuilds == 0) {
626 return static_cast<double>(_neighborListRebuildFrequency);
628 return static_cast<double>(iterationCount) / numRebuilds;
631 return static_cast<double>(_neighborListRebuildFrequency);
645 double maxVelocity = 0;
648 for (
auto iter = this->
begin(IteratorBehavior::owned | IteratorBehavior::containerOnly); iter.isValid(); ++iter) {
649 std::array<double, 3> tempVel = iter->getV();
650 double tempVelAbs = sqrt(dot(tempVel, tempVel));
651 maxVelocity = std::max(tempVelAbs, maxVelocity);
654 return skin / maxVelocity / deltaT / 2;
697 void initSpatialLocks(
const std::array<double, 3> &boxLength,
double interactionLengthInv) {
698 using namespace autopas::utils::ArrayMath::literals;
708 constexpr size_t maxNumSpacialLocks{1000000};
711 const std::array<size_t, 3> locksPerDim = [&]() {
714 const std::array<size_t, 3> locksPerDimNaive =
715 static_cast_copy_array<size_t>(ceil(boxLength * interactionLengthInv));
716 const auto totalLocksNaive =
717 std::accumulate(locksPerDimNaive.begin(), locksPerDimNaive.end(), 1ul, std::multiplies<>());
719 if (totalLocksNaive <= maxNumSpacialLocks) {
720 return locksPerDimNaive;
724 const std::array<double, 3> boxSideProportions = {
726 boxLength[0] / boxLength[1],
727 boxLength[0] / boxLength[2],
730 const auto prodProportions =
731 std::accumulate(boxSideProportions.begin(), boxSideProportions.end(), 1., std::multiplies<>());
733 const auto locksInFirstDimFloat = std::floor(std::cbrt(maxNumSpacialLocks * prodProportions));
735 const std::array<size_t, 3> locksPerDimLimited = {
736 static_cast<size_t>(locksInFirstDimFloat),
737 static_cast<size_t>(locksInFirstDimFloat / boxSideProportions[1]),
738 static_cast<size_t>(locksInFirstDimFloat / boxSideProportions[2]),
740 return locksPerDimLimited;
743 _spatialLocks.resize(locksPerDim[0]);
744 for (
auto &lockVecVec : _spatialLocks) {
745 lockVecVec.resize(locksPerDim[1]);
746 for (
auto &lockVec : lockVecVec) {
747 lockVec.resize(locksPerDim[2]);
748 for (
auto &lockPtr : lockVec) {
750 lockPtr = std::make_unique<std::mutex>();
764 template <
class Functor>
765 std::tuple<Configuration, std::unique_ptr<TraversalInterface>,
bool> selectConfiguration(
766 Functor &functor,
const InteractionTypeOption &interactionType);
788 template <
class Functor>
801 template <
class Functor>
802 void computeRemainderInteractions(
Functor &functor,
bool newton3,
bool useSoA);
809 void checkMinimalSize()
const;
815 unsigned int _neighborListRebuildFrequency;
820 unsigned int _verletClusterSize;
825 size_t _numRebuilds{0};
831 size_t _numRebuildsInNonTuningPhase{0};
836 size_t _sortingThreshold;
841 std::unordered_map<InteractionTypeOption::Value, std::unique_ptr<AutoTuner>> &_autoTunerRefs;
846 std::unique_ptr<ParticleContainerInterface<Particle_T>> _currentContainer{
nullptr};
866 std::set<InteractionTypeOption> _interactionTypes{};
871 std::atomic<bool> _neighborListsAreValid{
false};
876 unsigned int _stepsSinceLastListRebuild{0};
881 unsigned int _functorCalls{0};
886 unsigned int _iteration{0};
891 unsigned int _iterationAtEndOfLastTuningPhase{0};
896 std::atomic<size_t> _numParticlesOwned{0ul};
901 std::atomic<size_t> _numParticlesHalo{0ul};
906 std::vector<FullParticleCell<Particle_T>> _particleBuffer;
911 std::vector<FullParticleCell<Particle_T>> _haloParticleBuffer;
918 std::vector<std::vector<std::vector<std::unique_ptr<std::mutex>>>> _spatialLocks;
929 bool _neighborListInvalidDoDynamicRebuild{
false};
934 void updateRebuildPositions();
947template <
typename Particle_T>
949#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
954 for (
auto iter = this->begin(IteratorBehavior::owned | IteratorBehavior::containerOnly); iter.isValid(); ++iter) {
955 iter->resetRAtRebuild();
960template <
typename Particle_T>
963 for (
unsigned int dim = 0; dim < 3; ++dim) {
964 if (_currentContainer->getBoxMax()[dim] - _currentContainer->getBoxMin()[dim] <
965 _currentContainer->getInteractionLength()) {
967 "Box (boxMin[{}]={} and boxMax[{}]={}) is too small.\nHas to be at least cutoff({}) + skin({}) = {}.", dim,
968 _currentContainer->getBoxMin()[dim], dim, _currentContainer->getBoxMax()[dim], _currentContainer->getCutoff(),
969 _currentContainer->getVerletSkin(), _currentContainer->getCutoff() + _currentContainer->getVerletSkin());
974template <
typename Particle_T>
976 return _neighborListInvalidDoDynamicRebuild;
979template <
typename Particle_T>
982 const auto needRebuild = [&](
const InteractionTypeOption &interactionOption) {
983 return _interactionTypes.count(interactionOption) != 0 and
984 _autoTunerRefs[interactionOption]->willRebuildNeighborLists();
987 if (_stepsSinceLastListRebuild >= _neighborListRebuildFrequency
988#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
989 or getNeighborListsInvalidDoDynamicRebuild()
991 or needRebuild(InteractionTypeOption::pairwise) or needRebuild(InteractionTypeOption::triwise)) {
992 _neighborListsAreValid.store(
false, std::memory_order_relaxed);
995 return _neighborListsAreValid.load(std::memory_order_relaxed);
998template <
typename Particle_T>
1000#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
1001 const auto skin = getContainer().getVerletSkin();
1003 const auto halfSkinSquare = skin * skin * 0.25;
1007 AUTOPAS_OPENMP(parallel reduction(or : _neighborListInvalidDoDynamicRebuild))
1008 for (
auto iter = this->begin(IteratorBehavior::owned | IteratorBehavior::containerOnly); iter.isValid(); ++iter) {
1009 const auto distance = iter->calculateDisplacementSinceRebuild();
1012 _neighborListInvalidDoDynamicRebuild |= distanceSquare >= halfSkinSquare;
1017template <
typename Particle_T>
1019 _neighborListInvalidDoDynamicRebuild =
false;
1022template <
typename Particle_T>
1026 auto exchangeBuffer = [](
const auto &newBuffers,
auto &oldBuffers,
auto &particleCounter) {
1028 if (oldBuffers.size() < newBuffers.size()) {
1030 "The number of new buffers ({}) is larger than number of existing buffers ({})!", newBuffers.size(),
1035 const auto numParticlesInOldBuffers =
1036 std::transform_reduce(oldBuffers.begin(), std::next(oldBuffers.begin(), newBuffers.size()), 0, std::plus<>(),
1037 [](
const auto &cell) { return cell.size(); });
1038 particleCounter.fetch_sub(numParticlesInOldBuffers, std::memory_order_relaxed);
1041 size_t numParticlesInNewBuffers = 0;
1042 for (
size_t i = 0; i < newBuffers.size(); ++i) {
1043 oldBuffers[i].clear();
1044 for (
const auto &p : newBuffers[i]) {
1045 ++numParticlesInNewBuffers;
1046 oldBuffers[i].addParticle(p);
1050 particleCounter.fetch_add(numParticlesInNewBuffers, std::memory_order_relaxed);
1053 exchangeBuffer(particleBuffers, _particleBuffer, _numParticlesOwned);
1054 exchangeBuffer(haloParticleBuffers, _haloParticleBuffer, _numParticlesHalo);
1057template <
typename Particle_T>
1058std::tuple<const std::vector<FullParticleCell<Particle_T>> &,
const std::vector<FullParticleCell<Particle_T>> &>
1060 return {_particleBuffer, _haloParticleBuffer};
1063template <
typename Particle_T>
1064template <
class Functor>
1067 constexpr auto interactionType = [] {
1069 return InteractionTypeOption::pairwise;
1071 return InteractionTypeOption::triwise;
1074 "LogicHandler::computeInteractions(): Functor is not valid. Only pairwise and triwise functors are "
1076 "Please use a functor derived from "
1077 "PairwiseFunctor or TriwiseFunctor.");
1081 auto &autoTuner = *_autoTunerRefs[interactionType];
1082#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
1083 if (autoTuner.inFirstTuningIteration()) {
1084 _numRebuildsInNonTuningPhase = 0;
1093 if (autoTuner.inFirstConfigurationLastSample()) {
1096 double rebuildFrequencyEstimate =
1097 getVelocityMethodRFEstimate(_logicHandlerInfo.verletSkin, _logicHandlerInfo.deltaT);
1098 double userProvidedRF =
static_cast<double>(_neighborListRebuildFrequency);
1103 if (rebuildFrequencyEstimate > userProvidedRF) {
1104 autoTuner.setRebuildFrequency(userProvidedRF);
1106 autoTuner.setRebuildFrequency(rebuildFrequencyEstimate);
1114 long energyTotalRebuild;
1116 const bool energyMeasurementsPossible = autoTuner.resetEnergy();
1118 timerRebuild.
start();
1122 if (not _neighborListsAreValid.load(std::memory_order_relaxed)) {
1123#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
1124 this->updateRebuildPositions();
1126 _currentContainer->rebuildNeighborLists(&traversal);
1127#ifdef AUTOPAS_ENABLE_DYNAMIC_CONTAINERS
1128 this->resetNeighborListsInvalidDoDynamicRebuild();
1130 if (not autoTuner.inTuningPhase()) {
1131 _numRebuildsInNonTuningPhase++;
1134 _neighborListsAreValid.store(
true, std::memory_order_relaxed);
1136 timerRebuild.
stop();
1137 std::tie(std::ignore, std::ignore, std::ignore, energyTotalRebuild) = autoTuner.sampleEnergy();
1139 timerComputeInteractions.
start();
1140 _currentContainer->computeInteractions(&traversal);
1141 timerComputeInteractions.
stop();
1143 timerComputeRemainder.
start();
1144 const bool newton3 = autoTuner.getCurrentConfig().newton3;
1145 const auto dataLayout = autoTuner.getCurrentConfig().dataLayout;
1146 computeRemainderInteractions(functor, newton3, dataLayout);
1147 timerComputeRemainder.
stop();
1151 const auto [energyWatts, energyJoules, energyDeltaT, energyTotal] = autoTuner.sampleEnergy();
1154 constexpr auto nanD = std::numeric_limits<double>::quiet_NaN();
1155 constexpr auto nanL = std::numeric_limits<long>::quiet_NaN();
1160 energyMeasurementsPossible,
1161 energyMeasurementsPossible ? energyWatts : nanD,
1162 energyMeasurementsPossible ? energyJoules : nanD,
1163 energyMeasurementsPossible ? energyDeltaT : nanD,
1164 energyMeasurementsPossible ? energyTotalRebuild : nanL,
1165 energyMeasurementsPossible ? energyTotal - energyTotalRebuild
1167 energyMeasurementsPossible ? energyTotal : nanL};
1170template <
typename Particle_T>
1171template <
class Functor>
1176 _remainderPairwiseInteractionHandler.template computeRemainderInteractions<true>(
1177 &functor, actualContainerType, _particleBuffer, _haloParticleBuffer, useSoA);
1179 _remainderPairwiseInteractionHandler.template computeRemainderInteractions<false>(
1180 &functor, actualContainerType, _particleBuffer, _haloParticleBuffer, useSoA);
1184 _remainderTriwiseInteractionHandler.template computeRemainderInteractions<true>(
1185 &functor, actualContainerType, _particleBuffer, _haloParticleBuffer);
1187 _remainderTriwiseInteractionHandler.template computeRemainderInteractions<false>(
1188 &functor, actualContainerType, _particleBuffer, _haloParticleBuffer);
1194template <
typename Particle_T>
1195template <
class Functor>
1197 Functor &functor,
const InteractionTypeOption &interactionType) {
1198 auto &autoTuner = *_autoTunerRefs[interactionType];
1203#ifdef AUTOPAS_LOG_LIVEINFO
1204 auto particleIter = this->begin(IteratorBehavior::ownedOrHalo);
1205 info.gather(particleIter, _neighborListRebuildFrequency, getNumberOfParticlesOwned(), _logicHandlerInfo.boxMin,
1206 _logicHandlerInfo.boxMax, _logicHandlerInfo.cutoff, _logicHandlerInfo.verletSkin);
1207 _liveInfoLogger.logLiveInfo(info, _iteration);
1212 auto configuration = autoTuner.getCurrentConfig();
1213 auto [traversalPtr, _] = isConfigurationApplicable(configuration, functor);
1215 if (not traversalPtr) {
1218 "LogicHandler: Functor {} is not relevant for tuning but the given configuration is not applicable!",
1221 return {configuration, std::move(traversalPtr),
false};
1224 if (autoTuner.needsLiveInfo()) {
1226 if (info.get().empty()) {
1227 auto particleIter = this->begin(IteratorBehavior::ownedOrHalo);
1228 info.gather(particleIter, _neighborListRebuildFrequency, getNumberOfParticlesOwned(), _logicHandlerInfo.boxMin,
1229 _logicHandlerInfo.boxMax, _logicHandlerInfo.cutoff, _logicHandlerInfo.verletSkin);
1231 autoTuner.receiveLiveInfo(info);
1234 size_t numRejectedConfigs = 0;
1236 selectConfigurationTimer.
start();
1238 auto [configuration, stillTuning] = autoTuner.getNextConfig();
1243 auto [traversalPtr, rejectIndefinitely] = isConfigurationApplicable(configuration, functor);
1245 selectConfigurationTimer.
stop();
1246 AutoPasLog(TRACE,
"Select Configuration took {} ms. A total of {} configurations were rejected.",
1247 selectConfigurationTimer.
getTotalTime(), numRejectedConfigs);
1248 return {configuration, std::move(traversalPtr), stillTuning};
1251 numRejectedConfigs++;
1252 std::tie(configuration, stillTuning) = autoTuner.rejectConfig(configuration, rejectIndefinitely);
1256template <
typename Particle_T>
1260 if (_currentContainer !=
nullptr and newContainer !=
nullptr) {
1262 const auto numParticlesTotal = _currentContainer->size();
1264 numParticlesTotal, _currentContainer->getBoxMin(), _currentContainer->getBoxMax(),
1265 _currentContainer->getInteractionLength());
1267 newContainer->reserve(numParticlesTotal, numParticlesHalo);
1268 for (
auto particleIter = _currentContainer->begin(IteratorBehavior::ownedOrHalo); particleIter.isValid();
1271 if (particleIter->isOwned()) {
1272 newContainer->addParticle(*particleIter);
1274 newContainer->addHaloParticle(*particleIter);
1279 _currentContainer = std::move(newContainer);
1282template <
typename Particle_T>
1283template <
class Functor>
1285 const InteractionTypeOption &interactionType) {
1286 if (not _interactionTypes.count(interactionType)) {
1288 "LogicHandler::computeInteractionsPipeline(): AutPas was not initialized for the Functor's interactions type: "
1294 tuningTimer.
start();
1295 const auto [configuration, traversalPtr, stillTuning] = selectConfiguration(*functor, interactionType);
1297 auto &autoTuner = *_autoTunerRefs[interactionType];
1298 autoTuner.logTuningResult(stillTuning, tuningTimer.
getTotalTime());
1301 const auto rebuildIteration = not _neighborListsAreValid.load(std::memory_order_relaxed);
1304 AutoPasLog(DEBUG,
"Iterating with configuration: {} tuning: {}", configuration.toString(), stillTuning);
1308 auto bufferSizeListing = [](
const auto &buffers) -> std::string {
1309 std::stringstream ss;
1311 for (
const auto &buffer : buffers) {
1312 ss << buffer.size() <<
", ";
1313 sum += buffer.size();
1315 ss <<
" Total: " << sum;
1318 AutoPasLog(TRACE,
"particleBuffer size : {}", bufferSizeListing(_particleBuffer));
1319 AutoPasLog(TRACE,
"haloParticleBuffer size : {}", bufferSizeListing(_haloParticleBuffer));
1320 AutoPasLog(DEBUG,
"Type of interaction : {}", interactionType.to_string());
1329 _iterationLogger.logIteration(configuration, _iteration, functor->
getName(), stillTuning, tuningTimer.
getTotalTime(),
1339 const auto measurement = [&]() {
1340 switch (autoTuner.getTuningMetric()) {
1348 return std::make_pair(0l, 0l);
1351 autoTuner.addMeasurement(measurement.first, measurement.second, rebuildIteration);
1354 AutoPasLog(TRACE,
"Skipping adding of sample because functor is not marked relevant.");
1360template <
typename Particle_T>
1361template <
class Functor>
1365 const auto allContainerTraversals =
1367 if (allContainerTraversals.find(config.
traversal) == allContainerTraversals.end()) {
1368 AutoPasLog(WARN,
"Configuration rejected: Container {} does not support the traversal {}.", config.
container,
1370 return {
nullptr,
true};
1376 AutoPasLog(DEBUG,
"Configuration rejected: The functor doesn't support Newton 3 {}!", config.
newton3);
1377 return {
nullptr,
true};
1380 std::unique_ptr<ParticleContainerInterface<Particle_T>> containerPtr{
nullptr};
1381 auto containerInfo =
1383 _currentContainer->getCutoff(), config.
cellSizeFactor, _currentContainer->getVerletSkin(),
1384 _verletClusterSize, _sortingThreshold, config.
loadEstimator);
1388 const bool generateNewContainer = _currentContainer ==
nullptr or
1389 _currentContainer->getContainerType() != config.
container or
1390 containerInfo != _currentContainerSelectorInfo;
1392 if (generateNewContainer) {
1398 const auto traversalInfo =
1399 generateNewContainer ? containerPtr->getTraversalSelectorInfo() : _currentContainer->getTraversalSelectorInfo();
1403 TraversalSelector::generateTraversalFromConfig<Particle_T, Functor>(config, functor, traversalInfo);
1408 if (traversalPtr and generateNewContainer) {
1409 _currentContainerSelectorInfo = containerInfo;
1410 setCurrentContainer(std::move(containerPtr));
1413 return {std::move(traversalPtr),
false};
#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:24
LoadEstimatorOption loadEstimator
Load Estimator option.
Definition: Configuration.h:126
TraversalOption traversal
Traversal option.
Definition: Configuration.h:122
double cellSizeFactor
CellSizeFactor.
Definition: Configuration.h:138
InteractionTypeOption interactionType
Interaction type of the configuration.
Definition: Configuration.h:142
ContainerOption container
Container option.
Definition: Configuration.h:118
Newton3Option newton3
Newton 3 option.
Definition: Configuration.h:134
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:40
virtual size_t getNumFLOPs() const
Get the number of FLOPs.
Definition: Functor.h:177
virtual bool allowsNewton3()=0
Specifies whether the functor is capable of Newton3-like functors.
virtual void initTraversal()
This function is called at the start of each traversal.
Definition: Functor.h:63
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:70
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:187
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 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:48
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:1023
void reserve(size_t numParticles, size_t numHaloParticles)
Reserves space in the particle buffers and the container.
Definition: LogicHandler.h:303
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:1362
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:485
std::vector< Particle_T > updateContainer()
Updates the container.
Definition: LogicHandler.h:163
unsigned long getNumberOfParticlesOwned() const
Get the number of owned particles.
Definition: LogicHandler.h:548
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:1284
std::tuple< const std::vector< FullParticleCell< Particle_T > > &, const std::vector< FullParticleCell< Particle_T > > & > getParticleBuffers() const
Getter for the particle buffers.
Definition: LogicHandler.h:1059
void addParticle(const Particle_T &p)
Adds a particle to the container.
Definition: LogicHandler.h:323
void decreaseParticleCounter(Particle_T &particle)
Decrease the correct internal particle counters.
Definition: LogicHandler.h:418
std::vector< Particle_T > collectLeavingParticlesFromBuffer(bool insertOwnedParticlesToContainer)
Collects leaving particles from buffer and potentially inserts owned particles to the container.
Definition: LogicHandler.h:110
LogicHandler(std::unordered_map< InteractionTypeOption::Value, std::unique_ptr< AutoTuner > > &autotuners, const LogicHandlerInfo &logicHandlerInfo, unsigned int rebuildFrequency, const std::string &outputSuffix)
Constructor of the LogicHandler.
Definition: LogicHandler.h:57
bool neighborListsAreValid()
Checks if in the next iteration the neighbor lists have to be rebuilt.
Definition: LogicHandler.h:980
unsigned long getNumberOfParticlesHalo() const
Get the number of halo particles.
Definition: LogicHandler.h:554
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:396
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:642
bool checkTuningStates(const InteractionTypeOption &interactionType)
Check if other autotuners for any other interaction types are still in a tuning phase.
Definition: LogicHandler.h:561
void checkNeighborListsInvalidDoDynamicRebuild()
Checks if any particle has moved more than skin/2.
Definition: LogicHandler.h:999
Iterator::ParticleVecType gatherAdditionalVectors(IteratorBehavior behavior)
Create the additional vectors vector for a given iterator behavior.
Definition: LogicHandler.h:456
ParticleContainerInterface< Particle_T > & getContainer()
Returns a non-const reference to the currently selected particle container.
Definition: LogicHandler.h:103
void addHaloParticle(const Particle_T &haloParticle)
Adds a particle to the container that lies in the halo region of the container.
Definition: LogicHandler.h:350
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:524
void deleteAllParticles()
Deletes all particles.
Definition: LogicHandler.h:380
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:493
bool getNeighborListsInvalidDoDynamicRebuild()
getter function for _neighborListInvalidDoDynamicRebuild
Definition: LogicHandler.h:975
void resetNeighborListsInvalidDoDynamicRebuild()
Checks if any particle has moved more than skin/2.
Definition: LogicHandler.h:1018
double getMeanRebuildFrequency(bool considerOnlyLastNonTuningPhase=false) const
Getter for the mean rebuild frequency.
Definition: LogicHandler.h:619
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:210
void reserve(size_t numParticles)
Estimates the number of halo particles via autopas::utils::NumParticlesEstimator::estimateNumHalosUni...
Definition: LogicHandler.h:290
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:503
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:63
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:32
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