AutoPas  3.0.0
Loading...
Searching...
No Matches
AutoPasImpl.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <array>
10#include <memory>
11#include <ostream>
12#include <type_traits>
13#include <vector>
14
15// The LogicHandler includes dependencies to wide parts of AutoPas, making it expensive to compile and thus is moved
16// here from AutoPasDecl.h.
17#include "autopas/AutoPasDecl.h"
19#include "autopas/Version.h"
31
32namespace autopas {
33
34template <class Particle_T>
35AutoPas<Particle_T>::AutoPas(std::ostream &logOutputStream) {
36 Logger::create(logOutputStream);
37}
38
39template <class Particle_T>
40AutoPas<Particle_T>::AutoPas(const std::string &logFileName) {
41 Logger::create(logFileName);
42}
43
44template <class Particle_T>
46
47template <class Particle_T>
49 _tuningManager = std::move(other._tuningManager);
50 _logicHandler = std::move(other._logicHandler);
51 return *this;
52}
53
54template <class Particle_T>
56 int myRank{};
58 if (myRank == 0) {
59 AutoPasLog(INFO, "AutoPas Version: {}", AutoPas_VERSION);
60 AutoPasLog(INFO, "Compiled with : {}", utils::CompileInfo::getCompilerInfo());
61 }
62
63 if (_tuningStrategyFactoryInfo.autopasMpiCommunicator == AUTOPAS_MPI_COMM_NULL) {
64 AutoPas_MPI_Comm_dup(AUTOPAS_MPI_COMM_WORLD, &_tuningStrategyFactoryInfo.autopasMpiCommunicator);
65 } else {
66 _externalMPICommunicator = true;
67 }
68 if (std::find(_tuningStrategyOptions.begin(), _tuningStrategyOptions.end(),
69 TuningStrategyOption::mpiDivideAndConquer) != _tuningStrategyOptions.end()) {
70 _tuningStrategyFactoryInfo.mpiDivideAndConquer = true;
71 }
72
73 _logicHandlerInfo.sortingThreshold = _sortingThreshold;
74
75 // If an interval was given for the cell size factor, change it to the relevant values.
76 // Don't modify _allowedCellSizeFactors to preserve the initial (type) information.
77 const auto cellSizeFactors = [&]() -> NumberSetFinite<double> {
78 if (const auto *csfIntervalPtr = dynamic_cast<NumberInterval<double> *>(_allowedCellSizeFactors.get())) {
79 const auto interactionLength = _logicHandlerInfo.cutoff * _logicHandlerInfo.verletSkin;
80 const auto boxLengthX = _logicHandlerInfo.boxMax[0] - _logicHandlerInfo.boxMin[0];
81 return {SearchSpaceGenerators::calculateRelevantCsfs(*csfIntervalPtr, interactionLength, boxLengthX)};
82 } else {
83 // in this case _allowedCellSizeFactors is a finite set
84 return {_allowedCellSizeFactors->getAll()};
85 }
86 }();
87
88 _tuningManager = std::make_shared<TuningManager>(_autoTunerInfo);
89 // Create autotuners for each interaction type
90 for (const auto &interactionType : _allowedInteractionTypeOptions) {
91 const auto searchSpace = SearchSpaceGenerators::cartesianProduct(
92 _allowedContainers, _allowedTraversals[interactionType], _allowedLoadEstimators,
93 _allowedDataLayouts[interactionType], _allowedNewton3Options[interactionType], &cellSizeFactors,
94 _allowedVecPatternsOptions[interactionType], interactionType);
95
97 tuningStrategies.reserve(_tuningStrategyOptions.size());
98 for (const auto &strategy : _tuningStrategyOptions) {
99 tuningStrategies.emplace_back(TuningStrategyFactory::generateTuningStrategy(
100 searchSpace, strategy, _tuningStrategyFactoryInfo, interactionType, _outputSuffix));
101 }
102 if (_useTuningStrategyLoggerProxy) {
103 tuningStrategies.emplace_back(std::make_unique<TuningStrategyLogger>(_outputSuffix));
104 }
105 auto tunerOutputSuffix = _outputSuffix + "_" + interactionType.to_string();
106 _tuningManager->addAutoTuner(std::make_unique<AutoTuner>(tuningStrategies, searchSpace, _autoTunerInfo,
107 _verletRebuildFrequency, tunerOutputSuffix),
108 interactionType);
109 }
110
111 // Create logic handler
112 _logicHandler = std::make_unique<std::remove_reference_t<decltype(*_logicHandler)>>(
113 _tuningManager, _logicHandlerInfo, _verletRebuildFrequency, _outputSuffix);
114}
115
116template <class Particle_T>
117template <class Functor>
119 static_assert(
120 not std::is_same_v<Functor, autopas::Functor<Particle_T, Functor>>,
121 "The static type of Functor in computeInteractions is not allowed to be autopas::Functor. Please use the "
122 "derived type instead, e.g. by using a dynamic_cast.");
123 if (f->getCutoff() > this->getCutoff()) {
124 utils::ExceptionHandler::exception("Functor cutoff ({}) must not be larger than container cutoff ({})",
125 f->getCutoff(), this->getCutoff());
126 }
127
128 if constexpr (utils::isPairwiseFunctor<Functor>()) {
129 return _logicHandler->template computeInteractionsPipeline<Functor>(f, InteractionTypeOption::pairwise);
130 } else if constexpr (utils::isTriwiseFunctor<Functor>()) {
131 return _logicHandler->template computeInteractionsPipeline<Functor>(f, InteractionTypeOption::triwise);
132 } else {
134 "Functor is not valid. Only pairwise and triwise functors are supported. Please use a functor derived from "
135 "PairwiseFunctor or TriwiseFunctor.");
136 }
137 return false;
138}
139
140template <class Particle_T>
141size_t AutoPas<Particle_T>::getNumberOfParticles(IteratorBehavior behavior) const {
142 size_t numParticles{0};
143 if (behavior & IteratorBehavior::owned) {
144 numParticles += _logicHandler->getNumberOfParticlesOwned();
145 }
146 if (behavior & IteratorBehavior::halo) {
147 numParticles += _logicHandler->getNumberOfParticlesHalo();
148 }
149 // non fatal sanity check whether the behavior contained anything else
150 if (behavior & ~(IteratorBehavior::ownedOrHalo)) {
152 "AutoPas::getNumberOfParticles() does not support iterator behaviors other than owned or halo.");
153 }
154
155 return numParticles;
156}
157
158template <class Particle_T>
159void AutoPas<Particle_T>::reserve(size_t numParticles) {
160 _logicHandler->reserve(numParticles);
161}
162
163template <class Particle_T>
164void AutoPas<Particle_T>::reserve(size_t numParticles, size_t numHaloParticles) {
165 _logicHandler->reserve(numParticles, numHaloParticles);
166}
167
168template <class Particle_T>
169template <class F>
170void AutoPas<Particle_T>::addParticlesAux(size_t numParticlesToAdd, size_t numHalosToAdd, size_t collectionSize,
171 F loopBody) {
172 reserve(getNumberOfParticles(IteratorBehavior::owned) + numParticlesToAdd,
173 getNumberOfParticles(IteratorBehavior::halo) + numHalosToAdd);
174 AUTOPAS_OPENMP(parallel for schedule(static, std::max(1ul, collectionSize / omp_get_max_threads())))
175 for (auto i = 0; i < collectionSize; ++i) {
176 loopBody(i);
177 }
178}
179
180template <class Particle_T>
181void AutoPas<Particle_T>::addParticle(const Particle_T &p) {
182 _logicHandler->addParticle(p);
183}
184
185template <class Particle_T>
186template <class Collection>
187void AutoPas<Particle_T>::addParticles(Collection &&particles) {
188 addParticlesAux(particles.size(), 0, particles.size(), [&](auto i) { addParticle(particles[i]); });
189}
190
191template <class Particle_T>
192template <class Collection, class F>
193void AutoPas<Particle_T>::addParticlesIf(Collection &&particles, F predicate) {
194 std::vector<char> predicateMask(particles.size());
195 int numTrue = 0;
196 AUTOPAS_OPENMP(parallel for reduction(+ : numTrue))
197 for (auto i = 0; i < particles.size(); ++i) {
198 if (predicate(particles[i])) {
199 predicateMask[i] = static_cast<char>(true);
200 ++numTrue;
201 } else {
202 predicateMask[i] = static_cast<char>(false);
203 }
204 }
205
206 addParticlesAux(numTrue, 0, particles.size(), [&](auto i) {
207 if (predicateMask[i]) {
208 addParticle(particles[i]);
209 }
210 });
211}
212
213template <class Particle_T>
214std::vector<Particle_T> AutoPas<Particle_T>::updateContainer() {
215 return _logicHandler->updateContainer();
216}
217
218template <class Particle_T>
219std::vector<Particle_T> AutoPas<Particle_T>::resizeBox(const std::array<double, 3> &boxMin,
220 const std::array<double, 3> &boxMax) {
221 if (_allowedCellSizeFactors->isInterval()) {
222 AutoPasLog(WARN,
223 "The allowed Cell Size Factors are a continuous interval but internally only those values that "
224 "yield unique numbers of cells are used. Resizing does not cause these values to be recalculated so "
225 "the same configurations might now yield different and non-unique numbers of cells!");
226 }
227 _logicHandlerInfo.boxMin = boxMin;
228 _logicHandlerInfo.boxMax = boxMax;
229 return _logicHandler->resizeBox(boxMin, boxMax);
230}
231
232template <class Particle_T>
234 _tuningManager->forceRetune();
235}
236
237template <class Particle_T>
238void AutoPas<Particle_T>::addHaloParticle(const Particle_T &haloParticle) {
239 _logicHandler->addHaloParticle(haloParticle);
240}
241
242template <class Particle_T>
243template <class Collection>
244void AutoPas<Particle_T>::addHaloParticles(Collection &&particles) {
245 addParticlesAux(0, particles.size(), particles.size(), [&](auto i) { addHaloParticle(particles[i]); });
246}
247
248template <class Particle_T>
249template <class Collection, class F>
250void AutoPas<Particle_T>::addHaloParticlesIf(Collection &&particles, F predicate) {
251 std::vector<char> predicateMask(particles.size());
252 int numTrue = 0;
253 AUTOPAS_OPENMP(parallel for reduction(+ : numTrue))
254 for (auto i = 0; i < particles.size(); ++i) {
255 if (predicate(particles[i])) {
256 predicateMask[i] = static_cast<char>(true);
257 ++numTrue;
258 } else {
259 predicateMask[i] = static_cast<char>(false);
260 }
261 }
262
263 addParticlesAux(0, numTrue, particles.size(), [&](auto i) {
264 if (predicateMask[i]) {
265 addHaloParticle(particles[i]);
266 }
267 });
268}
269
270template <class Particle_T>
272 _logicHandler->deleteAllParticles();
273}
274
275template <class Particle_T>
277 _logicHandler->decreaseParticleCounter(*iter);
278 internal::deleteParticle(iter);
279}
280
281template <class Particle_T>
283 _logicHandler->decreaseParticleCounter(*iter);
284 internal::deleteParticle(iter);
285}
286
287template <class Particle_T>
288bool AutoPas<Particle_T>::deleteParticle(Particle_T &particle) {
289 _logicHandler->decreaseParticleCounter(particle);
290 // if the particle was not found in the logic handler's buffers it must be in the container
291 auto [particleDeleted, refStillValid] = _logicHandler->deleteParticleFromBuffers(particle);
292 if (not particleDeleted) {
293 refStillValid = _logicHandler->getContainer().deleteParticle(particle);
294 }
295 return refStillValid;
296}
297
298template <class Particle_T>
300 return _logicHandler->begin(behavior);
301}
302
303template <class Particle_T>
304typename AutoPas<Particle_T>::ConstIteratorT AutoPas<Particle_T>::begin(IteratorBehavior behavior) const {
305 return std::as_const(*_logicHandler).begin(behavior);
306}
307
308template <class Particle_T>
310 const std::array<double, 3> &lowerCorner, const std::array<double, 3> &higherCorner, IteratorBehavior behavior) {
311 return _logicHandler->getRegionIterator(lowerCorner, higherCorner, behavior);
312}
313
314template <class Particle_T>
316 const std::array<double, 3> &lowerCorner, const std::array<double, 3> &higherCorner,
317 IteratorBehavior behavior) const {
318 return std::as_const(*_logicHandler).getRegionIterator(lowerCorner, higherCorner, behavior);
319}
320
321template <class Particle_T>
323 return _logicHandler->getContainer().getContainerType();
324}
325
326template <class Particle_T>
327const std::array<double, 3> &AutoPas<Particle_T>::getBoxMin() const {
328 return _logicHandler->getContainer().getBoxMin();
329}
330
331template <class Particle_T>
332const std::array<double, 3> &AutoPas<Particle_T>::getBoxMax() const {
333 return _logicHandler->getContainer().getBoxMax();
334}
335
336template <class Particle_T>
338 return _logicHandler->getContainer();
339}
340
341template <class Particle_T>
342const autopas::ParticleContainerInterface<Particle_T> &AutoPas<Particle_T>::getContainer() const {
343 return _logicHandler->getContainer();
344}
345
346template <class Particle_T>
348 return _tuningManager->allSearchSpacesAreTrivial();
349}
350
351} // namespace autopas
#define AutoPasLog(lvl, fmt,...)
Macro for logging providing common meta information without filename.
Definition: Logger.h:24
#define AUTOPAS_MPI_COMM_NULL
Wrapper for MPI_COMM_NULL.
Definition: WrapMPI.h:118
#define AUTOPAS_MPI_COMM_WORLD
Wrapper for MPI_COMM_WORLD.
Definition: WrapMPI.h:120
#define AUTOPAS_OPENMP(args)
Empty macro to throw away any arguments.
Definition: WrapOpenMP.h:126
The AutoPas class is intended to be the main point of Interaction for the user.
Definition: AutoPasDecl.h:47
std::vector< Particle_T > updateContainer()
Updates the container.
Definition: AutoPasImpl.h:214
void reserve(size_t numParticles)
Reserve memory for a given number of particles in the container and logic layers.
Definition: AutoPasImpl.h:159
void addParticles(Collection &&particles)
Adds all particles from the collection to the container.
Definition: AutoPasImpl.h:187
void addParticlesIf(Collection &&particles, F predicate)
Adds all particles for which predicate(particle) == true to the container.
Definition: AutoPasImpl.h:193
void addHaloParticle(const Particle_T &haloParticle)
Adds a particle to the container that lies in the halo region of the container.
Definition: AutoPasImpl.h:238
AutoPas(std::ostream &logOutputStream=std::cout)
Constructor for the AutoPas class.
Definition: AutoPasImpl.h:35
RegionIteratorT getRegionIterator(const std::array< double, 3 > &lowerCorner, const std::array< double, 3 > &higherCorner, IteratorBehavior behavior=IteratorBehavior::ownedOrHalo)
Iterate over all particles in a specified region.
Definition: AutoPasImpl.h:309
AutoPas & operator=(AutoPas &&other) noexcept
Move assignment operator.
Definition: AutoPasImpl.h:48
void init()
Initialize AutoPas.
Definition: AutoPasImpl.h:55
void forceRetune()
Force the internal tuner to enter a new tuning phase upon the next call to computeInteractions().
Definition: AutoPasImpl.h:233
size_t getNumberOfParticles(IteratorBehavior behavior=IteratorBehavior::owned) const
Returns the number of particles in this container.
Definition: AutoPasImpl.h:141
void deleteAllParticles()
Deletes all particles.
Definition: AutoPasImpl.h:271
bool computeInteractions(Functor *f)
Function to iterate over all inter-particle interactions in the container This function only handles ...
Definition: AutoPasImpl.h:118
void addParticle(const Particle_T &p)
Adds a particle to the container.
Definition: AutoPasImpl.h:181
IteratorT begin(IteratorBehavior behavior=IteratorBehavior::ownedOrHalo)
Iterate over all particles by using for(auto iter = autoPas.begin(); iter.isValid(); ++iter)
Definition: AutoPasImpl.h:299
std::vector< std::unique_ptr< TuningStrategyInterface > > TuningStrategiesListType
Type for the member holding all tuning strategies.
Definition: AutoTuner.h:45
Public iterator class that iterates over a particle container and additional vectors (which are typic...
Definition: ContainerIterator.h:95
Functor base class.
Definition: Functor.h:41
double getCutoff() const
Getter for the functor's cutoff.
Definition: Functor.h:177
static void create(std::ostream &logOutputStream=std::cout)
Explicitly initialize/reset the logger to write to an output stream.
Definition: Logger.h:68
Class describing an interval.
Definition: NumberInterval.h:15
Class describing a finite set of numbers.
Definition: NumberSetFinite.h:19
The ParticleContainerInterface class provides a basic interface for all Containers within AutoPas.
Definition: ParticleContainerInterface.h:38
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:64
std::set< double > calculateRelevantCsfs(const NumberInterval< double > &numberInterval, double interactionLength, double domainLengthX)
For a given domain parametrization, calculate which cell size factors (csf) in an interval actually a...
Definition: SearchSpaceGenerators.cpp:84
std::set< Configuration > cartesianProduct(const std::set< ContainerOption > &allowedContainerOptions, const std::set< TraversalOption > &allowedTraversalOptions, const std::set< LoadEstimatorOption > &allowedLoadEstimatorOptions, const std::set< DataLayoutOption > &allowedDataLayoutOptions, const std::set< Newton3Option > &allowedNewton3Options, const NumberSet< double > *allowedCellSizeFactors, const std::set< VectorizationPatternOption > &allowedVecPatternOptions, const InteractionTypeOption &interactionType)
Fills the search space with the cartesian product of the given options (minus invalid combinations).
Definition: SearchSpaceGenerators.cpp:18
std::string getCompilerInfo()
Get name and version number of a list of known compilers.
Definition: CompileInfo.cpp:9
decltype(isTriwiseFunctorImpl(std::declval< FunctorT >())) isTriwiseFunctor
Check whether a Functor Type is inheriting from TriwiseFunctor.
Definition: checkFunctorType.h:56
decltype(isPairwiseFunctorImpl(std::declval< FunctorT >())) isPairwiseFunctor
Check whether a Functor Type is inheriting from PairwiseFunctor.
Definition: checkFunctorType.h:49
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:34
int AutoPas_MPI_Comm_dup(AutoPas_MPI_Comm comm, AutoPas_MPI_Comm *newComm)
Wrapper for MPI_Comm_dup.
Definition: WrapMPI.h:815
int AutoPas_MPI_Comm_rank(AutoPas_MPI_Comm comm, int *rank)
Wrapper for MPI_Comm_rank.
Definition: WrapMPI.h:807