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())}, has_ghost_id_{true}
52 PetscInt nghosts =
static_cast<PetscInt
>(ghost_ids.size());
55 VecCreateGhost(PETSC_COMM_WORLD, PETSC_DECIDE, vec_size, nghosts,
56 ghost_ids.data(), &
v_);
60 VecCreate(PETSC_COMM_WORLD, &
v_);
61 VecSetType(
v_, VECMPI);
62 VecSetSizes(
v_, vec_size, PETSC_DECIDE);
63 VecMPISetGhost(
v_, nghosts, ghost_ids.data());
68 for (PetscInt i = 0; i < nghosts; i++)
79 VecCopy(existing_vec.
v_,
v_);
84 : v_{std::move(other.v_)},
85 v_loc_{std::move(other.v_loc_)},
86 start_rank_{other.start_rank_},
87 end_rank_{other.end_rank_},
89 size_loc_{other.size_loc_},
90 size_ghosts_{other.size_ghosts_},
91 has_ghost_id_{other.has_ghost_id_},
92 global_ids2local_ids_ghost_{other.global_ids2local_ids_ghost_}
98 VecSetFromOptions(
v_);
104 VecSetOption(
v_, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE);
109 VecAssemblyBegin(
v_);
114 PetscScalar global_array[])
const
118 MPI_Comm_size(PETSC_COMM_WORLD, &size_rank);
121 std::vector<PetscInt> i_cnt(size_rank);
123 MPI_Allgather(&
size_loc_, 1, MPI_INT, &i_cnt[0], 1, MPI_INT,
129 std::vector<PetscInt> i_disp(size_rank);
130 for (PetscInt i = 0; i < size_rank; i++)
136 MPI_Allgatherv(local_array,
size_loc_, MPI_DOUBLE, global_array, &i_cnt[0],
137 &i_disp[0], MPI_DOUBLE, PETSC_COMM_WORLD);
142 #ifdef TEST_MEM_PETSC
143 PetscLogDouble mem1, mem2;
144 PetscMemoryGetCurrentUsage(&mem1);
147 assert(
static_cast<PetscInt
>(u.size()) ==
size_);
150 VecLockGet(
v_, &state);
153 OGS_FATAL(
"PETSc vector is already locked for {:s} access.",
154 state > 0 ?
"read" :
"write");
156 PetscScalar* xp =
nullptr;
157 VecGetArray(
v_, &xp);
165 VecRestoreArray(
v_, &xp);
168 #ifdef TEST_MEM_PETSC
169 PetscMemoryGetCurrentUsage(&mem2);
172 "### Memory usage by Updating. Before :%f After:%f Increase:%d\n", mem1,
173 mem2, (
int)(mem2 - mem1));
214 const PetscInt id_p = (idx == -
size_) ? 0 : std::abs(idx);
219 std::vector<IndexType>
const& indices)
const
221 std::vector<PetscScalar> local_x(indices.size());
228 for (std::size_t i = 0; i < indices.size(); i++)
235 for (std::size_t i = 0; i < indices.size(); i++)
239 (indices[i] == -
size_) ? 0 : std::abs(indices[i]);
249 PetscScalar* loc_array;
252 VecGhostUpdateBegin(
v_, INSERT_VALUES, SCATTER_FORWARD);
253 VecGhostUpdateEnd(
v_, INSERT_VALUES, SCATTER_FORWARD);
255 VecGetArray(
v_loc_, &loc_array);
258 VecGetArray(
v_, &loc_array);
264 if (global_index >= 0)
269 PetscInt real_global_index = (-global_index ==
size_) ? 0 : -global_index;
278 VecRestoreArray(
v_loc_, &array);
279 VecGhostRestoreLocalForm(
v_, &
v_loc_);
282 VecRestoreArray(
v_, &array);
286 const PetscViewerFormat vw_format)
const
289 PetscViewerASCIIOpen(PETSC_COMM_WORLD, file_name.c_str(), &
viewer);
290 PetscViewerPushFormat(
viewer, vw_format);
292 PetscObjectSetName((PetscObject)
v_, file_name.c_str());
317 VecSetOption(
v_, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE);
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.
bool has_ghost_id_
Flag to indicate whether the vector is created with ghost entry indices.
PetscInt size_loc_
Size of local entries.
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
PETSc_Vec & getRawVector()
Exposes the underlying PETSc vector.
void finalizeVectorAssembly(VEC_T &)
General function to finalize the vector assembly.