OGS
PETScVector.h
Go to the documentation of this file.
1
17#pragma once
18
19#include <petscvec.h>
20
21#include <map>
22#include <span>
23#include <string>
24#include <vector>
25
26namespace MathLib
27{
41{
42public:
43 using IndexType = PetscInt;
44 // TODO make this class opaque, s.t. the PETSc symbols are not scattered all
45 // over the global namespace
46 using PETSc_Vec = Vec;
47
48public:
61 PETScVector(const PetscInt vec_size, const bool is_global_size = true);
62
71 PETScVector(const PetscInt vec_size, const std::vector<PetscInt>& ghost_ids,
72 const bool is_global_size = true);
73
80 explicit PETScVector(const PETScVector& existing_vec,
81 const bool deep_copy = true);
82
83 PETScVector(PETScVector&& other);
84
87 void finalizeAssembly();
88
90 PetscInt size() const { return size_; }
92 PetscInt getLocalSize() const { return size_loc_; }
94 PetscInt getGhostSize() const { return size_ghosts_; }
96 PetscInt getRangeBegin() const { return start_rank_; }
98 PetscInt getRangeEnd() const { return end_rank_; }
105 void set(const PetscInt i, const PetscScalar value)
106 {
107 VecSetValue(v_, i, value, INSERT_VALUES);
108 }
109
115 void add(const PetscInt i, const PetscScalar value)
116 {
117 VecSetValue(v_, i, value, ADD_VALUES);
118 }
119
127 template <class T_SUBVEC>
128 void add(const std::vector<PetscInt>& e_idxs, const T_SUBVEC& sub_vec)
129 {
130 if constexpr (std::is_pointer_v<T_SUBVEC>)
131 {
132 VecSetValues(v_, e_idxs.size(), e_idxs.data(), sub_vec, ADD_VALUES);
133 }
134 else
135 {
136 VecSetValues(v_, e_idxs.size(), e_idxs.data(), sub_vec.data(),
137 ADD_VALUES);
138 }
139 }
140
148 template <class T_SUBVEC>
149 void set(const std::vector<PetscInt>& e_idxs, const T_SUBVEC& sub_vec)
150 {
151 if constexpr (std::is_pointer_v<T_SUBVEC>)
152 {
153 VecSetValues(v_, e_idxs.size(), e_idxs.data(), sub_vec,
154 INSERT_VALUES);
155 }
156 else
157 {
158 VecSetValues(v_, e_idxs.size(), e_idxs.data(), sub_vec.data(),
159 INSERT_VALUES);
160 }
161 }
162
163 // TODO preliminary
164 void setZero() { VecSet(v_, 0.0); }
169
172 void setLocalAccessibleVector() const;
173
176 std::vector<PetscScalar> get(std::vector<IndexType> const& indices) const;
177
180 PetscScalar operator[](PetscInt idx) const { return get(idx); }
186 void getGlobalVector(std::vector<PetscScalar>& u) const;
187
188 /* Get an entry value. This is an expensive operation,
189 and it only get local value. Use it for only test purpose
190 Get the value of an entry by [] operator.
191 setLocalAccessibleVector() must be called beforehand.
192 */
193 PetscScalar get(const PetscInt idx) const;
194
196 PETSc_Vec& getRawVector() { return v_; }
203 PETSc_Vec const& getRawVector() const { return v_; }
209 void copyValues(std::vector<PetscScalar>& u) const;
210
216 void copyValues(std::span<PetscScalar> u) const;
217
247 void viewer(
248 const std::string& file_name,
249 const PetscViewerFormat vw_format = PETSC_VIEWER_ASCII_MATLAB) const;
250
251 void shallowCopy(const PETScVector& v);
252
253private:
254 void destroy()
255 {
256 if (v_ != nullptr)
257 {
258 VecDestroy(&v_);
259 }
260 v_ = nullptr;
261 }
262
263 PETSc_Vec v_ = nullptr;
266 mutable PETSc_Vec v_loc_ = nullptr;
267
269 PetscInt start_rank_;
271 PetscInt end_rank_;
272
274 PetscInt size_;
276 PetscInt size_loc_;
278 PetscInt size_ghosts_ = 0;
279
282
290 mutable std::vector<PetscScalar> entry_array_;
291
293 mutable std::map<PetscInt, PetscInt> global_ids2local_ids_ghost_;
294
298 PetscScalar* getLocalVector() const;
299
301 PetscInt getLocalIndex(const PetscInt global_index) const;
302
307 inline void restoreArray(PetscScalar* array) const;
308
310 void config();
311
312 friend void finalizeVectorAssembly(PETScVector& vec);
313};
314
317
318} // namespace MathLib
Wrapper class for PETSc vector.
Definition PETScVector.h:41
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:96
void restoreArray(PetscScalar *array) const
PetscInt getLocalSize() const
Get the number of entries in the same rank.
Definition PETScVector.h:92
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:98
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:90
void shallowCopy(const PETScVector &v)
void add(const PetscInt i, const PetscScalar value)
void set(const PetscInt i, const PetscScalar value)
PETSc_Vec & getRawVector()
Exposes the underlying PETSc vector.
PetscInt getGhostSize() const
Get the number of ghost entries in the same rank.
Definition PETScVector.h:94
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