AutoPas  3.0.0
Loading...
Searching...
No Matches
SortedSoAView.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <algorithm>
11#include <array>
12#include <utility>
13#include <vector>
14
16
17namespace autopas {
18
33template <class Particle_T, class Functor_T>
35 using SoAArraysType = Particle_T::SoAArraysType;
36
37 public:
46 SortedSoAView(SoAView<SoAArraysType> source, const std::array<double, 3> &sortingDirection,
47 SoA<SoAArraysType> &cachedSoa, std::vector<std::pair<double, size_t>> &cachedProjIdx)
48 : _source(source), _sortedSoa(cachedSoa), _projIdx(cachedProjIdx) {
49 const size_t n = source.size();
50 if (n == 0) {
51 _sortedSoa.resizeArrays(0);
52 return;
53 }
54
55 // Step 1: project each particle onto sortingDirection and sort indices ascending.
56 _projIdx.resize(n);
57 const auto *xPtr = source.template begin<Particle_T::AttributeNames::posX>();
58 const auto *yPtr = source.template begin<Particle_T::AttributeNames::posY>();
59 const auto *zPtr = source.template begin<Particle_T::AttributeNames::posZ>();
60 for (size_t i = 0; i < n; ++i) {
61 _projIdx[i] = {xPtr[i] * sortingDirection[0] + yPtr[i] * sortingDirection[1] + zPtr[i] * sortingDirection[2], i};
62 }
63 std::sort(_projIdx.begin(), _projIdx.end(), [](const auto &a, const auto &b) { return a.first < b.first; });
64
65 // Step 2: resize all arrays (value-initializes to 0, covering the computed attributes).
66 _sortedSoa.resizeArrays(n);
67
68 // Step 3: pack getNeededAttr() attributes from source in sorted order.
69 packNeededImpl(std::make_index_sequence<Functor_T::getNeededAttr().size()>{}, n);
70
71 // Step 4: zero getComputedAttr() attributes so the functor accumulates from 0.
72 zeroComputedImpl(std::make_index_sequence<Functor_T::getComputedAttr().size()>{}, n);
73 }
74
79 SoAView<SoAArraysType> getView() { return _sortedSoa.constructView(); }
80
85 void scatterBack() {
86 scatterBackImpl(std::make_index_sequence<Functor_T::getComputedAttr().size()>{}, _projIdx.size());
87 }
88
89 private:
95 template <size_t AttrIdx>
96 void packAttr(size_t n) {
97 constexpr size_t attr = static_cast<size_t>(Functor_T::getNeededAttr()[AttrIdx]);
98 auto *dst = _sortedSoa.template begin<attr>();
99 const auto *src = _source.template begin<attr>();
100 for (size_t i = 0; i < n; ++i) {
101 dst[i] = src[_projIdx[i].second];
102 }
103 }
104
110 template <size_t... AttrIdxs>
111 void packNeededImpl(std::index_sequence<AttrIdxs...>, size_t n) {
112 (packAttr<AttrIdxs>(n), ...);
113 }
114
120 template <size_t AttrIdx>
121 void zeroAttr(size_t n) {
122 constexpr size_t attr = static_cast<size_t>(Functor_T::getComputedAttr()[AttrIdx]);
123 auto *ptr = _sortedSoa.template begin<attr>();
124 std::fill(ptr, ptr + n, 0);
125 }
126
132 template <size_t... AttrIdxs>
133 void zeroComputedImpl(std::index_sequence<AttrIdxs...>, size_t n) {
134 (zeroAttr<AttrIdxs>(n), ...);
135 }
136
143 template <size_t AttrIdx>
144 void scatterAttr(size_t n) {
145 constexpr size_t attr = static_cast<size_t>(Functor_T::getComputedAttr()[AttrIdx]);
146 auto *orig = _source.template begin<attr>();
147 const auto *sorted = _sortedSoa.template begin<attr>();
148 for (size_t i = 0; i < n; ++i) {
149 orig[_projIdx[i].second] += sorted[i];
150 }
151 }
152
158 template <size_t... AttrIdxs>
159 void scatterBackImpl(std::index_sequence<AttrIdxs...>, size_t n) {
160 (scatterAttr<AttrIdxs>(n), ...);
161 }
162
163 SoAView<SoAArraysType> _source;
164 SoA<SoAArraysType> &_sortedSoa;
168 std::vector<std::pair<double, size_t>> &_projIdx;
169};
170
171} // namespace autopas
View on a fixed part of a SoA between a start index and an end index.
Definition: SoAView.h:25
size_t size() const
Returns the number of particles in the view.
Definition: SoAView.h:85
Structur of the array class.
Definition: SoA.h:28
A sorted view on a SoA buffer.
Definition: SortedSoAView.h:34
SoAView< SoAArraysType > getView()
Returns a SoAView into the sorted, contiguous internal buffer.
Definition: SortedSoAView.h:79
SortedSoAView(SoAView< SoAArraysType > source, const std::array< double, 3 > &sortingDirection, SoA< SoAArraysType > &cachedSoa, std::vector< std::pair< double, size_t > > &cachedProjIdx)
Projects particles onto sortingDirection, sorts by projection, and packs needed attributes into an in...
Definition: SortedSoAView.h:46
void scatterBack()
Scatters computed attribute contributions accumulated in the sorted SoA back to the source via +=.
Definition: SortedSoAView.h:85
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:34