DOLFIN-X
DOLFIN-X C++ interface
PETScVector.h
1 // Copyright (C) 2004-2018 Johan Hoffman, Johan Jansson, Anders Logg and Garth
2 // N. Wells
3 //
4 // This file is part of DOLFINX (https://www.fenicsproject.org)
5 //
6 // SPDX-License-Identifier: LGPL-3.0-or-later
7 
8 #pragma once
9 
10 #include "utils.h"
11 #include <Eigen/Dense>
12 #include <array>
13 #include <cstdint>
14 #include <petscvec.h>
15 
16 namespace dolfinx
17 {
18 namespace common
19 {
20 class IndexMap;
21 }
22 namespace la
23 {
24 
32  const common::IndexMap& map,
33  const Eigen::Matrix<PetscScalar, Eigen::Dynamic, 1>& x);
34 
36 void petsc_error(int error_code, std::string filename,
37  std::string petsc_function);
38 
48 std::vector<IS>
49 create_petsc_index_sets(const std::vector<const common::IndexMap*>& maps);
50 
53 Vec create_petsc_vector(const common::IndexMap& map);
54 
58  MPI_Comm comm, std::array<std::int64_t, 2> range,
59  const Eigen::Ref<const Eigen::Array<std::int64_t, Eigen::Dynamic, 1>>&
60  ghost_indices,
61  int block_size);
62 
64 std::vector<Eigen::Matrix<PetscScalar, Eigen::Dynamic, 1>>
65 get_local_vectors(const Vec x,
66  const std::vector<const common::IndexMap*>& maps);
67 
70  Vec x,
71  const std::vector<Eigen::Matrix<PetscScalar, Eigen::Dynamic, 1>>& x_b,
72  const std::vector<const common::IndexMap*>& maps);
73 
79 
81 {
82 public:
84  PETScVector(const common::IndexMap& map);
85 
88  MPI_Comm comm, std::array<std::int64_t, 2> range,
89  const Eigen::Array<std::int64_t, Eigen::Dynamic, 1>& ghost_indices,
90  int block_size);
91 
92  // Delete copy constructor to avoid accidental copying of 'heavy' data
93  PETScVector(const PETScVector& x) = delete;
94 
96  PETScVector(PETScVector&& x) noexcept;
97 
103  PETScVector(Vec x, bool inc_ref_count);
104 
106  virtual ~PETScVector();
107 
108  // Assignment operator (disabled)
109  PETScVector& operator=(const PETScVector& x) = delete;
110 
112  PETScVector& operator=(PETScVector&& x);
113 
115  PETScVector copy() const;
116 
118  std::int64_t size() const;
119 
121  std::int32_t local_size() const;
122 
124  std::array<std::int64_t, 2> local_range() const;
125 
130  void apply_ghosts();
131 
134  void update_ghosts();
135 
137  MPI_Comm mpi_comm() const;
138 
140  PetscReal norm(la::Norm norm_type) const;
141 
143  void set_options_prefix(std::string options_prefix);
144 
147  std::string get_options_prefix() const;
148 
150  void set_from_options();
151 
153  Vec vec() const;
154 
155 private:
156  // PETSc Vec pointer
157  Vec _x;
158 };
159 } // namespace la
160 } // namespace dolfinx
dolfinx::la::Norm
Norm
Norm types.
Definition: utils.h:14
dolfinx::la::PETScVector::get_options_prefix
std::string get_options_prefix() const
Returns the prefix used by PETSc when searching the options database.
Definition: PETScVector.cpp:386
dolfinx::la::get_local_vectors
std::vector< Eigen::Matrix< PetscScalar, Eigen::Dynamic, 1 > > get_local_vectors(const Vec x, const std::vector< const common::IndexMap * > &maps)
Copy blocks from Vec into Eigen vectors.
Definition: PETScVector.cpp:136
dolfinx::la::create_petsc_index_sets
std::vector< IS > create_petsc_index_sets(const std::vector< const common::IndexMap * > &maps)
Definition: PETScVector.cpp:59
dolfinx::la::petsc_error
void petsc_error(int error_code, std::string filename, std::string petsc_function)
Print error message for PETSc calls that return an error.
Definition: PETScVector.cpp:28
dolfinx::common::IndexMap
This class represents the distribution index arrays across processes. An index array is a contiguous ...
Definition: IndexMap.h:45
dolfinx::la::PETScVector::set_options_prefix
void set_options_prefix(std::string options_prefix)
Sets the prefix used by PETSc when searching the options database.
Definition: PETScVector.cpp:379
dolfinx::la::PETScVector::size
std::int64_t size() const
Return global size of vector.
Definition: PETScVector.cpp:276
dolfinx::la::PETScVector::PETScVector
PETScVector(const common::IndexMap &map)
Create vector.
Definition: PETScVector.cpp:224
dolfinx::la::PETScVector::set_from_options
void set_from_options()
Call PETSc function VecSetFromOptions on the underlying Vec object.
Definition: PETScVector.cpp:395
dolfinx::la::PETScVector::copy
PETScVector copy() const
Copy vector.
Definition: PETScVector.cpp:266
dolfinx::la::PETScVector
It is a simple wrapper for a PETSc vector pointer (Vec). Its main purpose is to assist memory managem...
Definition: PETScVector.h:81
dolfinx::la::create_petsc_vector
Vec create_petsc_vector(const common::IndexMap &map)
Create a ghosted PETSc Vec. Caller is responsible for destroying the returned object.
Definition: PETScVector.cpp:100
dolfinx::la::scatter_local_vectors
void scatter_local_vectors(Vec x, const std::vector< Eigen::Matrix< PetscScalar, Eigen::Dynamic, 1 >> &x_b, const std::vector< const common::IndexMap * > &maps)
Scatter local Eigen vectors to Vec.
Definition: PETScVector.cpp:180
dolfinx::la::create_ghosted_vector
Vec create_ghosted_vector(const common::IndexMap &map, const Eigen::Matrix< PetscScalar, Eigen::Dynamic, 1 > &x)
Create a PETSc Vec that wraps the data in x.
Definition: PETScVector.cpp:79
dolfinx::la::PETScVector::local_size
std::int32_t local_size() const
Return local size of vector (belonging to this process)
Definition: PETScVector.cpp:285
dolfinx::la::PETScVector::norm
PetscReal norm(la::Norm norm_type) const
Return norm of vector.
Definition: PETScVector.cpp:353
dolfinx::la::PETScVector::mpi_comm
MPI_Comm mpi_comm() const
Return MPI communicator.
Definition: PETScVector.cpp:344
dolfinx::la::PETScVector::update_ghosts
void update_ghosts()
Update ghost values (gathers ghost values from the owning processes)
Definition: PETScVector.cpp:324
dolfinx::la::PETScVector::local_range
std::array< std::int64_t, 2 > local_range() const
Return ownership range for process.
Definition: PETScVector.cpp:294
dolfinx::la::PETScVector::~PETScVector
virtual ~PETScVector()
Destructor.
Definition: PETScVector.cpp:251
dolfinx::la::PETScVector::apply_ghosts
void apply_ghosts()
Update owned entries owned by this process and which are ghosts on other processes,...
Definition: PETScVector.cpp:304
dolfinx::la::PETScVector::vec
Vec vec() const
Return pointer to PETSc Vec object.
Definition: PETScVector.cpp:402