![]() |
OGS
|
Wrapper class for PETSc vector.
Definition at line 32 of file PETScVector.h.
#include <PETScVector.h>
Public Types | |
using | IndexType = PetscInt |
using | PETSc_Vec = Vec |
Public Member Functions | |
PETScVector () | |
PETScVector (const PetscInt vec_size, const bool is_global_size=true) | |
Constructor. More... | |
PETScVector (const PetscInt vec_size, const std::vector< PetscInt > &ghost_ids, const bool is_global_size=true) | |
Constructor. More... | |
PETScVector (const PETScVector &existing_vec, const bool deep_copy=true) | |
Copy constructor. More... | |
PETScVector (PETScVector &&other) | |
~PETScVector () | |
void | finalizeAssembly () |
Perform MPI collection of assembled entries in buffer. More... | |
PetscInt | size () const |
Get the global size of the vector. More... | |
PetscInt | getLocalSize () const |
Get the number of entries in the same rank. More... | |
PetscInt | getGhostSize () const |
Get the number of ghost entries in the same rank. More... | |
PetscInt | getRangeBegin () const |
Get the start index of the local vector. More... | |
PetscInt | getRangeEnd () const |
Get the end index of the local vector. More... | |
void | set (const PetscInt i, const PetscScalar value) |
void | add (const PetscInt i, const PetscScalar value) |
template<class T_SUBVEC > | |
void | add (const std::vector< PetscInt > &e_idxs, const T_SUBVEC &sub_vec) |
template<class T_SUBVEC > | |
void | set (const std::vector< PetscInt > &e_idxs, const T_SUBVEC &sub_vec) |
void | setZero () |
PETScVector & | operator= (PETScVector &&)=delete |
void | setLocalAccessibleVector () const |
std::vector< PetscScalar > | get (std::vector< IndexType > const &indices) const |
PetscScalar | operator[] (PetscInt idx) const |
void | getGlobalVector (std::vector< PetscScalar > &u) const |
PetscScalar | get (const PetscInt idx) const |
PETSc_Vec & | getRawVector () |
Exposes the underlying PETSc vector. More... | |
PETSc_Vec const & | getRawVector () const |
void | copyValues (std::vector< PetscScalar > &u) const |
void | viewer (const std::string &file_name, const PetscViewerFormat vw_format=PETSC_VIEWER_ASCII_MATLAB) const |
void | shallowCopy (const PETScVector &v) |
Private Member Functions | |
void | destroy () |
void | gatherLocalVectors (PetscScalar local_array[], PetscScalar global_array[]) const |
Collect local vectors. More... | |
PetscScalar * | getLocalVector () const |
PetscInt | getLocalIndex (const PetscInt global_index) const |
Get local index by a global index. More... | |
void | restoreArray (PetscScalar *array) const |
void | config () |
A function called by constructors to configure members. More... | |
Private Attributes | |
PETSc_Vec | v_ = nullptr |
PETSc_Vec | v_loc_ = nullptr |
PetscInt | start_rank_ |
Starting index in a rank. More... | |
PetscInt | end_rank_ |
Ending index in a rank. More... | |
PetscInt | size_ |
Size of the vector. More... | |
PetscInt | size_loc_ |
Size of local entries. More... | |
PetscInt | size_ghosts_ = 0 |
Size of local ghost entries. More... | |
bool | has_ghost_id_ = false |
Flag to indicate whether the vector is created with ghost entry indices. More... | |
std::vector< PetscScalar > | entry_array_ |
Array containing the entries of the vector. If the vector is created without given ghost IDs, the array contains all entries of the global vector, v_. Otherwise it only contains the entries of the global vector owned by the current rank. More... | |
std::map< PetscInt, PetscInt > | global_ids2local_ids_ghost_ |
Map global indices of ghost entries to local indices. More... | |
Friends | |
void | finalizeVectorAssembly (PETScVector &vec) |
Function to finalize the vector assembly. More... | |
using MathLib::PETScVector::IndexType = PetscInt |
Definition at line 35 of file PETScVector.h.
using MathLib::PETScVector::PETSc_Vec = Vec |
Definition at line 38 of file PETScVector.h.
|
inline |
Definition at line 41 of file PETScVector.h.
MathLib::PETScVector::PETScVector | ( | const PetscInt | vec_size, |
const bool | is_global_size = true |
||
) |
Constructor.
vec_size | The size of the vector, either global or local |
is_global_size | The flag of the type of vec_size, i.e. whether it is a global size or local size. The default is true. If is_global_size is true, the vector is created by the global size, the local size of the vector is determined by PETSc, and vice versa is the same. |
Definition at line 30 of file PETScVector.cpp.
MathLib::PETScVector::PETScVector | ( | const PetscInt | vec_size, |
const std::vector< PetscInt > & | ghost_ids, | ||
const bool | is_global_size = true |
||
) |
Constructor.
vec_size | The size of the vector, either global or local |
ghost_ids | The global indices of ghost entries |
is_global_size | The flag of the type of vec_size, i.e. whether it is a global size or local size. The default is true. |
Definition at line 47 of file PETScVector.cpp.
References config(), global_ids2local_ids_ghost_, size_loc_, and v_.
|
explicit |
Copy constructor.
existing_vec | The vector to be copied |
deep_copy | The flag for a deep copy, which means to copy the values as well, the default is true |
Definition at line 72 of file PETScVector.cpp.
References shallowCopy(), and v_.
MathLib::PETScVector::PETScVector | ( | PETScVector && | other | ) |
Definition at line 83 of file PETScVector.cpp.
|
inline |
|
inline |
Add a value to an entry.
i | Number of the entry |
value | Value. |
Definition at line 107 of file PETScVector.h.
References v_.
|
inline |
Add values to several entries.
e_idxs | Indices of entries to be added Note: std::size_t cannot be the type of e_idxs template argument. |
sub_vec | Entries to be added. |
Definition at line 120 of file PETScVector.h.
References v_.
|
private |
A function called by constructors to configure members.
Definition at line 96 of file PETScVector.cpp.
References end_rank_, size_, size_loc_, start_rank_, and v_.
Referenced by PETScVector().
void MathLib::PETScVector::copyValues | ( | std::vector< PetscScalar > & | u | ) | const |
Copy local entries including ghost ones to an array
u | Preallocated vector for the values of local entries. |
Definition at line 197 of file PETScVector.cpp.
References getGhostSize(), getLocalSize(), getLocalVector(), and restoreArray().
|
inlineprivate |
Definition at line 221 of file PETScVector.h.
References v_.
Referenced by ~PETScVector(), and shallowCopy().
void MathLib::PETScVector::finalizeAssembly | ( | ) |
Perform MPI collection of assembled entries in buffer.
Definition at line 107 of file PETScVector.cpp.
References v_.
Referenced by MathLib::applyKnownSolution(), and MathLib::LinAlg::finalizeAssembly().
|
private |
Collect local vectors.
local_array | Local array |
global_array | Global array |
Definition at line 113 of file PETScVector.cpp.
References size_loc_.
Referenced by getGlobalVector().
PetscScalar MathLib::PETScVector::get | ( | const PetscInt | idx | ) | const |
Definition at line 206 of file PETScVector.cpp.
References entry_array_, getLocalIndex(), global_ids2local_ids_ghost_, and size_.
std::vector< PetscScalar > MathLib::PETScVector::get | ( | std::vector< IndexType > const & | indices | ) | const |
Get several entries. setLocalAccessibleVector() must be called beforehand.
Definition at line 218 of file PETScVector.cpp.
References entry_array_, getLocalIndex(), global_ids2local_ids_ghost_, and size_.
Referenced by operator[]().
|
inline |
Get the number of ghost entries in the same rank.
Definition at line 86 of file PETScVector.h.
References size_ghosts_.
Referenced by copyValues().
void MathLib::PETScVector::getGlobalVector | ( | std::vector< PetscScalar > & | u | ) | const |
Get global vector
u | Array to store the global vector. Memory allocation is needed in advance |
Definition at line 140 of file PETScVector.cpp.
References gatherLocalVectors(), OGS_FATAL, size_, and v_.
Referenced by setLocalAccessibleVector().
|
private |
Get local index by a global index.
Definition at line 262 of file PETScVector.cpp.
References global_ids2local_ids_ghost_, size_, and start_rank_.
Referenced by get().
|
inline |
Get the number of entries in the same rank.
Definition at line 84 of file PETScVector.h.
References size_loc_.
Referenced by copyValues().
|
private |
Get local vector, i.e. entries in the same rank
Definition at line 247 of file PETScVector.cpp.
References has_ghost_id_, v_, and v_loc_.
Referenced by copyValues(), and setLocalAccessibleVector().
|
inline |
Get the start index of the local vector.
Definition at line 88 of file PETScVector.h.
References start_rank_.
|
inline |
Get the end index of the local vector.
Definition at line 90 of file PETScVector.h.
References end_rank_.
|
inline |
Exposes the underlying PETSc vector.
Definition at line 171 of file PETScVector.h.
References v_.
Referenced by MathLib::LinAlg::axpby(), MathLib::LinAlg::axpy(), MathLib::LinAlg::aypx(), MathLib::LinAlg::componentwiseDivide(), MathLib::LinAlg::copy(), MathLib::LinAlg::matMult(), MathLib::LinAlg::matMultAdd(), MathLib::LinAlg::norm1(), MathLib::LinAlg::norm2(), MathLib::LinAlg::normMax(), MathLib::LinAlg::scale(), MathLib::LinAlg::set(), shallowCopy(), and MathLib::PETScLinearSolver::solve().
|
inline |
Exposes the underlying PETSc vector.
Definition at line 178 of file PETScVector.h.
References v_.
|
delete |
Disallow moving.
|
inline |
Get the value of an entry by [] operator. setLocalAccessibleVector() must be called beforehand.
Definition at line 155 of file PETScVector.h.
References get().
|
inlineprivate |
Restore array after finish access local array
array | Pointer to the local array fetched by VecGetArray |
Definition at line 274 of file PETScVector.cpp.
References has_ghost_id_, v_, and v_loc_.
Referenced by copyValues(), and setLocalAccessibleVector().
|
inline |
Insert a single entry with value.
i | Entry index |
value | Entry value |
Definition at line 97 of file PETScVector.h.
References v_.
Referenced by MathLib::applyKnownSolution(), and MathLib::setVector().
|
inline |
Add values to several entries
e_idxs | Indices of entries to be added. Note: std::size_t cannot be the type of e_idxs template argument |
sub_vec | Entries to be added |
Definition at line 133 of file PETScVector.h.
References v_.
void MathLib::PETScVector::setLocalAccessibleVector | ( | ) | const |
Set local accessible vector in order to get entries. Call this before call operator[] or get(...).
Definition at line 177 of file PETScVector.cpp.
References entry_array_, getGlobalVector(), getLocalVector(), global_ids2local_ids_ghost_, restoreArray(), size_, size_ghosts_, and size_loc_.
Referenced by MathLib::LinAlg::setLocalAccessibleVector().
|
inline |
void MathLib::PETScVector::shallowCopy | ( | const PETScVector & | v | ) |
Definition at line 303 of file PETScVector.cpp.
References destroy(), end_rank_, getRawVector(), global_ids2local_ids_ghost_, has_ghost_id_, size_, size_ghosts_, size_loc_, start_rank_, and v_.
Referenced by PETScVector(), MathLib::LinAlg::copy(), MathLib::LinAlg::matMult(), and MathLib::LinAlg::matMultAdd().
|
inline |
void MathLib::PETScVector::viewer | ( | const std::string & | file_name, |
const PetscViewerFormat | vw_format = PETSC_VIEWER_ASCII_MATLAB |
||
) | const |
View the global vector for test purpose. Do not use it for output a big vector.
file_name | File name for output |
vw_format | File format listed as: PETSC_VIEWER_DEFAULT Default format PETSC_VIEWER_ASCII_MATLAB MATLAB format PETSC_VIEWER_ASCII_DENSE Print matrix as dense PETSC_VIEWER_ASCII_IMPL Implementation-specific format (which is in many cases the same as the default) PETSC_VIEWER_ASCII_INFO Basic information about object PETSC_VIEWER_ASCII_INFO_DETAIL More detailed info about object PETSC_VIEWER_ASCII_COMMON Identical output format for all objects of a particular type PETSC_VIEWER_ASCII_INDEX (for vectors) Prints the vector element number next to each vector entry PETSC_VIEWER_ASCII_SYMMODU Print parallel vectors without indicating the processor ranges PETSC_VIEWER_ASCII_VTK Outputs the object to a VTK file PETSC_VIEWER_NATIVE Store the object to the binary file in its native format (for example, dense matrices are stored as dense), DMDA vectors are dumped directly to the file instead of being first put in the natural ordering PETSC_VIEWER_DRAW_BASIC Views the vector with a simple 1d plot PETSC_VIEWER_DRAW_LG Views the vector with a line graph PETSC_VIEWER_DRAW_CONTOUR Views the vector with a contour plot |
Definition at line 285 of file PETScVector.cpp.
References v_.
|
friend |
Function to finalize the vector assembly.
Definition at line 320 of file PETScVector.cpp.
|
private |
Ending index in a rank.
Definition at line 236 of file PETScVector.h.
Referenced by config(), getRangeEnd(), and shallowCopy().
|
mutableprivate |
Array containing the entries of the vector. If the vector is created without given ghost IDs, the array contains all entries of the global vector, v_. Otherwise it only contains the entries of the global vector owned by the current rank.
Definition at line 255 of file PETScVector.h.
Referenced by get(), and setLocalAccessibleVector().
|
mutableprivate |
Map global indices of ghost entries to local indices.
Definition at line 258 of file PETScVector.h.
Referenced by PETScVector(), get(), getLocalIndex(), setLocalAccessibleVector(), and shallowCopy().
|
private |
Flag to indicate whether the vector is created with ghost entry indices.
Definition at line 246 of file PETScVector.h.
Referenced by getLocalVector(), restoreArray(), and shallowCopy().
|
private |
Size of the vector.
Definition at line 239 of file PETScVector.h.
Referenced by config(), get(), getGlobalVector(), getLocalIndex(), setLocalAccessibleVector(), shallowCopy(), and size().
|
private |
Size of local ghost entries.
Definition at line 243 of file PETScVector.h.
Referenced by getGhostSize(), setLocalAccessibleVector(), and shallowCopy().
|
private |
Size of local entries.
Definition at line 241 of file PETScVector.h.
Referenced by PETScVector(), config(), gatherLocalVectors(), getLocalSize(), setLocalAccessibleVector(), and shallowCopy().
|
private |
Starting index in a rank.
Definition at line 234 of file PETScVector.h.
Referenced by config(), getLocalIndex(), getRangeBegin(), and shallowCopy().
|
private |
Definition at line 228 of file PETScVector.h.
Referenced by PETScVector(), add(), config(), destroy(), finalizeAssembly(), getGlobalVector(), getLocalVector(), getRawVector(), restoreArray(), set(), setZero(), shallowCopy(), and viewer().
|
mutableprivate |
Local vector, which is only for the case that v_ is created with ghost entries.
Definition at line 231 of file PETScVector.h.
Referenced by getLocalVector(), and restoreArray().