OGS
PETScVector.h
Go to the documentation of this file.
1 
17 #pragma once
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 #include <petscvec.h>
24 
25 namespace MathLib
26 {
33 {
34 public:
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 
40 public:
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_; }
183  void copyValues(std::vector<PetscScalar>& u) const;
184 
214  void viewer(
215  const std::string& file_name,
216  const PetscViewerFormat vw_format = PETSC_VIEWER_ASCII_MATLAB) const;
217 
218  void shallowCopy(const PETScVector& v);
219 
220 private:
221  void destroy()
222  {
223  if (v_)
224  VecDestroy(&v_);
225  v_ = nullptr;
226  }
227 
228  PETSc_Vec v_ = nullptr;
231  mutable PETSc_Vec v_loc_ = nullptr;
232 
234  PetscInt start_rank_;
236  PetscInt end_rank_;
237 
239  PetscInt size_;
241  PetscInt size_loc_;
243  PetscInt size_ghosts_ = 0;
244 
246  bool has_ghost_id_ = false;
247 
255  mutable std::vector<PetscScalar> entry_array_;
256 
258  mutable std::map<PetscInt, PetscInt> global_ids2local_ids_ghost_;
259 
265  void gatherLocalVectors(PetscScalar local_array[],
266  PetscScalar global_array[]) const;
267 
271  PetscScalar* getLocalVector() const;
272 
274  PetscInt getLocalIndex(const PetscInt global_index) const;
275 
280  inline void restoreArray(PetscScalar* array) const;
281 
283  void config();
284 
285  friend void finalizeVectorAssembly(PETScVector& vec);
286 };
287 
290 
291 } // end namespace
Wrapper class for PETSc vector.
Definition: PETScVector.h:33
PetscInt start_rank_
Starting index in a rank.
Definition: PETScVector.h:234
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,...
Definition: PETScVector.h:255
PetscScalar operator[](PetscInt idx) const
Definition: PETScVector.h:155
void set(const std::vector< PetscInt > &e_idxs, const T_SUBVEC &sub_vec)
Definition: PETScVector.h:133
void config()
A function called by constructors to configure members.
Definition: PETScVector.cpp:96
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.
Definition: PETScVector.h:246
PetscInt size_loc_
Size of local entries.
Definition: PETScVector.h:241
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)
Definition: PETScVector.h:120
PETScVector & operator=(PETScVector &&)=delete
PetscInt size_ghosts_
Size of local ghost entries.
Definition: PETScVector.h:243
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.
Definition: PETScVector.h:239
friend void finalizeVectorAssembly(PETScVector &vec)
Function to finalize the vector assembly.
PETSc_Vec const & getRawVector() const
Definition: PETScVector.h:178
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)
Definition: PETScVector.h:107
void set(const PetscInt i, const PetscScalar value)
Definition: PETScVector.h:97
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.
Definition: PETScVector.h:258
PetscInt getLocalIndex(const PetscInt global_index) const
Get local index by a global index.
PetscInt end_rank_
Ending index in a rank.
Definition: PETScVector.h:236
void viewer(const std::string &file_name, const PetscViewerFormat vw_format=PETSC_VIEWER_ASCII_MATLAB) const
PETSc_Vec & getRawVector()
Exposes the underlying PETSc vector.
Definition: PETScVector.h:171
void finalizeVectorAssembly(VEC_T &)
General function to finalize the vector assembly.