From cfa5ba8a9b6d8d060c95a56e3931a1fc661e10bb Mon Sep 17 00:00:00 2001 From: Martin Kronbichler Date: Wed, 9 Aug 2023 09:30:11 +0200 Subject: [PATCH] Making hanging constraints: Reduce number of memory allocation --- .../lac/affine_constraints.templates.h | 1 + source/dofs/dof_tools_constraints.cc | 25 +++++++++++-------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/include/deal.II/lac/affine_constraints.templates.h b/include/deal.II/lac/affine_constraints.templates.h index 7f37bbdac1..1b2ad3e379 100644 --- a/include/deal.II/lac/affine_constraints.templates.h +++ b/include/deal.II/lac/affine_constraints.templates.h @@ -573,6 +573,7 @@ AffineConstraints::add_entries( // // in any case: skip this entry if an entry for this column already // exists, since we don't want to enter it twice + line.entries.reserve(line.entries.size() + col_weight_pairs.size()); for (const std::pair &col_weight_pair : col_weight_pairs) { Assert(constrained_dof_index != col_weight_pair.first, diff --git a/source/dofs/dof_tools_constraints.cc b/source/dofs/dof_tools_constraints.cc index b25e939cd4..bb837dbc0c 100644 --- a/source/dofs/dof_tools_constraints.cc +++ b/source/dofs/dof_tools_constraints.cc @@ -509,7 +509,10 @@ namespace DoFTools Assert(primary_dofs[col] != numbers::invalid_dof_index, ExcInternalError()); - + std::vector< + std::pair::size_type, number2>> + entries; + entries.reserve(n_primary_dofs); for (unsigned int row = 0; row != n_dependent_dofs; ++row) if (constraints.is_constrained(dependent_dofs[row]) == false) { @@ -558,16 +561,18 @@ namespace DoFTools // then enter those constraints that are larger than // 1e-14*abs_sum. everything else probably originated from // inexact inversion of matrices and similar effects. having - // those constraints in here will only lead to problems - // because it makes sparsity patterns fuller than necessary - // without producing any significant effect - constraints.add_line(dependent_dofs[row]); + // those constraints in here will only lead to problems because + // it makes sparsity patterns fuller than necessary without + // producing any significant effect. do this in two steps, first + // filling a vector and then adding to the constraints in order + // to reduce the number of memory allocations. + entries.clear(); for (unsigned int i = 0; i < n_primary_dofs; ++i) - if ((face_constraints(row, i) != 0) && - (std::fabs(face_constraints(row, i)) >= 1e-14 * abs_sum)) - constraints.add_entry(dependent_dofs[row], - primary_dofs[i], - face_constraints(row, i)); + if (std::fabs(face_constraints(row, i)) >= 1e-14 * abs_sum) + entries.emplace_back(primary_dofs[i], + face_constraints(row, i)); + constraints.add_line(dependent_dofs[row]); + constraints.add_entries(dependent_dofs[row], entries); constraints.set_inhomogeneity(dependent_dofs[row], 0.); } } -- 2.39.5