15 #ifndef ALLIUM_LA_VECTOR_STORAGE_HPP
16 #define ALLIUM_LA_VECTOR_STORAGE_HPP
18 #include <allium/util/cloneable.hpp>
19 #include <allium/util/numeric.hpp>
20 #include <allium/util/crtp.hpp>
21 #include <allium/util/except.hpp>
22 #include <allium/util/extern.hpp>
23 #include "vector_spec.hpp"
36 template <
typename>
class LocalSlice;
47 using Real = real_part_t<Number>;
59 virtual void add_scaled(Number factor,
const VectorStorage& other) = 0;
60 virtual Real l2_norm()
const = 0;
62 virtual void fill(N value) = 0;
70 std::enable_if_t<std::is_base_of<VectorStorage<typename T::Number>, T>::value,
72 friend allocate_like(
const T& o);
74 virtual Number* aquire_data_ptr() = 0;
75 const Number* aquire_data_ptr()
const {
79 virtual void release_data_ptr(Number* data) = 0;
80 void release_data_ptr(
const Number* data)
const {
81 const_cast<VectorStorage*
>(
this)->release_data_ptr(
const_cast<Number*
>(data));
88 template <
typename C,
typename T=
void>
89 using enable_if_vector_t =
90 std::enable_if_t<std::is_base_of<VectorStorage<typename C::Number>, C>::value, T>;
93 enable_if_vector_t<T, std::unique_ptr<T>>
94 allocate_like(
const T& o)
96 using Number =
typename T::Number;
100 allium_assert(
dynamic_cast<T*
>(allocated) !=
nullptr,
101 std::string(
"The method allocate_like is not overridden in")
102 +
typeid(*allocated).name()
105 return std::unique_ptr<T>(
static_cast<T*
>(allocated));
111 template <
typename P>
115 using Vector =
typename std::pointer_traits<P>::element_type;
116 using Number =
typename Vector::Number;
117 using Real =
typename Vector::Real;
119 typename std::conditional<std::is_const<Vector>::value,
122 using Reference = std::conditional_t<std::is_const<Vector>::value,
125 using Base = std::conditional_t<std::is_const<Vector>::value,
130 : m_data(
nullptr), m_size(0)
142 : m_data(other.m_data),
143 m_size(other.m_size),
146 other.m_data =
nullptr;
148 other.m_ptr =
nullptr;
152 if (rhs.size() != size()) {
153 throw std::logic_error(
"Invalid length of initilizer list.");
156 for (
size_t i_element = 0; i_element < size(); ++i_element) {
157 (*this)[i_element] = rhs[i_element];
161 template <
typename P2>
163 if (rhs.size() != size()) {
164 throw std::logic_error(
"Invalid length of initilizer list.");
167 for (
size_t i_element = 0; i_element < size(); ++i_element) {
168 (*this)[i_element] = rhs[i_element];
172 void reset(
const Pointer& ptr) {
176 m_data =
static_cast<Base*
>(m_ptr)->aquire_data_ptr();
177 m_size = m_ptr->spec().local_size();
181 if (m_data !=
nullptr) {
182 static_cast<Base*
>(m_ptr)->release_data_ptr(m_data);
188 LocalSlice& operator= (
const std::initializer_list<Number>& rhs) {
189 if (rhs.size() != m_size) {
190 throw std::logic_error(
"Invalid length of initilizer list.");
193 size_t i_element = 0;
194 for (
auto element : rhs) {
195 (*this)[i_element] = element;
201 Reference operator[] (
size_t index)
const {
202 #ifdef ALLIUM_BOUND_CHECKS
203 if (index >= m_size) {
204 throw std::logic_error(
"Index out of bounds.");
208 return m_data[index];
211 size_t size()
const {
return m_size; }
219 template <
typename N>
225 template <
typename N>
226 LocalSlice<const VectorStorage<N>*> local_slice(
const VectorStorage<N>& storage)
228 return LocalSlice<const VectorStorage<N>*>(&storage);
231 template <
typename T1,
typename T2>
232 enable_if_vector_t<T1>
233 copy(T1& dest, T2& src) {
234 local_slice(dest) = local_slice(src);
240 template <
typename Derived,
typename N>
247 using typename VectorStorage<N>::Real;
260 const Derived* derived_rhs =
dynamic_cast<const Derived*
>(&rhs);
261 if (derived_rhs !=
nullptr) {
262 return derived(
this) += (*derived_rhs);
269 const Derived* derived_rhs =
dynamic_cast<const Derived*
>(&rhs);
270 if (derived_rhs !=
nullptr) {
271 return derived(
this).dot(*derived_rhs);
278 allium_assert(
dynamic_cast<const Derived*
>(&other));
279 derived(
this).add_scaled(factor,
static_cast<const Derived&
>(other));
284 template <
typename V>
285 enable_if_vector_t<V> fill(V& vec,
typename V::Number value)
290 template <
typename N>
291 void set_zero(VectorStorage<N>& vec)
296 template <
typename V>
297 std::unique_ptr<V> zeros_like(
const V& vec)
299 auto v = allocate_like(vec);
306 #define ALLIUM_LA_VECTOR_STORAGE_DECL(extern, N) \
307 extern template class VectorStorage<N>;
308 ALLIUM_EXTERN_N(ALLIUM_LA_VECTOR_STORAGE_DECL)
A class that allows cloning of objects.
Definition: cloneable.hpp:30
Accessor for the locally stored part of a vector.
Definition: vector_storage.hpp:112
A vector based on a PETSc vector.
Definition: petsc_abstract_vector.hpp:193
The specification of a vector, needed to create distributed vectors.
Definition: vector_spec.hpp:26
Implements a set of common vector operations.
Definition: vector_storage.hpp:244
Abstract base class for any vector storage.
Definition: vector_storage.hpp:41
An exception thrown when trying to use a functionality that is not implemented.
Definition: except.hpp:25
Trait that implements the self helper routine for the curiously recurring template pattern.
Definition: crtp.hpp:36