19 VecCreate(PETSC_COMM_WORLD, &
v_);
20 VecSetSizes(
v_, PETSC_DECIDE, vec_size);
26 VecCreateMPI(PETSC_COMM_WORLD, vec_size, PETSC_DECIDE, &
v_);
33 const std::vector<PetscInt>& ghost_ids,
34 const bool is_global_size)
38 PetscInt nghosts =
static_cast<PetscInt
>(ghost_ids.size());
41 VecCreateGhost(PETSC_COMM_WORLD, PETSC_DECIDE, vec_size, nghosts,
42 ghost_ids.data(), &
v_);
46 VecCreate(PETSC_COMM_WORLD, &
v_);
47 VecSetType(
v_, VECMPI);
48 VecSetSizes(
v_, vec_size, PETSC_DECIDE);
49 VecMPISetGhost(
v_, nghosts, ghost_ids.data());
54 for (PetscInt i = 0; i < nghosts; i++)
67 VecCopy(existing_vec.
v_,
v_);
72 :
v_{std::move(other.
v_)},
86 VecSetFromOptions(
v_);
92 VecSetOption(
v_, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE);
104 PetscLogDouble mem1, mem2;
105 PetscMemoryGetCurrentUsage(&mem1);
108 assert(
static_cast<PetscInt
>(
u.size()) ==
size_);
111 VecLockGet(
v_, &state);
114 OGS_FATAL(
"PETSc vector is already locked for {:s} access.",
115 state > 0 ?
"read" :
"write");
117 PetscScalar* xp =
nullptr;
118 VecGetArray(
v_, &xp);
127 VecRestoreArray(
v_, &xp);
131 PetscMemoryGetCurrentUsage(&mem2);
134 "### Memory usage by Updating. Before :%f After:%f Increase:%d\n", mem1,
135 mem2, (
int)(mem2 - mem1));
165 "PETScVector::copyValues() size mismatch. Trying to copy a vector "
166 "of size {:d} to a span of size {:d}.",
186 std::vector<IndexType>
const& indices)
const
188 std::vector<PetscScalar> local_x(indices.size());
189 std::transform(indices.begin(), indices.end(), local_x.begin(),
190 [
this](
IndexType index) { return get(index); });
196 PetscScalar* loc_array;
199 VecGhostUpdateBegin(
v_, INSERT_VALUES, SCATTER_FORWARD);
200 VecGhostUpdateEnd(
v_, INSERT_VALUES, SCATTER_FORWARD);
202 VecGetArray(
v_loc_, &loc_array);
206 VecGetArray(
v_, &loc_array);
214 if (global_index >= 0)
217 if (global_index < start_rank_ || global_index >=
end_rank_)
220 "The global index {:d} is out of the range `[`{:d}, {:d}`)` of "
230 PetscInt real_global_index = (-global_index ==
size_) ? 0 : -global_index;
237 OGS_FATAL(
"The global index {:d} is not found as a ghost ID",
249 VecRestoreArray(
v_loc_, &array);
250 VecGhostRestoreLocalForm(
v_, &
v_loc_);
254 VecRestoreArray(
v_, &array);
259 const PetscViewerFormat vw_format)
const
262 PetscViewerASCIIOpen(PETSC_COMM_WORLD, file_name.c_str(), &
viewer);
263 PetscViewerPushFormat(
viewer, vw_format);
265 PetscObjectSetName((PetscObject)
v_, file_name.c_str());
280 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.