7#include <dune/common/classname.hh>
8#include <dune/common/deprecated.hh>
9#include <dune/common/hybridutilities.hh>
10#include <dune/common/rangeutilities.hh>
11#include <dune/common/shared_ptr.hh>
12#include <dune/common/typetraits.hh>
13#include <dune/common/std/type_traits.hh>
14#include <dune/functions/functionspacebases/concepts.hh>
16#include <amdis/Output.hpp>
17#include <amdis/algorithm/ForEach.hpp>
18#include <amdis/algorithm/InnerProduct.hpp>
19#include <amdis/algorithm/Transform.hpp>
20#include <amdis/common/Concepts.hpp>
21#include <amdis/common/ConceptsBase.hpp>
22#include <amdis/common/FakeContainer.hpp>
23#include <amdis/common/TypeTraits.hpp>
24#include <amdis/functions/NodeIndices.hpp>
25#include <amdis/operations/Assigner.hpp>
26#include <amdis/typetree/MultiIndex.hpp>
31 enum class VectorState
47 template <
class T,
template <
class>
class VectorImpl>
51 using Impl = VectorImpl<T>;
55 using VectorStateOf_t = std::conditional_t<std::is_same_v<A,Assigner::plus_assign>,
56 std::integral_constant<VectorState, VectorState::add_values>,
57 std::integral_constant<VectorState, VectorState::insert_values>>;
60 using size_type =
typename Impl::size_type;
61 using value_type =
typename Impl::value_type;
79 using HasLocalSize =
decltype(std::declval<V>().localSize());
82 using HasGlobalSize =
decltype(std::declval<V>().globalSize());
87 if constexpr (Dune::Std::is_detected<HasLocalSize,Impl>::value)
88 return impl_.localSize();
96 if constexpr (Dune::Std::is_detected<HasGlobalSize,Impl>::value)
97 return impl_.globalSize();
103 template <
class Basis>
110 template <
class Basis>
118 template <
class Basis>
119 void init(Basis
const& basis,
bool clear)
121 impl_.init(basis, clear);
122 state_ = clear ? VectorState::synchronized : VectorState::unknown;
129 state_ = VectorState::unknown;
144 REQUIRES(Concepts::MultiIndex<Index>)>
145 void insert(Index
const& idx,
typename Impl::value_type
const& value, Assign assign = {})
147 test_exit_dbg(state_ == VectorStateOf_t<Assign>::value ||
148 state_ == VectorState::unknown ||
149 state_ == VectorState::synchronized,
150 "Vector in invalid state {} for insertion by {}.", to_string(state_), Dune::className<Assign>());
152 impl_.insert(idx, value, assign);
155 state_ = VectorStateOf_t<Assign>::value;
159 template <
class Index,
160 REQUIRES(Concepts::MultiIndex<Index>)>
161 void set(Index
const& idx,
typename Impl::value_type
const& value)
167 template <
class Index,
168 REQUIRES(Concepts::MultiIndex<Index>)>
169 void add(Index
const& idx,
typename Impl::value_type
const& value)
175 template <
class Index,
176 REQUIRES(Concepts::MultiIndex<Index>)>
177 typename Impl::value_type
get(Index
const& idx)
const
179 const_cast<Self*
>(
this)->synchronize();
180 return impl_.at(idx);
198 template <
class LocalView,
class Node,
class Buffer,
199 REQUIRES(Concepts::LocalView<LocalView>),
200 REQUIRES(Concepts::BasisNode<Node>)>
203 test_exit(state_ == VectorState::unknown ||
204 state_ == VectorState::synchronized,
205 "Vector in invalid state {} for gather operations.", to_string(state_));
207 const_cast<Self*
>(
this)->synchronize();
209 buffer.resize(node.size());
210 impl_.gather(nodeIndices(localView, node), buffer.begin());
215 REQUIRES(Concepts::LocalView<LocalView>)>
218 gather(localView, localView.tree(), buffer);
222 template <
class Index,
class Buffer,
223 REQUIRES(Concepts::MultiIndex<Index>)>
224 void gather(std::vector<Index>
const& indices, Buffer& buffer)
226 test_exit(state_ == VectorState::unknown ||
227 state_ == VectorState::synchronized,
228 "Vector in invalid state {} for gather operations.", to_string(state_));
230 const_cast<Self*
>(
this)->synchronize();
232 buffer.resize(indices.size());
233 impl_.gather(indices, buffer.begin());
256 template <
class LocalView,
class Node,
class NodeVector,
class MaskRange,
class Assign,
257 REQUIRES(Concepts::LocalView<LocalView>),
258 REQUIRES(Concepts::BasisNode<Node>)>
260 MaskRange
const& mask, Assign assign)
262 test_exit(state_ == VectorStateOf_t<Assign>::value ||
263 state_ == VectorState::unknown ||
264 state_ == VectorState::synchronized,
265 "Vector in invalid state {} for insertion by {}.", to_string(state_), Dune::className<Assign>());
267 assert(localVector.size() == node.size());
270 impl_.scatter(nodeIndices(localView, node), localVector, mask, assign);
273 state_ = VectorStateOf_t<Assign>::value;
279 template <
class LocalView,
class Node,
class NodeVector,
class Assign,
280 REQUIRES(Concepts::LocalView<LocalView>),
281 REQUIRES(Concepts::BasisNode<Node>)>
282 void scatter(
LocalView const& localView, Node
const& node, NodeVector
const& localVector, Assign assign)
289 template <
class LocalView,
class LocalVector,
class Assign,
290 REQUIRES(Concepts::LocalView<LocalView>)>
293 scatter(localView, localView.tree(), localVector, assign);
297 template <
class Index,
class Buffer,
298 REQUIRES(Concepts::MultiIndex<Index>)>
299 void scatter(std::vector<Index>
const& indices, Buffer
const& values)
304 state_ = VectorStateOf_t<Assigner::assign>::value;
313 std::enable_if_t<Dune::IsNumber<S>::value,
int> = 0>
316 impl_.axpy(alpha, x.impl_);
322 std::enable_if_t<Dune::IsNumber<S>::value,
int> = 0>
325 impl_.aypx(alpha, x.impl_);
331 std::enable_if_t<Dune::IsNumber<S>::value,
int> = 0>
341 return this->
axpy(1.0, x);
347 return this->
axpy(-1.0, x);
352 std::enable_if_t<Dune::IsNumber<S>::value,
int> = 0>
361 std::enable_if_t<Dune::IsNumber<S>::value,
int> = 0>
364 impl_.scale(S(1)/alpha);
378 template <
class LocalInd,
class Func>
379 void forEach(LocalInd
const& localInd, Func&& func)
382 impl_.forEach(localInd, FWD(func));
392 template <
class LocalInd,
class Func>
393 void forEach(LocalInd
const& localInd, Func&& func)
const
395 const_cast<Self*
>(
this)->synchronize();
396 impl_.forEach(localInd, FWD(func));
412 if (state_ != VectorState::synchronized)
415 state_ = VectorState::synchronized;
419 static std::string to_string(VectorState
state)
422 case VectorState::synchronized:
return "synchronized";
423 case VectorState::insert_values:
return "insert_values";
424 case VectorState::add_values:
return "add_values";
425 default:
return "unknown";
435 VectorState state_ = VectorState::unknown;
441 template <
class T,
template <
class>
class Impl>
442 struct ForEach<VectorFacade<T,
Impl>>
444 template <
class Vec,
class UnaryFunction>
445 static void impl (Vec&& vec, UnaryFunction f)
448 Recursive::forEach(vec.impl(), f);
452 template <
class T,
template <
class>
class Impl>
453 struct Transform<VectorFacade<T,
Impl>>
455 template <
class Operation,
class... Ts>
456 static void impl (VectorFacade<T,Impl>& vecOut, Operation op, VectorFacade<Ts,Impl>
const&... vecIn)
459 Recursive::transform(vecOut.impl(), op, vecIn.impl()...);
463 template <
class S,
template <
class>
class Impl>
464 struct InnerProduct<VectorFacade<S,
Impl>>
466 template <
class In1,
class In2,
class T,
class BinOp1,
class BinOp2>
467 static T impl (In1
const& in1, In2
const& in2, T init, BinOp1 op1, BinOp2 op2)
470 return Recursive::innerProduct(in1.impl(), in2.impl(), std::move(init), op1, op2);
479 template <
class T,
class Facade>
480 struct VectorTypeImpl;
482 template <
class T,
class S,
template <
class>
class Impl>
483 struct VectorTypeImpl<T,VectorFacade<S,
Impl>>
485 using type = VectorFacade<T,Impl>;
491 template <
class T,
class Facade>
492 using VectorType_t =
typename Impl::VectorTypeImpl<T,Facade>::type;
A container-like data-structure not storing anything and with empty implementations in many container...
Definition FakeContainer.hpp:36
Global basis defined on a pre-basis.
Definition GlobalBasis.hpp:47
IndexDist const & indexDistribution() const
Return the index distribution.
Definition GlobalBasis.hpp:138
The restriction of a finite element basis to a single element.
Definition LocalView.hpp:16
The basic container that stores a base vector and a corresponding basis.
Definition VectorFacade.hpp:49
void gather(std::vector< Index > const &indices, Buffer &buffer)
Call gather the values associated to the indices into the buffer.
Definition VectorFacade.hpp:224
void scatter(LocalView const &localView, Node const &node, NodeVector const &localVector, Assign assign)
Call scatter with MaskRange set to FakeContainer.
Definition VectorFacade.hpp:282
void gather(LocalView const &localView, Node const &node, Buffer &buffer) const
Extract values from the vector referring to the given local indices and store it into a buffer.
Definition VectorFacade.hpp:201
Self & aypx(const S &alpha, const Self &x)
Implements *this = alpha * (*this) + x.
Definition VectorFacade.hpp:323
Self & operator-=(const Self &x)
return *this -= x
Definition VectorFacade.hpp:345
void forEach(LocalInd const &localInd, Func &&func) const
Apply func to each value at given indices localInd.
Definition VectorFacade.hpp:393
Self & operator/=(const S &alpha)
scale all entries by the factor 1.0/alpha
Definition VectorFacade.hpp:362
void add(Index const &idx, typename Impl::value_type const &value)
See insert for assignment operation Assigner::plus_assign.
Definition VectorFacade.hpp:169
void finish()
Finish the insertion of values, see init()
Definition VectorFacade.hpp:126
Self & operator=(const S &alpha)
Set all entries to alpha.
Definition VectorFacade.hpp:332
void init(Basis const &basis, bool clear)
Definition VectorFacade.hpp:119
VectorFacade(GlobalBasis const &basis)
Definition VectorFacade.hpp:67
void scatter(LocalView const &localView, LocalVector const &localVector, Assign assign)
Call scatter with Node given by the tree of the localView.
Definition VectorFacade.hpp:291
void insert(Index const &idx, typename Impl::value_type const &value, Assign assign={})
Insert a single value into the matrix (add or overwrite to existing value)
Definition VectorFacade.hpp:145
void forEach(LocalInd const &localInd, Func &&func)
Apply func to each value at given indices localInd.
Definition VectorFacade.hpp:379
void set(Index const &idx, typename Impl::value_type const &value)
See insert for assignment operation Assigner::assign.
Definition VectorFacade.hpp:161
Self & operator*=(const S &alpha)
scale all entries by the factor alpha
Definition VectorFacade.hpp:353
Self & axpy(const S &alpha, const Self &x)
Implements *this = *this + alpha * x.
Definition VectorFacade.hpp:314
std::size_t globalSize() const
Return the number of entries in the global vector.
Definition VectorFacade.hpp:94
void resize(Basis const &basis)
Resize the vector to the size of the basis.
Definition VectorFacade.hpp:104
Impl::value_type get(Index const &idx) const
Return the value of the vector at the local index idx.
Definition VectorFacade.hpp:177
std::size_t localSize() const
Return the number of entries in the local part of the vector.
Definition VectorFacade.hpp:85
VectorState state() const
Return the internal state of the vector.
Definition VectorFacade.hpp:403
Self & operator+=(const Self &x)
return *this += x
Definition VectorFacade.hpp:339
void scatter(std::vector< Index > const &indices, Buffer const &values)
Call scatter the values associated to the indices into the vector.
Definition VectorFacade.hpp:299
void scatter(LocalView const &localView, Node const &node, NodeVector const &localVector, MaskRange const &mask, Assign assign)
Insert a block of values into the vector (add or overwrite to existing values)
Definition VectorFacade.hpp:259
void resizeZero(Basis const &basis)
Resize the vector to the size of the basis and set to zero.
Definition VectorFacade.hpp:111
Impl const & impl() const
Return the underlying linear algebra backend.
Definition VectorFacade.hpp:74
Definition Assigner.hpp:8
Definition Assigner.hpp:17