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 <array>
12 #include <cstdint>
13 #include <dolfinx/common/span.hpp>
14 #include <petscvec.h>
15 #include <vector>
16 
17 namespace dolfinx
18 {
19 namespace common
20 {
21 class IndexMap;
22 }
23 namespace la
24 {
25 
33 Vec create_ghosted_vector(const common::IndexMap& map, int bs,
34  tcb::span<PetscScalar> x);
35 
37 void petsc_error(int error_code, std::string filename,
38  std::string petsc_function);
39 
51 std::vector<IS> create_petsc_index_sets(
52  const std::vector<
53  std::pair<std::reference_wrapper<const common::IndexMap>, int>>& maps);
54 
62 Vec create_petsc_vector(const common::IndexMap& map, int bs);
63 
74 Vec create_petsc_vector(MPI_Comm comm, std::array<std::int64_t, 2> range,
75  const std::vector<std::int64_t>& ghosts, int bs);
76 
78 std::vector<std::vector<PetscScalar>> get_local_vectors(
79  const Vec x,
80  const std::vector<
81  std::pair<std::reference_wrapper<const common::IndexMap>, int>>& maps);
82 
85  Vec x, const std::vector<tcb::span<const PetscScalar>>& x_b,
86  const std::vector<
87  std::pair<std::reference_wrapper<const common::IndexMap>, int>>& maps);
88 
95 {
96 public:
103  PETScVector(const common::IndexMap& map, int bs);
104 
105  // Delete copy constructor to avoid accidental copying of 'heavy' data
106  PETScVector(const PETScVector& x) = delete;
107 
110 
122  PETScVector(Vec x, bool inc_ref_count);
123 
125  virtual ~PETScVector();
126 
127  // Assignment operator (disabled)
128  PETScVector& operator=(const PETScVector& x) = delete;
129 
131  PETScVector& operator=(PETScVector&& x);
132 
136  PETScVector copy() const;
137 
139  std::int64_t size() const;
140 
142  std::int32_t local_size() const;
143 
145  std::array<std::int64_t, 2> local_range() const;
146 
148  MPI_Comm mpi_comm() const;
149 
156  PetscReal norm(la::Norm type) const;
157 
159  void set_options_prefix(std::string options_prefix);
160 
163  std::string get_options_prefix() const;
164 
166  void set_from_options();
167 
169  Vec vec() const;
170 
171 private:
172  // PETSc Vec pointer
173  Vec _x;
174 };
175 } // namespace la
176 } // namespace dolfinx
This class represents the distribution index arrays across processes. An index array is a contiguous ...
Definition: IndexMap.h:46
A simple wrapper for a PETSc vector pointer (Vec). Its main purpose is to assist with memory/lifetime...
Definition: PETScVector.h:95
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
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:45
Norm
Norm types.
Definition: utils.h:14
void scatter_local_vectors(Vec x, const std::vector< tcb::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
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:27
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, tcb::span< PetscScalar > x)
Create a PETSc Vec that wraps the data in an array.
Definition: PETScVector.cpp:65