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 <string>
23#include <vector>
24
25namespace MathLib
26{
33{
34public:
35 using IndexType = PetscInt;
36 // TODO make this class opaque, s.t. the PETSc symbols are not scattered all
37 // over the global namespace
38 using PETSc_Vec = Vec;
39
40public:
53 PETScVector(const PetscInt vec_size, const bool is_global_size = true);
54
63 PETScVector(const PetscInt vec_size, const std::vector<PetscInt>& ghost_ids,
64 const bool is_global_size = true);
65
72 explicit PETScVector(const PETScVector& existing_vec,
73 const bool deep_copy = true);
74
75 PETScVector(PETScVector&& other);
76
79 void finalizeAssembly();
80
82 PetscInt size() const { return size_; }
84 PetscInt getLocalSize() const { return size_loc_; }
86 PetscInt getGhostSize() const { return size_ghosts_; }
88 PetscInt getRangeBegin() const { return start_rank_; }
90 PetscInt getRangeEnd() const { return end_rank_; }
97 void set(const PetscInt i, const PetscScalar value)
98 {
99 VecSetValue(v_, i, value, INSERT_VALUES);
100 }
101
107 void add(const PetscInt i, const PetscScalar value)
108 {
109 VecSetValue(v_, i, value, ADD_VALUES);
110 }
111
119 template <class T_SUBVEC>
120 void add(const std::vector<PetscInt>& e_idxs, const T_SUBVEC& sub_vec)
121 {
122 VecSetValues(v_, e_idxs.size(), &e_idxs[0], &sub_vec[0], ADD_VALUES);
123 }
124
132 template <class T_SUBVEC>
133 void set(const std::vector<PetscInt>& e_idxs, const T_SUBVEC& sub_vec)
134 {
135 VecSetValues(v_, e_idxs.size(), &e_idxs[0], &sub_vec[0], INSERT_VALUES);
136 }
137
138 // TODO preliminary
139 void setZero() { VecSet(v_, 0.0); }
144
147 void setLocalAccessibleVector() const;
148
151 std::vector<PetscScalar> get(std::vector<IndexType> const& indices) const;
152
155 PetscScalar operator[](PetscInt idx) const { return get(idx); }
161 void getGlobalVector(std::vector<PetscScalar>& u) const;
162
163 /* Get an entry value. This is an expensive operation,
164 and it only get local value. Use it for only test purpose
165 Get the value of an entry by [] operator.
166 setLocalAccessibleVector() must be called beforehand.
167 */
168 PetscScalar get(const PetscInt idx) const;
169
171 PETSc_Vec& getRawVector() { return v_; }
178 PETSc_Vec const& getRawVector() const { return v_; }
184 void copyValues(std::vector<PetscScalar>& u) const;
185
215 void viewer(
216 const std::string& file_name,
217 const PetscViewerFormat vw_format = PETSC_VIEWER_ASCII_MATLAB) const;
218
219 void shallowCopy(const PETScVector& v);
220
221private:
222 void destroy()
223 {
224 if (v_ != nullptr)
225 {
226 VecDestroy(&v_);
227 }
228 v_ = nullptr;
229 }
230
231 PETSc_Vec v_ = nullptr;
234 mutable PETSc_Vec v_loc_ = nullptr;
235
237 PetscInt start_rank_;
239 PetscInt end_rank_;
240
242 PetscInt size_;
244 PetscInt size_loc_;
246 PetscInt size_ghosts_ = 0;
247
249 bool has_ghost_id_ = false;
250
258 mutable std::vector<PetscScalar> entry_array_;
259
261 mutable std::map<PetscInt, PetscInt> global_ids2local_ids_ghost_;
262
268 void gatherLocalVectors(PetscScalar local_array[],
269 PetscScalar global_array[]) const;
270
274 PetscScalar* getLocalVector() const;
275
277 PetscInt getLocalIndex(const PetscInt global_index) const;
278
283 inline void restoreArray(PetscScalar* array) const;
284
286 void config();
287
288 friend void finalizeVectorAssembly(PETScVector& vec);
289};
290
293
294} // namespace MathLib
Wrapper class for PETSc vector.
Definition PETScVector.h:33
PetscInt start_rank_
Starting index in a rank.
void finalizeAssembly()
Perform MPI collection of assembled entries in buffer.
void gatherLocalVectors(PetscScalar local_array[], PetscScalar global_array[]) const
Collect local vectors.
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:88
void restoreArray(PetscScalar *array) const
PetscInt getLocalSize() const
Get the number of entries in the same rank.
Definition PETScVector.h:84
bool has_ghost_id_
Flag to indicate whether the vector is created with ghost entry indices.
PetscInt size_loc_
Size of local entries.
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:90
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:82
void shallowCopy(const PETScVector &v)
void add(const PetscInt i, const PetscScalar value)
void set(const PetscInt i, const PetscScalar value)
Definition PETScVector.h:97
PETSc_Vec & getRawVector()
Exposes the underlying PETSc vector.
PetscInt getGhostSize() const
Get the number of ghost entries in the same rank.
Definition PETScVector.h:86
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