AutoPas  3.0.0
Loading...
Searching...
No Matches
ColorBasedTraversal.h
Go to the documentation of this file.
1
7#pragma once
8
16
17namespace autopas {
18
26template <class ParticleCell, class Functor, int collapseDepth = 3>
27class ColorBasedTraversal : public CellTraversal<ParticleCell>, public TraversalInterface {
28 protected:
39 explicit ColorBasedTraversal(const std::array<unsigned long, 3> &dims, Functor *functor,
40 const double interactionLength, const std::array<double, 3> &cellLength,
41 DataLayoutOption dataLayout, bool useNewton3)
43 TraversalInterface(dataLayout, useNewton3),
44 _interactionLength(interactionLength),
45 _cellLength(cellLength),
46 _dataLayoutConverter(functor, dataLayout) {
47 for (unsigned int d = 0; d < 3; d++) {
48 _overlap[d] = std::ceil(_interactionLength / _cellLength[d]);
49 }
50 }
51
55 ~ColorBasedTraversal() override = default;
56
57 public:
61 void initTraversal() override {
62 if (this->_cells) {
63 auto &cells = *(this->_cells);
65 AUTOPAS_OPENMP(parallel for)
66 for (size_t i = 0; i < cells.size(); ++i) {
67 _dataLayoutConverter.loadDataLayout(cells[i]);
68 }
69 }
70 }
71
75 void endTraversal() override {
76 if (this->_cells) {
77 auto &cells = *(this->_cells);
79 AUTOPAS_OPENMP(parallel for)
80 for (size_t i = 0; i < cells.size(); ++i) {
81 _dataLayoutConverter.storeDataLayout(cells[i]);
82 }
83 }
84 }
85
86 protected:
96 template <typename LoopBody>
97 inline void colorTraversal(LoopBody &&loopBody, const std::array<unsigned long, 3> &end,
98 const std::array<unsigned long, 3> &stride,
99 const std::array<unsigned long, 3> &offset = {0ul, 0ul, 0ul});
100
106 virtual void notifyColorChange(unsigned long newColor){};
107
111 const double _interactionLength;
112
116 const std::array<double, 3> _cellLength;
117
121 std::array<unsigned long, 3> _overlap;
122
123 private:
127 utils::DataLayoutConverter<Functor> _dataLayoutConverter;
128};
129
130template <class ParticleCell, class Functor, int collapseDepth>
131template <typename LoopBody>
133 LoopBody &&loopBody, const std::array<unsigned long, 3> &end, const std::array<unsigned long, 3> &stride,
134 const std::array<unsigned long, 3> &offset) {
135 using namespace autopas::utils::ArrayMath::literals;
136 AUTOPAS_OPENMP(parallel) {
137 const unsigned long numColors = stride[0] * stride[1] * stride[2];
138 for (unsigned long col = 0; col < numColors; ++col) {
139 AUTOPAS_OPENMP(single) {
140 // barrier at omp for of previous loop iteration, so fine to change it for everyone!
141 notifyColorChange(col);
142 // implicit barrier at end of function.
143 }
144 const std::array<unsigned long, 3> startWithoutOffset(utils::ThreeDimensionalMapping::oneToThreeD(col, stride));
145 const std::array<unsigned long, 3> start(startWithoutOffset + offset);
146
147 // intel compiler demands following:
148 const unsigned long start_x = start[0], start_y = start[1], start_z = start[2];
149 const unsigned long end_x = end[0], end_y = end[1], end_z = end[2];
150 const unsigned long stride_x = stride[0], stride_y = stride[1], stride_z = stride[2];
151 if (collapseDepth == 2) {
152 AUTOPAS_OPENMP(for schedule(dynamic, 1) collapse(2))
153 for (unsigned long z = start_z; z < end_z; z += stride_z) {
154 for (unsigned long y = start_y; y < end_y; y += stride_y) {
155 for (unsigned long x = start_x; x < end_x; x += stride_x) {
156 // Don't exchange order of execution (x must be last!), it would break other code
157 loopBody(x, y, z);
158 }
159 }
160 }
161 } else {
162 AUTOPAS_OPENMP(for schedule(dynamic, 1) collapse(3))
163 for (unsigned long z = start_z; z < end_z; z += stride_z) {
164 for (unsigned long y = start_y; y < end_y; y += stride_y) {
165 for (unsigned long x = start_x; x < end_x; x += stride_x) {
166 // Don't exchange order of execution (x must be last!), it would break other code
167 loopBody(x, y, z);
168 }
169 }
170 }
171 }
172 }
173 }
174}
175
176} // namespace autopas
#define AUTOPAS_OPENMP(args)
Empty macro to throw away any arguments.
Definition: WrapOpenMP.h:126
A cell pair traversal.
Definition: CellTraversal.h:23
std::vector< ParticleCell > * _cells
The cells to traverse.
Definition: CellTraversal.h:60
This class provides the base for traversals using base steps based on cell coloring.
Definition: ColorBasedTraversal.h:27
void initTraversal() override
load Data Layouts required for this Traversal if cells have been set through setCellsToTraverse().
Definition: ColorBasedTraversal.h:61
ColorBasedTraversal(const std::array< unsigned long, 3 > &dims, Functor *functor, const double interactionLength, const std::array< double, 3 > &cellLength, DataLayoutOption dataLayout, bool useNewton3)
Constructor of the ColorBasedTraversal.
Definition: ColorBasedTraversal.h:39
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
~ColorBasedTraversal() override=default
Destructor of ColorBasedTraversal.
const std::array< double, 3 > _cellLength
cell length in CellBlock3D.
Definition: ColorBasedTraversal.h:116
void endTraversal() override
write Data to AoS if cells have been set through setCellsToTraverse().
Definition: ColorBasedTraversal.h:75
void colorTraversal(LoopBody &&loopBody, const std::array< unsigned long, 3 > &end, const std::array< unsigned long, 3 > &stride, const std::array< unsigned long, 3 > &offset={0ul, 0ul, 0ul})
The main traversal of the ColorBasedTraversal.
Definition: ColorBasedTraversal.h:132
virtual void notifyColorChange(unsigned long newColor)
This method is called when the color during the traversal has changed.
Definition: ColorBasedTraversal.h:106
Functor base class.
Definition: Functor.h:40
Class for Cells of Particles.
Definition: ParticleCell.h:51
This interface serves as a common parent class for all traversals.
Definition: TraversalInterface.h:18
This converts cells to the target data Layout using the given functor.
Definition: DataLayoutConverter.h:19
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:32