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
16
17namespace autopas {
18
22enum class FunctorN3Modes {
23 Newton3Only,
24 Newton3Off,
25 Both,
26};
27
39template <class Particle_T, class CRTP_T>
40class Functor {
41 public:
45 using SoAArraysType = typename Particle_T::SoAArraysType;
46
50 using Functor_T = CRTP_T;
55 explicit Functor(double cutoff) : _cutoff(cutoff){};
56
57 virtual ~Functor() = default;
58
63 virtual void initTraversal(){};
64
70 virtual void endTraversal(bool newton3){};
71
77 constexpr static std::array<typename Particle_T::AttributeNames, 0> getNeededAttr() {
78 return std::array<typename Particle_T::AttributeNames, 0>{};
79 }
80
86 constexpr static std::array<typename Particle_T::AttributeNames, 0> getNeededAttr(std::false_type) {
87 return Functor_T::getNeededAttr();
88 }
89
95 constexpr static std::array<typename Particle_T::AttributeNames, 0> getComputedAttr() {
96 return std::array<typename Particle_T::AttributeNames, 0>{};
97 }
98
111 template <class ParticleCell>
112 void SoALoader(ParticleCell &cell, SoA<SoAArraysType> &soa, size_t offset, bool skipSoAResize) {
113 SoALoaderImpl(cell, soa, offset, skipSoAResize, std::make_index_sequence<Functor_T::getNeededAttr().size()>{});
114 }
115
125 template <typename ParticleCell>
126 void SoAExtractor(ParticleCell &cell, SoA<SoAArraysType> &soa, size_t offset) {
127 SoAExtractorImpl(cell, soa, offset, std::make_index_sequence<Functor_T::getComputedAttr().size()>{});
128 }
129
139 virtual bool allowsNewton3() = 0;
140
149 virtual bool allowsNonNewton3() = 0;
150
155 virtual bool isRelevantForTuning() = 0;
156
162 virtual std::string getName() = 0;
163
168 [[nodiscard]] double getCutoff() const { return _cutoff; }
169
177 [[nodiscard]] virtual size_t getNumFLOPs() const { return std::numeric_limits<size_t>::max(); }
178
187 [[nodiscard]] virtual double getHitRate() const { return std::numeric_limits<double>::quiet_NaN(); }
188
189 private:
202 template <typename cell_t, std::size_t... I>
203 void SoALoaderImpl(cell_t &cell, ::autopas::SoA<SoAArraysType> &soa, size_t offset, bool skipSoAResize,
204 std::index_sequence<I...>) {
205 if (not skipSoAResize) {
206 soa.resizeArrays(offset + cell.size());
207 }
208
209 if (cell.isEmpty()) return;
210
215 // maybe_unused necessary because gcc doesn't understand that pointer is used later
216 [[maybe_unused]] auto const pointer = std::make_tuple(soa.template begin<Functor_T::getNeededAttr()[I]>()...);
217
218 auto cellIter = cell.begin();
219 // load particles in SoAs
220 for (size_t i = offset; cellIter != cell.end(); ++cellIter, ++i) {
228 ((std::get<I>(pointer)[i] = cellIter->template get<Functor_T::getNeededAttr()[I]>()), ...);
229 }
230 }
231
240 template <typename cell_t, std::size_t... I>
241 void SoAExtractorImpl(cell_t &cell, ::autopas::SoA<SoAArraysType> &soa, size_t offset, std::index_sequence<I...>) {
242 if (cell.isEmpty()) return;
243
248 // maybe_unused necessary because gcc doesn't understand that pointer is used later
249 [[maybe_unused]] auto const pointer = std::make_tuple(soa.template begin<Functor_T::getComputedAttr()[I]>()...);
250
251 auto cellIter = cell.begin();
252 // write values in SoAs back to particles
253 for (size_t i = offset; cellIter != cell.end(); ++cellIter, ++i) {
262 (cellIter->template set<Functor_T::getComputedAttr()[I]>(std::get<I>(pointer)[i]), ...);
263 }
264 }
265
266 double _cutoff;
267};
268
269} // namespace autopas
Functor base class.
Definition: Functor.h:40
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:112
virtual size_t getNumFLOPs() const
Get the number of FLOPs.
Definition: Functor.h:177
CRTP_T Functor_T
Make the Implementation type template publicly available.
Definition: Functor.h:50
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:126
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
double getCutoff() const
Getter for the functor's cutoff.
Definition: Functor.h:168
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:86
virtual double getHitRate() const
Get the hit rate.
Definition: Functor.h:187
typename Particle_T::SoAArraysType SoAArraysType
Structure of the SoAs defined by the particle.
Definition: Functor.h:45
static constexpr std::array< typename Particle_T::AttributeNames, 0 > getNeededAttr()
Get attributes needed for computation.
Definition: Functor.h:77
static constexpr std::array< typename Particle_T::AttributeNames, 0 > getComputedAttr()
Get attributes computed by this functor.
Definition: Functor.h:95
Functor(double cutoff)
Constructor.
Definition: Functor.h:55
Class for Cells of Particles.
Definition: ParticleCell.h:51
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:32
FunctorN3Modes
Newton 3 modes for the Functor.
Definition: Functor.h:22