AutoPas  3.0.0
Loading...
Searching...
No Matches
VLListIterationTraversal.h
Go to the documentation of this file.
1
7#pragma once
8
13
14namespace autopas {
15
22template <class ParticleCell_T, class PairwiseFunctor_T>
24 using ParticleType = ParticleCell_T::ParticleType;
25
26 public:
33 explicit VLListIterationTraversal(PairwiseFunctor_T &pairwiseFunctor, const DataLayoutOption dataLayout,
34 const bool useNewton3)
35 : TraversalInterface(dataLayout, useNewton3), _functor(pairwiseFunctor) {
36 if (useNewton3) {
37 AutoPasLog(WARN,
38 "VLListIterationTraversal: This traversal is not parallelized for newton3 enabled and will utilize "
39 "only one thread.");
40 }
41 }
42
43 [[nodiscard]] TraversalOption getTraversalType() const override { return TraversalOption::vl_list_iteration; }
44
49 [[nodiscard]] bool isApplicableToDomain() const override { return true; }
50
51 void initTraversal() override {
52 if (_dataLayout == DataLayoutOption::soa) {
53 auto &cells = *(this->_cells);
54 // First, resize the SoA to the required number of elements to store. This avoids resizing successively the SoA in
55 // SoALoader.
56 std::vector<size_t> offsets(cells.size() + 1);
57 std::inclusive_scan(
58 cells.begin(), cells.end(), offsets.begin() + 1,
59 [](const size_t &partialSum, const auto &cell) { return partialSum + cell.size(); }, 0);
60
61 _soa.resizeArrays(offsets.back());
62
63 AUTOPAS_OPENMP(parallel for)
64 for (size_t i = 0; i < cells.size(); ++i) {
65 _functor.SoALoader(cells[i], _soa, offsets[i], /*skipSoAResize*/ true);
66 }
67 }
68 }
69
70 void endTraversal() override {
71 auto &cells = *(this->_cells);
72 if (_dataLayout == DataLayoutOption::soa) {
73 size_t offset = 0;
74 for (auto &cell : cells) {
75 _functor.SoAExtractor(cell, _soa, offset);
76 offset += cell.size();
77 }
78 }
79 }
80
81 void traverseParticles() override {
82 auto &neighborList = *(this->_neighborList);
83 const size_t numParticles = neighborList.size();
84 const auto &indexToParticle = *this->_indexToParticle;
85
86 switch (this->_dataLayout) {
87 case DataLayoutOption::aos: {
88 if (not _useNewton3) {
89 // Each particle i owns its own list slice — no write conflict between iterations.
90 AUTOPAS_OPENMP(parallel for schedule(dynamic))
91 for (size_t i = 0; i < numParticles; ++i) {
92 ParticleType &particleI = *indexToParticle[i];
93 const size_t numNeighbors = neighborList.count(i);
94 const size_t *neighborsIPtr = neighborList.begin(i);
95 for (size_t j = 0; j < numNeighbors; ++j) {
96 _functor.AoSFunctor(particleI, *indexToParticle[neighborsIPtr[j]], false);
97 }
98 }
99 } else {
100 // Newton3 cannot be parallelized here
101 for (size_t i = 0; i < numParticles; ++i) {
102 ParticleType &particleI = *indexToParticle[i];
103 const size_t numNeighbors = neighborList.count(i);
104 const size_t *neighborsIPtr = neighborList.begin(i);
105 for (size_t j = 0; j < numNeighbors; ++j) {
106 _functor.AoSFunctor(particleI, *indexToParticle[neighborsIPtr[j]], true);
107 }
108 }
109 }
110 return;
111 }
112
113 case DataLayoutOption::soa: {
114 if (not _useNewton3) {
115 AUTOPAS_OPENMP(parallel for schedule(dynamic, std::max(numParticles / (autopas::autopas_get_max_threads() * 10), 1ul)))
116 for (size_t i = 0; i < numParticles; ++i) {
117 _functor.SoAFunctorVerlet(_soa, i, neighborList.getNeighbors(i), false);
118 }
119 } else {
120 // Newton3 cannot be parallelized here
121 for (size_t i = 0; i < numParticles; ++i) {
122 _functor.SoAFunctorVerlet(_soa, i, neighborList.getNeighbors(i), true);
123 }
124 }
125 return;
126 }
127 default: {
128 utils::ExceptionHandler::exception("VerletList dataLayout {} not available", _dataLayout);
129 }
130 }
131 }
132
133 private:
137 PairwiseFunctor_T &_functor;
138
143};
144
145} // namespace autopas
#define AutoPasLog(lvl, fmt,...)
Macro for logging providing common meta information without filename.
Definition: Logger.h:74
#define AUTOPAS_OPENMP(args)
Empty macro to throw away any arguments.
Definition: WrapOpenMP.h:126
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 interface serves as a common parent class for all traversals.
Definition: TraversalInterface.h:18
DataLayoutOption _dataLayout
The datalayout used by this traversal.
Definition: TraversalInterface.h:77
bool _useNewton3
If this traversal makes use of newton3.
Definition: TraversalInterface.h:82
This class provides a Traversal for the verlet lists container.
Definition: VLListIterationTraversal.h:23
bool isApplicableToDomain() const override
VL List iteration is always applicable to the domain.
Definition: VLListIterationTraversal.h:49
VLListIterationTraversal(PairwiseFunctor_T &pairwiseFunctor, const DataLayoutOption dataLayout, const bool useNewton3)
Constructor for Verlet Traversal.
Definition: VLListIterationTraversal.h:33
void traverseParticles() override
Traverse the particles by pairs, triplets etc.
Definition: VLListIterationTraversal.h:81
TraversalOption getTraversalType() const override
Return a enum representing the name of the traversal class.
Definition: VLListIterationTraversal.h:43
void initTraversal() override
Initializes the traversal.
Definition: VLListIterationTraversal.h:51
void endTraversal() override
Finalizes the traversal.
Definition: VLListIterationTraversal.h:70
This class provides the Traversal Interface for the verlet lists container.
Definition: VLTraversalInterface.h:22
const std::vector< ParticleType * > * _indexToParticle
Vector mapping SoA indices to particle pointers.
Definition: VLTraversalInterface.h:69
std::vector< ParticleCell_T > * _cells
The cells of the underlying linked cells container of the verlet lists container.
Definition: VLTraversalInterface.h:52
VerletListHelpers< ParticleType >::NeighborListCRS * _neighborList
The flat CRS neighbor list.
Definition: VLTraversalInterface.h:57
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:64
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:34
int autopas_get_max_threads()
Dummy for omp_get_max_threads() when no OpenMP is available.
Definition: WrapOpenMP.h:144