OGS
PETScVector.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <petscvec.h>
7
8#include <map>
9#include <span>
10#include <string>
11#include <vector>
12
13namespace MathLib
14{
28{
29public:
30 using IndexType = PetscInt;
31 // TODO make this class opaque, s.t. the PETSc symbols are not scattered all
32 // over the global namespace
33 using PETSc_Vec = Vec;
34
35public:
48 PETScVector(const PetscInt vec_size, const bool is_global_size = true);
49
58 PETScVector(const PetscInt vec_size, const std::vector<PetscInt>& ghost_ids,
59 const bool is_global_size = true);
60
67 explicit PETScVector(const PETScVector& existing_vec,
68 const bool deep_copy = true);
69
70 PETScVector(PETScVector&& other);
71
74 void finalizeAssembly();
75
77 PetscInt size() const { return size_; }
79 PetscInt getLocalSize() const { return size_loc_; }
81 PetscInt getGhostSize() const { return size_ghosts_; }
83 PetscInt getRangeBegin() const { return start_rank_; }
85 PetscInt getRangeEnd() const { return end_rank_; }
92 void set(const PetscInt i, const PetscScalar value)
93 {
94 PetscCallAbort(PETSC_COMM_WORLD,
95 VecSetValue(v_, i, value, INSERT_VALUES));
96 }
97
103 void add(const PetscInt i, const PetscScalar value)
104 {
105 PetscCallAbort(PETSC_COMM_WORLD, VecSetValue(v_, i, value, ADD_VALUES));
106 }
107
115 template <class T_SUBVEC>
116 void add(const std::vector<PetscInt>& e_idxs, const T_SUBVEC& sub_vec)
117 {
118 if constexpr (std::is_pointer_v<T_SUBVEC>)
119 {
120 PetscCallAbort(PETSC_COMM_WORLD,
121 VecSetValues(v_, e_idxs.size(), &e_idxs[0],
122 &sub_vec[0], ADD_VALUES));
123 }
124 else
125 {
126 PetscCallAbort(PETSC_COMM_WORLD,
127 VecSetValues(v_, e_idxs.size(), e_idxs.data(),
128 sub_vec.data(), ADD_VALUES));
129 }
130 }
131
139 template <class T_SUBVEC>
140 void set(const std::vector<PetscInt>& e_idxs, const T_SUBVEC& sub_vec)
141 {
142 if constexpr (std::is_pointer_v<T_SUBVEC>)
143 {
144 PetscCallAbort(PETSC_COMM_WORLD,
145 VecSetValues(v_, e_idxs.size(), e_idxs.data(),
146 sub_vec, INSERT_VALUES));
147 }
148 else
149 {
150 PetscCallAbort(PETSC_COMM_WORLD,
151 VecSetValues(v_, e_idxs.size(), e_idxs.data(),
152 sub_vec.data(), INSERT_VALUES));
153 }
154 }
155
156 // TODO preliminary
157 void setZero() { PetscCallAbort(PETSC_COMM_WORLD, VecSet(v_, 0.0)); }
162
165 void setLocalAccessibleVector() const;
166
169 std::vector<PetscScalar> get(std::vector<IndexType> const& indices) const;
170
173 PetscScalar operator[](PetscInt idx) const { return get(idx); }
179 void getGlobalVector(std::vector<PetscScalar>& u) const;
180
181 /* Get an entry value. This is an expensive operation,
182 and it only get local value. Use it for only test purpose
183 Get the value of an entry by [] operator.
184 setLocalAccessibleVector() must be called beforehand.
185 */
186 PetscScalar get(const PetscInt idx) const;
187
189 PETSc_Vec& getRawVector() { return v_; }
196 PETSc_Vec const& getRawVector() const { return v_; }
202 void copyValues(std::vector<PetscScalar>& u) const;
203
209 void copyValues(std::span<PetscScalar> u) const;
210
240 void viewer(
241 const std::string& file_name,
242 const PetscViewerFormat vw_format = PETSC_VIEWER_ASCII_MATLAB) const;
243
244 void shallowCopy(const PETScVector& v);
245
246private:
247 void destroy()
248 {
249 if (v_ != nullptr)
250 {
251 PetscCallAbort(PETSC_COMM_WORLD, VecDestroy(&v_));
252 }
253 v_ = nullptr;
254 }
255
256 PETSc_Vec v_ = nullptr;
259 mutable PETSc_Vec v_loc_ = nullptr;
260
262 PetscInt start_rank_;
264 PetscInt end_rank_;
265
267 PetscInt size_;
269 PetscInt size_loc_;
271 PetscInt size_ghosts_ = 0;
272
275
283 mutable std::vector<PetscScalar> entry_array_;
284
286 mutable std::map<PetscInt, PetscInt> global_ids2local_ids_ghost_;
287
291 PetscScalar* getLocalVector() const;
292
294 PetscInt getLocalIndex(const PetscInt global_index) const;
295
300 inline void restoreArray(PetscScalar* array) const;
301
303 void config();
304
305 friend void finalizeVectorAssembly(PETScVector& vec);
306};
307
310
311} // namespace MathLib
Wrapper class for PETSc vector.
Definition PETScVector.h:28
PetscInt start_rank_
Starting index in a rank.
void finalizeAssembly()
Perform MPI collection of assembled entries in buffer.
std::vector< PetscScalar > entry_array_
Array containing the entries of the vector. If the vector is created without given ghost IDs,...
PetscScalar operator[](PetscInt idx) const
void set(const std::vector< PetscInt > &e_idxs, const T_SUBVEC &sub_vec)
PETSc_Vec const & getRawVector() const
void config()
A function called by constructors to configure members.
void setLocalAccessibleVector() const
PetscInt getRangeBegin() const
Get the start index of the local vector.
Definition PETScVector.h:83
void restoreArray(PetscScalar *array) const
PetscInt getLocalSize() const
Get the number of entries in the same rank.
Definition PETScVector.h:79
PetscInt size_loc_
Size of local entries.
bool created_with_ghost_id_
Flag to indicate whether the vector is created with ghost entry indices.
PetscScalar * getLocalVector() const
void getGlobalVector(std::vector< PetscScalar > &u) const
std::vector< PetscScalar > get(std::vector< IndexType > const &indices) const
void add(const std::vector< PetscInt > &e_idxs, const T_SUBVEC &sub_vec)
PetscInt size_ghosts_
Size of local ghost entries.
void copyValues(std::vector< PetscScalar > &u) const
PetscInt getRangeEnd() const
Get the end index of the local vector.
Definition PETScVector.h:85
PetscInt size_
Size of the vector.
PETScVector & operator=(PETScVector &&)=delete
friend void finalizeVectorAssembly(PETScVector &vec)
Function to finalize the vector assembly.
PetscInt size() const
Get the global size of the vector.
Definition PETScVector.h:77
void shallowCopy(const PETScVector &v)
void add(const PetscInt i, const PetscScalar value)
void set(const PetscInt i, const PetscScalar value)
Definition PETScVector.h:92
PETSc_Vec & getRawVector()
Exposes the underlying PETSc vector.
PetscInt getGhostSize() const
Get the number of ghost entries in the same rank.
Definition PETScVector.h:81
std::map< PetscInt, PetscInt > global_ids2local_ids_ghost_
Map global indices of ghost entries to local indices.
PetscInt getLocalIndex(const PetscInt global_index) const
Get local index by a global index.
PetscInt end_rank_
Ending index in a rank.
void viewer(const std::string &file_name, const PetscViewerFormat vw_format=PETSC_VIEWER_ASCII_MATLAB) const
void finalizeVectorAssembly(VEC_T &)
General function to finalize the vector assembly.
static const double u
static const double v