From 97ec2ef545f2c173f4a77cfbd70326584aab28e8 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Sun, 31 Dec 2023 18:46:15 -0700 Subject: [PATCH] Optimize setting a matrix to zero. --- source/lac/trilinos_sparse_matrix.cc | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/source/lac/trilinos_sparse_matrix.cc b/source/lac/trilinos_sparse_matrix.cc index b9c74f49b4..231e9e3f83 100644 --- a/source/lac/trilinos_sparse_matrix.cc +++ b/source/lac/trilinos_sparse_matrix.cc @@ -1761,14 +1761,23 @@ namespace TrilinosWrappers SparseMatrix & SparseMatrix::operator=(const double d) { + (void)d; Assert(d == 0, ExcScalarAssignmentOnlyForZeroValue()); compress(VectorOperation::unknown); // TODO: why do we do this? Should we // not check for is_compressed? - const int ierr = matrix->PutScalar(d); + // As checked above, we are only allowed to use d==0.0, so pass + // a constant zero (instead of a run-time value 'd' that *happens* to + // have a zero value) to the underlying class in hopes that the compiler + // can optimize this somehow. + const int ierr = matrix->PutScalar(/*d=*/0.0); AssertThrow(ierr == 0, ExcTrilinosError(ierr)); + if (nonlocal_matrix.get() != nullptr) - nonlocal_matrix->PutScalar(d); + { + const int ierr = nonlocal_matrix->PutScalar(/*d=*/0.0); + AssertThrow(ierr == 0, ExcTrilinosError(ierr)); + } return *this; } -- 2.39.5