]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Fix swap for PETSc vector base
authorStefano Zampini <stefano.zampini@gmail.com>
Mon, 16 Jan 2023 14:30:59 +0000 (17:30 +0300)
committerStefano Zampini <stefano.zampini@gmail.com>
Fri, 31 Mar 2023 08:56:32 +0000 (09:56 +0100)
source/lac/petsc_vector_base.cc
tests/petsc/copy_to_dealvec_block.cc

index 2d28ec1d9df89986b9734967b1e672e31b9a1998..9282eafc70209851393cc3cc157817ccfc3ccaa3 100644 (file)
@@ -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;
   }
 
 
index 2c4803c80be45ea6bd500acc12d3b8056ce76d8d..c6ede99bb4579d49103f4d4e4ede29bdb7dcda66 100644 (file)
@@ -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;

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.