Reference documentation for deal.II version 8.5.1
petsc_vector.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2004 - 2017 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 at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__petsc_vector_h
17 #define dealii__petsc_vector_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #ifdef DEAL_II_WITH_PETSC
23 
24 # include <deal.II/base/subscriptor.h>
25 # include <deal.II/lac/exceptions.h>
26 # include <deal.II/lac/petsc_vector_base.h>
27 # include <deal.II/lac/petsc_parallel_vector.h>
28 # include <deal.II/lac/vector.h>
29 # include <deal.II/lac/vector_type_traits.h>
30 
31 DEAL_II_NAMESPACE_OPEN
32 
33 
34 namespace PETScWrappers
35 {
54  class Vector : public VectorBase
55  {
56  public:
61 
75  static const bool supports_distributed_data DEAL_II_DEPRECATED = false;
76 
80  Vector ();
81 
92  explicit Vector (const size_type n);
93 
98  template <typename Number>
99  explicit Vector (const ::Vector<Number> &v);
100 
107  explicit Vector (const Vec &v);
108 
112  Vector (const Vector &v);
113 
123  explicit Vector (const MPI::Vector &v);
124 
129  void clear ();
130 
134  Vector &operator = (const Vector &v);
135 
144  Vector &operator = (const MPI::Vector &v);
145 
158  Vector &operator = (const PetscScalar s);
159 
164  template <typename number>
165  Vector &operator = (const ::Vector<number> &v);
166 
177  void reinit (const size_type N,
178  const bool omit_zeroing_entries = false);
179 
187  void reinit (const Vector &v,
188  const bool omit_zeroing_entries = false);
189 
190  protected:
195  void create_vector (const size_type n);
196  } DEAL_II_DEPRECATED;
197 
200 // ------------------ template and inline functions -------------
201 
202 
211  inline
212  void swap (Vector &u, Vector &v)
213  {
214  u.swap (v);
215  }
216 
217 
218 #ifndef DOXYGEN
219 
220  template <typename number>
221  Vector::Vector (const ::Vector<number> &v)
222  {
223  Vector::create_vector (v.size());
224 
225  *this = v;
226  }
227 
228 
229 
230  inline
231  Vector::Vector (const Vec &v)
232  :
233  VectorBase(v)
234  {}
235 
236 
237  inline
238  Vector &
239  Vector::operator = (const PetscScalar s)
240  {
242 
243  return *this;
244  }
245 
246 
247  inline
248  Vector &
249  Vector::operator = (const Vector &v)
250  {
251  // if the vectors have different sizes,
252  // then first resize the present one
253  if (size() != v.size())
254  reinit (v.size(), true);
255 
256  const PetscErrorCode ierr = VecCopy (v.vector, vector);
257  AssertThrow (ierr == 0, ExcPETScError(ierr));
258 
259  return *this;
260  }
261 
262 
263 
264  inline
265  Vector &
266  Vector::operator = (const MPI::Vector &v)
267  {
268  PetscErrorCode ierr;
269  if (attained_ownership)
270  {
271  // the petsc function we call wants to
272  // generate the vector itself, so destroy
273  // the old one first
274 #if DEAL_II_PETSC_VERSION_LT(3,2,0)
275  ierr = VecDestroy (vector);
276 #else
277  ierr = VecDestroy (&vector);
278 #endif
279  AssertThrow (ierr == 0, ExcPETScError(ierr));
280  }
281 
282  attained_ownership = true;
283 
284  // then do the gather
285  // operation. <rant>petsc has changed its
286  // interface again, and replaced a single
287  // function call by several calls that
288  // are hard to understand. gets me all
289  // annoyed at their development
290  // model</rant>
291 #if DEAL_II_PETSC_VERSION_LT(2,2,0)
292  ierr = VecConvertMPIToSeqAll (static_cast<const Vec &>(v),
293  &vector);
294  AssertThrow (ierr == 0, ExcPETScError(ierr));
295 
296 #else
297 
298  VecScatter ctx;
299 
300  ierr = VecScatterCreateToAll (static_cast<const Vec &>(v), &ctx, &vector);
301  AssertThrow (ierr == 0, ExcPETScError(ierr));
302 
303 #if ((PETSC_VERSION_MAJOR == 2) && \
304  ((PETSC_VERSION_MINOR < 3) || \
305  ((PETSC_VERSION_MINOR == 3) && \
306  (PETSC_VERSION_SUBMINOR < 3))))
307  ierr = VecScatterBegin (static_cast<const Vec &>(v), vector,
308  INSERT_VALUES, SCATTER_FORWARD, ctx);
309  AssertThrow (ierr == 0, ExcPETScError(ierr));
310 
311  ierr = VecScatterEnd (static_cast<const Vec &>(v), vector,
312  INSERT_VALUES, SCATTER_FORWARD, ctx);
313 
314 #else
315 
316  ierr = VecScatterBegin (ctx,static_cast<const Vec &>(v), vector,
317  INSERT_VALUES, SCATTER_FORWARD);
318  AssertThrow (ierr == 0, ExcPETScError(ierr));
319 
320  ierr = VecScatterEnd (ctx, static_cast<const Vec &>(v), vector,
321  INSERT_VALUES, SCATTER_FORWARD);
322 
323 #endif
324  AssertThrow (ierr == 0, ExcPETScError(ierr));
325 
326 #if DEAL_II_PETSC_VERSION_LT(3,2,0)
327  ierr = VecScatterDestroy (ctx);
328 #else
329  ierr = VecScatterDestroy (&ctx);
330 #endif
331  AssertThrow (ierr == 0, ExcPETScError(ierr));
332 #endif
333 
334  return *this;
335  }
336 
337 
338 
339  template <typename number>
340  inline
341  Vector &
342  Vector::operator = (const ::Vector<number> &v)
343  {
344  reinit (v.size(), true);
345  // the following isn't necessarily fast,
346  // but this is due to the fact that PETSc
347  // doesn't offer an inlined access
348  // operator.
349  //
350  // if someone wants to contribute some
351  // code: to make this code faster, one
352  // could either first convert all values
353  // to PetscScalar, and then set them all
354  // at once using VecSetValues. This has
355  // the drawback that it could take quite
356  // some memory, if the vector is large,
357  // and it would in addition allocate
358  // memory on the heap, which is
359  // expensive. an alternative would be to
360  // split the vector into chunks of, say,
361  // 128 elements, convert a chunk at a
362  // time and set it in the output vector
363  // using VecSetValues. since 128 elements
364  // is small enough, this could easily be
365  // allocated on the stack (as a local
366  // variable) which would make the whole
367  // thing much more efficient.
368  //
369  // a second way to make things faster is
370  // for the special case that
371  // number==PetscScalar. we could then
372  // declare a specialization of this
373  // template, and omit the conversion. the
374  // problem with this is that the best we
375  // can do is to use VecSetValues, but
376  // this isn't very efficient either: it
377  // wants to see an array of indices,
378  // which in this case a) again takes up a
379  // whole lot of memory on the heap, and
380  // b) is totally dumb since its content
381  // would simply be the sequence
382  // 0,1,2,3,...,n. the best of all worlds
383  // would probably be a function in Petsc
384  // that would take a pointer to an array
385  // of PetscScalar values and simply copy
386  // n elements verbatim into the vector...
387  for (size_type i=0; i<v.size(); ++i)
388  (*this)(i) = v(i);
389 
391 
392  return *this;
393  }
394 #endif // DOXYGEN
395 
396 }
397 
398 namespace internal
399 {
400  namespace LinearOperator
401  {
402  template <typename> class ReinitHelper;
403 
408  template<>
410  {
411  public:
412  template <typename Matrix>
413  static
414  void reinit_range_vector (const Matrix &matrix,
416  bool omit_zeroing_entries)
417  {
418  v.reinit( matrix.m(),
419  omit_zeroing_entries);
420  }
421 
422  template <typename Matrix>
423  static
424  void reinit_domain_vector(const Matrix &matrix,
426  bool omit_zeroing_entries)
427  {
428  v.reinit( matrix.n(),
429  omit_zeroing_entries);
430  }
431  };
432 
433  } /* namespace LinearOperator */
434 } /* namespace internal */
435 
436 
442 template <>
443 struct is_serial_vector< PETScWrappers::Vector > : std_cxx11::true_type
444 {
445 };
446 
447 
448 DEAL_II_NAMESPACE_CLOSE
449 
450 #endif // DEAL_II_WITH_PETSC
451 
452 /*---------------------------- petsc_vector.h ---------------------------*/
453 
454 #endif
455 /*---------------------------- petsc_vector.h ---------------------------*/
types::global_dof_index size_type
Definition: petsc_vector.h:60
#define AssertThrow(cond, exc)
Definition: exceptions.h:369
VectorBase & operator=(const PetscScalar s)
Vector & operator=(const Vector &v)
static void reinit_domain_vector(const Matrix &matrix, Vector &v, bool omit_zeroing_entries)
static const bool supports_distributed_data
Definition: petsc_vector.h:75
void compress(const VectorOperation::values operation)
unsigned int global_dof_index
Definition: types.h:88
std::size_t size() const
void create_vector(const size_type n)
static void reinit_range_vector(const Matrix &matrix, Vector &v, bool omit_zeroing_entries)
void swap(Vector &u, Vector &v)
Definition: petsc_vector.h:212
void reinit(const size_type N, const bool omit_zeroing_entries=false)
Definition: petsc_vector.cc:76