Note: this is documentation for an old release. View the latest documentation at docs.fenicsproject.org/v0.3.0/v0.9.0/cpp
DOLFINx  0.3.0
DOLFINx 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 <array>
12 #include <cstdint>
13 #include <functional>
14 #include <petscvec.h>
15 #include <vector>
16 #include <xtl/xspan.hpp>
17 
18 namespace dolfinx::common
19 {
20 class IndexMap;
21 }
22 namespace dolfinx::la
23 {
24 
32 Vec create_ghosted_vector(const common::IndexMap& map, int bs,
33  xtl::span<PetscScalar> x);
34 
36 void petsc_error(int error_code, std::string filename,
37  std::string petsc_function);
38 
50 std::vector<IS> create_petsc_index_sets(
51  const std::vector<
52  std::pair<std::reference_wrapper<const common::IndexMap>, int>>& maps);
53 
61 Vec create_petsc_vector(const common::IndexMap& map, int bs);
62 
73 Vec create_petsc_vector(MPI_Comm comm, std::array<std::int64_t, 2> range,
74  const std::vector<std::int64_t>& ghosts, int bs);
75 
77 std::vector<std::vector<PetscScalar>> get_local_vectors(
78  const Vec x,
79  const std::vector<
80  std::pair<std::reference_wrapper<const common::IndexMap>, int>>& maps);
81 
84  Vec x, const std::vector<xtl::span<const PetscScalar>>& x_b,
85  const std::vector<
86  std::pair<std::reference_wrapper<const common::IndexMap>, int>>& maps);
87 
94 {
95 public:
102  PETScVector(const common::IndexMap& map, int bs);
103 
104  // Delete copy constructor to avoid accidental copying of 'heavy' data
105  PETScVector(const PETScVector& x) = delete;
106 
109 
121  PETScVector(Vec x, bool inc_ref_count);
122 
124  virtual ~PETScVector();
125 
126  // Assignment operator (disabled)
127  PETScVector& operator=(const PETScVector& x) = delete;
128 
130  PETScVector& operator=(PETScVector&& x);
131 
135  PETScVector copy() const;
136 
138  std::int64_t size() const;
139 
141  std::int32_t local_size() const;
142 
144  std::array<std::int64_t, 2> local_range() const;
145 
147  MPI_Comm mpi_comm() const;
148 
155  PetscReal norm(la::Norm type) const;
156 
158  void set_options_prefix(std::string options_prefix);
159 
162  std::string get_options_prefix() const;
163 
165  void set_from_options();
166 
168  Vec vec() const;
169 
170 private:
171  // PETSc Vec pointer
172  Vec _x;
173 };
174 } // namespace dolfinx::la
This class represents the distribution index arrays across processes. An index array is a contiguous ...
Definition: IndexMap.h:49
A simple wrapper for a PETSc vector pointer (Vec). Its main purpose is to assist with memory/lifetime...
Definition: PETScVector.h:94
virtual ~PETScVector()
Destructor.
Definition: PETScVector.cpp:204
std::int32_t local_size() const
Return local size of vector (belonging to this process)
Definition: PETScVector.cpp:235
PETScVector(const common::IndexMap &map, int bs)
Create vector.
Definition: PETScVector.cpp:189
void set_from_options()
Call PETSc function VecSetFromOptions on the underlying Vec object.
Definition: PETScVector.cpp:305
std::string get_options_prefix() const
Returns the prefix used by PETSc when searching the options database.
Definition: PETScVector.cpp:296
void set_options_prefix(std::string options_prefix)
Sets the prefix used by PETSc when searching the options database.
Definition: PETScVector.cpp:289
PETScVector copy() const
Create a copy of the vector.
Definition: PETScVector.cpp:216
std::array< std::int64_t, 2 > local_range() const
Return ownership range for calling rank.
Definition: PETScVector.cpp:244
std::int64_t size() const
Return global size of vector.
Definition: PETScVector.cpp:226
MPI_Comm mpi_comm() const
Return MPI communicator.
Definition: PETScVector.cpp:254
PetscReal norm(la::Norm type) const
Compute norm of vector.
Definition: PETScVector.cpp:263
Vec vec() const
Return pointer to PETSc Vec object.
Definition: PETScVector.cpp:312
Miscellaneous classes, functions and types.
Linear algebra interface.
Definition: sparsitybuild.h:13
void scatter_local_vectors(Vec x, const std::vector< xtl::span< const PetscScalar >> &x_b, const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int >> &maps)
Scatter local vectors to Vec.
Definition: PETScVector.cpp:145
std::vector< std::vector< PetscScalar > > get_local_vectors(const Vec x, const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int >> &maps)
Copy blocks from Vec into local vectors.
Definition: PETScVector.cpp:102
std::vector< IS > create_petsc_index_sets(const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int >> &maps)
Definition: PETScVector.cpp:44
Norm
Norm types.
Definition: utils.h:13
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:26
Vec create_petsc_vector(const common::IndexMap &map, int bs)
Create a ghosted PETSc Vec.
Definition: PETScVector.cpp:77
Vec create_ghosted_vector(const common::IndexMap &map, int bs, xtl::span< PetscScalar > x)
Create a PETSc Vec that wraps the data in an array.
Definition: PETScVector.cpp:64