From: David Wells Date: Sat, 9 Jul 2016 15:26:49 +0000 (-0400) Subject: Wrap multiple versions of PETSc's KSPDestroy. X-Git-Tag: v8.5.0-rc1~872^2~4 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ed5ddaa17a8b8eeb80173356bf50c2937c7c7c93;p=dealii.git Wrap multiple versions of PETSc's KSPDestroy. Like the fix for MatDestroy, this avoids scattering version checks throughout the library and avoids throwing exceptions in destructors. --- diff --git a/include/deal.II/lac/petsc_compatibility.h b/include/deal.II/lac/petsc_compatibility.h index ae5e02945f..9b7b081d2a 100644 --- a/include/deal.II/lac/petsc_compatibility.h +++ b/include/deal.II/lac/petsc_compatibility.h @@ -25,6 +25,7 @@ #ifdef DEAL_II_WITH_PETSC #include +#include #include #include @@ -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 diff --git a/source/lac/petsc_solver.cc b/source/lac/petsc_solver.cc index 2f342d3b95..0acf74c25b 100644 --- a/source/lac/petsc_solver.cc +++ b/source/lac/petsc_solver.cc @@ -20,6 +20,7 @@ #ifdef DEAL_II_WITH_PETSC # include +# include # include # include # include @@ -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); }