31template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional = true>
43 explicit CellFunctor(ParticleFunctor_T &f,
const double sortingCutoff, DataLayoutOption dataLayout,
bool useNewton3)
44 : _functor(f), _sortingCutoff(sortingCutoff), _dataLayout(dataLayout), _useNewton3(useNewton3) {
45 if constexpr (ParticleFunctor_T::supportsSoASorting) {
46 if (dataLayout == DataLayoutOption::soa) {
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; }
137 const std::vector<std::pair<double, size_t>> &projIdxJ,
138 std::vector<size_t> &maxIndexCache,
139 std::vector<size_t> &minIndexCache)
const;
150 [[nodiscard]]
bool shouldUseSorting(
size_t particleCount,
const std::array<double, 3> &sortingDirection,
152 if (sortingDirection[0] != 0.0 or sortingDirection[1] != 0.0 or sortingDirection[2] != 0.0) {
153 const auto &thresholds = useSoA ? _soaSortingThresholds : _aosSortingThresholds;
154 return particleCount >= thresholds.
getThresholdByConfig(_useNewton3, sortingDirection, bidirectional);
171 if (
const auto *infoSingle =
dynamic_cast<const SortingThresholdInfoSingle *
>(&info)) {
172 return SortingThresholdInfo2B(infoSingle->threshold);
174 throw utils::ExceptionHandler::AutoPasException(
175 "CellFunctor was called with wrong SortingThresholdInfo Type. (Supported are single and 2B)");
187 void processCellAoSImpl(ParticleCell_T &cell);
195 void processCellPairAoSImpl(ParticleCell_T &cell1, ParticleCell_T &cell2,
196 const std::array<double, 3> &sortingDirection);
205 void processCellPairSoAImpl(ParticleCell_T &cell1, ParticleCell_T &cell2,
206 const std::array<double, 3> &sortingDirection);
208 ParticleFunctor_T &_functor;
210 const double _sortingCutoff;
217 SortingThresholdInfo2B _aosSortingThresholds{8};
218 SortingThresholdInfo2B _soaSortingThresholds{100};
220 const DataLayoutOption::Value _dataLayout;
222 const bool _useNewton3;
227 struct alignas(64) SoAThreadData {
228 SoA<typename ParticleCell_T::ParticleType::SoAArraysType> sortedSoa1;
229 SoA<typename ParticleCell_T::ParticleType::SoAArraysType> sortedSoa2;
230 std::vector<std::pair<double, size_t>> projIdx1;
231 std::vector<std::pair<double, size_t>> projIdx2;
232 std::vector<size_t> maxIndex;
233 std::vector<size_t> minIndex;
236 std::vector<SoAThreadData> _soaThreadData{};
239template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
242 _aosSortingThresholds = resolveThreshold(aosSortingThreshold);
245template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
248 _soaSortingThresholds = resolveThreshold(soaSortingThreshold);
251template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
253 const bool isAoS = _dataLayout == DataLayoutOption::aos;
254 const bool isSoA = _dataLayout == DataLayoutOption::soa;
257 if ((isSoA and cell._particleSoABuffer.size() == 0) or (isAoS and cell.isEmpty())) {
261 if (not cell.canHaveOwnedParticles()) {
266 processCellAoSImpl(cell);
268 _functor.SoAFunctorSingle(cell._particleSoABuffer, _useNewton3);
272template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
274 ParticleCell_T &cell1, ParticleCell_T &cell2,
const std::array<double, 3> &sortingDirection) {
275 const bool isAoS = _dataLayout == DataLayoutOption::aos;
276 const bool isSoA = _dataLayout == DataLayoutOption::soa;
279 if ((isSoA and (cell1._particleSoABuffer.size() == 0 or cell2._particleSoABuffer.size() == 0)) or
280 (isAoS and (cell1.isEmpty() or cell2.isEmpty()))) {
284 if (not cell1.canHaveOwnedParticles()) {
286 if constexpr (not bidirectional) {
287 if (not _useNewton3) {
292 if (not cell2.canHaveOwnedParticles()) {
298 processCellPairAoSImpl(cell1, cell2, sortingDirection);
300 processCellPairSoAImpl(cell1, cell2, sortingDirection);
304template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
307 const auto interactParticles = [
this](
auto &p1,
auto &p2) {
308 this->_functor.AoSFunctor(p1, p2, this->_useNewton3);
310 if (not this->_useNewton3) {
311 this->_functor.AoSFunctor(p2, p1,
false);
316 if (cell.size() >= _aosSortingThresholds.getThresholdByConfig(_useNewton3, {1, 0, 0}, bidirectional)) {
319 for (
auto cellIter1 = cellSorted._particles.begin(); cellIter1 != cellSorted._particles.end(); ++cellIter1) {
320 auto &[p1Projection, p1Ptr] = *cellIter1;
322 for (
auto cellIter2 = std::next(cellIter1); cellIter2 != cellSorted._particles.end(); ++cellIter2) {
323 auto &[p2Projection, p2Ptr] = *cellIter2;
324 if (std::abs(p2Projection - p1Projection) > _sortingCutoff) {
327 interactParticles(*p1Ptr, *p2Ptr);
331 for (
auto p1Ptr = cell.begin(); p1Ptr != cell.end(); ++p1Ptr) {
334 for (; p2Ptr != cell.end(); ++p2Ptr) {
335 interactParticles(*p1Ptr, *p2Ptr);
341template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
342void CellFunctor<ParticleCell_T, ParticleFunctor_T, bidirectional>::processCellPairAoSImpl(
343 ParticleCell_T &cell1, ParticleCell_T &cell2,
const std::array<double, 3> &sortingDirection) {
344 const auto interactParticles = [
this](
auto &p1,
auto &p2) {
345 this->_functor.AoSFunctor(p1, p2, this->_useNewton3);
346 if constexpr (bidirectional) {
347 if (not this->_useNewton3) {
348 this->_functor.AoSFunctor(p2, p1,
false);
353 if (shouldUseSorting(cell1.size() + cell2.size(), sortingDirection,
false)) {
355 SortedCellView<ParticleCell_T> cell1Sorted(cell1, sortingDirection);
356 SortedCellView<ParticleCell_T> cell2Sorted(cell2, sortingDirection);
358 for (
auto &[p1Projection, p1Ptr] : cell1Sorted._particles) {
359 for (
auto &[p2Projection, p2Ptr] : cell2Sorted._particles) {
360 if (std::abs(p2Projection - p1Projection) > _sortingCutoff) {
364 interactParticles(*p1Ptr, *p2Ptr);
369 for (
auto &p1 : cell1) {
370 for (
auto &p2 : cell2) {
371 interactParticles(p1, p2);
377template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
379 const std::vector<std::pair<double, size_t>> &projIdxI,
const std::vector<std::pair<double, size_t>> &projIdxJ,
380 std::vector<size_t> &maxIndexCache, std::vector<size_t> &minIndexCache)
const {
381 const size_t nI = projIdxI.size();
382 const size_t nJ = projIdxJ.size();
387 maxIndexCache.assign(nI, 0);
388 minIndexCache.assign(nI, 0);
389 return {nI, nI, maxIndexCache, minIndexCache};
396 const double startThreshold = projIdxJ[0].first - _sortingCutoff;
397 auto startIter = std::lower_bound(projIdxI.begin(), projIdxI.end(), startThreshold,
398 [](
const auto &elem,
double val) { return elem.first < val; });
399 const size_t startI =
static_cast<size_t>(startIter - projIdxI.begin());
404 const double endThreshold = projIdxJ[nJ - 1].first + _sortingCutoff;
405 auto endIter = std::upper_bound(projIdxI.begin(), projIdxI.end(), endThreshold,
406 [](
double val,
const auto &elem) { return val < elem.first; });
407 const size_t endI =
static_cast<size_t>(endIter - projIdxI.begin());
413 maxIndexCache.assign(nI, 0);
414 minIndexCache.assign(nI, 0);
415 size_t jUpper = 0, jLower = 0;
416 for (
size_t i = startI; i < endI; ++i) {
418 while (jUpper < nJ and projIdxJ[jUpper].first <= projIdxI[i].first + _sortingCutoff) {
421 maxIndexCache[i] = jUpper;
423 while (jLower < nJ and projIdxJ[jLower].first < projIdxI[i].first - _sortingCutoff) {
426 minIndexCache[i] = jLower;
429 return {startI, endI, maxIndexCache, minIndexCache};
432template <
class ParticleCell_T,
class ParticleFunctor_T,
bool b
idirectional>
434 ParticleCell_T &cell1, ParticleCell_T &cell2,
const std::array<double, 3> &sortingDirection) {
435 if constexpr (ParticleFunctor_T::supportsSoASorting) {
436 if (shouldUseSorting(cell1._particleSoABuffer.size() + cell2._particleSoABuffer.size(), sortingDirection,
438 using Particle_T = ParticleCell_T::ParticleType;
442 threadData.sortedSoa1, threadData.projIdx1);
444 threadData.sortedSoa2, threadData.projIdx2);
446 _functor.SoAFunctorPairSorted(
447 view1.getView(), view2.getView(),
448 computeSortingData(threadData.projIdx1, threadData.projIdx2, threadData.maxIndex, threadData.minIndex),
451 if constexpr (bidirectional) {
452 if (not _useNewton3) {
455 _functor.SoAFunctorPairSorted(
456 view2.getView(), view1.getView(),
457 computeSortingData(threadData.projIdx2, threadData.projIdx1, threadData.maxIndex, threadData.minIndex),
464 if constexpr (bidirectional) {
466 }
else if (_useNewton3) {
472 _functor.SoAFunctorPair(cell1._particleSoABuffer, cell2._particleSoABuffer, _useNewton3);
473 if constexpr (bidirectional) {
474 if (not _useNewton3) {
475 _functor.SoAFunctorPair(cell2._particleSoABuffer, cell1._particleSoABuffer,
false);
A sorted view on a SoA buffer.
Definition: SortedSoAView.h:34
A cell functor.
Definition: CellFunctor.h:32
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: CellFunctor.h:273
SoASortingData computeSortingData(const std::vector< std::pair< double, size_t > > &projIdxI, const std::vector< std::pair< double, size_t > > &projIdxJ, std::vector< size_t > &maxIndexCache, std::vector< size_t > &minIndexCache) const
Computes conservative per-particle index bounds into projIdxJ based on a 1-D projection cutoff check.
Definition: CellFunctor.h:378
bool getNewton3() const
Getter.
Definition: CellFunctor.h:78
void setAoSSortingThresholds(const SortingThresholdInfoInterface &aosSortingThreshold)
Set the aos-sorting-threshold.
Definition: CellFunctor.h:240
bool getBidirectional() const
Getter.
Definition: CellFunctor.h:84
void processCell(ParticleCell_T &cell)
Process the interactions inside one cell.
Definition: CellFunctor.h:252
DataLayoutOption::Value getDataLayout() const
Getter.
Definition: CellFunctor.h:72
void setSoASortingThresholds(const SortingThresholdInfoInterface &soaSortingThreshold)
Set the SoA sorting-threshold.
Definition: CellFunctor.h:246
CellFunctor(ParticleFunctor_T &f, const double sortingCutoff, DataLayoutOption dataLayout, bool useNewton3)
The constructor of CellFunctor.
Definition: CellFunctor.h:43
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
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
Precomputed index bounds for iterating a pre-sorted pair of SoA buffers, soa1 as the outer (i) loop a...
Definition: PairwiseFunctor.h:33
Per-Newton3-state, per-SortingDirectionOption pair-sorting thresholds for a 2-body CellFunctor.
Definition: SortingThresholdInfo2B.h:21
size_t getThresholdByConfig(bool newton3, std::array< double, 3 > sortingDirection, bool bidirectional) const
Getter to automatically get the correct threshold based on current newton3/CellDirection/bidirectiona...
Definition: SortingThresholdInfo2B.h:113
Polymorphic base for sorting-threshold storage.
Definition: SortingThresholdInfoInterface.h:18