24template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional = true>
36 explicit CellFunctor3B(ParticleFunctor_T &f,
const double sortingCutoff, DataLayoutOption dataLayout,
bool useNewton3)
37 : _functor(f), _sortingCutoff(sortingCutoff), _dataLayout(dataLayout), _useNewton3(useNewton3) {}
55 const std::array<double, 3> &sortingDirection = {0., 0., 0.});
65 void processCellTriple(ParticleCell_T &cell1, ParticleCell_T &cell2, ParticleCell_T &cell3,
66 const std::array<double, 3> &sortingDirection = {0., 0., 0.});
72 [[nodiscard]] DataLayoutOption::Value
getDataLayout()
const {
return _dataLayout; }
78 [[nodiscard]]
bool getNewton3()
const {
return _useNewton3; }
109 [[nodiscard]]
bool shouldUseSorting(
size_t particleCount,
const std::array<double, 3> &sortingDirection)
const {
110 return particleCount >= _aosSortingThreshold and
111 (sortingDirection[0] != 0.0 or sortingDirection[1] != 0.0 or sortingDirection[2] != 0.0);
122 return infoSingle->threshold;
124 throw utils::ExceptionHandler::AutoPasException(
125 "CellFunctor3b was called with wrong SortingThresholdInfo Type. (Supported is single)");
137 void processCellAoSImpl(ParticleCell_T &cell);
146 void processCellPairAoSImpl(ParticleCell_T &cell1, ParticleCell_T &cell2,
147 const std::array<double, 3> &sortingDirection);
159 void processCellTripleAoSImpl(ParticleCell_T &cell1, ParticleCell_T &cell2, ParticleCell_T &cell3,
160 const std::array<double, 3> &sortingDirection);
167 void processCellPairSoAImpl(ParticleCell_T &cell1, ParticleCell_T &cell2);
175 void processCellTripleSoAImpl(ParticleCell_T &cell1, ParticleCell_T &cell2, ParticleCell_T &cell3);
177 ParticleFunctor_T &_functor;
179 const double _sortingCutoff;
186 size_t _aosSortingThreshold{8};
193 size_t _soaSortingThreshold{100};
195 const DataLayoutOption::Value _dataLayout;
197 const bool _useNewton3;
200template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
203 _aosSortingThreshold = resolveThreshold(aosSortingThreshold);
206template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
209 _soaSortingThreshold = resolveThreshold(soaSortingThreshold);
212template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
214 const bool isAoS = _dataLayout == DataLayoutOption::aos;
215 const bool isSoA = _dataLayout == DataLayoutOption::soa;
218 if ((isSoA and cell._particleSoABuffer.size() == 0) or (isAoS and cell.isEmpty())) {
222 if (not cell.canHaveOwnedParticles()) {
227 processCellAoSImpl(cell);
229 _functor.SoAFunctorSingle(cell._particleSoABuffer, _useNewton3);
233template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
235 ParticleCell_T &cell1, ParticleCell_T &cell2,
const std::array<double, 3> &sortingDirection) {
236 const bool isAoS = _dataLayout == DataLayoutOption::aos;
237 const bool isSoA = _dataLayout == DataLayoutOption::soa;
240 if ((isSoA and (cell1._particleSoABuffer.size() == 0 or cell2._particleSoABuffer.size() == 0)) or
241 (isAoS and (cell1.isEmpty() or cell2.isEmpty()))) {
245 if (not cell1.canHaveOwnedParticles()) {
247 if constexpr (not bidirectional) {
248 if (not _useNewton3) {
253 if (not cell2.canHaveOwnedParticles()) {
259 processCellPairAoSImpl(cell1, cell2, sortingDirection);
261 processCellPairSoAImpl(cell1, cell2);
265template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
267 ParticleCell_T &cell1, ParticleCell_T &cell2, ParticleCell_T &cell3,
268 const std::array<double, 3> &sortingDirection) {
269 const bool isAoS = _dataLayout == DataLayoutOption::aos;
270 const bool isSoA = _dataLayout == DataLayoutOption::soa;
273 if ((isSoA and (cell1._particleSoABuffer.size() == 0 or cell2._particleSoABuffer.size() == 0 or
274 cell3._particleSoABuffer.size() == 0)) or
275 (isAoS and (cell1.isEmpty() or cell2.isEmpty() or cell3.isEmpty()))) {
279 if (not cell1.canHaveOwnedParticles()) {
281 if constexpr (not bidirectional) {
282 if (not _useNewton3) {
287 if (not cell2.canHaveOwnedParticles() and not cell3.canHaveOwnedParticles()) {
293 processCellTripleAoSImpl(cell1, cell2, cell3, sortingDirection);
295 processCellTripleSoAImpl(cell1, cell2, cell3);
299template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
302 const auto interactParticles = [
this](
auto &p1,
auto &p2,
auto &p3) {
303 this->_functor.AoSFunctor(p1, p2, p3, this->_useNewton3);
304 if (not this->_useNewton3) {
305 this->_functor.AoSFunctor(p2, p1, p3,
false);
306 this->_functor.AoSFunctor(p3, p1, p2,
false);
310 if (cell.size() >= _aosSortingThreshold) {
313 for (
auto cellIter1 = cellSorted._particles.begin(); cellIter1 != cellSorted._particles.end(); ++cellIter1) {
314 auto &[p1Projection, p1Ptr] = *cellIter1;
316 for (
auto cellIter2 = std::next(cellIter1); cellIter2 != cellSorted._particles.end(); ++cellIter2) {
317 auto &[p2Projection, p2Ptr] = *cellIter2;
318 if (std::abs(p2Projection - p1Projection) > _sortingCutoff) {
322 for (
auto cellIter3 = std::next(cellIter2); cellIter3 != cellSorted._particles.end(); ++cellIter3) {
323 auto &[p3Projection, p3Ptr] = *cellIter3;
324 if (std::abs(p3Projection - p1Projection) > _sortingCutoff or
325 std::abs(p3Projection - p2Projection) > _sortingCutoff) {
328 interactParticles(*p1Ptr, *p2Ptr, *p3Ptr);
333 for (
auto p1Ptr = cell.begin(); p1Ptr != cell.end(); ++p1Ptr) {
336 for (; p2Ptr != cell.end(); ++p2Ptr) {
339 for (; p3Ptr != cell.end(); ++p3Ptr) {
340 interactParticles(*p1Ptr, *p2Ptr, *p3Ptr);
347template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
348void CellFunctor3B<ParticleCell_T, ParticleFunctor_T, bidirectional>::processCellPairAoSImpl(
349 ParticleCell_T &cell1, ParticleCell_T &cell2,
const std::array<double, 3> &sortingDirection) {
350 const auto interactParticles = [
this](
auto &p1,
auto &p2,
auto &p3,
const bool p2FromCell1) {
351 this->_functor.AoSFunctor(p1, p2, p3, this->_useNewton3);
352 if (not this->_useNewton3) {
354 this->_functor.AoSFunctor(p2, p1, p3,
false);
356 if constexpr (bidirectional) {
357 this->_functor.AoSFunctor(p2, p1, p3,
false);
360 if constexpr (bidirectional) {
361 this->_functor.AoSFunctor(p3, p1, p2,
false);
366 if (shouldUseSorting(cell1.size() + cell2.size(), sortingDirection)) {
367 SortedCellView<ParticleCell_T> cell1Sorted(cell1, sortingDirection);
368 SortedCellView<ParticleCell_T> cell2Sorted(cell2, sortingDirection);
371 for (
auto cellIter1 = cell1Sorted._particles.begin(); cellIter1 != cell1Sorted._particles.end(); ++cellIter1) {
372 auto &[p1Projection, p1Ptr] = *cellIter1;
375 for (
auto cellIter2 = std::next(cellIter1); cellIter2 != cell1Sorted._particles.end(); ++cellIter2) {
376 auto &[p2Projection, p2Ptr] = *cellIter2;
377 if (std::abs(p2Projection - p1Projection) > _sortingCutoff) {
380 for (
auto &[p3Projection, p3Ptr] : cell2Sorted._particles) {
381 if (std::abs(p3Projection - p2Projection) > _sortingCutoff or
382 std::abs(p3Projection - p1Projection) > _sortingCutoff) {
385 interactParticles(*p1Ptr, *p2Ptr, *p3Ptr,
true);
390 for (
auto cellIter2 = cell2Sorted._particles.begin(); cellIter2 != cell2Sorted._particles.end(); ++cellIter2) {
391 auto &[p2Projection, p2Ptr] = *cellIter2;
392 if (std::abs(p2Projection - p1Projection) > _sortingCutoff) {
395 for (
auto cellIter3 = std::next(cellIter2); cellIter3 != cell2Sorted._particles.end(); ++cellIter3) {
396 auto &[p3Projection, p3Ptr] = *cellIter3;
397 if (std::abs(p3Projection - p2Projection) > _sortingCutoff or
398 std::abs(p3Projection - p1Projection) > _sortingCutoff) {
401 interactParticles(*p1Ptr, *p2Ptr, *p3Ptr,
false);
407 for (
auto p1Ptr = cell1.begin(); p1Ptr != cell1.end(); ++p1Ptr) {
411 for (; p2Ptr != cell1.end(); ++p2Ptr) {
412 for (
auto &p3 : cell2) {
413 interactParticles(*p1Ptr, *p2Ptr, p3,
true);
418 for (
auto p2Ptr = cell2.begin(); p2Ptr != cell2.end(); ++p2Ptr) {
421 for (; p3Ptr != cell2.end(); ++p3Ptr) {
422 interactParticles(*p1Ptr, *p2Ptr, *p3Ptr,
false);
429template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
430void CellFunctor3B<ParticleCell_T, ParticleFunctor_T, bidirectional>::processCellTripleAoSImpl(
431 ParticleCell_T &cell1, ParticleCell_T &cell2, ParticleCell_T &cell3,
432 const std::array<double, 3> &sortingDirection) {
433 const auto interactParticles = [
this](
auto &p1,
auto &p2,
auto &p3) {
434 this->_functor.AoSFunctor(p1, p2, p3, this->_useNewton3);
436 if constexpr (bidirectional) {
437 if (not this->_useNewton3) {
438 this->_functor.AoSFunctor(p2, p1, p3,
false);
439 this->_functor.AoSFunctor(p3, p1, p2,
false);
444 if (shouldUseSorting(cell1.size() + cell2.size() + cell3.size(), sortingDirection)) {
445 SortedCellView<ParticleCell_T> cell1Sorted(cell1, sortingDirection);
446 SortedCellView<ParticleCell_T> cell2Sorted(cell2, sortingDirection);
448 for (
auto &[p1Projection, p1Ptr] : cell1Sorted._particles) {
449 for (
auto &[p2Projection, p2Ptr] : cell2Sorted._particles) {
450 if (std::abs(p2Projection - p1Projection) > _sortingCutoff) {
453 for (
auto &p3 : cell3) {
454 interactParticles(*p1Ptr, *p2Ptr, p3);
459 for (
auto &p1 : cell1) {
460 for (
auto &p2 : cell2) {
461 for (
auto &p3 : cell3) {
462 interactParticles(p1, p2, p3);
469template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
470void CellFunctor3B<ParticleCell_T, ParticleFunctor_T, bidirectional>::processCellPairSoAImpl(ParticleCell_T &cell1,
471 ParticleCell_T &cell2) {
472 _functor.SoAFunctorPair(cell1._particleSoABuffer, cell2._particleSoABuffer, _useNewton3);
473 if constexpr (bidirectional) {
474 if (not _useNewton3) {
475 _functor.SoAFunctorPair(cell2._particleSoABuffer, cell1._particleSoABuffer,
false);
480template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
481void CellFunctor3B<ParticleCell_T, ParticleFunctor_T, bidirectional>::processCellTripleSoAImpl(ParticleCell_T &cell1,
482 ParticleCell_T &cell2,
483 ParticleCell_T &cell3) {
484 _functor.SoAFunctorTriple(cell1._particleSoABuffer, cell2._particleSoABuffer, cell3._particleSoABuffer, _useNewton3);
485 if constexpr (bidirectional) {
486 if (not _useNewton3) {
487 _functor.SoAFunctorTriple(cell2._particleSoABuffer, cell1._particleSoABuffer, cell3._particleSoABuffer,
false);
488 _functor.SoAFunctorTriple(cell3._particleSoABuffer, cell1._particleSoABuffer, cell2._particleSoABuffer,
false);
A cell functor.
Definition: CellFunctor3B.h:25
DataLayoutOption::Value getDataLayout() const
Getter.
Definition: CellFunctor3B.h:72
void processCellTriple(ParticleCell_T &cell1, ParticleCell_T &cell2, ParticleCell_T &cell3, const std::array< double, 3 > &sortingDirection={0., 0., 0.})
Process the interactions between 3 particles, all located in a different cell.
Definition: CellFunctor3B.h:266
void processCell(ParticleCell_T &cell)
Process the interactions inside one cell.
Definition: CellFunctor3B.h:213
CellFunctor3B(ParticleFunctor_T &f, const double sortingCutoff, DataLayoutOption dataLayout, bool useNewton3)
The constructor of CellFunctor3B.
Definition: CellFunctor3B.h:36
bool getBidirectional() const
Getter.
Definition: CellFunctor3B.h:84
void processCellPair(ParticleCell_T &cell1, ParticleCell_T &cell2, const std::array< double, 3 > &sortingDirection={0., 0., 0.})
Process the interactions between the particles of cell1 with particles of cell2.
Definition: CellFunctor3B.h:234
bool getNewton3() const
Getter.
Definition: CellFunctor3B.h:78
void setSoASortingThresholds(const SortingThresholdInfoInterface &soaSortingThreshold)
Set the SoA sorting-threshold.
Definition: CellFunctor3B.h:207
void setAoSSortingThresholds(const SortingThresholdInfoInterface &aosSortingThreshold)
Set the aos-sorting-threshold for AoS traversals.
Definition: CellFunctor3B.h:201
This namespace is used for implementation specifics.
Definition: CellFunctor.h:21
constexpr std::array< T, SIZE > normalize(const std::array< T, SIZE > &a)
Generates a normalized array (|a| = 1).
Definition: ArrayMath.h:304
Polymorphic base for sorting-threshold storage.
Definition: SortingThresholdInfoInterface.h:18
Single threshold value struct.
Definition: SortingThresholdInfoSingle.h:18