Reference documentation for deal.II version GIT 4e92747666 2022-12-02 08:40:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
petsc_vector.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2004 - 2021 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_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/index_set.h>
27 
28 # include <deal.II/lac/exceptions.h>
30 # include <deal.II/lac/vector.h>
33 
35 
36 
41 namespace PETScWrappers
42 {
49  namespace MPI
50  {
158  class Vector : public VectorBase
159  {
160  public:
165 
169  Vector();
170 
187  explicit Vector(const MPI_Comm &communicator,
188  const size_type n,
190 
201  template <typename Number>
202  explicit Vector(const MPI_Comm & communicator,
203  const ::Vector<Number> &v,
205 
229  Vector(const IndexSet &local,
230  const IndexSet &ghost,
231  const MPI_Comm &communicator);
232 
244  explicit Vector(const IndexSet &local, const MPI_Comm &communicator);
245 
249  Vector(const Vector &v);
250 
255  virtual void
256  clear() override;
257 
262  Vector &
263  operator=(const Vector &v);
264 
271  Vector &
272  operator=(const PetscScalar s);
273 
283  template <typename number>
284  Vector &
285  operator=(const ::Vector<number> &v);
286 
303  void
304  reinit(const MPI_Comm &communicator,
305  const size_type N,
307  const bool omit_zeroing_entries = false);
308 
318  void
319  reinit(const Vector &v, const bool omit_zeroing_entries = false);
320 
328  void
329  reinit(const IndexSet &local,
330  const IndexSet &ghost,
331  const MPI_Comm &communicator);
332 
340  void
341  reinit(const IndexSet &local, const MPI_Comm &communicator);
342 
347  void
348  reinit(
349  const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner);
350 
355  const MPI_Comm &
356  get_mpi_communicator() const override;
357 
369  void
370  print(std::ostream & out,
371  const unsigned int precision = 3,
372  const bool scientific = true,
373  const bool across = true) const;
374 
381  bool
382  all_zero() const;
383 
384  protected:
391  virtual void
393 
394 
395 
401  virtual void
402  create_vector(const size_type n,
404  const IndexSet &ghostnodes);
405 
406 
407  private:
411  MPI_Comm communicator;
412  };
413 
414 
415  // ------------------ template and inline functions -------------
416 
417 
425  inline void
427  {
428  u.swap(v);
429  }
430 
431 
432 # ifndef DOXYGEN
433 
434  template <typename number>
435  Vector::Vector(const MPI_Comm & communicator,
436  const ::Vector<number> &v,
437  const size_type locally_owned_size)
438  : communicator(communicator)
439  {
441 
442  *this = v;
443  }
444 
445 
446 
447  inline Vector &
448  Vector::operator=(const PetscScalar s)
449  {
451 
452  return *this;
453  }
454 
455 
456 
457  template <typename number>
458  inline Vector &
459  Vector::operator=(const ::Vector<number> &v)
460  {
461  Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size()));
462 
463  // FIXME: the following isn't necessarily fast, but this is due to
464  // the fact that PETSc doesn't offer an inlined access operator.
465  //
466  // if someone wants to contribute some code: to make this code
467  // faster, one could either first convert all values to PetscScalar,
468  // and then set them all at once using VecSetValues. This has the
469  // drawback that it could take quite some memory, if the vector is
470  // large, and it would in addition allocate memory on the heap, which
471  // is expensive. an alternative would be to split the vector into
472  // chunks of, say, 128 elements, convert a chunk at a time and set it
473  // in the output vector using VecSetValues. since 128 elements is
474  // small enough, this could easily be allocated on the stack (as a
475  // local variable) which would make the whole thing much more
476  // efficient.
477  //
478  // a second way to make things faster is for the special case that
479  // number==PetscScalar. we could then declare a specialization of
480  // this template, and omit the conversion. the problem with this is
481  // that the best we can do is to use VecSetValues, but this isn't
482  // very efficient either: it wants to see an array of indices, which
483  // in this case a) again takes up a whole lot of memory on the heap,
484  // and b) is totally dumb since its content would simply be the
485  // sequence 0,1,2,3,...,n. the best of all worlds would probably be a
486  // function in Petsc that would take a pointer to an array of
487  // PetscScalar values and simply copy n elements verbatim into the
488  // vector...
489  for (size_type i = 0; i < v.size(); ++i)
490  (*this)(i) = v(i);
491 
493 
494  return *this;
495  }
496 
497 
498 
499  inline const MPI_Comm &
501  {
502  return communicator;
503  }
504 
505 # endif // DOXYGEN
506  } // namespace MPI
507 } // namespace PETScWrappers
508 
509 namespace internal
510 {
511  namespace LinearOperatorImplementation
512  {
513  template <typename>
514  class ReinitHelper;
515 
520  template <>
522  {
523  public:
524  template <typename Matrix>
525  static void
526  reinit_range_vector(const Matrix & matrix,
528  bool /*omit_zeroing_entries*/)
529  {
530  v.reinit(matrix.locally_owned_range_indices(),
531  matrix.get_mpi_communicator());
532  }
533 
534  template <typename Matrix>
535  static void
536  reinit_domain_vector(const Matrix & matrix,
538  bool /*omit_zeroing_entries*/)
539  {
540  v.reinit(matrix.locally_owned_domain_indices(),
541  matrix.get_mpi_communicator());
542  }
543  };
544 
545  } // namespace LinearOperatorImplementation
546 } /* namespace internal */
547 
554 template <>
555 struct is_serial_vector<PETScWrappers::MPI::Vector> : std::false_type
556 {};
557 
558 
560 
561 #endif // DEAL_II_WITH_PETSC
562 
563 #endif
types::global_dof_index size_type
Definition: petsc_vector.h:164
Vector(const MPI_Comm &communicator, const ::Vector< Number > &v, const size_type locally_owned_size)
Vector & operator=(const PetscScalar s)
const MPI_Comm & get_mpi_communicator() const override
virtual void create_vector(const size_type n, const size_type locally_owned_size)
Vector & operator=(const ::Vector< number > &v)
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
virtual void clear() override
void reinit(const MPI_Comm &communicator, const size_type N, const size_type locally_owned_size, const bool omit_zeroing_entries=false)
Vector & operator=(const Vector &v)
void swap(Vector &u, Vector &v)
Definition: petsc_vector.h:426
void compress(const VectorOperation::values operation)
VectorBase & operator=(const VectorBase &)=delete
size_type locally_owned_size() const
Definition: vector.h:109
static void reinit_domain_vector(const Matrix &matrix, PETScWrappers::MPI::Vector &v, bool)
Definition: petsc_vector.h:536
static void reinit_range_vector(const Matrix &matrix, PETScWrappers::MPI::Vector &v, bool)
Definition: petsc_vector.h:526
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:458
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:459
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
#define Assert(cond, exc)
Definition: exceptions.h:1501
@ matrix
Contents is actually a matrix.
static const char N
types::global_dof_index size_type
unsigned int global_dof_index
Definition: types.h:81