From b2b19d9e18487ffb668b73e48ab9b87323dde1db Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Wed, 10 Jan 2024 15:21:28 -0700 Subject: [PATCH] Avoid using two temporary vectors when not necessary. --- .../numerics/data_out_dof_data.templates.h | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/include/deal.II/numerics/data_out_dof_data.templates.h b/include/deal.II/numerics/data_out_dof_data.templates.h index 32ce2c2212..92e019b511 100644 --- a/include/deal.II/numerics/data_out_dof_data.templates.h +++ b/include/deal.II/numerics/data_out_dof_data.templates.h @@ -803,15 +803,33 @@ namespace internal const VectorType &src, LinearAlgebra::distributed::Vector &dst) { - LinearAlgebra::ReadWriteVector 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) + { + LinearAlgebra::ReadWriteVector + temp; + temp.reinit(src.locally_owned_elements()); + temp.import_elements(src, VectorOperation::insert); - LinearAlgebra::ReadWriteVector 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 + temp; + temp.reinit(src.locally_owned_elements()); + temp.import_elements(src, VectorOperation::insert); - dst.import_elements(temp2, VectorOperation::insert); + LinearAlgebra::ReadWriteVector temp2; + temp2.reinit(temp, true); + temp2 = temp; + + dst.import_elements(temp2, VectorOperation::insert); + } } #ifdef DEAL_II_WITH_TRILINOS -- 2.39.5