]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Making hanging constraints: Reduce number of memory allocation 15864/head
authorMartin Kronbichler <martin.kronbichler@uni-a.de>
Wed, 9 Aug 2023 07:30:11 +0000 (09:30 +0200)
committerMartin Kronbichler <martin.kronbichler@uni-a.de>
Wed, 9 Aug 2023 07:30:11 +0000 (09:30 +0200)
include/deal.II/lac/affine_constraints.templates.h
source/dofs/dof_tools_constraints.cc

index 7f37bbdac14160aed992253eb28d6aadcf854d32..1b2ad3e379b881d24c1909f467417287ebfb468b 100644 (file)
@@ -573,6 +573,7 @@ AffineConstraints<number>::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<size_type, number> &col_weight_pair : col_weight_pairs)
     {
       Assert(constrained_dof_index != col_weight_pair.first,
index b25e939cd49870f76fdf7b9430b4d78483f86aaa..bb837dbc0cb87ebd9980104db3c06120111e9105 100644 (file)
@@ -509,7 +509,10 @@ namespace DoFTools
           Assert(primary_dofs[col] != numbers::invalid_dof_index,
                  ExcInternalError());
 
-
+        std::vector<
+          std::pair<typename AffineConstraints<number2>::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.);
             }
       }

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.