From 4c47ad8a6bbe6715d63cd42e9c035f0d37bcaf02 Mon Sep 17 00:00:00 2001 From: Alex Lindsay Date: Mon, 24 Jun 2024 07:57:40 -0700 Subject: [PATCH 1/3] Use rvalue ref unique_ptr for restore_subvector --- include/numerics/numeric_vector.h | 3 ++- include/numerics/petsc_vector.h | 2 +- src/numerics/petsc_vector.C | 4 ++-- src/systems/condensed_eigen_system.C | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/numerics/numeric_vector.h b/include/numerics/numeric_vector.h index 0ac3de4cec7..7b72e3d61c2 100644 --- a/include/numerics/numeric_vector.h +++ b/include/numerics/numeric_vector.h @@ -749,7 +749,8 @@ class NumericVector : public ReferenceCountedObject>, * rows. This is currently only implemented for * PetscVectors. */ - virtual void restore_subvector(NumericVector &&, const std::vector &) + virtual void restore_subvector(std::unique_ptr> &&, + const std::vector &) { libmesh_not_implemented(); } diff --git a/include/numerics/petsc_vector.h b/include/numerics/petsc_vector.h index 19320538ac1..c04fdaf099b 100644 --- a/include/numerics/petsc_vector.h +++ b/include/numerics/petsc_vector.h @@ -349,7 +349,7 @@ class PetscVector final : public NumericVector virtual std::unique_ptr> get_subvector(const std::vector & rows) override; - virtual void restore_subvector(NumericVector && subvector, + virtual void restore_subvector(std::unique_ptr> && subvector, const std::vector & rows) override; private: diff --git a/src/numerics/petsc_vector.C b/src/numerics/petsc_vector.C index 494a9dd7d89..956e868c4b9 100644 --- a/src/numerics/petsc_vector.C +++ b/src/numerics/petsc_vector.C @@ -1431,10 +1431,10 @@ PetscVector::get_subvector(const std::vector & rows) template void -PetscVector::restore_subvector(NumericVector && subvector, +PetscVector::restore_subvector(std::unique_ptr> && subvector, const std::vector & rows) { - auto * const petsc_subvector = cast_ptr *>(&subvector); + auto * const petsc_subvector = cast_ptr *>(subvector.get()); // Construct index set WrappedPetsc parent_is; diff --git a/src/systems/condensed_eigen_system.C b/src/systems/condensed_eigen_system.C index 9ff89e5fc41..65301521d5b 100644 --- a/src/systems/condensed_eigen_system.C +++ b/src/systems/condensed_eigen_system.C @@ -267,7 +267,7 @@ CondensedEigenSystem::copy_sub_to_super(const NumericVector & sub, super.local_size()); auto super_sub_view = super.get_subvector(local_non_condensed_dofs_vector); *super_sub_view = sub; - super.restore_subvector(std::move(*super_sub_view), local_non_condensed_dofs_vector); + super.restore_subvector(std::move(super_sub_view), local_non_condensed_dofs_vector); } void @@ -279,7 +279,7 @@ CondensedEigenSystem::copy_super_to_sub(NumericVector & super, super.local_size()); auto super_sub_view = super.get_subvector(local_non_condensed_dofs_vector); sub = *super_sub_view; - super.restore_subvector(std::move(*super_sub_view), local_non_condensed_dofs_vector); + super.restore_subvector(std::move(super_sub_view), local_non_condensed_dofs_vector); } void From 89d70d3c6137b402d2b22642fd167ee5750cec55 Mon Sep 17 00:00:00 2001 From: Alex Lindsay Date: Tue, 25 Jun 2024 13:14:35 -0700 Subject: [PATCH 2/3] When restoring a subvector, we must make sure to update ghost entries --- src/numerics/petsc_vector.C | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/numerics/petsc_vector.C b/src/numerics/petsc_vector.C index 956e868c4b9..53604aa28cb 100644 --- a/src/numerics/petsc_vector.C +++ b/src/numerics/petsc_vector.C @@ -1426,6 +1426,8 @@ PetscVector::get_subvector(const std::vector & rows) ierr = VecGetSubVector(_vec, parent_is, &subvec); LIBMESH_CHKERR(ierr); + this->_is_closed = false; + return std::make_unique>(subvec, this->comm()); } @@ -1448,6 +1450,11 @@ PetscVector::restore_subvector(std::unique_ptr> && subvector Vec subvec = petsc_subvector->vec(); ierr = VecRestoreSubVector(_vec, parent_is, &subvec); LIBMESH_CHKERR(ierr); + + if (this->type() == GHOSTED) + VecGhostUpdateBeginEnd(this->comm(), _vec, INSERT_VALUES, SCATTER_FORWARD); + + this->_is_closed = true; } //------------------------------------------------------------------ From eaa757fb7de7844614f08c1fa13bba1c1c5f189c Mon Sep 17 00:00:00 2001 From: Alex Lindsay Date: Tue, 2 Jul 2024 06:50:30 -0700 Subject: [PATCH 3/3] Pass by value instead of rvalue ref This ensures the move constructor is invoked and the caller releases ownership per https://stackoverflow.com/a/71930927 --- include/numerics/numeric_vector.h | 2 +- include/numerics/petsc_vector.h | 2 +- src/numerics/petsc_vector.C | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/numerics/numeric_vector.h b/include/numerics/numeric_vector.h index 7b72e3d61c2..e9a7097ebdf 100644 --- a/include/numerics/numeric_vector.h +++ b/include/numerics/numeric_vector.h @@ -749,7 +749,7 @@ class NumericVector : public ReferenceCountedObject>, * rows. This is currently only implemented for * PetscVectors. */ - virtual void restore_subvector(std::unique_ptr> &&, + virtual void restore_subvector(std::unique_ptr>, const std::vector &) { libmesh_not_implemented(); diff --git a/include/numerics/petsc_vector.h b/include/numerics/petsc_vector.h index c04fdaf099b..4843109f3fd 100644 --- a/include/numerics/petsc_vector.h +++ b/include/numerics/petsc_vector.h @@ -349,7 +349,7 @@ class PetscVector final : public NumericVector virtual std::unique_ptr> get_subvector(const std::vector & rows) override; - virtual void restore_subvector(std::unique_ptr> && subvector, + virtual void restore_subvector(std::unique_ptr> subvector, const std::vector & rows) override; private: diff --git a/src/numerics/petsc_vector.C b/src/numerics/petsc_vector.C index 53604aa28cb..43de8c3f2f8 100644 --- a/src/numerics/petsc_vector.C +++ b/src/numerics/petsc_vector.C @@ -1433,7 +1433,7 @@ PetscVector::get_subvector(const std::vector & rows) template void -PetscVector::restore_subvector(std::unique_ptr> && subvector, +PetscVector::restore_subvector(std::unique_ptr> subvector, const std::vector & rows) { auto * const petsc_subvector = cast_ptr *>(subvector.get());