Reference documentation for deal.II version Git 7e76c9f1da 2019-09-19 09:07:25 +0200
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
cuda_vector.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2016 - 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_cuda_vector_h
17 #define dealii_cuda_vector_h
18 
19 #include <deal.II/base/config.h>
20 
21 #include <deal.II/base/exceptions.h>
22 #include <deal.II/base/index_set.h>
23 
24 #include <deal.II/lac/vector_operation.h>
25 #include <deal.II/lac/vector_space_vector.h>
26 
27 #ifdef DEAL_II_WITH_CUDA
28 
29 DEAL_II_NAMESPACE_OPEN
30 
31 // Forward declarations
32 # ifndef DOXYGEN
33 class CommunicationPatternBase;
34 template <typename Number>
35 class ReadWriteVector;
36 # endif
37 
38 namespace LinearAlgebra
39 {
43  namespace CUDAWrappers
44  {
55  template <typename Number>
56  class Vector : public VectorSpaceVector<Number>
57  {
58  public:
59  using value_type = typename VectorSpaceVector<Number>::value_type;
60  using size_type = typename VectorSpaceVector<Number>::size_type;
61  using real_type = typename VectorSpaceVector<Number>::real_type;
62 
66  Vector();
67 
71  Vector(const Vector<Number> &V);
72 
76  Vector(Vector<Number> &&) noexcept = default;
77 
88  explicit Vector(const size_type n);
89 
93  Vector &
94  operator=(const Vector<Number> &v);
95 
99  Vector &
100  operator=(Vector<Number> &&v) noexcept = default;
101 
117  virtual void
118  swap(Vector<Number> &v);
119 
125  void
126  reinit(const size_type n, const bool omit_zeroing_entries = false);
127 
132  virtual void
134  const bool omit_zeroing_entries = false) override;
135 
144  virtual void
145  import(
146  const ReadWriteVector<Number> & V,
147  VectorOperation::values operation,
148  std::shared_ptr<const CommunicationPatternBase> communication_pattern =
149  std::shared_ptr<const CommunicationPatternBase>()) override;
150 
155  virtual Vector<Number> &
156  operator=(const Number s) override;
157 
161  virtual Vector<Number> &
162  operator*=(const Number factor) override;
163 
167  virtual Vector<Number> &
168  operator/=(const Number factor) override;
169 
173  virtual Vector<Number> &
174  operator+=(const VectorSpaceVector<Number> &V) override;
175 
179  virtual Vector<Number> &
180  operator-=(const VectorSpaceVector<Number> &V) override;
181 
185  virtual Number
186  operator*(const VectorSpaceVector<Number> &V) const override;
187 
191  virtual void
192  add(const Number a) override;
193 
197  virtual void
198  add(const Number a, const VectorSpaceVector<Number> &V) override;
199 
203  virtual void
204  add(const Number a,
205  const VectorSpaceVector<Number> &V,
206  const Number b,
207  const VectorSpaceVector<Number> &W) override;
208 
213  virtual void
214  sadd(const Number s,
215  const Number a,
216  const VectorSpaceVector<Number> &V) override;
217 
223  virtual void
224  scale(const VectorSpaceVector<Number> &scaling_factors) override;
225 
229  virtual void
230  equ(const Number a, const VectorSpaceVector<Number> &V) override;
231 
235  virtual bool
236  all_zero() const override;
237 
241  virtual value_type
242  mean_value() const override;
243 
248  virtual real_type
249  l1_norm() const override;
250 
255  virtual real_type
256  l2_norm() const override;
257 
261  real_type
262  norm_sqr() const;
263 
268  virtual real_type
269  linfty_norm() const override;
270 
290  virtual Number
291  add_and_dot(const Number a,
292  const VectorSpaceVector<Number> &V,
293  const VectorSpaceVector<Number> &W) override;
294 
299  Number *
300  get_values() const;
301 
305  virtual size_type
306  size() const override;
307 
312  virtual ::IndexSet
313  locally_owned_elements() const override;
314 
318  virtual void
319  print(std::ostream & out,
320  const unsigned int precision = 2,
321  const bool scientific = true,
322  const bool across = true) const override;
323 
327  virtual std::size_t
328  memory_consumption() const override;
329 
336 
337  private:
341  std::unique_ptr<Number[], void (*)(Number *)> val;
342 
346  size_type n_elements;
347  };
348  } // namespace CUDAWrappers
349 } // namespace LinearAlgebra
350 
351 // ---------------------------- Inline functions --------------------------
352 
361 template <typename Number>
362 inline void
365 {
366  u.swap(v);
367 }
368 
369 namespace LinearAlgebra
370 {
371  namespace CUDAWrappers
372  {
373  template <typename Number>
374  inline Number *
376  {
377  return val.get();
378  }
379 
380 
381 
382  template <typename Number>
383  inline typename Vector<Number>::size_type
385  {
386  return n_elements;
387  }
388 
389 
390  template <typename Number>
391  inline IndexSet
393  {
394  return complete_index_set(n_elements);
395  }
396 
397 
398 
399  template <typename Number>
400  inline void
402  {
403  std::swap(val, v.val);
405  }
406  } // namespace CUDAWrappers
407 } // namespace LinearAlgebra
408 
409 DEAL_II_NAMESPACE_CLOSE
410 
411 #endif
412 
413 #endif
virtual Number add_and_dot(const Number a, const VectorSpaceVector< Number > &V, const VectorSpaceVector< Number > &W) override
virtual void equ(const Number a, const VectorSpaceVector< Number > &V) override
void reinit(const size_type n, const bool omit_zeroing_entries=false)
std::unique_ptr< Number[], void(*)(Number *)> val
Definition: cuda_vector.h:341
virtual Vector< Number > & operator-=(const VectorSpaceVector< Number > &V) override
virtual real_type l1_norm() const override
virtual void swap(Vector< Number > &v)
Definition: cuda_vector.h:401
virtual value_type mean_value() const override
virtual ::IndexSet locally_owned_elements() const override
Definition: cuda_vector.h:392
virtual void add(const Number a) override
#define DeclException0(Exception0)
Definition: exceptions.h:473
void swap(LinearAlgebra::CUDAWrappers::Vector< Number > &u, LinearAlgebra::CUDAWrappers::Vector< Number > &v)
Definition: cuda_vector.h:363
static ::ExceptionBase & ExcVectorTypeNotCompatible()
Vector & operator=(const Vector< Number > &v)
virtual Vector< Number > & operator*=(const Number factor) override
virtual bool all_zero() const override
virtual Number operator*(const VectorSpaceVector< Number > &V) const override
virtual Vector< Number > & operator/=(const Number factor) override
void swap(Vector< Number > &u, Vector< Number > &v)
Definition: vector.h:1376
virtual std::size_t memory_consumption() const override
virtual real_type l2_norm() const override
virtual Vector< Number > & operator+=(const VectorSpaceVector< Number > &V) override
virtual void scale(const VectorSpaceVector< Number > &scaling_factors) override
virtual void sadd(const Number s, const Number a, const VectorSpaceVector< Number > &V) override
virtual void print(std::ostream &out, const unsigned int precision=2, const bool scientific=true, const bool across=true) const override
virtual size_type size() const override
Definition: cuda_vector.h:384
virtual real_type linfty_norm() const override