From: David Wells Date: Sun, 11 Aug 2024 20:12:04 +0000 (-0600) Subject: PETScWrappers::SparseMatrix: add index conversion checks. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1deefdbc7662d1af9f6dd2b3774647140c311734;p=dealii.git PETScWrappers::SparseMatrix: add index conversion checks. --- diff --git a/source/lac/petsc_sparse_matrix.cc b/source/lac/petsc_sparse_matrix.cc index 252de3ebb7..f1357b7cba 100644 --- a/source/lac/petsc_sparse_matrix.cc +++ b/source/lac/petsc_sparse_matrix.cc @@ -151,17 +151,11 @@ namespace PETScWrappers const std::vector &row_lengths, const bool is_symmetric) { - Assert(row_lengths.size() == m, - ExcDimensionMismatch(row_lengths.size(), m)); - - // use the call sequence indicating a - // maximal number of elements for each - // row individually. annoyingly, we - // always use unsigned ints for cases - // like this, while PETSc wants to see - // signed integers. so we have to - // convert, unless we want to play dirty - // tricks with conversions of pointers + AssertDimension(row_lengths.size(), m); + + for (const auto &row_length : row_lengths) + AssertThrowIntegerConversion(static_cast(row_length), + row_length); const std::vector int_row_lengths(row_lengths.begin(), row_lengths.end()); @@ -183,6 +177,13 @@ namespace PETScWrappers SparseMatrix::do_reinit(const SparsityPatternType &sparsity_pattern, const bool preset_nonzero_locations) { + // If the sparsity pattern's dimensions can be converted to PetscInts then + // the rest of the conversions will succeed + AssertIntegerConversion(static_cast(sparsity_pattern.n_rows()), + sparsity_pattern.n_rows()); + AssertIntegerConversion(static_cast(sparsity_pattern.n_cols()), + sparsity_pattern.n_cols()); + std::vector row_lengths(sparsity_pattern.n_rows()); for (size_type i = 0; i < sparsity_pattern.n_rows(); ++i) row_lengths[i] = sparsity_pattern.row_length(i); @@ -212,14 +213,18 @@ namespace PETScWrappers for (size_type i = 0; i < sparsity_pattern.n_rows(); ++i) { row_entries.resize(row_lengths[i]); - row_values.resize(row_lengths[i], 0.0); + row_values.resize(row_lengths[i]); for (size_type j = 0; j < row_lengths[i]; ++j) - row_entries[j] = sparsity_pattern.column_number(i, j); + { + const auto petsc_j = + static_cast(sparsity_pattern.column_number(i, j)); + row_entries[j] = petsc_j; + } - const PetscInt int_row = i; + const auto petsc_i = static_cast(i); const PetscErrorCode ierr = MatSetValues(matrix, 1, - &int_row, + &petsc_i, row_lengths[i], row_entries.data(), row_values.data(),