Reference documentation for deal.II version 9.1.1
\(\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 class CommunicationPatternBase;
32 template <typename Number>
33 class ReadWriteVector;
34 
35 namespace LinearAlgebra
36 {
40  namespace CUDAWrappers
41  {
52  template <typename Number>
53  class Vector : public VectorSpaceVector<Number>
54  {
55  public:
56  using value_type = typename VectorSpaceVector<Number>::value_type;
57  using size_type = typename VectorSpaceVector<Number>::size_type;
58  using real_type = typename VectorSpaceVector<Number>::real_type;
59 
63  Vector();
64 
68  Vector(const Vector<Number> &V);
69 
73  Vector(Vector<Number> &&) noexcept = default;
74 
85  explicit Vector(const size_type n);
86 
90  Vector &
91  operator=(const Vector<Number> &v);
92 
96  Vector &
97  operator=(Vector<Number> &&v) noexcept = default;
98 
114  virtual void
115  swap(Vector<Number> &v);
116 
122  void
123  reinit(const size_type n, const bool omit_zeroing_entries = false);
124 
129  virtual void
130  reinit(const VectorSpaceVector<Number> &V,
131  const bool omit_zeroing_entries = false) override;
132 
141  virtual void
142  import(
143  const ReadWriteVector<Number> & V,
144  VectorOperation::values operation,
145  std::shared_ptr<const CommunicationPatternBase> communication_pattern =
146  std::shared_ptr<const CommunicationPatternBase>()) override;
147 
152  virtual Vector<Number> &
153  operator=(const Number s) override;
154 
158  virtual Vector<Number> &
159  operator*=(const Number factor) override;
160 
164  virtual Vector<Number> &
165  operator/=(const Number factor) override;
166 
170  virtual Vector<Number> &
171  operator+=(const VectorSpaceVector<Number> &V) override;
172 
176  virtual Vector<Number> &
177  operator-=(const VectorSpaceVector<Number> &V) override;
178 
182  virtual Number
183  operator*(const VectorSpaceVector<Number> &V) const override;
184 
188  virtual void
189  add(const Number a) override;
190 
194  virtual void
195  add(const Number a, const VectorSpaceVector<Number> &V) override;
196 
200  virtual void
201  add(const Number a,
202  const VectorSpaceVector<Number> &V,
203  const Number b,
204  const VectorSpaceVector<Number> &W) override;
205 
210  virtual void
211  sadd(const Number s,
212  const Number a,
213  const VectorSpaceVector<Number> &V) override;
214 
220  virtual void
221  scale(const VectorSpaceVector<Number> &scaling_factors) override;
222 
226  virtual void
227  equ(const Number a, const VectorSpaceVector<Number> &V) override;
228 
232  virtual bool
233  all_zero() const override;
234 
238  virtual value_type
239  mean_value() const override;
240 
245  virtual real_type
246  l1_norm() const override;
247 
252  virtual real_type
253  l2_norm() const override;
254 
258  real_type
259  norm_sqr() const;
260 
265  virtual real_type
266  linfty_norm() const override;
267 
287  virtual Number
288  add_and_dot(const Number a,
289  const VectorSpaceVector<Number> &V,
290  const VectorSpaceVector<Number> &W) override;
291 
296  Number *
297  get_values() const;
298 
302  virtual size_type
303  size() const override;
304 
309  virtual ::IndexSet
310  locally_owned_elements() const override;
311 
315  virtual void
316  print(std::ostream & out,
317  const unsigned int precision = 2,
318  const bool scientific = true,
319  const bool across = true) const override;
320 
324  virtual std::size_t
325  memory_consumption() const override;
326 
333 
334  private:
338  std::unique_ptr<Number[], void (*)(Number *)> val;
339 
343  size_type n_elements;
344  };
345  } // namespace CUDAWrappers
346 } // namespace LinearAlgebra
347 
348 // ---------------------------- Inline functions --------------------------
349 
358 template <typename Number>
359 inline void
361  LinearAlgebra::CUDAWrappers::Vector<Number> &v)
362 {
363  u.swap(v);
364 }
365 
366 namespace LinearAlgebra
367 {
368  namespace CUDAWrappers
369  {
370  template <typename Number>
371  inline Number *
373  {
374  return val.get();
375  }
376 
377 
378 
379  template <typename Number>
380  inline typename Vector<Number>::size_type
382  {
383  return n_elements;
384  }
385 
386 
387  template <typename Number>
388  inline IndexSet
390  {
391  return complete_index_set(n_elements);
392  }
393 
394 
395 
396  template <typename Number>
397  inline void
399  {
400  std::swap(val, v.val);
401  std::swap(n_elements, v.n_elements);
402  }
403  } // namespace CUDAWrappers
404 } // namespace LinearAlgebra
405 
406 DEAL_II_NAMESPACE_CLOSE
407 
408 #endif
409 
410 #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:338
virtual real_type l1_norm() const override
STL namespace.
virtual void swap(Vector< Number > &v)
Definition: cuda_vector.h:398
LinearAlgebra::distributed::Vector< Number > Vector
virtual value_type mean_value() const override
virtual ::IndexSet locally_owned_elements() const override
Definition: cuda_vector.h:389
virtual void add(const Number a) override
#define DeclException0(Exception0)
Definition: exceptions.h:473
static ::ExceptionBase & ExcVectorTypeNotCompatible()
virtual bool all_zero() const 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 void scale(const VectorSpaceVector< Number > &scaling_factors) override
virtual void sadd(const Number s, const Number a, const VectorSpaceVector< Number > &V) override
virtual void import(const ReadWriteVector< Number > &V, VectorOperation::values operation, std::shared_ptr< const CommunicationPatternBase > communication_pattern=std::shared_ptr< const CommunicationPatternBase >()) 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:381
virtual real_type linfty_norm() const override