36 VecCreate(PETSC_COMM_WORLD, &
v_);
37 VecSetSizes(
v_, PETSC_DECIDE, vec_size);
43 VecCreateMPI(PETSC_COMM_WORLD, vec_size, PETSC_DECIDE, &
v_);
50 const std::vector<PetscInt>& ghost_ids,
51 const bool is_global_size)
52 : size_ghosts_{static_cast<PetscInt>(ghost_ids.size())},
53 created_with_ghost_id_{true}
55 PetscInt nghosts =
static_cast<PetscInt
>(ghost_ids.size());
58 VecCreateGhost(PETSC_COMM_WORLD, PETSC_DECIDE, vec_size, nghosts,
59 ghost_ids.data(), &
v_);
63 VecCreate(PETSC_COMM_WORLD, &
v_);
64 VecSetType(
v_, VECMPI);
65 VecSetSizes(
v_, vec_size, PETSC_DECIDE);
66 VecMPISetGhost(
v_, nghosts, ghost_ids.data());
71 for (PetscInt i = 0; i < nghosts; i++)
84 VecCopy(existing_vec.
v_,
v_);
89 : v_{std::move(other.v_)},
90 v_loc_{std::move(other.v_loc_)},
91 start_rank_{other.start_rank_},
92 end_rank_{other.end_rank_},
94 size_loc_{other.size_loc_},
95 size_ghosts_{other.size_ghosts_},
96 created_with_ghost_id_{other.created_with_ghost_id_},
97 global_ids2local_ids_ghost_{other.global_ids2local_ids_ghost_}
103 VecSetFromOptions(
v_);
109 VecSetOption(
v_, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE);
114 VecAssemblyBegin(
v_);
121 PetscLogDouble mem1, mem2;
122 PetscMemoryGetCurrentUsage(&mem1);
125 assert(
static_cast<PetscInt
>(
u.size()) ==
size_);
128 VecLockGet(
v_, &state);
131 OGS_FATAL(
"PETSc vector is already locked for {:s} access.",
132 state > 0 ?
"read" :
"write");
134 PetscScalar* xp =
nullptr;
135 VecGetArray(
v_, &xp);
144 VecRestoreArray(
v_, &xp);
148 PetscMemoryGetCurrentUsage(&mem2);
151 "### Memory usage by Updating. Before :%f After:%f Increase:%d\n", mem1,
152 mem2, (
int)(mem2 - mem1));
188 std::vector<IndexType>
const& indices)
const
190 std::vector<PetscScalar> local_x(indices.size());
191 std::transform(indices.begin(), indices.end(), local_x.begin(),
192 [
this](
IndexType index) { return get(index); });
198 PetscScalar* loc_array;
201 VecGhostUpdateBegin(
v_, INSERT_VALUES, SCATTER_FORWARD);
202 VecGhostUpdateEnd(
v_, INSERT_VALUES, SCATTER_FORWARD);
204 VecGetArray(
v_loc_, &loc_array);
208 VecGetArray(
v_, &loc_array);
216 if (global_index >= 0)
219 if (global_index < start_rank_ || global_index >=
end_rank_)
222 "The global index {:d} is out of the range `[`{:d}, {:d}`)` of "
232 PetscInt real_global_index = (-global_index ==
size_) ? 0 : -global_index;
239 OGS_FATAL(
"The global index {:d} is not found as a ghost ID",
251 VecRestoreArray(
v_loc_, &array);
252 VecGhostRestoreLocalForm(
v_, &
v_loc_);
256 VecRestoreArray(
v_, &array);
261 const PetscViewerFormat vw_format)
const
264 PetscViewerASCIIOpen(PETSC_COMM_WORLD, file_name.c_str(), &
viewer);
265 PetscViewerPushFormat(
viewer, vw_format);
267 PetscObjectSetName((PetscObject)
v_, file_name.c_str());
282 VecDuplicate(
v.getRawVector(), &
v_);
Declaration of class PETScVector, which provides an interface to PETSc vector routines.
Wrapper class for PETSc vector.
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.
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.