19 PetscCallAbort(PETSC_COMM_WORLD, VecCreate(PETSC_COMM_WORLD, &
v_));
20 PetscCallAbort(PETSC_COMM_WORLD,
21 VecSetSizes(
v_, PETSC_DECIDE, vec_size));
29 VecCreateMPI(PETSC_COMM_WORLD, vec_size, PETSC_DECIDE, &
v_));
36 const std::vector<PetscInt>& ghost_ids,
37 const bool is_global_size)
41 PetscInt nghosts =
static_cast<PetscInt
>(ghost_ids.size());
44 PetscCallAbort(PETSC_COMM_WORLD,
45 VecCreateGhost(PETSC_COMM_WORLD, PETSC_DECIDE, vec_size,
46 nghosts, ghost_ids.data(), &
v_));
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()));
60 for (PetscInt i = 0; i < nghosts; i++)
73 PetscCallAbort(PETSC_COMM_WORLD, VecCopy(existing_vec.
v_,
v_));
78 :
v_{std::move(other.
v_)},
92 PetscCallAbort(PETSC_COMM_WORLD, VecSetFromOptions(
v_));
93 PetscCallAbort(PETSC_COMM_WORLD,
96 PetscCallAbort(PETSC_COMM_WORLD, VecGetLocalSize(
v_, &
size_loc_));
97 PetscCallAbort(PETSC_COMM_WORLD, VecGetSize(
v_, &
size_));
99 PetscCallAbort(PETSC_COMM_WORLD,
100 VecSetOption(
v_, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE));
105 PetscCallAbort(PETSC_COMM_WORLD, VecAssemblyBegin(
v_));
106 PetscCallAbort(PETSC_COMM_WORLD, VecAssemblyEnd(
v_));
112 PetscLogDouble mem1, mem2;
113 PetscMemoryGetCurrentUsage(&mem1);
116 assert(
static_cast<PetscInt
>(
u.size()) ==
size_);
119 PetscCallAbort(PETSC_COMM_WORLD, VecLockGet(
v_, &state));
122 OGS_FATAL(
"PETSc vector is already locked for {:s} access.",
123 state > 0 ?
"read" :
"write");
125 PetscScalar* xp =
nullptr;
126 PetscCallAbort(PETSC_COMM_WORLD, VecGetArray(
v_, &xp));
135 PetscCallAbort(PETSC_COMM_WORLD, VecRestoreArray(
v_, &xp));
139 PetscMemoryGetCurrentUsage(&mem2);
142 "### Memory usage by Updating. Before :%f After:%f Increase:%d\n", mem1,
143 mem2, (
int)(mem2 - mem1));
173 "PETScVector::copyValues() size mismatch. Trying to copy a vector "
174 "of size {:d} to a span of size {:d}.",
194 std::vector<IndexType>
const& indices)
const
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); });
204 PetscScalar* loc_array;
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));
216 PetscCallAbort(PETSC_COMM_WORLD, VecGetArray(
v_, &loc_array));
224 if (global_index >= 0)
227 if (global_index < start_rank_ || global_index >=
end_rank_)
230 "The global index {:d} is out of the range `[`{:d}, {:d}`)` of "
240 PetscInt real_global_index = (-global_index ==
size_) ? 0 : -global_index;
247 OGS_FATAL(
"The global index {:d} is not found as a ghost ID",
259 PetscCallAbort(PETSC_COMM_WORLD, VecRestoreArray(
v_loc_, &array));
260 PetscCallAbort(PETSC_COMM_WORLD, VecGhostRestoreLocalForm(
v_, &
v_loc_));
264 PetscCallAbort(PETSC_COMM_WORLD, VecRestoreArray(
v_, &array));
269 const PetscViewerFormat vw_format)
const
272 PetscViewerASCIIOpen(PETSC_COMM_WORLD, file_name.c_str(), &
viewer);
273 PetscViewerPushFormat(
viewer, vw_format);
275 PetscObjectSetName((PetscObject)
v_, file_name.c_str());
290 PetscCallAbort(PETSC_COMM_WORLD, VecDuplicate(
v.getRawVector(), &
v_));
PetscInt start_rank_
Starting index in a rank.
void finalizeAssembly()
Perform MPI collection of assembled entries in buffer.
std::vector< PetscScalar > entry_array_
Array containing the entries of the vector. If the vector is created without given ghost IDs,...
void config()
A function called by constructors to configure members.
void setLocalAccessibleVector() const
void restoreArray(PetscScalar *array) const
PetscInt getLocalSize() const
Get the number of entries in the same rank.
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
PetscInt size_ghosts_
Size of local ghost entries.
void copyValues(std::vector< PetscScalar > &u) const
PetscInt size_
Size of the vector.
PetscInt size() const
Get the global size of the vector.
void shallowCopy(const PETScVector &v)
PetscInt getGhostSize() const
Get the number of ghost entries in the same rank.
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
static std::vector< int > allgatherv(std::span< T > const send_buffer, std::vector< std::remove_const_t< T > > &receive_buffer, Mpi const &mpi)
void finalizeVectorAssembly(VEC_T &)
General function to finalize the vector assembly.