Allium  0.1.0
vector_storage.hpp
1 // Copyright 2020 Hannah Rittich
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ALLIUM_LA_VECTOR_STORAGE_HPP
16 #define ALLIUM_LA_VECTOR_STORAGE_HPP
17 
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"
24 
25 namespace allium {
36  template <typename> class LocalSlice;
37 
39  template <typename N>
40  class VectorStorage : public Cloneable
41  {
42  public:
43  template <typename> friend class LocalSlice;
44 
45  // type traits
46  using Number = N;
47  using Real = real_part_t<Number>;
48 
49  VectorStorage(VectorSpec spec) : m_spec(spec) {}
50  virtual ~VectorStorage();
51 
52  // assignment
53  virtual void assign(const VectorStorage& rhs) = 0;
54 
55  // Public virtual vector interface
56  virtual VectorStorage& operator+=(const VectorStorage& rhs) = 0;
57  virtual VectorStorage& operator*=(const Number& factor) = 0;
58 
59  virtual void add_scaled(Number factor, const VectorStorage& other) = 0;
60  virtual Real l2_norm() const = 0;
61  virtual Number dot(const VectorStorage& rhs) const = 0;
62  virtual void fill(N value) = 0;
63 
64  VectorSpec spec() const { return m_spec; }
65  private:
66  // Private virtual interface
67  virtual VectorStorage* allocate_like() const& = 0;
68 
69  template <typename T>
70  std::enable_if_t<std::is_base_of<VectorStorage<typename T::Number>, T>::value,
71  std::unique_ptr<T>>
72  friend allocate_like(const T& o);
73 
74  virtual Number* aquire_data_ptr() = 0;
75  const Number* aquire_data_ptr() const {
76  return const_cast<VectorStorage*>(this)->aquire_data_ptr();
77  }
78 
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));
82  }
83 
84  private:
85  VectorSpec m_spec;
86  };
87 
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>;
91 
92  template <typename T>
93  enable_if_vector_t<T, std::unique_ptr<T>>
94  allocate_like(const T& o)
95  {
96  using Number = typename T::Number;
97 
98  VectorStorage<Number>* allocated =
99  static_cast<const VectorStorage<Number>&>(o).allocate_like();;
100  allium_assert(dynamic_cast<T*>(allocated) != nullptr,
101  std::string("The method allocate_like is not overridden in")
102  + typeid(*allocated).name()
103  + ".");
104 
105  return std::unique_ptr<T>(static_cast<T*>(allocated));
106  }
107 
111  template <typename P>
112  class LocalSlice final {
113  public:
114  using Pointer = P;
115  using Vector = typename std::pointer_traits<P>::element_type;
116  using Number = typename Vector::Number;
117  using Real = typename Vector::Real;
118  using DataPointer =
119  typename std::conditional<std::is_const<Vector>::value,
120  const Number*,
121  Number*>::type;
122  using Reference = std::conditional_t<std::is_const<Vector>::value,
123  Number,
124  Number&>;
125  using Base = std::conditional_t<std::is_const<Vector>::value,
126  const VectorStorage<Number>,
128 
129  explicit LocalSlice(const Pointer& ptr)
130  : m_data(nullptr), m_size(0)
131  {
132  reset(ptr);
133  }
134 
135  LocalSlice(const LocalSlice&) = delete;
136 
137  ~LocalSlice() {
138  release();
139  }
140 
141  LocalSlice(LocalSlice&& other)
142  : m_data(other.m_data),
143  m_size(other.m_size),
144  m_ptr(other.m_ptr)
145  {
146  other.m_data = nullptr;
147  other.m_size = 0;
148  other.m_ptr = nullptr;
149  }
150 
151  void operator= (const LocalSlice& rhs) {
152  if (rhs.size() != size()) {
153  throw std::logic_error("Invalid length of initilizer list.");
154  }
155 
156  for (size_t i_element = 0; i_element < size(); ++i_element) {
157  (*this)[i_element] = rhs[i_element];
158  }
159  }
160 
161  template <typename P2>
162  void operator= (const LocalSlice<P2>& rhs) {
163  if (rhs.size() != size()) {
164  throw std::logic_error("Invalid length of initilizer list.");
165  }
166 
167  for (size_t i_element = 0; i_element < size(); ++i_element) {
168  (*this)[i_element] = rhs[i_element];
169  }
170  }
171 
172  void reset(const Pointer& ptr) {
173  release();
174 
175  m_ptr = ptr;
176  m_data = static_cast<Base*>(m_ptr)->aquire_data_ptr();
177  m_size = m_ptr->spec().local_size();
178  }
179 
180  void release() {
181  if (m_data != nullptr) {
182  static_cast<Base*>(m_ptr)->release_data_ptr(m_data);
183  m_data = nullptr;
184  m_size = 0;
185  }
186  }
187 
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.");
191  }
192 
193  size_t i_element = 0;
194  for (auto element : rhs) {
195  (*this)[i_element] = element;
196  ++i_element;
197  }
198  return *this;
199  }
200 
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.");
205  }
206  #endif
207 
208  return m_data[index];
209  }
210 
211  size_t size() const { return m_size; }
212  protected:
213  DataPointer m_data;
214  size_t m_size;
215  private:
216  Pointer m_ptr;
217  };
218 
219  template <typename N>
220  LocalSlice<VectorStorage<N>*> local_slice(VectorStorage<N>& storage)
221  {
222  return LocalSlice<VectorStorage<N>*>(&storage);
223  }
224 
225  template <typename N>
226  LocalSlice<const VectorStorage<N>*> local_slice(const VectorStorage<N>& storage)
227  {
228  return LocalSlice<const VectorStorage<N>*>(&storage);
229  }
230 
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);
235  }
236 
240  template <typename Derived, typename N>
242  : public VectorStorage<N>,
243  public CrtpTrait<Derived>
244  {
245  public:
246  using typename VectorStorage<N>::Number;
247  using typename VectorStorage<N>::Real;
249 
251  VectorStorageTrait(const VectorStorageTrait&) = delete;
252  VectorStorageTrait& operator= (const VectorStorageTrait&) = delete;
253 
254  void assign(const VectorStorage<N>& other) {
255  copy(*this, other);
256  }
257 
258  // @todo: Turn these calls into static casts and add assertions
259  VectorStorageTrait& operator+=(const VectorStorage<N>& rhs) override {
260  const Derived* derived_rhs = dynamic_cast<const Derived*>(&rhs);
261  if (derived_rhs != nullptr) {
262  return derived(this) += (*derived_rhs);
263  } else {
264  throw not_implemented();
265  }
266  }
267 
268  N dot(const VectorStorage<N>& rhs) const override {
269  const Derived* derived_rhs = dynamic_cast<const Derived*>(&rhs);
270  if (derived_rhs != nullptr) {
271  return derived(this).dot(*derived_rhs);
272  } else {
273  throw not_implemented();
274  }
275  }
276 
277  void add_scaled(Number factor, const VectorStorage<N>& other) override {
278  allium_assert(dynamic_cast<const Derived*>(&other));
279  derived(this).add_scaled(factor, static_cast<const Derived&>(other));
280  }
281 
282  };
283 
284  template <typename V>
285  enable_if_vector_t<V> fill(V& vec, typename V::Number value)
286  {
287  vec.fill(value);
288  }
289 
290  template <typename N>
291  void set_zero(VectorStorage<N>& vec)
292  {
293  fill(vec, N(0.0));
294  }
295 
296  template <typename V>
297  std::unique_ptr<V> zeros_like(const V& vec)
298  {
299  auto v = allocate_like(vec);
300  set_zero(*v);
301  return v;
302  }
303 
305 
306  #define ALLIUM_LA_VECTOR_STORAGE_DECL(extern, N) \
307  extern template class VectorStorage<N>;
308  ALLIUM_EXTERN_N(ALLIUM_LA_VECTOR_STORAGE_DECL)
309 }
310 
311 #endif
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