AutoPas  3.0.0
Loading...
Searching...
No Matches
Functor.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <type_traits>
11
17
18namespace autopas {
19
23enum class FunctorN3Modes {
24 Newton3Only,
25 Newton3Off,
26 Both,
27};
28
40template <class Particle_T, class CRTP_T>
41class Functor {
42 public:
46 using SoAArraysType = typename Particle_T::SoAArraysType;
47
51 using Functor_T = CRTP_T;
56 explicit Functor(double cutoff) : _cutoff(cutoff){};
57
58 virtual ~Functor() = default;
59
64 virtual void initTraversal(){};
65
71 virtual void endTraversal(bool newton3){};
72
78 constexpr static std::array<typename Particle_T::AttributeNames, 0> getNeededAttr() {
79 return std::array<typename Particle_T::AttributeNames, 0>{};
80 }
81
87 constexpr static std::array<typename Particle_T::AttributeNames, 0> getNeededAttr(std::false_type) {
88 return Functor_T::getNeededAttr();
89 }
90
96 constexpr static std::array<typename Particle_T::AttributeNames, 0> getComputedAttr() {
97 return std::array<typename Particle_T::AttributeNames, 0>{};
98 }
99
112 static constexpr bool supportsSoASorting = false;
113
126 template <class ParticleCell>
127 void SoALoader(ParticleCell &cell, SoA<SoAArraysType> &soa, size_t offset, bool skipSoAResize) {
128 SoALoaderImpl(cell, soa, offset, skipSoAResize, std::make_index_sequence<Functor_T::getNeededAttr().size()>{});
129 }
130
140 template <typename ParticleCell>
141 void SoAExtractor(ParticleCell &cell, SoA<SoAArraysType> &soa, size_t offset) {
142 SoAExtractorImpl(cell, soa, offset, std::make_index_sequence<Functor_T::getComputedAttr().size()>{});
143 }
144
154 virtual bool allowsNewton3() = 0;
155
164 virtual bool allowsNonNewton3() = 0;
165
174 virtual bool isVecPatternAllowed(const VectorizationPatternOption::Value vecPattern) = 0;
175
180 virtual bool isRelevantForTuning() = 0;
181
187 virtual std::string getName() = 0;
188
193 [[nodiscard]] double getCutoff() const { return _cutoff; }
194
199 virtual void setVecPattern(const VectorizationPatternOption::Value vecPattern) {}
200
208 [[nodiscard]] virtual size_t getNumFLOPs() const { return std::numeric_limits<size_t>::max(); }
209
218 [[nodiscard]] virtual double getHitRate() const { return std::numeric_limits<double>::quiet_NaN(); }
219
220 private:
233 template <typename cell_t, std::size_t... I>
234 void SoALoaderImpl(cell_t &cell, ::autopas::SoA<SoAArraysType> &soa, size_t offset, bool skipSoAResize,
235 std::index_sequence<I...>) {
236 if (not skipSoAResize) {
237 soa.resizeArrays(offset + cell.size());
238 }
239
240 if (cell.isEmpty()) return;
241
246 // maybe_unused necessary because gcc doesn't understand that pointer is used later
247 [[maybe_unused]] auto const pointer = std::make_tuple(soa.template begin<Functor_T::getNeededAttr()[I]>()...);
248
249 auto cellIter = cell.begin();
250 // load particles in SoAs
251 for (size_t i = offset; cellIter != cell.end(); ++cellIter, ++i) {
259 ((std::get<I>(pointer)[i] = cellIter->template get<Functor_T::getNeededAttr()[I]>()), ...);
260 }
261 }
262
271 template <typename cell_t, std::size_t... I>
272 void SoAExtractorImpl(cell_t &cell, ::autopas::SoA<SoAArraysType> &soa, size_t offset, std::index_sequence<I...>) {
273 if (cell.isEmpty()) return;
274
279 // maybe_unused necessary because gcc doesn't understand that pointer is used later
280 [[maybe_unused]] auto const pointer = std::make_tuple(soa.template begin<Functor_T::getComputedAttr()[I]>()...);
281
282 auto cellIter = cell.begin();
283 // write values in SoAs back to particles
284 for (size_t i = offset; cellIter != cell.end(); ++cellIter, ++i) {
293 (cellIter->template set<Functor_T::getComputedAttr()[I]>(std::get<I>(pointer)[i]), ...);
294 }
295 }
296
297 double _cutoff;
298};
299
300} // namespace autopas
Functor base class.
Definition: Functor.h:41
void SoALoader(ParticleCell &cell, SoA< SoAArraysType > &soa, size_t offset, bool skipSoAResize)
Copies the AoS data of the given cell in the given soa.
Definition: Functor.h:127
virtual size_t getNumFLOPs() const
Get the number of FLOPs.
Definition: Functor.h:208
virtual bool isVecPatternAllowed(const VectorizationPatternOption::Value vecPattern)=0
Specifies whether the functor is capable of using the specified Vectorization Pattern in the SoA func...
CRTP_T Functor_T
Make the Implementation type template publicly available.
Definition: Functor.h:51
virtual bool allowsNewton3()=0
Specifies whether the functor is capable of Newton3-like functors.
void SoAExtractor(ParticleCell &cell, SoA< SoAArraysType > &soa, size_t offset)
Copies the data stored in the soa back into the cell.
Definition: Functor.h:141
virtual void setVecPattern(const VectorizationPatternOption::Value vecPattern)
Setter for the vectorization pattern to be used.
Definition: Functor.h:199
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
double getCutoff() const
Getter for the functor's cutoff.
Definition: Functor.h:193
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.
static constexpr std::array< typename Particle_T::AttributeNames, 0 > getNeededAttr(std::false_type)
Get attributes needed for computation without N3 optimization.
Definition: Functor.h:87
virtual double getHitRate() const
Get the hit rate.
Definition: Functor.h:218
typename Particle_T::SoAArraysType SoAArraysType
Structure of the SoAs defined by the particle.
Definition: Functor.h:46
static constexpr std::array< typename Particle_T::AttributeNames, 0 > getNeededAttr()
Get attributes needed for computation.
Definition: Functor.h:78
static constexpr std::array< typename Particle_T::AttributeNames, 0 > getComputedAttr()
Get attributes computed by this functor.
Definition: Functor.h:96
Functor(double cutoff)
Constructor.
Definition: Functor.h:56
static constexpr bool supportsSoASorting
Whether this functor supports the SortedSoAView optimization path (SoAFunctorPairSorted).
Definition: Functor.h:112
Class for Cells of Particles.
Definition: ParticleCell.h:49
Structur of the array class.
Definition: SoA.h:28
void resizeArrays(size_t length)
Resizes all Vectors to the given length.
Definition: SoA.h:45
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:34
FunctorN3Modes
Newton 3 modes for the Functor.
Definition: Functor.h:23