AutoPas  3.0.0
Loading...
Searching...
No Matches
LCC04Traversal.h
Go to the documentation of this file.
1
8#pragma once
9
17
18namespace autopas {
19
29template <class ParticleCell, class PairwiseFunctor>
30class LCC04Traversal : public C08BasedTraversal<ParticleCell, PairwiseFunctor>, public LCTraversalInterface {
31 public:
42 LCC04Traversal(const std::array<unsigned long, 3> &dims, PairwiseFunctor *pairwiseFunctor, double interactionLength,
43 const std::array<double, 3> &cellLength, DataLayoutOption dataLayout, bool useNewton3)
44 : C08BasedTraversal<ParticleCell, PairwiseFunctor>(dims, pairwiseFunctor, interactionLength, cellLength,
45 dataLayout, useNewton3),
46 _cellOffsets32Pack(computeOffsets32Pack()),
47 _cellHandler(pairwiseFunctor, this->_cellsPerDimension, interactionLength, cellLength, this->_overlap,
48 dataLayout, useNewton3),
49 _end(utils::ArrayMath::subScalar(utils::ArrayUtils::static_cast_copy_array<long>(this->_cellsPerDimension),
50 1l)) {}
51
52 void traverseParticles() override;
53
54 [[nodiscard]] TraversalOption getTraversalType() const override { return TraversalOption::lc_c04; }
55
60 [[nodiscard]] bool isApplicable() const override {
61 // The cellsize cannot be smaller then the cutoff, if OpenMP is used.
62 // Also see: https://github.com/AutoPas/AutoPas/issues/464
63 const double minLength = *std::min_element(this->_cellLength.cbegin(), this->_cellLength.cend());
64 const unsigned long minDim = *std::min_element(this->_cellsPerDimension.cbegin(), this->_cellsPerDimension.cend());
65
66 return minLength >= this->_interactionLength and minDim > 3;
67 }
68
72 void setSortingThreshold(size_t sortingThreshold) override { _cellHandler.setSortingThreshold(sortingThreshold); }
73
74 private:
75 void traverseSingleColor(std::vector<ParticleCell> &cells, int color);
76
77 void processBasePack32(std::vector<ParticleCell> &cells, const std::array<long, 3> &base3DIndex);
78
79 constexpr auto computeOffsets32Pack() const;
80
81 [[nodiscard]] constexpr long parity(long x, long y, long z) const { return (x + y + z + 24) % 8; }
82
83 std::array<std::array<long, 3>, 32> _cellOffsets32Pack;
84
85 LCC08CellHandler<ParticleCell, PairwiseFunctor> _cellHandler;
86
87 std::array<long, 3> _end;
88};
89
96template <class ParticleCell, class PairwiseFunctor>
97constexpr auto LCC04Traversal<ParticleCell, PairwiseFunctor>::computeOffsets32Pack() const {
98 using std::make_pair;
100
101 std::array<std::array<long, 3>, 32> cellOffsets32Pack = {};
102
103 unsigned int i = 0;
104 long z = 0l;
105 cellOffsets32Pack[i++] = {1l, 1l, z};
106 cellOffsets32Pack[i++] = {1l, 2l, z};
107 cellOffsets32Pack[i++] = {2l, 1l, z};
108 cellOffsets32Pack[i++] = {2l, 2l, z};
109
110 // z = 1ul; z = 2ul
111 for (z = 1l; z < 3l; ++z) {
112 for (long y = 0l; y < 4l; y++) {
113 for (long x = 0l; x < 4l; x++) {
114 if ((x == 0l and y == 0l) or (x == 3l and y == 0l) or (x == 0l and y == 3l) or (x == 3l and y == 3l)) {
115 continue;
116 }
117 cellOffsets32Pack[i++] = {x, y, z};
118 }
119 }
120 }
121
122 z = 3ul;
123 cellOffsets32Pack[i++] = {1l, 1l, z};
124 cellOffsets32Pack[i++] = {1l, 2l, z};
125 cellOffsets32Pack[i++] = {2l, 1l, z};
126 cellOffsets32Pack[i++] = {2l, 2l, z};
127
128 if (i != 32) {
129 utils::ExceptionHandler::exception("Internal error: Wrong number of offsets (expected: 32, actual: {})", i);
130 }
131
132 return cellOffsets32Pack;
133}
134
144template <class ParticleCell, class PairwiseFunctor>
145void LCC04Traversal<ParticleCell, PairwiseFunctor>::processBasePack32(std::vector<ParticleCell> &cells,
146 const std::array<long, 3> &base3DIndex) {
148 std::array<long, 3> index{};
149 const std::array<long, 3> signedDims = utils::ArrayUtils::static_cast_copy_array<long>(this->_cellsPerDimension);
150
151 for (auto offset32Pack : _cellOffsets32Pack) {
152 // compute 3D index
153 bool isIn = true;
154 for (int d = 0; d < 3; ++d) {
155 index[d] = base3DIndex[d] + offset32Pack[d];
156 isIn &= (index[d] >= 0l) and (index[d] < _end[d]);
157 }
158
159 if (isIn) {
160 const unsigned long ulIndex = threeToOneD(index, signedDims);
161 _cellHandler.processBaseCell(cells, ulIndex);
162 }
163 }
164}
165
173template <class ParticleCell, class PairwiseFunctor>
175 auto &cells = *(this->_cells);
176 AUTOPAS_OPENMP(parallel) {
177 for (int color = 0; color < 4; ++color) {
178 traverseSingleColor(cells, color);
179
180 if (color < 3) {
181 AUTOPAS_OPENMP(barrier)
182 }
183 }
184 } // close parallel region
185}
186
187template <class ParticleCell, class PairwiseFunctor>
188void LCC04Traversal<ParticleCell, PairwiseFunctor>::traverseSingleColor(std::vector<ParticleCell> &cells, int color) {
189 // we need to traverse one body-centered cubic (BCC) grid, which consists of two cartesian grids
190
191 // colors 0 and 2 form one cartesian grid
192 // colors 1 and 3 form another cartesian grid, whose origin is shifted by (2,2,2)
193
194 // determine a starting point of one of the grids
195 std::array<long, 3> startOfThisColor{};
196
197 switch (color % 2) {
198 case 0:
199 // colours 0 and 2
200 startOfThisColor = {-2l, -2l, -2l};
201 break;
202 case 1:
203 // colours 1 and 3
204 startOfThisColor = {0l, 0l, 0l};
205 break;
206 }
207
208 // calculate whether the calculated starting point is part of the color
209 long correctParity = parity(startOfThisColor[0], startOfThisColor[1], startOfThisColor[2]);
210 if (color >= 2) {
211 correctParity += 4;
212 }
213
214 // to fix intel64 icpc compiler complaints about perfectly nested loop (tested with version 19.0.4.20190416).
215 const long startX = startOfThisColor[0], endX = _end[0];
216 const long startY = startOfThisColor[1], endY = _end[1];
217 const long startZ = startOfThisColor[2], endZ = _end[2];
218
219 // first cartesian grid
220 // grids are interlinked: one grid fills the gaps in the other grid
221 AUTOPAS_OPENMP(for schedule(dynamic, 1) collapse(3) nowait)
222 for (long z = startZ; z < endZ; z += 4) {
223 for (long y = startY; y < endY; y += 4) {
224 for (long x = startX; x < endX; x += 4) {
225 const long par = parity(x, y, z);
226
227 if (par != correctParity) {
228 continue;
229 }
230
231 const std::array<long, 3> base3DIndex = {x, y, z};
232 processBasePack32(cells, base3DIndex);
233 }
234 }
235 }
236}
237
238} // 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:30
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:72
void traverseParticles() override
Go through one color and search for blocks belonging to the specified color.
Definition: LCC04Traversal.h:174
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:60
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:42
TraversalOption getTraversalType() const override
Return a enum representing the name of the traversal class.
Definition: LCC04Traversal.h:54
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:51
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