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 VecSetValue(v_, i, value, INSERT_VALUES);
95 }
96
102 void add(const PetscInt i, const PetscScalar value)
103 {
104 VecSetValue(v_, i, value, ADD_VALUES);
105 }
106
114 template <class T_SUBVEC>
115 void add(const std::vector<PetscInt>& e_idxs, const T_SUBVEC& sub_vec)
116 {
117 if constexpr (std::is_pointer_v<T_SUBVEC>)
118 {
119 VecSetValues(v_, e_idxs.size(), e_idxs.data(), sub_vec, ADD_VALUES);
120 }
121 else
122 {
123 VecSetValues(v_, e_idxs.size(), e_idxs.data(), sub_vec.data(),
124 ADD_VALUES);
125 }
126 }
127
135 template <class T_SUBVEC>
136 void set(const std::vector<PetscInt>& e_idxs, const T_SUBVEC& sub_vec)
137 {
138 if constexpr (std::is_pointer_v<T_SUBVEC>)
139 {
140 VecSetValues(v_, e_idxs.size(), e_idxs.data(), sub_vec,
141 INSERT_VALUES);
142 }
143 else
144 {
145 VecSetValues(v_, e_idxs.size(), e_idxs.data(), sub_vec.data(),
146 INSERT_VALUES);
147 }
148 }
149
150 // TODO preliminary
151 void setZero() { VecSet(v_, 0.0); }
156
159 void setLocalAccessibleVector() const;
160
163 std::vector<PetscScalar> get(std::vector<IndexType> const& indices) const;
164
167 PetscScalar operator[](PetscInt idx) const { return get(idx); }
173 void getGlobalVector(std::vector<PetscScalar>& u) const;
174
175 /* Get an entry value. This is an expensive operation,
176 and it only get local value. Use it for only test purpose
177 Get the value of an entry by [] operator.
178 setLocalAccessibleVector() must be called beforehand.
179 */
180 PetscScalar get(const PetscInt idx) const;
181
183 PETSc_Vec& getRawVector() { return v_; }
190 PETSc_Vec const& getRawVector() const { return v_; }
196 void copyValues(std::vector<PetscScalar>& u) const;
197
203 void copyValues(std::span<PetscScalar> u) const;
204
234 void viewer(
235 const std::string& file_name,
236 const PetscViewerFormat vw_format = PETSC_VIEWER_ASCII_MATLAB) const;
237
238 void shallowCopy(const PETScVector& v);
239
240private:
241 void destroy()
242 {
243 if (v_ != nullptr)
244 {
245 VecDestroy(&v_);
246 }
247 v_ = nullptr;
248 }
249
250 PETSc_Vec v_ = nullptr;
253 mutable PETSc_Vec v_loc_ = nullptr;
254
256 PetscInt start_rank_;
258 PetscInt end_rank_;
259
261 PetscInt size_;
263 PetscInt size_loc_;
265 PetscInt size_ghosts_ = 0;
266
269
277 mutable std::vector<PetscScalar> entry_array_;
278
280 mutable std::map<PetscInt, PetscInt> global_ids2local_ids_ghost_;
281
285 PetscScalar* getLocalVector() const;
286
288 PetscInt getLocalIndex(const PetscInt global_index) const;
289
294 inline void restoreArray(PetscScalar* array) const;
295
297 void config();
298
299 friend void finalizeVectorAssembly(PETScVector& vec);
300};
301
304
305} // 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