]> https://gitweb.dealii.org/ - dealii.git/commitdiff
PETScWrappers::MPI::SparseMatrix: add index conversion checks.
authorDavid Wells <drwells@email.unc.edu>
Sun, 11 Aug 2024 20:08:48 +0000 (14:08 -0600)
committerDavid Wells <drwells@email.unc.edu>
Mon, 12 Aug 2024 02:42:37 +0000 (20:42 -0600)
source/lac/petsc_parallel_sparse_matrix.cc

index 6bd74623a827cfef1b841008298ef51f8d3f7a24..0511b307e17b06f6550d2eac5ba0a6aa2406e34f 100644 (file)
@@ -185,6 +185,15 @@ namespace PETScWrappers
                             const IndexSet            &local_columns,
                             const SparsityPatternType &sparsity_pattern)
     {
+      // If the sparsity pattern's dimensions can be converted to PetscInts then
+      // the rest of the conversions will succeed
+      AssertThrowIntegerConversion(static_cast<PetscInt>(
+                                     sparsity_pattern.n_rows()),
+                                   sparsity_pattern.n_rows());
+      AssertThrowIntegerConversion(static_cast<PetscInt>(
+                                     sparsity_pattern.n_cols()),
+                                   sparsity_pattern.n_cols());
+
       Assert(sparsity_pattern.n_rows() == local_rows.size(),
              ExcMessage(
                "SparsityPattern and IndexSet have different number of rows"));
@@ -272,9 +281,9 @@ namespace PETScWrappers
           // dummy entry at the end to make
           // sure petsc doesn't read past the
           // end
-          std::vector<PetscInt>
-
-            rowstart_in_window(local_row_end - local_row_start + 1, 0),
+          std::vector<PetscInt> rowstart_in_window(local_row_end -
+                                                     local_row_start + 1,
+                                                   0),
             colnums_in_window;
           {
             unsigned int n_cols = 0;
@@ -351,6 +360,14 @@ namespace PETScWrappers
                                   local_columns_per_process.size()));
       Assert(this_process < local_rows_per_process.size(), ExcInternalError());
       assert_is_compressed();
+      // If the sparsity pattern's dimensions can be converted to PetscInts then
+      // the rest of the conversions will succeed
+      AssertThrowIntegerConversion(static_cast<PetscInt>(
+                                     sparsity_pattern.n_rows()),
+                                   sparsity_pattern.n_rows());
+      AssertThrowIntegerConversion(static_cast<PetscInt>(
+                                     sparsity_pattern.n_cols()),
+                                   sparsity_pattern.n_cols());
 
       // for each row that we own locally, we
       // have to count how many of the
@@ -410,17 +427,20 @@ namespace PETScWrappers
           // dummy entry at the end to make
           // sure petsc doesn't read past the
           // end
-          std::vector<PetscInt>
-
-            rowstart_in_window(local_row_end - local_row_start + 1, 0),
+          std::vector<PetscInt> rowstart_in_window(local_row_end -
+                                                     local_row_start + 1,
+                                                   0),
             colnums_in_window;
           {
             size_type n_cols = 0;
             for (size_type i = local_row_start; i < local_row_end; ++i)
               {
                 const size_type row_length = sparsity_pattern.row_length(i);
-                rowstart_in_window[i + 1 - local_row_start] =
+                const auto      row_start =
                   rowstart_in_window[i - local_row_start] + row_length;
+                const auto petsc_row_start = static_cast<PetscInt>(row_start);
+                AssertIntegerConversion(petsc_row_start, row_start);
+                rowstart_in_window[i + 1 - local_row_start] = petsc_row_start;
                 n_cols += row_length;
               }
             colnums_in_window.resize(n_cols + 1, -1);
@@ -435,7 +455,11 @@ namespace PETScWrappers
                      sparsity_pattern.begin(i);
                    p != sparsity_pattern.end(i);
                    ++p, ++ptr)
-                *ptr = p->column();
+                {
+                  const auto petsc_column = static_cast<PetscInt>(p->column());
+                  AssertIntegerConversion(petsc_column, p->column());
+                  *ptr = petsc_column;
+                }
           }
 
 
@@ -471,6 +495,15 @@ namespace PETScWrappers
                             const IndexSet            &local_active_columns,
                             const SparsityPatternType &sparsity_pattern)
     {
+      // If the sparsity pattern's dimensions can be converted to PetscInts then
+      // the rest of the conversions will succeed
+      AssertThrowIntegerConversion(static_cast<PetscInt>(
+                                     sparsity_pattern.n_rows()),
+                                   sparsity_pattern.n_rows());
+      AssertThrowIntegerConversion(static_cast<PetscInt>(
+                                     sparsity_pattern.n_cols()),
+                                   sparsity_pattern.n_cols());
+
 #  if DEAL_II_PETSC_VERSION_GTE(3, 10, 0)
       Assert(sparsity_pattern.n_rows() == local_rows.size(),
              ExcMessage(

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.