]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Avoid using two temporary vectors when not necessary. 16454/head
authorWolfgang Bangerth <bangerth@colostate.edu>
Wed, 10 Jan 2024 22:21:28 +0000 (15:21 -0700)
committerWolfgang Bangerth <bangerth@colostate.edu>
Wed, 10 Jan 2024 22:21:36 +0000 (15:21 -0700)
include/deal.II/numerics/data_out_dof_data.templates.h

index 32ce2c2212331ed044531f1c42b8f5263ddba314..92e019b5118e54a5f1fd021fe1cc063c1e60a7bb 100644 (file)
@@ -803,15 +803,33 @@ namespace internal
         const VectorType                           &src,
         LinearAlgebra::distributed::Vector<Number> &dst)
       {
-        LinearAlgebra::ReadWriteVector<typename VectorType::value_type> temp;
-        temp.reinit(src.locally_owned_elements());
-        temp.import_elements(src, VectorOperation::insert);
+        // If source and destination vector have the same underlying scalar,
+        // we can directly import elements by using only one temporary vector:
+        if constexpr (std::is_same_v<typename VectorType::value_type, Number>)
+          {
+            LinearAlgebra::ReadWriteVector<typename VectorType::value_type>
+              temp;
+            temp.reinit(src.locally_owned_elements());
+            temp.import_elements(src, VectorOperation::insert);
 
-        LinearAlgebra::ReadWriteVector<Number> temp2;
-        temp2.reinit(temp, true);
-        temp2 = temp;
+            dst.import_elements(temp, VectorOperation::insert);
+          }
+        else
+          // The source and destination vector have different scalar types. We
+          // need to split the parallel import and local copy operations into
+          // two phases
+          {
+            LinearAlgebra::ReadWriteVector<typename VectorType::value_type>
+              temp;
+            temp.reinit(src.locally_owned_elements());
+            temp.import_elements(src, VectorOperation::insert);
 
-        dst.import_elements(temp2, VectorOperation::insert);
+            LinearAlgebra::ReadWriteVector<Number> temp2;
+            temp2.reinit(temp, true);
+            temp2 = temp;
+
+            dst.import_elements(temp2, VectorOperation::insert);
+          }
       }
 
 #ifdef DEAL_II_WITH_TRILINOS

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.