]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Refactor out MatSetOption in a few places.
authorDavid Wells <wellsd2@rpi.edu>
Sat, 9 Jul 2016 16:41:24 +0000 (12:41 -0400)
committerDavid Wells <wellsd2@rpi.edu>
Tue, 12 Jul 2016 01:37:39 +0000 (21:37 -0400)
include/deal.II/lac/petsc_compatibility.h
source/lac/petsc_parallel_sparse_matrix.cc
source/lac/petsc_sparse_matrix.cc

index 9b7b081d2a67348c78d5bb4b9a6293aadbd35409..3968c2ae98d04fe9ed87930f4993d2be04b3a4bf 100644 (file)
@@ -21,6 +21,7 @@
 #define dealii__petsc_compatibility_h
 
 #include <deal.II/base/config.h>
+#include <deal.II/lac/exceptions.h>
 
 #ifdef DEAL_II_WITH_PETSC
 
@@ -99,6 +100,67 @@ namespace PETScWrappers
     return KSPDestroy (&krylov_solver);
 #endif
   }
+
+
+
+  /**
+   * Set a PETSc matrix option. This function wraps MatSetOption with a
+   * version check.
+   *
+   * @warning The argument option_value is ignored in versions of PETSc
+   * before 3.0.0 since the corresponding function did not take this argument.
+   */
+  inline void set_matrix_option (Mat &matrix,
+                                 const MatOption option_name,
+                                 const PetscBooleanType option_value = PETSC_FALSE)
+  {
+#if DEAL_II_PETSC_VERSION_LT(3,0,0)
+    const int ierr = MatSetOption (matrix, option_name);
+#else
+    const int ierr = MatSetOption (matrix, option_name, option_value);
+#endif
+
+    AssertThrow (ierr == 0, ExcPETScError(ierr));
+  }
+
+
+
+  /**
+   * Tell PETSc that we are not planning on adding new entries to the
+   * matrix. Generate errors in debug mode.
+   */
+  inline void close_matrix (Mat &matrix)
+  {
+#if DEAL_II_PETSC_VERSION_LT(3, 0, 0)
+#  ifdef DEBUG
+    set_matrix_option (matrix, MAT_NEW_NONZERO_LOCATION_ERR);
+#  else
+    set_matrix_option (matrix, MAT_NO_NEW_NONZERO_LOCATIONS);
+#  endif
+#else
+#  ifdef DEBUG
+    set_matrix_option (matrix, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE);
+#  else
+    set_matrix_option (matrix, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
+#  endif
+#endif
+  }
+
+
+
+  /**
+   * Tell PETSc to keep the SparsityPattern entries even if we delete a
+   * row with clear_rows() which calls MatZeroRows(). Otherwise one can
+   * not write into that row afterwards.
+   */
+  inline void set_keep_zero_rows (Mat &matrix)
+  {
+#if DEAL_II_PETSC_VERSION_LT(3, 1, 0)
+    set_matrix_option (matrix, MAT_KEEP_ZEROED_ROWS, PETSC_TRUE);
+#else
+    set_matrix_option (matrix, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
+#endif
+  }
 }
 
 DEAL_II_NAMESPACE_CLOSE
index 5b706537d591b1aa7d3007f06157a8366d732227..7d3897a940efecba9cf7fd9ffb0ae03a8c4085ae 100644 (file)
@@ -30,7 +30,6 @@ namespace PETScWrappers
 {
   namespace MPI
   {
-
     SparseMatrix::SparseMatrix ()
     {
       // just like for vectors: since we
@@ -257,22 +256,14 @@ namespace PETScWrappers
                         &matrix);
       AssertThrow (ierr == 0, ExcPETScError(ierr));
 
-      ierr = MatSetOption (matrix, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
+      set_matrix_option (matrix, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
 #endif
       AssertThrow (ierr == 0, ExcPETScError(ierr));
 
       // set symmetric flag, if so requested
       if (is_symmetric == true)
         {
-#if DEAL_II_PETSC_VERSION_LT(3,0,0)
-          const int ierr
-            = MatSetOption (matrix, MAT_SYMMETRIC);
-#else
-          const int ierr
-            = MatSetOption (matrix, MAT_SYMMETRIC, PETSC_TRUE);
-#endif
-
-          AssertThrow (ierr == 0, ExcPETScError(ierr));
+          set_matrix_option (matrix, MAT_SYMMETRIC, PETSC_TRUE);
         }
     }
 
@@ -338,23 +329,14 @@ namespace PETScWrappers
       AssertThrow (ierr == 0, ExcPETScError(ierr));
 
 //TODO: Sometimes the actual number of nonzero entries allocated is greater than the number of nonzero entries, which petsc will complain about unless explicitly disabled with MatSetOption. There is probably a way to prevent a different number nonzero elements being allocated in the first place. (See also previous TODO).
-
-      ierr = MatSetOption (matrix, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
+      set_matrix_option (matrix, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
 #endif
       AssertThrow (ierr == 0, ExcPETScError(ierr));
 
       // set symmetric flag, if so requested
       if (is_symmetric == true)
         {
-#if DEAL_II_PETSC_VERSION_LT(3,0,0)
-          const int ierr
-            = MatSetOption (matrix, MAT_SYMMETRIC);
-#else
-          const int ierr
-            = MatSetOption (matrix, MAT_SYMMETRIC, PETSC_TRUE);
-#endif
-
-          AssertThrow (ierr == 0, ExcPETScError(ierr));
+          set_matrix_option (matrix, MAT_SYMMETRIC, PETSC_TRUE);
         }
     }
 
@@ -491,50 +473,9 @@ namespace PETScWrappers
       compress (dealii::VectorOperation::insert);
 
       {
-
-        // Tell PETSc that we are not
-        // planning on adding new entries
-        // to the matrix. Generate errors
-        // in debug mode.
-        int ierr;
-#if DEAL_II_PETSC_VERSION_LT(3,0,0)
-#ifdef DEBUG
-        ierr = MatSetOption (matrix, MAT_NEW_NONZERO_LOCATION_ERR);
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
-#else
-        ierr = MatSetOption (matrix, MAT_NO_NEW_NONZERO_LOCATIONS);
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
-#endif
-#else
-#ifdef DEBUG
-        ierr = MatSetOption (matrix, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE);
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
-#else
-        ierr = MatSetOption (matrix, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
-#endif
-#endif
-
-        // Tell PETSc to keep the
-        // SparsityPattern entries even if
-        // we delete a row with
-        // clear_rows() which calls
-        // MatZeroRows(). Otherwise one can
-        // not write into that row
-        // afterwards.
-#if DEAL_II_PETSC_VERSION_LT(3,0,0)
-        ierr = MatSetOption (matrix, MAT_KEEP_ZEROED_ROWS);
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
-#elif DEAL_II_PETSC_VERSION_LT(3,1,0)
-        ierr = MatSetOption (matrix, MAT_KEEP_ZEROED_ROWS, PETSC_TRUE);
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
-#else
-        ierr = MatSetOption (matrix, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
-#endif
-
+        close_matrix (matrix);
+        set_keep_zero_rows (matrix);
       }
-
     }
 
 
@@ -741,48 +682,8 @@ namespace PETScWrappers
           *this = 0;
 #endif // version <=2.3.3
 
-
-          // Tell PETSc that we are not
-          // planning on adding new entries
-          // to the matrix. Generate errors
-          // in debug mode.
-          int ierr;
-#if DEAL_II_PETSC_VERSION_LT(3,0,0)
-#ifdef DEBUG
-          ierr = MatSetOption (matrix, MAT_NEW_NONZERO_LOCATION_ERR);
-          AssertThrow (ierr == 0, ExcPETScError(ierr));
-#else
-          ierr = MatSetOption (matrix, MAT_NO_NEW_NONZERO_LOCATIONS);
-          AssertThrow (ierr == 0, ExcPETScError(ierr));
-#endif
-#else
-#ifdef DEBUG
-          ierr = MatSetOption (matrix, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE);
-          AssertThrow (ierr == 0, ExcPETScError(ierr));
-#else
-          ierr = MatSetOption (matrix, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
-          AssertThrow (ierr == 0, ExcPETScError(ierr));
-#endif
-#endif
-
-          // Tell PETSc to keep the
-          // SparsityPattern entries even if
-          // we delete a row with
-          // clear_rows() which calls
-          // MatZeroRows(). Otherwise one can
-          // not write into that row
-          // afterwards.
-#if DEAL_II_PETSC_VERSION_LT(3,0,0)
-          ierr = MatSetOption (matrix, MAT_KEEP_ZEROED_ROWS);
-          AssertThrow (ierr == 0, ExcPETScError(ierr));
-#elif DEAL_II_PETSC_VERSION_LT(3,1,0)
-          ierr = MatSetOption (matrix, MAT_KEEP_ZEROED_ROWS, PETSC_TRUE);
-          AssertThrow (ierr == 0, ExcPETScError(ierr));
-#else
-          ierr = MatSetOption (matrix, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
-          AssertThrow (ierr == 0, ExcPETScError(ierr));
-#endif
-
+          close_matrix (matrix);
+          set_keep_zero_rows(matrix);
         }
     }
 
index 5e4e12e1bc163c0f746304d97252431a233043ae..3efacaf7ea0bd36f0080cb64141e7da73b6af343 100644 (file)
@@ -153,15 +153,7 @@ namespace PETScWrappers
     // set symmetric flag, if so requested
     if (is_symmetric == true)
       {
-#if DEAL_II_PETSC_VERSION_LT(3,0,0)
-        const int ierr
-          = MatSetOption (matrix, MAT_SYMMETRIC);
-#else
-        const int ierr
-          = MatSetOption (matrix, MAT_SYMMETRIC, PETSC_TRUE);
-#endif
-
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
+        set_matrix_option (matrix, MAT_SYMMETRIC, PETSC_TRUE);
       }
   }
 
@@ -195,15 +187,7 @@ namespace PETScWrappers
     // set symmetric flag, if so requested
     if (is_symmetric == true)
       {
-#if DEAL_II_PETSC_VERSION_LT(3,0,0)
-        const int ierr
-          = MatSetOption (matrix, MAT_SYMMETRIC);
-#else
-        const int ierr
-          = MatSetOption (matrix, MAT_SYMMETRIC, PETSC_TRUE);
-#endif
-
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
+        set_matrix_option(matrix, MAT_SYMMETRIC, PETSC_TRUE);
       }
   }
 
@@ -253,48 +237,8 @@ namespace PETScWrappers
           }
         compress (VectorOperation::insert);
 
-
-        // Tell PETSc that we are not
-        // planning on adding new entries
-        // to the matrix. Generate errors
-        // in debug mode.
-        int ierr;
-#if DEAL_II_PETSC_VERSION_LT(3,0,0)
-#ifdef DEBUG
-        ierr = MatSetOption (matrix, MAT_NEW_NONZERO_LOCATION_ERR);
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
-#else
-        ierr = MatSetOption (matrix, MAT_NO_NEW_NONZERO_LOCATIONS);
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
-#endif
-#else
-#ifdef DEBUG
-        ierr = MatSetOption (matrix, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE);
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
-#else
-        ierr = MatSetOption (matrix, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
-#endif
-#endif
-
-        // Tell PETSc to keep the
-        // SparsityPattern entries even if
-        // we delete a row with
-        // clear_rows() which calls
-        // MatZeroRows(). Otherwise one can
-        // not write into that row
-        // afterwards.
-#if DEAL_II_PETSC_VERSION_LT(3,0,0)
-        ierr = MatSetOption (matrix, MAT_KEEP_ZEROED_ROWS);
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
-#elif DEAL_II_PETSC_VERSION_LT(3,1,0)
-        ierr = MatSetOption (matrix, MAT_KEEP_ZEROED_ROWS, PETSC_TRUE);
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
-#else
-        ierr = MatSetOption (matrix, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
-#endif
-
+        close_matrix (matrix);
+        set_keep_zero_rows (matrix);
       }
   }
 

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.