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{
40{
41public:
42 using IndexType = PetscInt;
43 // TODO make this class opaque, s.t. the PETSc symbols are not scattered all
44 // over the global namespace
45 using PETSc_Vec = Vec;
46
47public:
60 PETScVector(const PetscInt vec_size, const bool is_global_size = true);
61
70 PETScVector(const PetscInt vec_size, const std::vector<PetscInt>& ghost_ids,
71 const bool is_global_size = true);
72
79 explicit PETScVector(const PETScVector& existing_vec,
80 const bool deep_copy = true);
81
82 PETScVector(PETScVector&& other);
83
86 void finalizeAssembly();
87
89 PetscInt size() const { return size_; }
91 PetscInt getLocalSize() const { return size_loc_; }
93 PetscInt getGhostSize() const { return size_ghosts_; }
95 PetscInt getRangeBegin() const { return start_rank_; }
97 PetscInt getRangeEnd() const { return end_rank_; }
104 void set(const PetscInt i, const PetscScalar value)
105 {
106 VecSetValue(v_, i, value, INSERT_VALUES);
107 }
108
114 void add(const PetscInt i, const PetscScalar value)
115 {
116 VecSetValue(v_, i, value, ADD_VALUES);
117 }
118
126 template <class T_SUBVEC>
127 void add(const std::vector<PetscInt>& e_idxs, const T_SUBVEC& sub_vec)
128 {
129 VecSetValues(v_, e_idxs.size(), &e_idxs[0], &sub_vec[0], ADD_VALUES);
130 }
131
139 template <class T_SUBVEC>
140 void set(const std::vector<PetscInt>& e_idxs, const T_SUBVEC& sub_vec)
141 {
142 VecSetValues(v_, e_idxs.size(), &e_idxs[0], &sub_vec[0], INSERT_VALUES);
143 }
144
145 // TODO preliminary
146 void setZero() { VecSet(v_, 0.0); }
151
154 void setLocalAccessibleVector() const;
155
158 std::vector<PetscScalar> get(std::vector<IndexType> const& indices) const;
159
162 PetscScalar operator[](PetscInt idx) const { return get(idx); }
168 void getGlobalVector(std::vector<PetscScalar>& u) const;
169
170 /* Get an entry value. This is an expensive operation,
171 and it only get local value. Use it for only test purpose
172 Get the value of an entry by [] operator.
173 setLocalAccessibleVector() must be called beforehand.
174 */
175 PetscScalar get(const PetscInt idx) const;
176
178 PETSc_Vec& getRawVector() { return v_; }
185 PETSc_Vec const& getRawVector() const { return v_; }
191 void copyValues(std::vector<PetscScalar>& u) const;
192
222 void viewer(
223 const std::string& file_name,
224 const PetscViewerFormat vw_format = PETSC_VIEWER_ASCII_MATLAB) const;
225
226 void shallowCopy(const PETScVector& v);
227
228private:
229 void destroy()
230 {
231 if (v_ != nullptr)
232 {
233 VecDestroy(&v_);
234 }
235 v_ = nullptr;
236 }
237
238 PETSc_Vec v_ = nullptr;
241 mutable PETSc_Vec v_loc_ = nullptr;
242
244 PetscInt start_rank_;
246 PetscInt end_rank_;
247
249 PetscInt size_;
251 PetscInt size_loc_;
253 PetscInt size_ghosts_ = 0;
254
257
265 mutable std::vector<PetscScalar> entry_array_;
266
268 mutable std::map<PetscInt, PetscInt> global_ids2local_ids_ghost_;
269
275 void gatherLocalVectors(PetscScalar local_array[],
276 PetscScalar global_array[]) const;
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:40
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:95
void restoreArray(PetscScalar *array) const
PetscInt getLocalSize() const
Get the number of entries in the same rank.
Definition PETScVector.h:91
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:97
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:89
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:93
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