AutoPas  3.0.0
Loading...
Searching...
No Matches
LCC04Traversal.h
Go to the documentation of this file.
1
8#pragma once
9
16
17namespace autopas {
18
28template <class ParticleCell_T, class Functor_T>
29class LCC04Traversal : public C08BasedTraversal<ParticleCell_T, Functor_T>, public LCTraversalInterface {
30 public:
41 LCC04Traversal(const std::array<unsigned long, 3> &dims, Functor_T &functor, double interactionLength,
42 const std::array<double, 3> &cellLength, DataLayoutOption dataLayout, bool useNewton3)
43 : C08BasedTraversal<ParticleCell_T, Functor_T>(dims, functor, interactionLength, cellLength, dataLayout,
44 useNewton3),
45 _cellOffsets32Pack(computeOffsets32Pack()),
46 _cellHandler(functor, this->_cellsPerDimension, interactionLength, cellLength, this->_overlap, dataLayout,
47 useNewton3),
48 _end(utils::ArrayMath::subScalar(utils::ArrayUtils::static_cast_copy_array<long>(this->_cellsPerDimension),
49 1l)) {}
50
51 void traverseParticles() override;
52
53 [[nodiscard]] TraversalOption getTraversalType() const override { return TraversalOption::lc_c04; }
54
59 [[nodiscard]] bool isApplicableToDomain() const override {
60 // The cellsize cannot be smaller then the cutoff, if OpenMP is used.
61 // Also see: https://github.com/AutoPas/AutoPas/issues/464
62 const double minLength = *std::min_element(this->_cellLength.cbegin(), this->_cellLength.cend());
63 const unsigned long minDim = *std::min_element(this->_cellsPerDimension.cbegin(), this->_cellsPerDimension.cend());
64
65 return minLength >= this->_interactionLength and minDim > 3;
66 }
67
71 void setAoSSortingThreshold(size_t aosSortingThreshold) override {
72 _cellHandler.setAoSSortingThreshold(aosSortingThreshold);
73 }
77 void setSoASortingThreshold(size_t soaSortingThreshold) override {
78 _cellHandler.setSoASortingThreshold(soaSortingThreshold);
79 }
80
81 private:
82 void traverseSingleColor(std::vector<ParticleCell_T> &cells, int color);
83
84 void processBasePack32(std::vector<ParticleCell_T> &cells, const std::array<long, 3> &base3DIndex);
85
86 static constexpr auto computeOffsets32Pack();
87
88 [[nodiscard]] constexpr long parity(long x, long y, long z) const { return (x + y + z + 24) % 8; }
89
90 std::array<std::array<long, 3>, 32> _cellOffsets32Pack;
91
92 LCC08CellHandler<ParticleCell_T, Functor_T> _cellHandler;
93
94 std::array<long, 3> _end;
95};
96
103template <class ParticleCell_T, class Functor_T>
104constexpr auto LCC04Traversal<ParticleCell_T, Functor_T>::computeOffsets32Pack() {
105 using std::make_pair;
107
108 std::array<std::array<long, 3>, 32> cellOffsets32Pack = {};
109
110 unsigned int i = 0;
111 long z = 0l;
112 cellOffsets32Pack[i++] = {1l, 1l, z};
113 cellOffsets32Pack[i++] = {1l, 2l, z};
114 cellOffsets32Pack[i++] = {2l, 1l, z};
115 cellOffsets32Pack[i++] = {2l, 2l, z};
116
117 // z = 1ul; z = 2ul
118 for (z = 1l; z < 3l; ++z) {
119 for (long y = 0l; y < 4l; y++) {
120 for (long x = 0l; x < 4l; x++) {
121 if ((x == 0l and y == 0l) or (x == 3l and y == 0l) or (x == 0l and y == 3l) or (x == 3l and y == 3l)) {
122 continue;
123 }
124 cellOffsets32Pack[i++] = {x, y, z};
125 }
126 }
127 }
128
129 z = 3ul;
130 cellOffsets32Pack[i++] = {1l, 1l, z};
131 cellOffsets32Pack[i++] = {1l, 2l, z};
132 cellOffsets32Pack[i++] = {2l, 1l, z};
133 cellOffsets32Pack[i++] = {2l, 2l, z};
134
135 if (i != 32) {
136 utils::ExceptionHandler::exception("Internal error: Wrong number of offsets (expected: 32, actual: {})", i);
137 }
138
139 return cellOffsets32Pack;
140}
141
151template <class ParticleCell_T, class Functor_T>
152void LCC04Traversal<ParticleCell_T, Functor_T>::processBasePack32(std::vector<ParticleCell_T> &cells,
153 const std::array<long, 3> &base3DIndex) {
155 std::array<long, 3> index{};
156 const std::array<long, 3> signedDims = utils::ArrayUtils::static_cast_copy_array<long>(this->_cellsPerDimension);
157
158 for (auto offset32Pack : _cellOffsets32Pack) {
159 // compute 3D index
160 bool isIn = true;
161 for (int d = 0; d < 3; ++d) {
162 index[d] = base3DIndex[d] + offset32Pack[d];
163 isIn &= (index[d] >= 0l) and (index[d] < _end[d]);
164 }
165
166 if (isIn) {
167 const unsigned long ulIndex = threeToOneD(index, signedDims);
168 _cellHandler.processBaseCell(cells, ulIndex);
169 }
170 }
171}
172
180template <class ParticleCell_T, class Functor_T>
182 auto &cells = *(this->_cells);
183 AUTOPAS_OPENMP(parallel) {
184 for (int color = 0; color < 4; ++color) {
185 traverseSingleColor(cells, color);
186
187 if (color < 3) {
188 AUTOPAS_OPENMP(barrier)
189 }
190 }
191 } // close parallel region
192}
193
194template <class ParticleCell_T, class Functor_T>
195void LCC04Traversal<ParticleCell_T, Functor_T>::traverseSingleColor(std::vector<ParticleCell_T> &cells, int color) {
196 // we need to traverse one body-centered cubic (BCC) grid, which consists of two cartesian grids
197
198 // colors 0 and 2 form one cartesian grid
199 // colors 1 and 3 form another cartesian grid, whose origin is shifted by (2,2,2)
200
201 // determine a starting point of one of the grids
202 std::array<long, 3> startOfThisColor{};
203
204 switch (color % 2) {
205 case 0:
206 // colours 0 and 2
207 startOfThisColor = {-2l, -2l, -2l};
208 break;
209 case 1:
210 // colours 1 and 3
211 startOfThisColor = {0l, 0l, 0l};
212 break;
213 }
214
215 // calculate whether the calculated starting point is part of the color
216 long correctParity = parity(startOfThisColor[0], startOfThisColor[1], startOfThisColor[2]);
217 if (color >= 2) {
218 correctParity += 4;
219 }
220
221 // to fix intel64 icpc compiler complaints about perfectly nested loop (tested with version 19.0.4.20190416).
222 const long startX = startOfThisColor[0], endX = _end[0];
223 const long startY = startOfThisColor[1], endY = _end[1];
224 const long startZ = startOfThisColor[2], endZ = _end[2];
225
226 // first cartesian grid
227 // grids are interlinked: one grid fills the gaps in the other grid
228 AUTOPAS_OPENMP(for schedule(dynamic, 1) collapse(3) nowait)
229 for (long z = startZ; z < endZ; z += 4) {
230 for (long y = startY; y < endY; y += 4) {
231 for (long x = startX; x < endX; x += 4) {
232 const long par = parity(x, y, z);
233
234 if (par != correctParity) {
235 continue;
236 }
237
238 const std::array<long, 3> base3DIndex = {x, y, z};
239 processBasePack32(cells, base3DIndex);
240 }
241 }
242 }
243}
244
245} // namespace autopas
#define AUTOPAS_OPENMP(args)
Empty macro to throw away any arguments.
Definition: WrapOpenMP.h:126
This class provides the base for traversals using the c08 base step.
Definition: C08BasedTraversal.h:24
std::array< unsigned long, 3 > _cellsPerDimension
The dimensions of the cellblock.
Definition: CellTraversal.h:63
const double _interactionLength
Interaction length (cutoff + skin).
Definition: ColorBasedTraversal.h:111
std::array< unsigned long, 3 > _overlap
overlap of interacting cells.
Definition: ColorBasedTraversal.h:121
const std::array< double, 3 > _cellLength
cell length in CellBlock3D.
Definition: ColorBasedTraversal.h:116
This class provides the c04 traversal.
Definition: LCC04Traversal.h:29
void setSoASortingThreshold(size_t soaSortingThreshold) override
Set the SoA sorting-threshold for traversals that use the CellFunctor.
Definition: LCC04Traversal.h:77
LCC04Traversal(const std::array< unsigned long, 3 > &dims, Functor_T &functor, double interactionLength, const std::array< double, 3 > &cellLength, DataLayoutOption dataLayout, bool useNewton3)
Constructor of the c04 traversal.
Definition: LCC04Traversal.h:41
void setAoSSortingThreshold(size_t aosSortingThreshold) override
Set the aos-sorting-threshold for traversals that use the CellFunctor If the sum of the number of par...
Definition: LCC04Traversal.h:71
bool isApplicableToDomain() const override
C04 traversals are usable, if cellSizeFactor >= 1.0 and there are at least 3 cells for each dimension...
Definition: LCC04Traversal.h:59
TraversalOption getTraversalType() const override
Return a enum representing the name of the traversal class.
Definition: LCC04Traversal.h:53
void traverseParticles() override
Go through one color and search for blocks belonging to the specified color.
Definition: LCC04Traversal.h:181
Interface for traversals used by the LinkedCell class.
Definition: LCTraversalInterface.h:18
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:64
constexpr T threeToOneD(T x, T y, T z, const std::array< T, 3 > &dims)
Convert a 3d index to a 1d index.
Definition: ThreeDimensionalMapping.h:29
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:34