OGS
MathLib::PETScVector Class Reference

Detailed Description

Wrapper class for PETSc vector.

It can be used to create a global vector for either parallel or serial computing.

Caution: Using it to create a local vector is not allowed, as the created vector will be partitioned and distributed across all ranks in an MPI environment.

Definition at line 27 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.
 PETScVector (const PetscInt vec_size, const std::vector< PetscInt > &ghost_ids, const bool is_global_size=true)
 Constructor.
 PETScVector (const PETScVector &existing_vec, const bool deep_copy=true)
 Copy constructor.
 PETScVector (PETScVector &&other)
 ~PETScVector ()
void finalizeAssembly ()
 Perform MPI collection of assembled entries in buffer.
PetscInt size () const
 Get the global size of the vector.
PetscInt getLocalSize () const
 Get the number of entries in the same rank.
PetscInt getGhostSize () const
 Get the number of ghost entries in the same rank.
PetscInt getRangeBegin () const
 Get the start index of the local vector.
PetscInt getRangeEnd () const
 Get the end index of the local vector.
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 ()
PETScVectoroperator= (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_VecgetRawVector ()
 Exposes the underlying PETSc vector.
PETSc_Vec const & getRawVector () const
void copyValues (std::vector< PetscScalar > &u) const
void copyValues (std::span< 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 ()
PetscScalar * getLocalVector () const
PetscInt getLocalIndex (const PetscInt global_index) const
 Get local index by a global index.
void restoreArray (PetscScalar *array) const
void config ()
 A function called by constructors to configure members.

Private Attributes

PETSc_Vec v_ = nullptr
PETSc_Vec v_loc_ = nullptr
PetscInt start_rank_
 Starting index in a rank.
PetscInt end_rank_
 Ending index in a rank.
PetscInt size_
 Size of the vector.
PetscInt size_loc_
 Size of local entries.
PetscInt size_ghosts_ = 0
 Size of local ghost entries.
bool created_with_ghost_id_ = false
 Flag to indicate whether the vector is created with ghost entry indices.
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.
std::map< PetscInt, PetscInt > global_ids2local_ids_ghost_
 Map global indices of ghost entries to local indices.

Friends

void finalizeVectorAssembly (PETScVector &vec)
 Function to finalize the vector assembly.

Member Typedef Documentation

◆ IndexType

Definition at line 30 of file PETScVector.h.

◆ PETSc_Vec

Definition at line 33 of file PETScVector.h.

Constructor & Destructor Documentation

◆ PETScVector() [1/5]

MathLib::PETScVector::PETScVector ( )
inline

Definition at line 36 of file PETScVector.h.

36{}

Referenced by PETScVector(), PETScVector(), finalizeVectorAssembly, operator=(), and shallowCopy().

◆ PETScVector() [2/5]

MathLib::PETScVector::PETScVector ( const PetscInt vec_size,
const bool is_global_size = true )

Constructor.

Parameters
vec_sizeThe size of the vector, either global or local
is_global_sizeThe 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 15 of file PETScVector.cpp.

16{
17 if (is_global_size)
18 {
19 PetscCallAbort(PETSC_COMM_WORLD, VecCreate(PETSC_COMM_WORLD, &v_));
20 PetscCallAbort(PETSC_COMM_WORLD,
21 VecSetSizes(v_, PETSC_DECIDE, vec_size));
22 }
23 else
24 {
25 // Fix size partitioning
26 // the size can be associated to specific memory allocation of a matrix
27 PetscCallAbort(
28 PETSC_COMM_WORLD,
29 VecCreateMPI(PETSC_COMM_WORLD, vec_size, PETSC_DECIDE, &v_));
30 }
31
32 config();
33}
void config()
A function called by constructors to configure members.

References config(), and v_.

◆ PETScVector() [3/5]

MathLib::PETScVector::PETScVector ( const PetscInt vec_size,
const std::vector< PetscInt > & ghost_ids,
const bool is_global_size = true )

Constructor.

Parameters
vec_sizeThe size of the vector, either global or local
ghost_idsThe global indices of ghost entries
is_global_sizeThe 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 35 of file PETScVector.cpp.

38 : size_ghosts_{static_cast<PetscInt>(ghost_ids.size())},
40{
41 PetscInt nghosts = static_cast<PetscInt>(ghost_ids.size());
42 if (is_global_size)
43 {
44 PetscCallAbort(PETSC_COMM_WORLD,
45 VecCreateGhost(PETSC_COMM_WORLD, PETSC_DECIDE, vec_size,
46 nghosts, ghost_ids.data(), &v_));
47 }
48 else
49 {
50 PetscCallAbort(PETSC_COMM_WORLD, VecCreate(PETSC_COMM_WORLD, &v_));
51 PetscCallAbort(PETSC_COMM_WORLD, VecSetType(v_, VECMPI));
52 PetscCallAbort(PETSC_COMM_WORLD,
53 VecSetSizes(v_, vec_size, PETSC_DECIDE));
54 PetscCallAbort(PETSC_COMM_WORLD,
55 VecMPISetGhost(v_, nghosts, ghost_ids.data()));
56 }
57
58 config();
59
60 for (PetscInt i = 0; i < nghosts; i++)
61 {
62 global_ids2local_ids_ghost_.emplace(ghost_ids[i], size_loc_ + i);
63 }
64}
PetscInt size_loc_
Size of local entries.
bool created_with_ghost_id_
Flag to indicate whether the vector is created with ghost entry indices.
PetscInt size_ghosts_
Size of local ghost entries.
std::map< PetscInt, PetscInt > global_ids2local_ids_ghost_
Map global indices of ghost entries to local indices.

References config(), created_with_ghost_id_, global_ids2local_ids_ghost_, size(), size_ghosts_, size_loc_, and v_.

◆ PETScVector() [4/5]

MathLib::PETScVector::PETScVector ( const PETScVector & existing_vec,
const bool deep_copy = true )
explicit

Copy constructor.

Parameters
existing_vecThe vector to be copied
deep_copyThe flag for a deep copy, which means to copy the values as well, the default is true

Definition at line 66 of file PETScVector.cpp.

67{
68 shallowCopy(existing_vec);
69
70 // Copy values
71 if (deep_copy)
72 {
73 PetscCallAbort(PETSC_COMM_WORLD, VecCopy(existing_vec.v_, v_));
74 }
75}
void shallowCopy(const PETScVector &v)

References PETScVector(), shallowCopy(), and v_.

◆ PETScVector() [5/5]

MathLib::PETScVector::PETScVector ( PETScVector && other)

Definition at line 77 of file PETScVector.cpp.

78 : v_{std::move(other.v_)},
79 v_loc_{std::move(other.v_loc_)},
80 start_rank_{other.start_rank_},
81 end_rank_{other.end_rank_},
82 size_{other.size_},
83 size_loc_{other.size_loc_},
84 size_ghosts_{other.size_ghosts_},
85 created_with_ghost_id_{other.created_with_ghost_id_},
86 global_ids2local_ids_ghost_{other.global_ids2local_ids_ghost_}
87{
88}
PetscInt start_rank_
Starting index in a rank.
PetscInt size_
Size of the vector.
PetscInt end_rank_
Ending index in a rank.

References PETScVector(), created_with_ghost_id_, end_rank_, global_ids2local_ids_ghost_, size_, size_ghosts_, size_loc_, start_rank_, v_, and v_loc_.

◆ ~PETScVector()

MathLib::PETScVector::~PETScVector ( )
inline

Definition at line 72 of file PETScVector.h.

72{ destroy(); }

References destroy().

Member Function Documentation

◆ add() [1/2]

void MathLib::PETScVector::add ( const PetscInt i,
const PetscScalar value )
inline

Add a value to an entry.

Parameters
iNumber of the entry
valueValue.

Definition at line 103 of file PETScVector.h.

104 {
105 PetscCallAbort(PETSC_COMM_WORLD, VecSetValue(v_, i, value, ADD_VALUES));
106 }

References v_.

◆ add() [2/2]

template<class T_SUBVEC>
void MathLib::PETScVector::add ( const std::vector< PetscInt > & e_idxs,
const T_SUBVEC & sub_vec )
inline

Add values to several entries.

Parameters
e_idxsIndices of entries to be added Note: std::size_t cannot be the type of e_idxs template argument.
sub_vecEntries to be added.

Definition at line 116 of file PETScVector.h.

117 {
118 if constexpr (std::is_pointer_v<T_SUBVEC>)
119 {
120 PetscCallAbort(PETSC_COMM_WORLD,
121 VecSetValues(v_, e_idxs.size(), &e_idxs[0],
122 &sub_vec[0], ADD_VALUES));
123 }
124 else
125 {
126 PetscCallAbort(PETSC_COMM_WORLD,
127 VecSetValues(v_, e_idxs.size(), e_idxs.data(),
128 sub_vec.data(), ADD_VALUES));
129 }
130 }

References v_.

◆ config()

void MathLib::PETScVector::config ( )
private

A function called by constructors to configure members.

Definition at line 90 of file PETScVector.cpp.

91{
92 PetscCallAbort(PETSC_COMM_WORLD, VecSetFromOptions(v_));
93 PetscCallAbort(PETSC_COMM_WORLD,
94 VecGetOwnershipRange(v_, &start_rank_, &end_rank_));
95
96 PetscCallAbort(PETSC_COMM_WORLD, VecGetLocalSize(v_, &size_loc_));
97 PetscCallAbort(PETSC_COMM_WORLD, VecGetSize(v_, &size_));
98
99 PetscCallAbort(PETSC_COMM_WORLD,
100 VecSetOption(v_, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE));
101}

References end_rank_, size_, size_loc_, start_rank_, and v_.

Referenced by PETScVector(), PETScVector(), and shallowCopy().

◆ copyValues() [1/2]

void MathLib::PETScVector::copyValues ( std::span< PetscScalar > u) const

Copy local entries including ghost ones to a span.

Parameters
ua span for the values of local entries. If the sizes of the vector and the span mismatch, an exception will be thrown.

Definition at line 168 of file PETScVector.cpp.

169{
170 if (u.size() != static_cast<std::size_t>(getLocalSize() + getGhostSize()))
171 {
172 OGS_FATAL(
173 "PETScVector::copyValues() size mismatch. Trying to copy a vector "
174 "of size {:d} to a span of size {:d}.",
175 getLocalSize() + getGhostSize(), u.size());
176 }
177
178 PetscScalar* loc_x = getLocalVector();
179 std::copy_n(loc_x, getLocalSize() + getGhostSize(), u.begin());
180 restoreArray(loc_x);
181}
#define OGS_FATAL(...)
Definition Error.h:19
void restoreArray(PetscScalar *array) const
PetscInt getLocalSize() const
Get the number of entries in the same rank.
Definition PETScVector.h:79
PetscScalar * getLocalVector() const
PetscInt getGhostSize() const
Get the number of ghost entries in the same rank.
Definition PETScVector.h:81
static const double u

References getGhostSize(), getLocalSize(), getLocalVector(), OGS_FATAL, restoreArray(), and MathLib::u.

◆ copyValues() [2/2]

void MathLib::PETScVector::copyValues ( std::vector< PetscScalar > & u) const

Copy local entries including ghost ones to a vector.

Parameters
ua vector for the values of local entries. It will be resized to hold the current vector data.

Definition at line 159 of file PETScVector.cpp.

160{
161 u.resize(getLocalSize() + getGhostSize());
162
163 PetscScalar* loc_x = getLocalVector();
164 std::copy_n(loc_x, getLocalSize() + getGhostSize(), u.begin());
165 restoreArray(loc_x);
166}

References getGhostSize(), getLocalSize(), getLocalVector(), restoreArray(), and MathLib::u.

Referenced by setLocalAccessibleVector().

◆ destroy()

void MathLib::PETScVector::destroy ( )
inlineprivate

Definition at line 247 of file PETScVector.h.

248 {
249 if (v_ != nullptr)
250 {
251 PetscCallAbort(PETSC_COMM_WORLD, VecDestroy(&v_));
252 }
253 v_ = nullptr;
254 }

References v_.

Referenced by ~PETScVector(), and shallowCopy().

◆ finalizeAssembly()

void MathLib::PETScVector::finalizeAssembly ( )

Perform MPI collection of assembled entries in buffer.

Definition at line 103 of file PETScVector.cpp.

104{
105 PetscCallAbort(PETSC_COMM_WORLD, VecAssemblyBegin(v_));
106 PetscCallAbort(PETSC_COMM_WORLD, VecAssemblyEnd(v_));
107}

References v_.

Referenced by MathLib::applyKnownSolution(), MathLib::LinAlg::finalizeAssembly(), and finalizeVectorAssembly.

◆ get() [1/2]

PetscScalar MathLib::PETScVector::get ( const PetscInt idx) const

Definition at line 183 of file PETScVector.cpp.

184{
186 {
187 return entry_array_[getLocalIndex(idx)];
188 }
189
190 return entry_array_[idx];
191}
std::vector< PetscScalar > entry_array_
Array containing the entries of the vector. If the vector is created without given ghost IDs,...
PetscInt getLocalIndex(const PetscInt global_index) const
Get local index by a global index.

References created_with_ghost_id_, entry_array_, and getLocalIndex().

◆ get() [2/2]

std::vector< PetscScalar > MathLib::PETScVector::get ( std::vector< IndexType > const & indices) const

Get several entries. setLocalAccessibleVector() must be called beforehand.

Definition at line 193 of file PETScVector.cpp.

195{
196 std::vector<PetscScalar> local_x(indices.size());
197 std::transform(indices.begin(), indices.end(), local_x.begin(),
198 [this](IndexType index) { return get(index); });
199 return local_x;
200}

Referenced by operator[]().

◆ getGhostSize()

PetscInt MathLib::PETScVector::getGhostSize ( ) const
inline

Get the number of ghost entries in the same rank.

Definition at line 81 of file PETScVector.h.

81{ return size_ghosts_; }

References size_ghosts_.

Referenced by copyValues(), and copyValues().

◆ getGlobalVector()

void MathLib::PETScVector::getGlobalVector ( std::vector< PetscScalar > & u) const

Get global vector

Parameters
uArray to store the global vector. Memory allocation is needed in advance

Definition at line 109 of file PETScVector.cpp.

110{
111#ifdef TEST_MEM_PETSC
112 PetscLogDouble mem1, mem2;
113 PetscMemoryGetCurrentUsage(&mem1);
114#endif
115
116 assert(static_cast<PetscInt>(u.size()) == size_);
117
118 PetscInt state;
119 PetscCallAbort(PETSC_COMM_WORLD, VecLockGet(v_, &state));
120 if (state != 0)
121 {
122 OGS_FATAL("PETSc vector is already locked for {:s} access.",
123 state > 0 ? "read" : "write");
124 }
125 PetscScalar* xp = nullptr;
126 PetscCallAbort(PETSC_COMM_WORLD, VecGetArray(v_, &xp));
127
128 BaseLib::MPI::Mpi mpi{PETSC_COMM_WORLD};
129 BaseLib::MPI::allgatherv(std::span(xp, size_loc_), u, mpi);
130
131 // This following line may be needed late on
132 // for a communication load balance:
133 // MPI_Barrier(PETSC_COMM_WORLD);
134
135 PetscCallAbort(PETSC_COMM_WORLD, VecRestoreArray(v_, &xp));
136
137// TEST
138#ifdef TEST_MEM_PETSC
139 PetscMemoryGetCurrentUsage(&mem2);
140 PetscPrintf(
141 PETSC_COMM_WORLD,
142 "### Memory usage by Updating. Before :%f After:%f Increase:%d\n", mem1,
143 mem2, (int)(mem2 - mem1));
144#endif
145}
static std::vector< int > allgatherv(std::span< T > const send_buffer, std::vector< std::remove_const_t< T > > &receive_buffer, Mpi const &mpi)
Definition MPI.h:147

References BaseLib::MPI::allgatherv(), OGS_FATAL, size_, size_loc_, MathLib::u, and v_.

Referenced by setLocalAccessibleVector().

◆ getLocalIndex()

PetscInt MathLib::PETScVector::getLocalIndex ( const PetscInt global_index) const
private

Get local index by a global index.

Definition at line 222 of file PETScVector.cpp.

223{
224 if (global_index >= 0) // non-ghost entry.
225 {
226#ifndef NDEBUG
227 if (global_index < start_rank_ || global_index >= end_rank_)
228 {
229 OGS_FATAL(
230 "The global index {:d} is out of the range `[`{:d}, {:d}`)` of "
231 "the current rank.",
232 global_index, start_rank_, end_rank_);
233 }
234#endif
235 return global_index - start_rank_;
236 }
237
238 // A special case for a ghost location with global index equal to
239 // the size of the local vector:
240 PetscInt real_global_index = (-global_index == size_) ? 0 : -global_index;
241
242#ifndef NDEBUG
243 if (global_ids2local_ids_ghost_.find(real_global_index) ==
246 {
247 OGS_FATAL("The global index {:d} is not found as a ghost ID",
248 global_index);
249 }
250#endif
251
252 return global_ids2local_ids_ghost_.at(real_global_index);
253}

References end_rank_, global_ids2local_ids_ghost_, OGS_FATAL, size_, and start_rank_.

Referenced by get().

◆ getLocalSize()

PetscInt MathLib::PETScVector::getLocalSize ( ) const
inline

Get the number of entries in the same rank.

Definition at line 79 of file PETScVector.h.

79{ return size_loc_; }

References size_loc_.

Referenced by copyValues(), and copyValues().

◆ getLocalVector()

PetscScalar * MathLib::PETScVector::getLocalVector ( ) const
private

Get local vector, i.e. entries in the same rank

Definition at line 202 of file PETScVector.cpp.

203{
204 PetscScalar* loc_array;
206 {
207 PetscCallAbort(PETSC_COMM_WORLD,
208 VecGhostUpdateBegin(v_, INSERT_VALUES, SCATTER_FORWARD));
209 PetscCallAbort(PETSC_COMM_WORLD,
210 VecGhostUpdateEnd(v_, INSERT_VALUES, SCATTER_FORWARD));
211 PetscCallAbort(PETSC_COMM_WORLD, VecGhostGetLocalForm(v_, &v_loc_));
212 PetscCallAbort(PETSC_COMM_WORLD, VecGetArray(v_loc_, &loc_array));
213 }
214 else
215 {
216 PetscCallAbort(PETSC_COMM_WORLD, VecGetArray(v_, &loc_array));
217 }
218
219 return loc_array;
220}

References created_with_ghost_id_, global_ids2local_ids_ghost_, v_, and v_loc_.

Referenced by copyValues(), and copyValues().

◆ getRangeBegin()

PetscInt MathLib::PETScVector::getRangeBegin ( ) const
inline

Get the start index of the local vector.

Definition at line 83 of file PETScVector.h.

83{ return start_rank_; }

References start_rank_.

◆ getRangeEnd()

PetscInt MathLib::PETScVector::getRangeEnd ( ) const
inline

Get the end index of the local vector.

Definition at line 85 of file PETScVector.h.

85{ return end_rank_; }

References end_rank_.

◆ getRawVector() [1/2]

◆ getRawVector() [2/2]

PETSc_Vec const & MathLib::PETScVector::getRawVector ( ) const
inline

Exposes the underlying PETSc vector.

Warning
This method is dangerous insofar as you can do arbitrary things also with a const PETSc vector!

Definition at line 196 of file PETScVector.h.

196{ return v_; }

References v_.

◆ operator=()

PETScVector & MathLib::PETScVector::operator= ( PETScVector && )
delete

Disallow moving.

Todo
This operator should be implemented properly when doing a general cleanup of all matrix and vector classes.

References PETScVector().

◆ operator[]()

PetscScalar MathLib::PETScVector::operator[] ( PetscInt idx) const
inline

Get the value of an entry by [] operator. setLocalAccessibleVector() must be called beforehand.

Definition at line 173 of file PETScVector.h.

173{ return get(idx); }
std::vector< PetscScalar > get(std::vector< IndexType > const &indices) const

References get().

◆ restoreArray()

void MathLib::PETScVector::restoreArray ( PetscScalar * array) const
inlineprivate

Restore array after finish access local array

Parameters
arrayPointer to the local array fetched by VecGetArray

Definition at line 255 of file PETScVector.cpp.

256{
258 {
259 PetscCallAbort(PETSC_COMM_WORLD, VecRestoreArray(v_loc_, &array));
260 PetscCallAbort(PETSC_COMM_WORLD, VecGhostRestoreLocalForm(v_, &v_loc_));
261 }
262 else
263 {
264 PetscCallAbort(PETSC_COMM_WORLD, VecRestoreArray(v_, &array));
265 }
266}

References created_with_ghost_id_, global_ids2local_ids_ghost_, v_, and v_loc_.

Referenced by copyValues(), and copyValues().

◆ set() [1/2]

void MathLib::PETScVector::set ( const PetscInt i,
const PetscScalar value )
inline

Insert a single entry with value.

Parameters
iEntry index
valueEntry value

Definition at line 92 of file PETScVector.h.

93 {
94 PetscCallAbort(PETSC_COMM_WORLD,
95 VecSetValue(v_, i, value, INSERT_VALUES));
96 }

References v_.

Referenced by MathLib::applyKnownSolution().

◆ set() [2/2]

template<class T_SUBVEC>
void MathLib::PETScVector::set ( const std::vector< PetscInt > & e_idxs,
const T_SUBVEC & sub_vec )
inline

Add values to several entries

Parameters
e_idxsIndices of entries to be added. Note: std::size_t cannot be the type of e_idxs template argument
sub_vecEntries to be added

Definition at line 140 of file PETScVector.h.

141 {
142 if constexpr (std::is_pointer_v<T_SUBVEC>)
143 {
144 PetscCallAbort(PETSC_COMM_WORLD,
145 VecSetValues(v_, e_idxs.size(), e_idxs.data(),
146 sub_vec, INSERT_VALUES));
147 }
148 else
149 {
150 PetscCallAbort(PETSC_COMM_WORLD,
151 VecSetValues(v_, e_idxs.size(), e_idxs.data(),
152 sub_vec.data(), INSERT_VALUES));
153 }
154 }

References v_.

◆ setLocalAccessibleVector()

void MathLib::PETScVector::setLocalAccessibleVector ( ) const

Set local accessible vector in order to get entries. Call this before call operator[] or get(...).

Definition at line 147 of file PETScVector.cpp.

148{
150 {
152 return;
153 }
154
155 entry_array_.resize(size_);
157}
void getGlobalVector(std::vector< PetscScalar > &u) const
void copyValues(std::vector< PetscScalar > &u) const

References copyValues(), created_with_ghost_id_, entry_array_, getGlobalVector(), and size_.

Referenced by MathLib::LinAlg::setLocalAccessibleVector().

◆ setZero()

void MathLib::PETScVector::setZero ( )
inline

Definition at line 157 of file PETScVector.h.

157{ PetscCallAbort(PETSC_COMM_WORLD, VecSet(v_, 0.0)); }

References v_.

◆ shallowCopy()

void MathLib::PETScVector::shallowCopy ( const PETScVector & v)

Definition at line 286 of file PETScVector.cpp.

287{
288 destroy();
289
290 PetscCallAbort(PETSC_COMM_WORLD, VecDuplicate(v.getRawVector(), &v_));
291
292 start_rank_ = v.start_rank_;
293 end_rank_ = v.end_rank_;
294 size_ = v.size_;
295 size_loc_ = v.size_loc_;
296 size_ghosts_ = v.size_ghosts_;
297 created_with_ghost_id_ = v.created_with_ghost_id_;
298 global_ids2local_ids_ghost_ = v.global_ids2local_ids_ghost_;
299
300 config();
301}
static const double v

References PETScVector(), config(), created_with_ghost_id_, destroy(), end_rank_, global_ids2local_ids_ghost_, size_, size_ghosts_, size_loc_, start_rank_, MathLib::v, and v_.

Referenced by PETScVector(), MathLib::LinAlg::copy(), MathLib::LinAlg::matMult(), and MathLib::LinAlg::matMultAdd().

◆ size()

PetscInt MathLib::PETScVector::size ( ) const
inline

Get the global size of the vector.

Definition at line 77 of file PETScVector.h.

77{ return size_; }

References size_.

Referenced by PETScVector().

◆ viewer()

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.

Parameters
file_nameFile name for output
vw_formatFile 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 268 of file PETScVector.cpp.

270{
271 PetscViewer viewer;
272 PetscViewerASCIIOpen(PETSC_COMM_WORLD, file_name.c_str(), &viewer);
273 PetscViewerPushFormat(viewer, vw_format);
274
275 PetscObjectSetName((PetscObject)v_, file_name.c_str());
276 VecView(v_, viewer);
277
278#define nEXIT_TEST
279#ifdef EXIT_TEST
280 VecDestroy(v_);
281 PetscFinalize();
282 exit(0);
283#endif
284}
void viewer(const std::string &file_name, const PetscViewerFormat vw_format=PETSC_VIEWER_ASCII_MATLAB) const

References v_, and viewer().

Referenced by viewer().

◆ finalizeVectorAssembly

void finalizeVectorAssembly ( PETScVector & vec)
friend

Function to finalize the vector assembly.

Definition at line 303 of file PETScVector.cpp.

304{
305 vec.finalizeAssembly();
306}

References PETScVector(), and finalizeAssembly().

Member Data Documentation

◆ created_with_ghost_id_

bool MathLib::PETScVector::created_with_ghost_id_ = false
private

Flag to indicate whether the vector is created with ghost entry indices.

Definition at line 274 of file PETScVector.h.

Referenced by PETScVector(), PETScVector(), get(), getLocalVector(), restoreArray(), setLocalAccessibleVector(), and shallowCopy().

◆ end_rank_

PetscInt MathLib::PETScVector::end_rank_
private

Ending index in a rank.

Definition at line 264 of file PETScVector.h.

Referenced by PETScVector(), config(), getLocalIndex(), getRangeEnd(), and shallowCopy().

◆ entry_array_

std::vector<PetscScalar> MathLib::PETScVector::entry_array_
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 283 of file PETScVector.h.

Referenced by get(), and setLocalAccessibleVector().

◆ global_ids2local_ids_ghost_

std::map<PetscInt, PetscInt> MathLib::PETScVector::global_ids2local_ids_ghost_
mutableprivate

Map global indices of ghost entries to local indices.

Definition at line 286 of file PETScVector.h.

Referenced by PETScVector(), PETScVector(), getLocalIndex(), getLocalVector(), restoreArray(), and shallowCopy().

◆ size_

PetscInt MathLib::PETScVector::size_
private

Size of the vector.

Definition at line 267 of file PETScVector.h.

Referenced by PETScVector(), config(), getGlobalVector(), getLocalIndex(), setLocalAccessibleVector(), shallowCopy(), and size().

◆ size_ghosts_

PetscInt MathLib::PETScVector::size_ghosts_ = 0
private

Size of local ghost entries.

Definition at line 271 of file PETScVector.h.

Referenced by PETScVector(), PETScVector(), getGhostSize(), and shallowCopy().

◆ size_loc_

PetscInt MathLib::PETScVector::size_loc_
private

Size of local entries.

Definition at line 269 of file PETScVector.h.

Referenced by PETScVector(), PETScVector(), config(), getGlobalVector(), getLocalSize(), and shallowCopy().

◆ start_rank_

PetscInt MathLib::PETScVector::start_rank_
private

Starting index in a rank.

Definition at line 262 of file PETScVector.h.

Referenced by PETScVector(), config(), getLocalIndex(), getRangeBegin(), and shallowCopy().

◆ v_

◆ v_loc_

PETSc_Vec MathLib::PETScVector::v_loc_ = nullptr
mutableprivate

Local vector, which is only for the case that v_ is created with ghost entries.

Definition at line 259 of file PETScVector.h.

Referenced by PETScVector(), getLocalVector(), and restoreArray().


The documentation for this class was generated from the following files: