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 VecSetValues(v_, e_idxs.size(), &e_idxs[0], &sub_vec[0], ADD_VALUES);
131 }
132
140 template <class T_SUBVEC>
141 void set(const std::vector<PetscInt>& e_idxs, const T_SUBVEC& sub_vec)
142 {
143 VecSetValues(v_, e_idxs.size(), &e_idxs[0], &sub_vec[0], INSERT_VALUES);
144 }
145
146 // TODO preliminary
147 void setZero() { VecSet(v_, 0.0); }
152
155 void setLocalAccessibleVector() const;
156
159 std::vector<PetscScalar> get(std::vector<IndexType> const& indices) const;
160
163 PetscScalar operator[](PetscInt idx) const { return get(idx); }
169 void getGlobalVector(std::vector<PetscScalar>& u) const;
170
171 /* Get an entry value. This is an expensive operation,
172 and it only get local value. Use it for only test purpose
173 Get the value of an entry by [] operator.
174 setLocalAccessibleVector() must be called beforehand.
175 */
176 PetscScalar get(const PetscInt idx) const;
177
179 PETSc_Vec& getRawVector() { return v_; }
186 PETSc_Vec const& getRawVector() const { return v_; }
192 void copyValues(std::vector<PetscScalar>& u) const;
193
199 void copyValues(std::span<PetscScalar> u) const;
200
230 void viewer(
231 const std::string& file_name,
232 const PetscViewerFormat vw_format = PETSC_VIEWER_ASCII_MATLAB) const;
233
234 void shallowCopy(const PETScVector& v);
235
236private:
237 void destroy()
238 {
239 if (v_ != nullptr)
240 {
241 VecDestroy(&v_);
242 }
243 v_ = nullptr;
244 }
245
246 PETSc_Vec v_ = nullptr;
249 mutable PETSc_Vec v_loc_ = nullptr;
250
252 PetscInt start_rank_;
254 PetscInt end_rank_;
255
257 PetscInt size_;
259 PetscInt size_loc_;
261 PetscInt size_ghosts_ = 0;
262
265
273 mutable std::vector<PetscScalar> entry_array_;
274
276 mutable std::map<PetscInt, PetscInt> global_ids2local_ids_ghost_;
277
281 PetscScalar* getLocalVector() const;
282
284 PetscInt getLocalIndex(const PetscInt global_index) const;
285
290 inline void restoreArray(PetscScalar* array) const;
291
293 void config();
294
295 friend void finalizeVectorAssembly(PETScVector& vec);
296};
297
300
301} // 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