]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Wrap multiple versions of PETSc's KSPDestroy.
authorDavid Wells <wellsd2@rpi.edu>
Sat, 9 Jul 2016 15:26:49 +0000 (11:26 -0400)
committerDavid Wells <wellsd2@rpi.edu>
Tue, 12 Jul 2016 00:08:58 +0000 (20:08 -0400)
Like the fix for MatDestroy, this avoids scattering version checks
throughout the library and avoids throwing exceptions in destructors.

include/deal.II/lac/petsc_compatibility.h
source/lac/petsc_solver.cc

index ae5e02945f09635ebe1e18ac2a42f4920fda78ed..9b7b081d2a67348c78d5bb4b9a6293aadbd35409 100644 (file)
@@ -25,6 +25,7 @@
 #ifdef DEAL_II_WITH_PETSC
 
 #include <petscconf.h>
+#include <petscksp.h>
 #include <petscmat.h>
 #include <petscpc.h>
 
@@ -76,6 +77,28 @@ namespace PETScWrappers
     return MatDestroy (&matrix);
 #endif
   }
+
+
+
+  /**
+   * Destroy a Krylov Subspace (KSP) PETSc solver. This function wraps
+   * KSPDestroy with a version check (the signature of this function changed
+   * in PETSc 3.2.0).
+   *
+   * @warning Since the primary intent of this function is to enable RAII
+   * semantics in the PETSc wrappers, this function will not throw an
+   * exception if an error occurs, but instead just returns the error code
+   * given by MatDestroy.
+   */
+  inline PetscErrorCode destroy_krylov_solver (KSP &krylov_solver)
+  {
+    // PETSc will check whether or not matrix is NULL.
+#if DEAL_II_PETSC_VERSION_LT(3, 2, 0)
+    return KSPDestroy (krylov_solver);
+#else
+    return KSPDestroy (&krylov_solver);
+#endif
+  }
 }
 
 DEAL_II_NAMESPACE_CLOSE
index 2f342d3b9542705c98303d16d6f75d27d9758c4b..0acf74c25b5dca7e220f6113dea76ea1292bab3d 100644 (file)
@@ -20,6 +20,7 @@
 #ifdef DEAL_II_WITH_PETSC
 
 #  include <deal.II/lac/exceptions.h>
+#  include <deal.II/lac/petsc_compatibility.h>
 #  include <deal.II/lac/petsc_matrix_base.h>
 #  include <deal.II/lac/petsc_vector_base.h>
 #  include <deal.II/lac/petsc_precondition.h>
@@ -34,17 +35,7 @@ namespace PETScWrappers
 
   SolverBase::SolverData::~SolverData ()
   {
-    if (ksp != NULL)
-      {
-        // destroy the solver object
-#if DEAL_II_PETSC_VERSION_LT(3,2,0)
-        int ierr = KSPDestroy (ksp);
-#else
-        int ierr = KSPDestroy (&ksp);
-#endif
-
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
-      }
+    destroy_krylov_solver (ksp);
   }
 
 
@@ -651,14 +642,7 @@ namespace PETScWrappers
 
   SparseDirectMUMPS::SolverDataMUMPS::~SolverDataMUMPS ()
   {
-    // destroy the solver object
-#if DEAL_II_PETSC_VERSION_LT(3,2,0)
-    int ierr = KSPDestroy (ksp);
-#else
-    int ierr = KSPDestroy (&ksp);
-#endif
-
-    AssertThrow (ierr == 0, ExcPETScError(ierr));
+    destroy_krylov_solver (ksp);
   }
 
 

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.