From: David Wells Date: Mon, 12 Aug 2024 22:24:45 +0000 (-0600) Subject: PETScWrappers::MatrixBase: consolidate the add and set functions. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=853c2cde419ca5730d0356af295707ad2654a457;p=dealii.git PETScWrappers::MatrixBase: consolidate the add and set functions. The only difference is the VectorOperation. --- diff --git a/include/deal.II/lac/petsc_matrix_base.h b/include/deal.II/lac/petsc_matrix_base.h index 4c4fc852c1..8dea3097ab 100644 --- a/include/deal.II/lac/petsc_matrix_base.h +++ b/include/deal.II/lac/petsc_matrix_base.h @@ -1092,6 +1092,18 @@ namespace PETScWrappers // To allow calling protected prepare_add() and prepare_set(). template friend class dealii::BlockMatrixBase; + + + /** + * Internal function wrapping MatSetValues(). + */ + void + add_or_set(const VectorOperation::values &operation, + const size_type row, + const size_type n_cols, + const size_type *col_indices, + const PetscScalar *values, + const bool elide_zero_values); }; @@ -1311,56 +1323,12 @@ namespace PETScWrappers const PetscScalar *values, const bool elide_zero_values) { - prepare_action(VectorOperation::insert); - - const auto petsc_row = static_cast(row); - AssertIntegerConversion(petsc_row, row); - - std::vector column_indices; - column_indices.reserve(n_cols); - std::vector column_values; - column_values.reserve(n_cols); - if (elide_zero_values == false) - { - column_indices.resize(n_cols); - column_values.resize(n_cols); - - for (size_type j = 0; j < n_cols; ++j) - { - AssertIsFinite(values[j]); - column_indices[j] = static_cast(col_indices[j]); - AssertIntegerConversion(column_indices[j], col_indices[j]); - column_values[j] = values[j]; - } - } - else - { - // Otherwise, extract nonzero values in each row and get the - // respective index. - for (size_type j = 0; j < n_cols; ++j) - { - const PetscScalar value = values[j]; - AssertIsFinite(value); - if (value != PetscScalar()) - { - column_indices.push_back(static_cast(col_indices[j])); - AssertIntegerConversion(column_indices.back(), col_indices[j]); - column_values.push_back(value); - } - } - } - - const auto petsc_n_columns = static_cast(column_indices.size()); - AssertIntegerConversion(petsc_n_columns, column_indices.size()); - - const PetscErrorCode ierr = MatSetValues(matrix, - 1, - &petsc_row, - petsc_n_columns, - column_indices.data(), - column_values.data(), - INSERT_VALUES); - AssertThrow(ierr == 0, ExcPETScError(ierr)); + add_or_set(VectorOperation::insert, + row, + n_cols, + col_indices, + values, + elide_zero_values); } @@ -1452,12 +1420,30 @@ namespace PETScWrappers const bool elide_zero_values, const bool /*col_indices_are_sorted*/) { - prepare_action(VectorOperation::add); + add_or_set(VectorOperation::add, + row, + n_cols, + col_indices, + values, + elide_zero_values); + } + + + + inline void + MatrixBase::add_or_set(const VectorOperation::values &operation, + const size_type row, + const size_type n_cols, + const size_type *col_indices, + const PetscScalar *values, + const bool elide_zero_values) + { + prepare_action(operation); const auto petsc_row = static_cast(row); AssertIntegerConversion(petsc_row, row); - std::vector column_indices; + std::vector column_indices; column_indices.reserve(n_cols); std::vector column_values; column_values.reserve(n_cols); @@ -1494,13 +1480,18 @@ namespace PETScWrappers const auto petsc_n_columns = static_cast(column_indices.size()); AssertIntegerConversion(petsc_n_columns, column_indices.size()); - const PetscErrorCode ierr = MatSetValues(matrix, - 1, - &petsc_row, - petsc_n_columns, - column_indices.data(), - column_values.data(), - ADD_VALUES); + Assert(operation == VectorOperation::insert || + operation == VectorOperation::add, + ExcInternalError()); + const PetscErrorCode ierr = + MatSetValues(matrix, + 1, + &petsc_row, + petsc_n_columns, + column_indices.data(), + column_values.data(), + operation == VectorOperation::insert ? INSERT_VALUES : + ADD_VALUES); AssertThrow(ierr == 0, ExcPETScError(ierr)); }