]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Fix petsc/copy_to_dealvec_block 9910/head
authorDaniel Arndt <arndtd@ornl.gov>
Thu, 16 Apr 2020 18:20:26 +0000 (14:20 -0400)
committerDaniel Arndt <arndtd@ornl.gov>
Thu, 16 Apr 2020 18:20:55 +0000 (14:20 -0400)
include/deal.II/lac/la_parallel_block_vector.templates.h
include/deal.II/lac/la_parallel_vector.templates.h

index 59935321b0288d7b181bd0c1a12e3c33e45c1ff5..7cfff39de5c2de393e2726f74ccc49ab39d6fa07 100644 (file)
@@ -213,6 +213,38 @@ namespace LinearAlgebra
 
 #ifdef DEAL_II_WITH_PETSC
 
+    namespace petsc_helpers
+    {
+      template <typename PETSC_Number, typename Number>
+      void
+      copy_petsc_vector(const PETSC_Number *petsc_start_ptr,
+                        const PETSC_Number *petsc_end_ptr,
+                        Number *            ptr)
+      {
+        std::copy(petsc_start_ptr, petsc_end_ptr, ptr);
+      }
+
+      template <typename PETSC_Number, typename Number>
+      void
+      copy_petsc_vector(const std::complex<PETSC_Number> *petsc_start_ptr,
+                        const std::complex<PETSC_Number> *petsc_end_ptr,
+                        std::complex<Number> *            ptr)
+      {
+        std::copy(petsc_start_ptr, petsc_end_ptr, ptr);
+      }
+
+      template <typename PETSC_Number, typename Number>
+      void
+      copy_petsc_vector(const std::complex<PETSC_Number> * /*petsc_start_ptr*/,
+                        const std::complex<PETSC_Number> * /*petsc_end_ptr*/,
+                        Number * /*ptr*/)
+      {
+        AssertThrow(false, ExcMessage("Tried to copy complex -> real"));
+      }
+    } // namespace petsc_helpers
+
+
+
     template <typename Number>
     BlockVector<Number> &
     BlockVector<Number>::
@@ -221,14 +253,34 @@ namespace LinearAlgebra
       AssertDimension(this->n_blocks(), petsc_vec.n_blocks());
       for (unsigned int i = 0; i < this->n_blocks(); ++i)
         {
-          const auto &partitioner  = this->block(i).get_partitioner();
-          IndexSet    combined_set = partitioner->locally_owned_range();
-          combined_set.add_indices(partitioner->ghost_indices());
-          ReadWriteVector<Number> rw_vector(combined_set);
-          rw_vector.import(petsc_vec.block(i), VectorOperation::insert);
-          this->block(i).import(rw_vector, VectorOperation::insert);
-
-          if (this->block(i).has_ghost_elements() ||
+          // We would like to use the same compact infrastructure as for the
+          // Trilinos vector below, but the interface through ReadWriteVector
+          // does not support overlapping (ghosted) PETSc vectors, which we need
+          // for backward compatibility.
+
+          Assert(petsc_vec.block(i).locally_owned_elements() ==
+                   this->block(i).locally_owned_elements(),
+                 StandardExceptions::ExcInvalidState());
+
+          // get a representation of the vector and copy it
+          PetscScalar *  start_ptr;
+          PetscErrorCode ierr =
+            VecGetArray(static_cast<const Vec &>(petsc_vec.block(i)),
+                        &start_ptr);
+          AssertThrow(ierr == 0, ExcPETScError(ierr));
+
+          const size_type vec_size = this->block(i).local_size();
+          petsc_helpers::copy_petsc_vector(start_ptr,
+                                           start_ptr + vec_size,
+                                           this->block(i).begin());
+
+          // restore the representation of the vector
+          ierr = VecRestoreArray(static_cast<const Vec &>(petsc_vec.block(i)),
+                                 &start_ptr);
+          AssertThrow(ierr == 0, ExcPETScError(ierr));
+
+          // spread ghost values between processes?
+          if (this->block(i).vector_is_ghosted ||
               petsc_vec.block(i).has_ghost_elements())
             this->block(i).update_ghost_values();
         }
index 0e336338251a146de92353973f8d0009768d998a..ad6d4f41f0896f5e059943da3ed35411876bc5d3 100644 (file)
@@ -749,42 +749,6 @@ namespace LinearAlgebra
 
 
 
-#ifdef DEAL_II_WITH_PETSC
-
-    namespace petsc_helpers
-    {
-      template <typename PETSC_Number, typename Number>
-      void
-      copy_petsc_vector(const PETSC_Number *petsc_start_ptr,
-                        const PETSC_Number *petsc_end_ptr,
-                        Number *            ptr)
-      {
-        std::copy(petsc_start_ptr, petsc_end_ptr, ptr);
-      }
-
-      template <typename PETSC_Number, typename Number>
-      void
-      copy_petsc_vector(const std::complex<PETSC_Number> *petsc_start_ptr,
-                        const std::complex<PETSC_Number> *petsc_end_ptr,
-                        std::complex<Number> *            ptr)
-      {
-        std::copy(petsc_start_ptr, petsc_end_ptr, ptr);
-      }
-
-      template <typename PETSC_Number, typename Number>
-      void
-      copy_petsc_vector(const std::complex<PETSC_Number> * /*petsc_start_ptr*/,
-                        const std::complex<PETSC_Number> * /*petsc_end_ptr*/,
-                        Number * /*ptr*/)
-      {
-        AssertThrow(false, ExcMessage("Tried to copy complex -> real"));
-      }
-    } // namespace petsc_helpers
-
-#endif
-
-
-
     template <typename Number, typename MemorySpaceType>
     void
     Vector<Number, MemorySpaceType>::compress(

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.