34 VecCreate(PETSC_COMM_WORLD, &
v_);
35 VecSetSizes(
v_, PETSC_DECIDE, vec_size);
41 VecCreateMPI(PETSC_COMM_WORLD, vec_size, PETSC_DECIDE, &
v_);
48 const std::vector<PetscInt>& ghost_ids,
49 const bool is_global_size)
50 : size_ghosts_{static_cast<PetscInt>(ghost_ids.size())},
51 created_with_ghost_id_{true}
53 PetscInt nghosts =
static_cast<PetscInt
>(ghost_ids.size());
56 VecCreateGhost(PETSC_COMM_WORLD, PETSC_DECIDE, vec_size, nghosts,
57 ghost_ids.data(), &
v_);
61 VecCreate(PETSC_COMM_WORLD, &
v_);
62 VecSetType(
v_, VECMPI);
63 VecSetSizes(
v_, vec_size, PETSC_DECIDE);
64 VecMPISetGhost(
v_, nghosts, ghost_ids.data());
69 for (PetscInt i = 0; i < nghosts; i++)
82 VecCopy(existing_vec.
v_,
v_);
87 : v_{std::move(other.v_)},
88 v_loc_{std::move(other.v_loc_)},
89 start_rank_{other.start_rank_},
90 end_rank_{other.end_rank_},
92 size_loc_{other.size_loc_},
93 size_ghosts_{other.size_ghosts_},
94 created_with_ghost_id_{other.created_with_ghost_id_},
95 global_ids2local_ids_ghost_{other.global_ids2local_ids_ghost_}
101 VecSetFromOptions(
v_);
107 VecSetOption(
v_, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE);
112 VecAssemblyBegin(
v_);
117 PetscScalar global_array[])
const
121 MPI_Comm_size(PETSC_COMM_WORLD, &size_rank);
124 std::vector<PetscInt> i_cnt(size_rank);
126 MPI_Allgather(&
size_loc_, 1, MPI_INT, &i_cnt[0], 1, MPI_INT,
132 std::vector<PetscInt> i_disp(size_rank);
133 for (PetscInt i = 0; i < size_rank; i++)
139 MPI_Allgatherv(local_array,
size_loc_, MPI_DOUBLE, global_array, &i_cnt[0],
140 &i_disp[0], MPI_DOUBLE, PETSC_COMM_WORLD);
146 PetscLogDouble mem1, mem2;
147 PetscMemoryGetCurrentUsage(&mem1);
150 assert(
static_cast<PetscInt
>(
u.size()) ==
size_);
153 VecLockGet(
v_, &state);
156 OGS_FATAL(
"PETSc vector is already locked for {:s} access.",
157 state > 0 ?
"read" :
"write");
159 PetscScalar* xp =
nullptr;
160 VecGetArray(
v_, &xp);
168 VecRestoreArray(
v_, &xp);
172 PetscMemoryGetCurrentUsage(&mem2);
175 "### Memory usage by Updating. Before :%f After:%f Increase:%d\n", mem1,
176 mem2, (
int)(mem2 - mem1));
212 std::vector<IndexType>
const& indices)
const
214 std::vector<PetscScalar> local_x(indices.size());
215 std::transform(indices.begin(), indices.end(), local_x.begin(),
216 [
this](
IndexType index) { return get(index); });
222 PetscScalar* loc_array;
225 VecGhostUpdateBegin(
v_, INSERT_VALUES, SCATTER_FORWARD);
226 VecGhostUpdateEnd(
v_, INSERT_VALUES, SCATTER_FORWARD);
228 VecGetArray(
v_loc_, &loc_array);
232 VecGetArray(
v_, &loc_array);
240 if (global_index >= 0)
243 if (global_index < start_rank_ || global_index >=
end_rank_)
246 "The global index {:d} is out of the range `[`{:d}, {:d}`)` of "
256 PetscInt real_global_index = (-global_index ==
size_) ? 0 : -global_index;
263 OGS_FATAL(
"The global index {:d} is not found as a ghost ID",
275 VecRestoreArray(
v_loc_, &array);
276 VecGhostRestoreLocalForm(
v_, &
v_loc_);
280 VecRestoreArray(
v_, &array);
285 const PetscViewerFormat vw_format)
const
288 PetscViewerASCIIOpen(PETSC_COMM_WORLD, file_name.c_str(), &
viewer);
289 PetscViewerPushFormat(
viewer, vw_format);
291 PetscObjectSetName((PetscObject)
v_, file_name.c_str());
306 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.
void gatherLocalVectors(PetscScalar local_array[], PetscScalar global_array[]) const
Collect local vectors.
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
void finalizeVectorAssembly(VEC_T &)
General function to finalize the vector assembly.