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, class PairwiseFunctor>
29class LCC04Traversal : public C08BasedTraversal<ParticleCell, PairwiseFunctor>, public LCTraversalInterface {
30 public:
41 LCC04Traversal(const std::array<unsigned long, 3> &dims, PairwiseFunctor *pairwiseFunctor, double interactionLength,
42 const std::array<double, 3> &cellLength, DataLayoutOption dataLayout, bool useNewton3)
43 : C08BasedTraversal<ParticleCell, PairwiseFunctor>(dims, pairwiseFunctor, interactionLength, cellLength,
44 dataLayout, useNewton3),
45 _cellOffsets32Pack(computeOffsets32Pack()),
46 _cellHandler(pairwiseFunctor, this->_cellsPerDimension, interactionLength, cellLength, this->_overlap,
47 dataLayout, 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 isApplicable() 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 setSortingThreshold(size_t sortingThreshold) override { _cellHandler.setSortingThreshold(sortingThreshold); }
72
73 private:
74 void traverseSingleColor(std::vector<ParticleCell> &cells, int color);
75
76 void processBasePack32(std::vector<ParticleCell> &cells, const std::array<long, 3> &base3DIndex);
77
78 constexpr auto computeOffsets32Pack() const;
79
80 [[nodiscard]] constexpr long parity(long x, long y, long z) const { return (x + y + z + 24) % 8; }
81
82 std::array<std::array<long, 3>, 32> _cellOffsets32Pack;
83
84 LCC08CellHandler<ParticleCell, PairwiseFunctor> _cellHandler;
85
86 std::array<long, 3> _end;
87};
88
95template <class ParticleCell, class PairwiseFunctor>
96constexpr auto LCC04Traversal<ParticleCell, PairwiseFunctor>::computeOffsets32Pack() const {
97 using std::make_pair;
99
100 std::array<std::array<long, 3>, 32> cellOffsets32Pack = {};
101
102 unsigned int i = 0;
103 long z = 0l;
104 cellOffsets32Pack[i++] = {1l, 1l, z};
105 cellOffsets32Pack[i++] = {1l, 2l, z};
106 cellOffsets32Pack[i++] = {2l, 1l, z};
107 cellOffsets32Pack[i++] = {2l, 2l, z};
108
109 // z = 1ul; z = 2ul
110 for (z = 1l; z < 3l; ++z) {
111 for (long y = 0l; y < 4l; y++) {
112 for (long x = 0l; x < 4l; x++) {
113 if ((x == 0l and y == 0l) or (x == 3l and y == 0l) or (x == 0l and y == 3l) or (x == 3l and y == 3l)) {
114 continue;
115 }
116 cellOffsets32Pack[i++] = {x, y, z};
117 }
118 }
119 }
120
121 z = 3ul;
122 cellOffsets32Pack[i++] = {1l, 1l, z};
123 cellOffsets32Pack[i++] = {1l, 2l, z};
124 cellOffsets32Pack[i++] = {2l, 1l, z};
125 cellOffsets32Pack[i++] = {2l, 2l, z};
126
127 if (i != 32) {
128 utils::ExceptionHandler::exception("Internal error: Wrong number of offsets (expected: 32, actual: {})", i);
129 }
130
131 return cellOffsets32Pack;
132}
133
143template <class ParticleCell, class PairwiseFunctor>
144void LCC04Traversal<ParticleCell, PairwiseFunctor>::processBasePack32(std::vector<ParticleCell> &cells,
145 const std::array<long, 3> &base3DIndex) {
147 std::array<long, 3> index{};
148 const std::array<long, 3> signedDims = utils::ArrayUtils::static_cast_copy_array<long>(this->_cellsPerDimension);
149
150 for (auto offset32Pack : _cellOffsets32Pack) {
151 // compute 3D index
152 bool isIn = true;
153 for (int d = 0; d < 3; ++d) {
154 index[d] = base3DIndex[d] + offset32Pack[d];
155 isIn &= (index[d] >= 0l) and (index[d] < _end[d]);
156 }
157
158 if (isIn) {
159 const unsigned long ulIndex = threeToOneD(index, signedDims);
160 _cellHandler.processBaseCell(cells, ulIndex);
161 }
162 }
163}
164
172template <class ParticleCell, class PairwiseFunctor>
174 auto &cells = *(this->_cells);
175 AUTOPAS_OPENMP(parallel) {
176 for (int color = 0; color < 4; ++color) {
177 traverseSingleColor(cells, color);
178
179 if (color < 3) {
180 AUTOPAS_OPENMP(barrier)
181 }
182 }
183 } // close parallel region
184}
185
186template <class ParticleCell, class PairwiseFunctor>
187void LCC04Traversal<ParticleCell, PairwiseFunctor>::traverseSingleColor(std::vector<ParticleCell> &cells, int color) {
188 // we need to traverse one body-centered cubic (BCC) grid, which consists of two cartesian grids
189
190 // colors 0 and 2 form one cartesian grid
191 // colors 1 and 3 form another cartesian grid, whose origin is shifted by (2,2,2)
192
193 // determine a starting point of one of the grids
194 std::array<long, 3> startOfThisColor{};
195
196 switch (color % 2) {
197 case 0:
198 // colours 0 and 2
199 startOfThisColor = {-2l, -2l, -2l};
200 break;
201 case 1:
202 // colours 1 and 3
203 startOfThisColor = {0l, 0l, 0l};
204 break;
205 }
206
207 // calculate whether the calculated starting point is part of the color
208 long correctParity = parity(startOfThisColor[0], startOfThisColor[1], startOfThisColor[2]);
209 if (color >= 2) {
210 correctParity += 4;
211 }
212
213 // to fix intel64 icpc compiler complaints about perfectly nested loop (tested with version 19.0.4.20190416).
214 const long startX = startOfThisColor[0], endX = _end[0];
215 const long startY = startOfThisColor[1], endY = _end[1];
216 const long startZ = startOfThisColor[2], endZ = _end[2];
217
218 // first cartesian grid
219 // grids are interlinked: one grid fills the gaps in the other grid
220 AUTOPAS_OPENMP(for schedule(dynamic, 1) collapse(3) nowait)
221 for (long z = startZ; z < endZ; z += 4) {
222 for (long y = startY; y < endY; y += 4) {
223 for (long x = startX; x < endX; x += 4) {
224 const long par = parity(x, y, z);
225
226 if (par != correctParity) {
227 continue;
228 }
229
230 const std::array<long, 3> base3DIndex = {x, y, z};
231 processBasePack32(cells, base3DIndex);
232 }
233 }
234 }
235}
236
237} // 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:55
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 setSortingThreshold(size_t sortingThreshold) override
Set the sorting-threshold for traversals that use the CellFunctor If the sum of the number of particl...
Definition: LCC04Traversal.h:71
void traverseParticles() override
Go through one color and search for blocks belonging to the specified color.
Definition: LCC04Traversal.h:173
bool isApplicable() const override
C04 traversals are usable, if cellSizeFactor >= 1.0 and there are at least 3 cells for each dimension...
Definition: LCC04Traversal.h:59
LCC04Traversal(const std::array< unsigned long, 3 > &dims, PairwiseFunctor *pairwiseFunctor, double interactionLength, const std::array< double, 3 > &cellLength, DataLayoutOption dataLayout, bool useNewton3)
Constructor of the c04 traversal.
Definition: LCC04Traversal.h:41
TraversalOption getTraversalType() const override
Return a enum representing the name of the traversal class.
Definition: LCC04Traversal.h:53
Interface for traversals used by the LinkedCell class.
Definition: LCTraversalInterface.h:18
PairwiseFunctor class.
Definition: PairwiseFunctor.h:31
Class for Cells of Particles.
Definition: ParticleCell.h:49
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:63
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:32