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
172 virtual bool isVecPatternAllowed(const VectorizationPatternOption::Value vecPattern) = 0;
173
178 virtual bool isRelevantForTuning() = 0;
179
185 virtual std::string getName() = 0;
186
191 [[nodiscard]] double getCutoff() const { return _cutoff; }
192
197 virtual void setVecPattern(const VectorizationPatternOption::Value vecPattern) {}
198
206 [[nodiscard]] virtual size_t getNumFLOPs() const { return std::numeric_limits<size_t>::max(); }
207
216 [[nodiscard]] virtual double getHitRate() const { return std::numeric_limits<double>::quiet_NaN(); }
217
218 private:
231 template <typename cell_t, std::size_t... I>
232 void SoALoaderImpl(cell_t &cell, ::autopas::SoA<SoAArraysType> &soa, size_t offset, bool skipSoAResize,
233 std::index_sequence<I...>) {
234 if (not skipSoAResize) {
235 soa.resizeArrays(offset + cell.size());
236 }
237
238 if (cell.isEmpty()) return;
239
244 // maybe_unused necessary because gcc doesn't understand that pointer is used later
245 [[maybe_unused]] auto const pointer = std::make_tuple(soa.template begin<Functor_T::getNeededAttr()[I]>()...);
246
247 auto cellIter = cell.begin();
248 // load particles in SoAs
249 for (size_t i = offset; cellIter != cell.end(); ++cellIter, ++i) {
257 ((std::get<I>(pointer)[i] = cellIter->template get<Functor_T::getNeededAttr()[I]>()), ...);
258 }
259 }
260
269 template <typename cell_t, std::size_t... I>
270 void SoAExtractorImpl(cell_t &cell, ::autopas::SoA<SoAArraysType> &soa, size_t offset, std::index_sequence<I...>) {
271 if (cell.isEmpty()) return;
272
277 // maybe_unused necessary because gcc doesn't understand that pointer is used later
278 [[maybe_unused]] auto const pointer = std::make_tuple(soa.template begin<Functor_T::getComputedAttr()[I]>()...);
279
280 auto cellIter = cell.begin();
281 // write values in SoAs back to particles
282 for (size_t i = offset; cellIter != cell.end(); ++cellIter, ++i) {
291 (cellIter->template set<Functor_T::getComputedAttr()[I]>(std::get<I>(pointer)[i]), ...);
292 }
293 }
294
295 double _cutoff;
296};
297
298} // 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:206
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:197
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:191
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:216
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