80template <
class ParticleCell,
class Functor,
bool combineSoA = false>
96 explicit LCC01Traversal(
const std::array<unsigned long, 3> &dims,
Functor &functor,
const double interactionLength,
97 const std::array<double, 3> &cellLength, DataLayoutOption dataLayout,
bool useNewton3)
99 dataLayout, useNewton3),
100 _cellFunctor(functor, interactionLength ,
101 dataLayout, useNewton3),
121 return (combineSoA) ? TraversalOption::lc_c01_combined_SoA : TraversalOption::lc_c01;
127 void setSortingThreshold(
size_t sortingThreshold)
override { _cellFunctor.setSortingThreshold(sortingThreshold); }
131 using CellOffsetsType = std::conditional_t<decltype(utils::isPairwiseFunctor<Functor>())::value,
132 std::vector<std::vector<std::pair<
long, std::array<double, 3>>>>,
133 std::vector<std::tuple<
long,
long, std::array<double, 3>>>>;
136 using CellFunctorType = std::conditional_t<decltype(utils::isPairwiseFunctor<Functor>())::value,
148 inline void processBaseCell(std::vector<ParticleCell> &cells,
unsigned long x,
unsigned long y,
unsigned long z);
154 inline void processBaseCellPairwise(std::vector<ParticleCell> &cells,
unsigned long x,
unsigned long y,
161 inline void processBaseCellTriwise(std::vector<ParticleCell> &cells,
unsigned long x,
unsigned long y,
168 void computePairwiseOffsets();
174 void computeTriwiseOffsets();
182 template <std::size_t... I>
185 appendCell._particleSoABuffer);
192 void resizeBuffers();
198 CellOffsetsType _cellOffsets;
203 CellFunctorType _cellFunctor;
213 std::vector<std::vector<ParticleCell>> _combinationSlices;
218 std::vector<unsigned int> _currentSlices;
223 const unsigned int _cacheOffset;
226template <
class ParticleCell,
class Functor,
bool combineSoA>
229 computePairwiseOffsets();
231 computeTriwiseOffsets();
237template <
class ParticleCell,
class Functor,
bool combineSoA>
239 _cellOffsets.resize(2 * this->_overlap[0] + 1);
241 const auto interactionLengthSquare{this->_interactionLength * this->_interactionLength};
243 for (
long x = -this->_overlap[0]; x <= 0l; ++x) {
244 for (
long y = -this->_overlap[1]; y <= static_cast<long>(this->_overlap[1]); ++y) {
245 for (
long z = -this->_overlap[2]; z <= static_cast<long>(this->_overlap[2]); ++z) {
246 const std::array<double, 3> pos = {
247 std::max(0l, (std::abs(x) - 1l)) * this->_cellLength[0],
248 std::max(0l, (std::abs(y) - 1l)) * this->_cellLength[1],
249 std::max(0l, (std::abs(z) - 1l)) * this->_cellLength[2],
252 if (distSquare <= interactionLengthSquare) {
254 x, y, z, utils::ArrayUtils::static_cast_copy_array<long>(this->_cellsPerDimension));
255 const bool containCurrentOffset =
256 std::any_of(_cellOffsets[x + this->_overlap[0]].cbegin(), _cellOffsets[x + this->_overlap[0]].cend(),
257 [currentOffset](
const auto &e) {
return e.first == currentOffset; });
258 if (containCurrentOffset) {
261 for (
long ix = x; ix <= std::abs(x); ++ix) {
263 ix, y, z, utils::ArrayUtils::static_cast_copy_array<long>(this->_cellsPerDimension));
264 const size_t index = ix + this->_overlap[0];
268 std::array<double, 3> sortingDir = {
static_cast<double>(ix) * this->_cellLength[0],
269 static_cast<double>(y) * this->_cellLength[1],
270 static_cast<double>(z) * this->_cellLength[2]};
273 if (ix == 0 and y == 0 and z == 0) {
274 sortingDir = {1., 1., 1.};
278 if (y == 0l and z == 0l) {
280 _cellOffsets[index].insert(_cellOffsets[index].cbegin(), std::make_pair(offset, sortingDir));
282 _cellOffsets[index].emplace_back(offset, sortingDir);
291template <
class ParticleCell,
class Functor,
bool combineSoA>
292inline void LCC01Traversal<ParticleCell, Functor, combineSoA>::computeTriwiseOffsets() {
293 using namespace utils::ArrayMath::literals;
295 const int cubeSize = this->_overlap[0] * this->_overlap[1] * this->_overlap[2];
296 _cellOffsets.reserve(cubeSize * cubeSize / 4);
299 auto cellDistance = [&](
long x1,
long y1,
long z1,
long x2,
long y2,
long z2) {
300 return std::array<double, 3>{std::max(0l, (std::abs(x1 - x2) - 1l)) * this->_cellLength[0],
301 std::max(0l, (std::abs(y1 - y2) - 1l)) * this->_cellLength[1],
302 std::max(0l, (std::abs(z1 - z2) - 1l)) * this->_cellLength[2]};
305 const auto interactionLengthSquare{this->_interactionLength * this->_interactionLength};
306 _cellOffsets.emplace_back(0, 0, std::array<double, 3>{1., 1., 1.});
309 for (
long x1 = -this->_overlap[0]; x1 <= static_cast<long>(this->_overlap[0]); ++x1) {
310 for (
long y1 = -this->_overlap[1]; y1 <= static_cast<long>(this->_overlap[1]); ++y1) {
311 for (
long z1 = -this->_overlap[2]; z1 <= static_cast<long>(this->_overlap[2]); ++z1) {
313 const auto dist01 = cellDistance(0l, 0l, 0l, x1, y1, z1);
316 if (distSquare > interactionLengthSquare)
continue;
319 for (
long x2 = -this->_overlap[0]; x2 <= static_cast<long>(this->_overlap[0]); ++x2) {
320 for (
long y2 = -this->_overlap[1]; y2 <= static_cast<long>(this->_overlap[1]); ++y2) {
321 for (
long z2 = -this->_overlap[2]; z2 <= static_cast<long>(this->_overlap[2]); ++z2) {
323 const auto dist12 = cellDistance(x1, y1, z1, x2, y2, z2);
326 if (dist12Squared > interactionLengthSquare)
continue;
329 const auto dist02 = cellDistance(0l, 0l, 0l, x2, y2, z2);
332 if (dist02Squared > interactionLengthSquare)
continue;
335 x1, y1, z1, utils::ArrayUtils::static_cast_copy_array<long>(this->_cellsPerDimension));
338 x2, y2, z2, utils::ArrayUtils::static_cast_copy_array<long>(this->_cellsPerDimension));
341 if (offset2 <= offset1)
continue;
344 std::array<double, 3> sortDirection{};
346 sortDirection = {x2 * this->_cellLength[0], y2 * this->_cellLength[1], z2 * this->_cellLength[2]};
348 sortDirection = {x1 * this->_cellLength[0], y1 * this->_cellLength[1], z1 * this->_cellLength[2]};
359template <
class ParticleCell,
class Functor,
bool combineSoA>
360inline void LCC01Traversal<ParticleCell, Functor, combineSoA>::processBaseCell(std::vector<ParticleCell> &cells,
361 unsigned long x,
unsigned long y,
363 if constexpr (utils::isPairwiseFunctor<Functor>()) {
364 processBaseCellPairwise(cells, x, y, z);
365 }
else if constexpr (utils::isTriwiseFunctor<Functor>()) {
366 processBaseCellTriwise(cells, x, y, z);
369 "LCC01Traversal::processBaseCell(): Functor {} is not of type PairwiseFunctor or TriwiseFunctor.",
374template <
class ParticleCell,
class Functor,
bool combineSoA>
375inline void LCC01Traversal<ParticleCell, Functor, combineSoA>::processBaseCellPairwise(std::vector<ParticleCell> &cells,
376 unsigned long x,
unsigned long y,
379 ParticleCell &baseCell = cells[baseIndex];
380 const size_t cOffSize = _cellOffsets.size();
382 if constexpr (combineSoA) {
386 auto ¤tSlice = _currentSlices[threadID * _cacheOffset];
387 auto &combinationSlice = _combinationSlices[threadID];
390 if (x == this->_overlap[0]) {
392 for (
unsigned int offsetSlice = 0; offsetSlice < cOffSize; offsetSlice++) {
393 combinationSlice[offsetSlice]._particleSoABuffer.clear();
394 for (
const auto &offset : _cellOffsets[offsetSlice]) {
395 const unsigned long otherIndex = baseIndex + offset.first;
396 ParticleCell &otherCell = cells[otherIndex];
397 appendNeeded(combinationSlice[offsetSlice], otherCell,
404 const size_t midSlice = (currentSlice + this->_overlap[0] + 1) % cOffSize;
405 for (
size_t slice = (currentSlice + 1) % cOffSize; slice != midSlice; ++slice %= cOffSize, ++i) {
407 for (
const auto &offset : _cellOffsets[i]) {
408 const unsigned long otherIndex = baseIndex + offset.first;
409 ParticleCell &otherCell = cells[otherIndex];
410 newSize += otherCell.size();
412 combinationSlice[slice]._particleSoABuffer.resizeArrays(newSize);
415 for (
size_t slice = midSlice; slice != currentSlice; ++slice %= cOffSize, ++i) {
416 for (
auto offsetIndex = _cellOffsets[(i + 1) % cOffSize].size(); offsetIndex < _cellOffsets[i].size();
418 const unsigned long otherIndex = baseIndex + _cellOffsets[i][offsetIndex].first;
419 ParticleCell &otherCell = cells[otherIndex];
420 appendNeeded(combinationSlice[slice], otherCell,
425 combinationSlice[currentSlice]._particleSoABuffer.clear();
427 for (
const auto &offset : _cellOffsets.back()) {
428 const unsigned long otherIndex = baseIndex + offset.first;
429 ParticleCell &otherCell = cells[otherIndex];
430 appendNeeded(combinationSlice[currentSlice], otherCell,
434 ++currentSlice %= cOffSize;
438 for (
unsigned int slice = 0; slice < cOffSize; slice++) {
439 if (slice == (currentSlice + this->_overlap[0]) % cOffSize) {
442 auto startIndex = baseCell.size();
443 auto endIndex = combinationSlice[slice]._particleSoABuffer.size();
444 _functor.SoAFunctorPair(baseCell._particleSoABuffer,
445 {&(combinationSlice[slice]._particleSoABuffer), startIndex, endIndex},
false);
447 this->_cellFunctor.processCell(baseCell);
449 this->_cellFunctor.processCellPair(baseCell, combinationSlice[slice]);
453 for (
const auto &slice : _cellOffsets) {
454 for (
auto const &[offset, r] : slice) {
455 const unsigned long otherIndex = baseIndex + offset;
456 ParticleCell &otherCell = cells[otherIndex];
458 if (baseIndex == otherIndex) {
459 this->_cellFunctor.processCell(baseCell);
461 this->_cellFunctor.processCellPair(baseCell, otherCell, r);
468template <
class ParticleCell,
class Functor,
bool combineSoA>
469inline void LCC01Traversal<ParticleCell, Functor, combineSoA>::processBaseCellTriwise(std::vector<ParticleCell> &cells,
470 unsigned long x,
unsigned long y,
473 ParticleCell &baseCell = cells[baseIndex];
475 for (
auto const &[offset1, offset2, r] : _cellOffsets) {
476 const unsigned long otherIndex1 = baseIndex + offset1;
477 const unsigned long otherIndex2 = baseIndex + offset2;
478 ParticleCell &otherCell1 = cells[otherIndex1];
479 ParticleCell &otherCell2 = cells[otherIndex2];
481 if (baseIndex == otherIndex1 and baseIndex == otherIndex2) {
482 this->_cellFunctor.processCell(baseCell);
483 }
else if (baseIndex == otherIndex1 and baseIndex != otherIndex2) {
484 this->_cellFunctor.processCellPair(baseCell, otherCell2);
485 }
else if (baseIndex != otherIndex1 and baseIndex == otherIndex2) {
486 this->_cellFunctor.processCellPair(baseCell, otherCell1);
487 }
else if (baseIndex != otherIndex1 and otherIndex1 == otherIndex2) {
488 this->_cellFunctor.processCellPair(baseCell, otherCell1);
490 this->_cellFunctor.processCellTriple(baseCell, otherCell1, otherCell2, r);
495template <
class ParticleCell,
class PairwiseFunctor,
bool combineSoA>
496inline void LCC01Traversal<ParticleCell, PairwiseFunctor, combineSoA>::resizeBuffers() {
498 if (_combinationSlices.size() != numThreads) {
499 _combinationSlices.resize(numThreads);
500 const auto cellOffsetsSize = _cellOffsets.size();
501 std::for_each(_combinationSlices.begin(), _combinationSlices.end(),
502 [cellOffsetsSize](
auto &e) { e.resize(cellOffsetsSize); });
503 _currentSlices.resize(numThreads * _cacheOffset);
507template <
class ParticleCell,
class Functor,
bool combineSoA>
509 auto &cells = *(this->_cells);
510 if (not this->isApplicableToDomain()) {
511 if constexpr (combineSoA) {
513 "The C01 traversal with combined SoA buffers cannot work with data layout AoS and enabled newton3 (unless "
514 "only one thread is used)!");
517 "The C01 traversal cannot work with enabled newton3 (unless only one thread is used)!");
520 if constexpr (combineSoA) {
523 this->c01Traversal([&](
unsigned long x,
unsigned long y,
unsigned long z) { this->processBaseCell(cells, x, y, z); });
This class provides the base for traversals using the c01 base step.
Definition: C01BasedTraversal.h:25
Functor base class.
Definition: Functor.h:41
static constexpr std::array< typename Particle_T::AttributeNames, 0 > getNeededAttr()
Get attributes needed for computation.
Definition: Functor.h:78
This class provides the c01 traversal and the c01 traversal with combined SoA buffers.
Definition: LCC01Traversal.h:82
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: LCC01Traversal.h:127
void traverseParticles() override
Traverse the particles by pairs, triplets etc.
Definition: LCC01Traversal.h:508
TraversalOption getTraversalType() const override
Return a enum representing the name of the traversal class.
Definition: LCC01Traversal.h:120
void computeOffsets()
Computes all combinations of cells used in processBaseCell()
Definition: LCC01Traversal.h:227
bool isApplicableToDomain() const override
LC C01 is always applicable to the domain.
Definition: LCC01Traversal.h:118
LCC01Traversal(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 c01 traversal.
Definition: LCC01Traversal.h:96
Interface for traversals used by the LinkedCell class.
Definition: LCTraversalInterface.h:18
Class for Cells of Particles.
Definition: ParticleCell.h:49
A cell functor.
Definition: CellFunctor3B.h:24
A cell functor.
Definition: CellFunctor.h:25
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:64
constexpr T dot(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Generates the dot product of two arrays.
Definition: ArrayMath.h:233
constexpr std::array< T, SIZE > normalize(const std::array< T, SIZE > &a)
Generates a normalized array (|a| = 1).
Definition: ArrayMath.h:304
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
decltype(isTriwiseFunctorImpl(std::declval< FunctorT >())) isTriwiseFunctor
Check whether a Functor Type is inheriting from TriwiseFunctor.
Definition: checkFunctorType.h:56
decltype(isPairwiseFunctorImpl(std::declval< FunctorT >())) isPairwiseFunctor
Check whether a Functor Type is inheriting from PairwiseFunctor.
Definition: checkFunctorType.h:49
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:34
int autopas_get_max_threads()
Dummy for omp_get_max_threads() when no OpenMP is available.
Definition: WrapOpenMP.h:144
int autopas_get_thread_num()
Dummy for omp_set_lock() when no OpenMP is available.
Definition: WrapOpenMP.h:132
constexpr unsigned int DEFAULT_CACHE_LINE_SIZE
Default size for a cache line.
Definition: AlignedAllocator.h:21