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 template <class ParticleCell>
113 void SoALoader(ParticleCell &cell, SoA<SoAArraysType> &soa, size_t offset, bool skipSoAResize) {
114 SoALoaderImpl(cell, soa, offset, skipSoAResize, std::make_index_sequence<Functor_T::getNeededAttr().size()>{});
115 }
116
126 template <typename ParticleCell>
127 void SoAExtractor(ParticleCell &cell, SoA<SoAArraysType> &soa, size_t offset) {
128 SoAExtractorImpl(cell, soa, offset, std::make_index_sequence<Functor_T::getComputedAttr().size()>{});
129 }
130
140 virtual bool allowsNewton3() = 0;
141
150 virtual bool allowsNonNewton3() = 0;
151
158 virtual bool isVecPatternAllowed(const VectorizationPatternOption::Value vecPattern) = 0;
159
164 virtual bool isRelevantForTuning() = 0;
165
171 virtual std::string getName() = 0;
172
177 [[nodiscard]] double getCutoff() const { return _cutoff; }
178
183 virtual void setVecPattern(const VectorizationPatternOption::Value vecPattern) {}
184
192 [[nodiscard]] virtual size_t getNumFLOPs() const { return std::numeric_limits<size_t>::max(); }
193
202 [[nodiscard]] virtual double getHitRate() const { return std::numeric_limits<double>::quiet_NaN(); }
203
204 private:
217 template <typename cell_t, std::size_t... I>
218 void SoALoaderImpl(cell_t &cell, ::autopas::SoA<SoAArraysType> &soa, size_t offset, bool skipSoAResize,
219 std::index_sequence<I...>) {
220 if (not skipSoAResize) {
221 soa.resizeArrays(offset + cell.size());
222 }
223
224 if (cell.isEmpty()) return;
225
230 // maybe_unused necessary because gcc doesn't understand that pointer is used later
231 [[maybe_unused]] auto const pointer = std::make_tuple(soa.template begin<Functor_T::getNeededAttr()[I]>()...);
232
233 auto cellIter = cell.begin();
234 // load particles in SoAs
235 for (size_t i = offset; cellIter != cell.end(); ++cellIter, ++i) {
243 ((std::get<I>(pointer)[i] = cellIter->template get<Functor_T::getNeededAttr()[I]>()), ...);
244 }
245 }
246
255 template <typename cell_t, std::size_t... I>
256 void SoAExtractorImpl(cell_t &cell, ::autopas::SoA<SoAArraysType> &soa, size_t offset, std::index_sequence<I...>) {
257 if (cell.isEmpty()) return;
258
263 // maybe_unused necessary because gcc doesn't understand that pointer is used later
264 [[maybe_unused]] auto const pointer = std::make_tuple(soa.template begin<Functor_T::getComputedAttr()[I]>()...);
265
266 auto cellIter = cell.begin();
267 // write values in SoAs back to particles
268 for (size_t i = offset; cellIter != cell.end(); ++cellIter, ++i) {
277 (cellIter->template set<Functor_T::getComputedAttr()[I]>(std::get<I>(pointer)[i]), ...);
278 }
279 }
280
281 double _cutoff;
282};
283
284} // 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:113
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...
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:127
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
double getCutoff() const
Getter for the functor's cutoff.
Definition: Functor.h:177
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:202
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
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