AutoPas  3.0.0
Loading...
Searching...
No Matches
VLListIterationC27Traversal.h
Go to the documentation of this file.
1
7#pragma once
8
14
15namespace autopas {
16
23template <class ParticleCell_T, class PairwiseFunctor_T>
25 using ParticleType = ParticleCell_T::ParticleType;
26
27 public:
35 explicit VLListIterationC27Traversal(PairwiseFunctor_T &pairwiseFunctor, DataLayoutOption dataLayout, bool useNewton3,
36 const std::array<unsigned long, 3> &cellsPerDim)
37 : TraversalInterface(dataLayout, useNewton3), _functor(pairwiseFunctor), _cellsPerDim(cellsPerDim) {}
38
39 [[nodiscard]] TraversalOption getTraversalType() const override { return TraversalOption::vl_list_iteration_c27; }
40
45 [[nodiscard]] bool isApplicableToDomain() const override { return true; }
46
47 void initTraversal() override {
48 auto &cells = *(this->_cells);
49
50 // Pre-compute per-cell offsets so each SoALoader call can be parallelized
51 // independently without knowing the preceding cells' sizes. Also needed for coloring.
52 std::vector<size_t> offsets(cells.size() + 1);
53 offsets[0] = 0;
54 for (size_t c = 0; c < cells.size(); ++c) {
55 offsets[c + 1] = offsets[c] + cells[c].size();
56 }
57
58 // Initialize/clear color cells lists
59 for (auto &colorGroup : _colorCells) {
60 colorGroup.clear();
61 }
62
63 for (size_t c = 0; c < cells.size(); ++c) {
64 if (offsets[c] < offsets[c + 1]) {
65 auto c3D = utils::ThreeDimensionalMapping::oneToThreeD(c, _cellsPerDim);
66 const size_t color = (c3D[0] % 3) + 3 * (c3D[1] % 3) + 9 * (c3D[2] % 3);
67 _colorCells[color].push_back({offsets[c], offsets[c + 1]});
68 }
69 }
70
71 if (_dataLayout == DataLayoutOption::soa) {
72 _soa.resizeArrays(offsets.back());
73
74 AUTOPAS_OPENMP(parallel for)
75 for (size_t i = 0; i < cells.size(); ++i) {
76 _functor.SoALoader(cells[i], _soa, offsets[i], /*skipSoAResize*/ true);
77 }
78 }
79 }
80
81 void endTraversal() override {
82 auto &cells = *(this->_cells);
83 if (_dataLayout == DataLayoutOption::soa) {
84 size_t offset = 0;
85 for (auto &cell : cells) {
86 _functor.SoAExtractor(cell, _soa, offset);
87 offset += cell.size();
88 }
89 }
90 }
91
92 void traverseParticles() override {
93 auto &neighborList = *(this->_neighborList);
94 const auto &indexToParticle = *this->_indexToParticle;
95
96 switch (this->_dataLayout) {
97 case DataLayoutOption::aos: {
98 // Parallelized AoS with Newton3 using C27 coloring
99 for (int color = 0; color < 27; ++color) {
100 const auto &cellsOfColor = _colorCells[color];
101 AUTOPAS_OPENMP(parallel for schedule(dynamic))
102 for (size_t c = 0; c < cellsOfColor.size(); ++c) {
103 const auto &range = cellsOfColor[c];
104 for (size_t i = range.first; i < range.second; ++i) {
105 ParticleType &particleI = *indexToParticle[i];
106 const size_t numNeighbors = neighborList.count(i);
107 const size_t *neighborsIPtr = neighborList.begin(i);
108 for (size_t j = 0; j < numNeighbors; ++j) {
109 _functor.AoSFunctor(particleI, *indexToParticle[neighborsIPtr[j]], _useNewton3);
110 }
111 }
112 }
113 }
114 return;
115 }
116
117 case DataLayoutOption::soa: {
118 // Parallelized SoA with Newton3 using C27 coloring
119 for (int color = 0; color < 27; ++color) {
120 const auto &cellsOfColor = _colorCells[color];
121 AUTOPAS_OPENMP(parallel for schedule(dynamic))
122 for (size_t c = 0; c < cellsOfColor.size(); ++c) {
123 const auto &range = cellsOfColor[c];
124 for (size_t i = range.first; i < range.second; ++i) {
125 _functor.SoAFunctorVerlet(_soa, i, neighborList.getNeighbors(i), _useNewton3);
126 }
127 }
128 }
129 return;
130 }
131 default: {
132 utils::ExceptionHandler::exception("VerletList dataLayout {} not available", _dataLayout);
133 }
134 }
135 }
136
137 private:
141 PairwiseFunctor_T &_functor;
142
147
151 std::array<unsigned long, 3> _cellsPerDim;
152
156 std::array<std::vector<std::pair<size_t, size_t>>, 27> _colorCells;
157};
158
159} // namespace autopas
#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 colored Traversal for the verlet lists container.
Definition: VLListIterationC27Traversal.h:24
bool isApplicableToDomain() const override
VL List iteration C27 is always applicable to the domain.
Definition: VLListIterationC27Traversal.h:45
void endTraversal() override
Finalizes the traversal.
Definition: VLListIterationC27Traversal.h:81
VLListIterationC27Traversal(PairwiseFunctor_T &pairwiseFunctor, DataLayoutOption dataLayout, bool useNewton3, const std::array< unsigned long, 3 > &cellsPerDim)
Constructor for colored Verlet Traversal.
Definition: VLListIterationC27Traversal.h:35
TraversalOption getTraversalType() const override
Return a enum representing the name of the traversal class.
Definition: VLListIterationC27Traversal.h:39
void initTraversal() override
Initializes the traversal.
Definition: VLListIterationC27Traversal.h:47
void traverseParticles() override
Traverse the particles by pairs, triplets etc.
Definition: VLListIterationC27Traversal.h:92
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
constexpr std::array< T, 3 > oneToThreeD(T ind, const std::array< T, 3 > &dims)
Convert a 1d index to a 3d index.
Definition: ThreeDimensionalMapping.h:55
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:34