From: Stefano Zampini Date: Mon, 16 Jan 2023 14:30:59 +0000 (+0300) Subject: Fix swap for PETSc vector base X-Git-Tag: v9.5.0-rc1~369^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fa46fb8b44af0d84642502dbfaa1e3f6ee97fa37;p=dealii.git Fix swap for PETSc vector base --- diff --git a/source/lac/petsc_vector_base.cc b/source/lac/petsc_vector_base.cc index 2d28ec1d9d..9282eafc70 100644 --- a/source/lac/petsc_vector_base.cc +++ b/source/lac/petsc_vector_base.cc @@ -1021,8 +1021,13 @@ namespace PETScWrappers void VectorBase::swap(VectorBase &v) { - const PetscErrorCode ierr = VecSwap(vector, v.vector); - AssertThrow(ierr == 0, ExcPETScError(ierr)); + std::swap(this->vector, v.vector); + std::swap(this->ghosted, v.ghosted); + std::swap(this->last_action, v.last_action); + // missing swap for IndexSet + IndexSet t(this->ghost_indices); + this->ghost_indices = v.ghost_indices; + v.ghost_indices = t; } diff --git a/tests/petsc/copy_to_dealvec_block.cc b/tests/petsc/copy_to_dealvec_block.cc index 2c4803c80b..c6ede99bb4 100644 --- a/tests/petsc/copy_to_dealvec_block.cc +++ b/tests/petsc/copy_to_dealvec_block.cc @@ -75,7 +75,23 @@ test() Assert(v_one.ghost_elements() == v_one_from_vec.ghost_elements(), ExcInternalError()); - + // Test swap + IndexSet local_active_2(numproc * 4); + local_active_2.add_range(myid * 4, myid * 4 + 4); + PETScWrappers::MPI::Vector vs1(local_active, MPI_COMM_WORLD); + PETScWrappers::MPI::Vector vs2(local_active_2, + local_relevant, + MPI_COMM_WORLD); + vs1.swap(vs2); + Assert(vs1.size() == numproc * 4, ExcInternalError()); + Assert(vs2.size() == numproc * 2, ExcInternalError()); + Assert(vs1.locally_owned_size() == 4, ExcInternalError()); + Assert(vs2.locally_owned_size() == 2, ExcInternalError()); + Assert(vs1.has_ghost_elements() == true, ExcInternalError()); + Assert(vs2.has_ghost_elements() == false, ExcInternalError()); + Assert(vs1.ghost_elements() == v_one.ghost_elements(), ExcInternalError()); + + // Now BlockVectors PETScWrappers::MPI::BlockVector vb, v; vb.reinit(2); v.reinit(2); @@ -139,6 +155,13 @@ test() ExcInternalError()); } + // Test swap + auto old_v_vb2 = vb2.petsc_vector(); + auto old_v_vb = vb.petsc_vector(); + vb.swap(vb2); + Assert(vb.petsc_vector() == old_v_vb2, ExcInternalError()); + Assert(vb2.petsc_vector() == old_v_vb, ExcInternalError()); + // done if (myid == 0) deallog << "OK" << std::endl;