Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
vector.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Francesco Daniele <francesco.daniele@polimi.it>
22// Author: Francesco Rosnati <francesco.rosnati@polimi.it>
23// Author: Matteo Caldana <matteo.caldana@polimi.it>
24// Author: Pedro Peixoto <ppeixoto@usp.br>
25
26#ifndef VULPES_LA_VECTOR_HPP_
27#define VULPES_LA_VECTOR_HPP_
28
29#include <petscsys.h>
30#include <petscvec.h>
31
32#include <algorithm>
33#include <array>
34#include <cstddef>
35#include <memory>
36#include <string>
37#include <type_traits>
38#include <utility>
39#include <vector>
40
41#include <Eigen/Dense>
42
46
47namespace vulpes {
48namespace la {
49
54class Vector {
55 static_assert(std::is_same_v<double, PetscScalar>);
56
57 public:
59 template <typename DofHandler>
60 explicit Vector(const DofHandler& dof_handler, PetscInt block_size = 1) {
61 static_assert(std::is_same_v<typename DofHandler::index_t, PetscInt>);
62 init(dof_handler, block_size);
63 }
64
66 explicit Vector(const std::pair<PetscInt, PetscInt>& sizes) {
67 createVector(); // Create the PETSc vector
68 setVectorSizes(sizes.first, sizes.second); // Set local and global sizes
69 setupVector(); // Finalize setup
70 }
71
74 struct GhostTag {};
75
85 template <typename DofHandler>
86 Vector(GhostTag, const DofHandler& dof_handler) {
87 initGhosted(dof_handler);
88 }
89
92 template <typename DofHandler>
93 static Vector createGhosted(const DofHandler& dof_handler) {
94 return Vector(GhostTag{}, dof_handler);
95 }
96
97 Vector& operator=(const Vector& other) = delete;
98
100 Vector(const Vector& other);
101
103 template <typename DofHandler>
104 void init(const DofHandler& dof_handler, PetscInt block_size) {
105 createVector();
106 if (block_size > 1) {
107 PetscCallAbortWorld(VecSetBlockSize(m_petsc_vector, block_size));
108 }
109 const PetscInt n_glob = dof_handler.ndofsGlobal();
110 PetscInt dof_begin = 0;
111 PetscInt dof_end = n_glob;
112 if constexpr (DofHandler::parallel) {
113 dof_begin = dof_handler.dofOffset()[mpi::rank()];
114 dof_end = dof_handler.dofOffset()[mpi::rank() + 1];
115 }
116 const PetscInt n_loc = dof_end - dof_begin;
117 setVectorSizes(n_loc * block_size, n_glob * block_size);
118 setupVector();
119 }
120
123 template <typename DofHandler>
124 void initGhosted(const DofHandler& dof_handler) {
125 static_assert(std::is_same_v<typename DofHandler::index_t, PetscInt>);
126 const auto& ghost_dofs = dof_handler.getGhostDofs();
127 const PetscInt n_glob = dof_handler.ndofsGlobal();
128 const PetscInt n_owned = dof_handler.nOwnedDofs();
129 const PetscInt n_ghost = static_cast<PetscInt>(ghost_dofs.size());
130 PetscCallAbortWorld(VecCreateGhost(
131 PETSC_COMM_WORLD, n_owned, n_glob, n_ghost, ghost_dofs.data(),
133 m_is_ghosted = true;
134 }
135
137 void copy(const Vector& other);
138
140 void compress();
142 void print() const;
144 void save(
145 const std::string& filename,
146 const std::string& vecname = "",
147 PetscViewerFormat format = PETSC_VIEWER_ASCII_MATLAB) const;
149 void saveBinary(
150 const std::string& filename, const std::string& vecname = "") const;
151
160 void insertValues(
161 const PetscScalar* values,
162 const PetscInt* indices,
163 PetscInt n_values,
164 InsertMode mode = ADD_VALUES);
165
176 const PetscScalar* values,
177 const PetscInt* indices,
178 PetscInt n_values,
179 InsertMode mode = ADD_VALUES);
180
188 void insertValues(
189 const Eigen::Ref<const Eigen::MatrixXd>& loc_vec,
191 InsertMode mode = ADD_VALUES);
192
202 const Eigen::Ref<const Eigen::MatrixXd>& loc_vec,
204 InsertMode mode = ADD_VALUES);
205
207 void setValue(
208 PetscInt idx, PetscScalar value, InsertMode mode = INSERT_VALUES);
210 PetscScalar getValue(PetscInt idx) const;
211
213 void getLocalValues(std::vector<PetscScalar>& values) const;
215 std::array<PetscInt, 2> getOwnershipRange() const;
217 PetscInt getBlockSize() const;
220 Eigen::Map<const Eigen::VectorXd> toEigenMap() const;
222 Eigen::Map<const Eigen::VectorXd> toLocalEigenMap() const;
223
229 void updateGhosts();
232 void updateGhostsBegin();
233 void updateGhostsEnd();
239 void accumulateGhostsEnd();
244 Eigen::Map<Eigen::VectorXd> getLocalGhostedView();
246
264 Eigen::VectorXd toLocalGhostedEigenVector() const;
266 std::pair<PetscInt, PetscInt> getLocAndGlobSize() const;
268 const Vec& getPetscVector() const { return m_petsc_vector; }
271
273 void zero();
275 void set(double value);
277 void scale(double value);
279 PetscScalar norm(NormType norm_type = NORM_2) const;
281 void add(const Vector& x) { addScaled(1.0, x); }
283 void add(PetscScalar alpha, const Vector& x);
285 void addScaled(PetscScalar alpha, const Vector& x);
287 void addGeneric(PetscScalar alpha, PetscScalar beta, const Vector& x);
289 void addCouple(PetscScalar alpha, const Vector& x, const Vector& y);
291 void addTriplet(
292 PetscScalar alpha,
293 PetscScalar beta,
294 PetscScalar gamma,
295 const Vector& x,
296 const Vector& y);
298 void pow(PetscScalar power);
300 void abs();
302 PetscInt getSize() const;
304 PetscInt getLocalSize() const;
306 PetscScalar sum() const;
308 PetscScalar norm2() const;
309
311 static bool compareVectors(
312 const Vector& vec1, const Vector& vec2, double tolerance = 1e-12);
313
314 ~Vector();
315
316 protected:
317 void createVector();
318 void setVectorSizes(const PetscInt n_loc, const PetscInt n_glob);
319 void setupVector();
320
322 bool m_is_ghosted = false;
323 // local form and its array while a getLocalGhostedView() is open
324 Vec m_local_form = nullptr;
325 PetscScalar* m_local_array = nullptr;
326};
327
328double dot(const Vector& a, const Vector& b);
329
330} // namespace la
331} // namespace vulpes
332
333#endif // VULPES_LA_VECTOR_HPP_
A wrapper around PETSc Vec for distributed vectors.
Definition vector.hpp:54
void addTriplet(PetscScalar alpha, PetscScalar beta, PetscScalar gamma, const Vector &x, const Vector &y)
Add a vector in place (this = alpha x + beta y + gamma this).
Definition vector.cpp:318
void insertValues(const PetscScalar *values, const PetscInt *indices, PetscInt n_values, InsertMode mode=ADD_VALUES)
Insert values into the vector at specified indices.
Definition vector.cpp:108
void copy(const Vector &other)
Copy the contents of another vector.
Definition vector.cpp:364
void save(const std::string &filename, const std::string &vecname="", PetscViewerFormat format=PETSC_VIEWER_ASCII_MATLAB) const
Save the vector to a file.
Definition vector.cpp:58
Eigen::Map< Eigen::VectorXd > getLocalGhostedView()
Writable view of the local form of a ghost-aware Vector (owned dofs, then ghost dofs,...
Definition vector.cpp:211
void setVectorSizes(const PetscInt n_loc, const PetscInt n_glob)
Definition vector.cpp:41
void set(double value)
Set all values in the vector to a constant.
Definition vector.cpp:280
PetscScalar getValue(PetscInt idx) const
Get a single value from the vector.
Definition vector.cpp:150
PetscScalar * m_local_array
Definition vector.hpp:325
void addCouple(PetscScalar alpha, const Vector &x, const Vector &y)
Add a vector in place (this = alpha x + y).
Definition vector.cpp:313
void initGhosted(const DofHandler &dof_handler)
Initialize a ghost-aware vector from a DofHandler (see the GhostTag constructor).
Definition vector.hpp:124
PetscInt getSize() const
Get the size of the vector.
Definition vector.cpp:337
std::pair< PetscInt, PetscInt > getLocAndGlobSize() const
Get local and global sizes of the vector.
Definition vector.cpp:256
Vec & getPetscVector()
Get the underlying PETSc vector (non-const version).
Definition vector.hpp:270
PetscScalar norm(NormType norm_type=NORM_2) const
Compute the norm of the vector.
Definition vector.cpp:288
void print() const
Print the vector to standard output.
Definition vector.cpp:54
void setValue(PetscInt idx, PetscScalar value, InsertMode mode=INSERT_VALUES)
Set a single value in the vector.
Definition vector.cpp:146
void add(const Vector &x)
Add a vector in place (this += x).
Definition vector.hpp:281
static bool compareVectors(const Vector &vec1, const Vector &vec2, double tolerance=1e-12)
Static method to compare two vectors with tolerance.
Definition vector.cpp:378
Vector(const std::pair< PetscInt, PetscInt > &sizes)
Construct a Vector given local and global sizes.
Definition vector.hpp:66
void accumulateGhostsEnd()
Definition vector.cpp:205
void abs()
Replaces each component of a vector by its absolute value.
Definition vector.cpp:333
void zero()
Zero out the vector.
Definition vector.cpp:276
void accumulateGhostsBegin()
Add the ghost entries of the local form (see getLocalGhostedView()) into the entries of their owning ...
Definition vector.cpp:199
void setupVector()
Definition vector.cpp:45
void insertValuesBlocked(const PetscScalar *values, const PetscInt *indices, PetscInt n_values, InsertMode mode=ADD_VALUES)
Insert blocked values into the vector at specified indices.
Definition vector.cpp:117
void updateGhostsBegin()
updateGhosts() split in two, so that the exchanges of several vectors overlap: begin all of them,...
Definition vector.cpp:187
Vector(const DofHandler &dof_handler, PetscInt block_size=1)
Construct a Vector from a DofHandler.
Definition vector.hpp:60
PetscScalar sum() const
Compute the sum of all elements in the vector.
Definition vector.cpp:349
void createVector()
Definition vector.cpp:36
PetscInt getBlockSize() const
Get the block size of the vector.
Definition vector.cpp:270
void init(const DofHandler &dof_handler, PetscInt block_size)
Initialize the vector based on a DofHandler.
Definition vector.hpp:104
void compress()
Compress the vector (do intracommunication).
Definition vector.cpp:49
void pow(PetscScalar power)
Replaces each component of a vector by its p-th power.
Definition vector.cpp:329
std::array< PetscInt, 2 > getOwnershipRange() const
Get the ownership range of the vector.
Definition vector.cpp:263
void addGeneric(PetscScalar alpha, PetscScalar beta, const Vector &x)
Add a vector in place (this = alpha x + beta this).
Definition vector.cpp:308
Vec m_local_form
Definition vector.hpp:324
void saveBinary(const std::string &filename, const std::string &vecname="") const
Save the vector to a binary file.
Definition vector.cpp:86
Eigen::VectorXd toLocalGhostedEigenVector() const
Local-form snapshot of a ghost-aware Vector (see createGhosted()): this rank's owned dofs first ([0,...
Definition vector.cpp:229
PetscInt getLocalSize() const
Get the local size of the vector.
Definition vector.cpp:343
Eigen::Map< const Eigen::VectorXd > toLocalEigenMap() const
Convert the locally-owned vector entries to an Eigen map.
Definition vector.cpp:173
~Vector()
Definition vector.cpp:360
void updateGhosts()
Refresh the ghost padding of a ghost-aware Vector (see createGhosted()) from each ghost dof's owning ...
Definition vector.cpp:182
Vec m_petsc_vector
Definition vector.hpp:321
void getLocalValues(std::vector< PetscScalar > &values) const
Get all local values of the vector.
Definition vector.cpp:156
Vector(GhostTag, const DofHandler &dof_handler)
Construct a ghost-aware Vector from a DofHandler: the local form additionally carries a padding segme...
Definition vector.hpp:86
Eigen::Map< const Eigen::VectorXd > toEigenMap() const
Convert the locally-owned vector entries to an Eigen map (same as toLocalEigenMap()).
Definition vector.cpp:169
static Vector createGhosted(const DofHandler &dof_handler)
Construct a ghost-aware Vector from a DofHandler. See the GhostTag constructor above for what this en...
Definition vector.hpp:93
void scale(double value)
Scale the vector by a constant.
Definition vector.cpp:284
void restoreLocalGhostedView()
Definition vector.cpp:221
const Vec & getPetscVector() const
Get the underlying PETSc vector (const version).
Definition vector.hpp:268
void addScaled(PetscScalar alpha, const Vector &x)
Add a vector in place (this = x + alpha this).
Definition vector.cpp:304
bool m_is_ghosted
Definition vector.hpp:322
Vector & operator=(const Vector &other)=delete
PetscScalar norm2() const
Compute the Euclidean norm of the vector.
Definition vector.cpp:374
void updateGhostsEnd()
Definition vector.cpp:193
Definition boomer_amg.hpp:27
double dot(const Vector &a, const Vector &b)
Definition vector.cpp:368
static int rank()
Return the MPI rank in MPI_COMM_WORLD.
Definition mpi_wrappers.hpp:116
Definition cell_manager.hpp:38
#define PetscCallAbortWorld(...)
Definition petsc_utils.hpp:32
Tag selecting the ghost-aware constructor overload (see createGhosted()).
Definition vector.hpp:74