From: Wolfgang Bangerth Date: Mon, 1 Jan 2024 01:46:15 +0000 (-0700) Subject: Optimize setting a matrix to zero. X-Git-Tag: relicensing~210^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=97ec2ef545f2c173f4a77cfbd70326584aab28e8;p=dealii.git Optimize setting a matrix to zero. --- 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; }