AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
VectorBase.hpp
1#pragma once
2
3#include <iterator>
4
5#include <dune/common/rangeutilities.hh>
6#include <dune/functions/common/multiindex.hh>
7
8#include <amdis/algorithm/ForEach.hpp>
9#include <amdis/algorithm/InnerProduct.hpp>
10#include <amdis/algorithm/Transform.hpp>
11#include <amdis/common/FakeContainer.hpp>
12#include <amdis/utility/MappedRangeView.hpp>
13
14namespace AMDiS
15{
17 template <class Derived>
19 {
20 private:
21 Derived const& asDerived() const
22 {
23 return static_cast<Derived const&>(*this);
24 }
25
26 Derived& asDerived()
27 {
28 return static_cast<Derived&>(*this);
29 }
30
31 protected:
34 VectorBase() = default;
35
36 public:
37 void finish() { /* do nothing */ }
38 void synchronize() { /* do nothing */ }
39
41 template <class MultiIndex, class ValueType, class Assign>
42 void insert(MultiIndex const& idx, ValueType const& value, Assign assign)
43 {
44 assign(value, asDerived().at(idx));
45 }
46
48 template <class IndexRange, class OutputIterator>
49 void gather(IndexRange const& localInd, OutputIterator buffer) const
50 {
51 for (auto const& idx : localInd)
52 *buffer++ = asDerived().at(idx);
53 }
54
55
57 template <class IndexRange, class LocalVec, class Assign>
58 void scatter(IndexRange const& localInd, LocalVec const& vec, FakeContainer<bool,true>, Assign assign)
59 {
60 auto vec_it = std::begin(vec);
61 for (auto const& idx : localInd)
62 assign(*vec_it++, asDerived().at(idx));
63 }
64
67 template <class IndexRange, class LocalVec, class MaskRange, class Assign>
68 void scatter(IndexRange const& localInd, LocalVec const& vec, MaskRange const& mask, Assign assign)
69 {
70 auto vec_it = std::begin(vec);
71 auto mask_it = std::begin(mask);
72 auto ind_it = std::begin(localInd);
73 for (; vec_it != std::end(vec); ++vec_it, ++mask_it, ++ind_it) {
74 if (*mask_it)
75 assign(*vec_it, asDerived().at(*ind_it));
76 }
77 }
78
79
81 template <class IndexRange, class Func>
82 void forEach(IndexRange const& localInd, Func&& f) const
83 {
84 for (auto const& idx : localInd)
85 f(idx, asDerived().at(idx));
86 }
87
89 template <class IndexRange, class Func>
90 void forEach(IndexRange const& localInd, Func&& f)
91 {
92 for (auto const& idx : localInd)
93 f(idx, asDerived().at(idx));
94 }
95 };
96
97
98 namespace Recursive
99 {
100 template <class D>
102 {
103 template <class UnaryFunction>
104 static void impl (D& vec, UnaryFunction f)
105 {
106 for (std::size_t i = 0; i < std::size_t(vec.size()); ++i) {
107 auto idx = Dune::Functions::StaticMultiIndex<std::size_t,1>{i};
108 Recursive::forEach(vec.at(idx), f);
109 }
110 }
111 };
112
113 template <class D>
115 {
116 template <class Operation, class... Ds>
117 static void impl (D& vecOut, Operation op, VectorBase<Ds> const&... vecIn)
118 {
119 for (std::size_t i = 0; i < std::size_t(vecOut.size()); ++i) {
120 auto idx = Dune::Functions::StaticMultiIndex<std::size_t,1>{i};
121 Recursive::transform(vecOut.at(idx), op, static_cast<Ds const&>(vecIn).at(idx)...);
122 }
123 }
124 };
125
126 template <class D>
128 {
129 template <class D2, class T, class BinOp1, class BinOp2>
130 static T impl (D const& in1, VectorBase<D2> const& in2, T init, BinOp1 op1, BinOp2 op2)
131 {
132 for (std::size_t i = 0; i < std::size_t(in1.size()); ++i) {
133 auto idx = Dune::Functions::StaticMultiIndex<std::size_t,1>{i};
134 init = Recursive::innerProduct(in1.at(idx), static_cast<D2 const&>(in2).at(idx), std::move(init), op1, op2);
135 }
136 return init;
137 }
138 };
139
140 } // end namespace Recursive
141} // end namespace AMDiS
A container-like data-structure not storing anything and with empty implementations in many container...
Definition: FakeContainer.hpp:36
CRTP base class for flat vector backends.
Definition: VectorBase.hpp:19
VectorBase()=default
void forEach(IndexRange const &localInd, Func &&f)
Apply the functor f to all entries of the list of multi-indices localInd.
Definition: VectorBase.hpp:90
void scatter(IndexRange const &localInd, LocalVec const &vec, FakeContainer< bool, true >, Assign assign)
Scatter the values from the local vec into the container using the passed assign functor.
Definition: VectorBase.hpp:58
void insert(MultiIndex const &idx, ValueType const &value, Assign assign)
Insert or add value at position idx, using the passed assign functor.
Definition: VectorBase.hpp:42
void scatter(IndexRange const &localInd, LocalVec const &vec, MaskRange const &mask, Assign assign)
Definition: VectorBase.hpp:68
void gather(IndexRange const &localInd, OutputIterator buffer) const
Gather values from the vector into the output range buffer decribed as an output iterator.
Definition: VectorBase.hpp:49
void forEach(IndexRange const &localInd, Func &&f) const
Apply the functor f to all entries of the list of multi-indices localInd.
Definition: VectorBase.hpp:82
constexpr bool MultiIndex
A multi-index type.
Definition: Concepts.hpp:149
Definition: ForEach.hpp:39
General implementation of recursive inner-product.
Definition: InnerProduct.hpp:58
Definition: Transform.hpp:35