From: Stefano Zampini Date: Wed, 19 Jul 2023 17:17:51 +0000 (+0200) Subject: PETScWrappers: Support with-strict-petscerrorcode configurations X-Git-Tag: relicensing~663^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=07e83ca8fb01ab2de85fd42ed323142ed27582a8;p=dealii.git PETScWrappers: Support with-strict-petscerrorcode configurations SNES and TS intentionally left out since those changes will be in separate commits --- diff --git a/include/deal.II/lac/petsc_compatibility.h b/include/deal.II/lac/petsc_compatibility.h index e1e2cbc666..449f68190f 100644 --- a/include/deal.II/lac/petsc_compatibility.h +++ b/include/deal.II/lac/petsc_compatibility.h @@ -32,12 +32,14 @@ #ifdef DEAL_II_WITH_PETSC -# include # include # include # include # include # include +# if DEAL_II_PETSC_VERSION_LT(3, 19, 0) +# define PETSC_SUCCESS 0 +# endif # include diff --git a/source/base/mpi.cc b/source/base/mpi.cc index 04f7af7fa9..0663721d44 100644 --- a/source/base/mpi.cc +++ b/source/base/mpi.cc @@ -818,21 +818,23 @@ namespace Utilities // we are allowed to call MPI_Init ourselves and PETScInitialize will // detect this. This allows us to use MPI_Init_thread instead. #ifdef DEAL_II_WITH_PETSC + PetscErrorCode pierr; # ifdef DEAL_II_WITH_SLEPC // Initialize SLEPc (with PETSc): finalize_petscslepc = SlepcInitializeCalled ? false : true; - ierr = SlepcInitialize(&argc, &argv, nullptr, nullptr); - AssertThrow(ierr == 0, SLEPcWrappers::SolverBase::ExcSLEPcError(ierr)); + pierr = SlepcInitialize(&argc, &argv, nullptr, nullptr); + AssertThrow(pierr == 0, SLEPcWrappers::SolverBase::ExcSLEPcError(pierr)); # else // or just initialize PETSc alone: finalize_petscslepc = PetscInitializeCalled ? false : true; - ierr = PetscInitialize(&argc, &argv, nullptr, nullptr); - AssertThrow(ierr == 0, ExcPETScError(ierr)); + pierr = PetscInitialize(&argc, &argv, nullptr, nullptr); + AssertThrow(pierr == 0, ExcPETScError(pierr)); # endif // Disable PETSc exception handling. This just prints a large wall // of text that is not particularly helpful for what we do: - PetscPopSignalHandler(); + pierr = PetscPopSignalHandler(); + AssertThrow(pierr == 0, ExcPETScError(pierr)); #endif // Initialize zoltan @@ -1014,11 +1016,17 @@ namespace Utilities # ifdef DEAL_II_WITH_SLEPC // and now end SLEPc with PETSc if we did so if (finalize_petscslepc) - SlepcFinalize(); + { + PetscErrorCode ierr = SlepcFinalize(); + AssertThrow(ierr == 0, SLEPcWrappers::SolverBase::ExcSLEPcError(ierr)) + } # else // or just end PETSc if we did so if (finalize_petscslepc) - PetscFinalize(); + { + PetscErrorCode ierr = PetscFinalize(); + AssertThrow(ierr == 0, ExcPETScError(ierr)); + } # endif #endif diff --git a/source/lac/exceptions.cc b/source/lac/exceptions.cc index c17a1db790..a1de99319b 100644 --- a/source/lac/exceptions.cc +++ b/source/lac/exceptions.cc @@ -18,7 +18,6 @@ #include #ifdef DEAL_II_WITH_PETSC -# include # include #endif // DEAL_II_WITH_PETSC @@ -39,9 +38,10 @@ namespace LACExceptions // PetscErrorMessage changes the value in a pointer to refer to a // statically allocated description of the current error message. const char * petsc_message; - const PetscErrorCode ierr = PetscErrorMessage(error_code, - &petsc_message, - /*specific=*/nullptr); + const PetscErrorCode ierr = + PetscErrorMessage(static_cast(error_code), + &petsc_message, + /*specific=*/nullptr); if (ierr == 0 && petsc_message != nullptr) { out << "The description of the error provided by PETSc is \"" diff --git a/source/lac/petsc_matrix_base.cc b/source/lac/petsc_matrix_base.cc index 163d2f3bc5..2949542d66 100644 --- a/source/lac/petsc_matrix_base.cc +++ b/source/lac/petsc_matrix_base.cc @@ -168,16 +168,18 @@ namespace PETScWrappers // since this is a collective operation IS index_set; - ISCreateGeneral(get_mpi_communicator(), - rows.size(), - petsc_rows.data(), - PETSC_COPY_VALUES, - &index_set); + PetscErrorCode ierr; + ierr = ISCreateGeneral(get_mpi_communicator(), + rows.size(), + petsc_rows.data(), + PETSC_COPY_VALUES, + &index_set); + AssertThrow(ierr == 0, ExcPETScError(ierr)); - const PetscErrorCode ierr = - MatZeroRowsIS(matrix, index_set, new_diag_value, nullptr, nullptr); + ierr = MatZeroRowsIS(matrix, index_set, new_diag_value, nullptr, nullptr); + AssertThrow(ierr == 0, ExcPETScError(ierr)); + ierr = ISDestroy(&index_set); AssertThrow(ierr == 0, ExcPETScError(ierr)); - ISDestroy(&index_set); } void @@ -195,16 +197,19 @@ namespace PETScWrappers // since this is a collective operation IS index_set; - ISCreateGeneral(get_mpi_communicator(), - rows.size(), - petsc_rows.data(), - PETSC_COPY_VALUES, - &index_set); + PetscErrorCode ierr; + ierr = ISCreateGeneral(get_mpi_communicator(), + rows.size(), + petsc_rows.data(), + PETSC_COPY_VALUES, + &index_set); + AssertThrow(ierr == 0, ExcPETScError(ierr)); - const PetscErrorCode ierr = + ierr = MatZeroRowsColumnsIS(matrix, index_set, new_diag_value, nullptr, nullptr); AssertThrow(ierr == 0, ExcPETScError(ierr)); - ISDestroy(&index_set); + ierr = ISDestroy(&index_set); + AssertThrow(ierr == 0, ExcPETScError(ierr)); } diff --git a/source/lac/petsc_precondition.cc b/source/lac/petsc_precondition.cc index 2e51d0cdfe..e7f221de42 100644 --- a/source/lac/petsc_precondition.cc +++ b/source/lac/petsc_precondition.cc @@ -1114,7 +1114,7 @@ namespace PETScWrappers // interface code of PCSetUp in PETSc. // We handle it here. PetscCall(pc_set_failed_reason(ppc, PC_NOERROR)); - PetscFunctionReturn(0); + PetscFunctionReturn(PETSC_SUCCESS); } PetscErrorCode @@ -1155,7 +1155,7 @@ namespace PETScWrappers "Failure in pcapply from dealii::PETScWrappers::NonlinearSolver"); } petsc_increment_state_counter(y); - PetscFunctionReturn(0); + PetscFunctionReturn(PETSC_SUCCESS); } PetscErrorCode @@ -1196,7 +1196,7 @@ namespace PETScWrappers "Failure in pcapply_transpose from dealii::PETScWrappers::NonlinearSolver"); } petsc_increment_state_counter(y); - PetscFunctionReturn(0); + PetscFunctionReturn(PETSC_SUCCESS); } diff --git a/source/lac/petsc_solver.cc b/source/lac/petsc_solver.cc index d5b8b2f37e..7b9308fefd 100644 --- a/source/lac/petsc_solver.cc +++ b/source/lac/petsc_solver.cc @@ -163,13 +163,14 @@ namespace PETScWrappers } - int + PetscErrorCode SolverBase::convergence_test(KSP /*ksp*/, const PetscInt iteration, const PetscReal residual_norm, KSPConvergedReason *reason, void * solver_control_x) { + PetscFunctionBeginUser; SolverControl &solver_control = *reinterpret_cast(solver_control_x); @@ -197,8 +198,7 @@ namespace PETScWrappers Assert(false, ExcNotImplemented()); } - // return without failure - return 0; + PetscFunctionReturn(PETSC_SUCCESS); } diff --git a/tests/tests.h b/tests/tests.h index c9575230dd..0fa6bf2eea 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -461,8 +461,11 @@ namespace void check_petsc_allocations() { - PetscStageLog stageLog; - PetscLogGetStageLog(&stageLog); + PetscStageLog stageLog; + PetscErrorCode ierr; + + ierr = PetscLogGetStageLog(&stageLog); + AssertThrow(ierr == 0, ExcPETScError(ierr)); // I don't quite understand petsc and it looks like // stageLog->stageInfo->classLog->classInfo[i].id is always -1, so we look