From: Wolfgang Bangerth Date: Wed, 16 Nov 2022 21:52:19 +0000 (-0700) Subject: Provide the ability to query the communicator from PETSc precondiditioners. X-Git-Tag: v9.5.0-rc1~841^2~7 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5d24b5c1ae552cbfe47164cd94d7ae491ca4131d;p=dealii.git Provide the ability to query the communicator from PETSc precondiditioners. --- diff --git a/include/deal.II/lac/petsc_precondition.h b/include/deal.II/lac/petsc_precondition.h index 5304d0b0ec..c5fb9d702d 100644 --- a/include/deal.II/lac/petsc_precondition.h +++ b/include/deal.II/lac/petsc_precondition.h @@ -62,7 +62,7 @@ namespace PETScWrappers /** * Constructor. */ - PreconditionBase(); + PreconditionBase(const MPI_Comm &mpi_communicator = MPI_COMM_NULL); /** * Destructor. @@ -95,9 +95,20 @@ namespace PETScWrappers const PC & get_pc() const; + /** + * Return the MPI communicator object used by this preconditioner. + */ + MPI_Comm + get_mpi_communicator() const; + protected: /** - * the PETSc preconditioner object + * The communicator to be used for this preconditioner. + */ + MPI_Comm mpi_communicator; + + /** + * The PETSc preconditioner object */ PC pc; diff --git a/source/lac/petsc_precondition.cc b/source/lac/petsc_precondition.cc index eb5e5c80ba..6f145d31de 100644 --- a/source/lac/petsc_precondition.cc +++ b/source/lac/petsc_precondition.cc @@ -33,11 +33,14 @@ DEAL_II_NAMESPACE_OPEN namespace PETScWrappers { - PreconditionBase::PreconditionBase() - : pc(nullptr) + PreconditionBase::PreconditionBase(const MPI_Comm &comm) + : mpi_communicator(comm) + , pc(nullptr) , matrix(nullptr) {} + + PreconditionBase::~PreconditionBase() { try @@ -48,9 +51,12 @@ namespace PETScWrappers {} } + void PreconditionBase::clear() { + mpi_communicator = MPI_COMM_NULL; + matrix = nullptr; if (pc != nullptr) @@ -72,6 +78,7 @@ namespace PETScWrappers } + void PreconditionBase::Tvmult(VectorBase &dst, const VectorBase &src) const { @@ -82,6 +89,15 @@ namespace PETScWrappers } + + MPI_Comm + PreconditionBase::get_mpi_communicator() const + { + return mpi_communicator; + } + + + void PreconditionBase::create_pc() { @@ -89,15 +105,9 @@ namespace PETScWrappers // preconditioner once AssertThrow(pc == nullptr, StandardExceptions::ExcInvalidState()); - MPI_Comm comm; - // this ugly cast is necessary because the - // type Mat and PETScObject are - // unrelated. - PetscErrorCode ierr = - PetscObjectGetComm(reinterpret_cast(matrix), &comm); - AssertThrow(ierr == 0, ExcPETScError(ierr)); + MPI_Comm comm = get_mpi_communicator(); - ierr = PCCreate(comm, &pc); + PetscErrorCode ierr = PCCreate(comm, &pc); AssertThrow(ierr == 0, ExcPETScError(ierr)); # if DEAL_II_PETSC_VERSION_LT(3, 5, 0) @@ -125,6 +135,7 @@ namespace PETScWrappers /* ----------------- PreconditionJacobi -------------------- */ PreconditionJacobi::PreconditionJacobi(const MPI_Comm & comm, const AdditionalData &additional_data_) + : PreconditionBase(comm) { additional_data = additional_data_; @@ -138,10 +149,13 @@ namespace PETScWrappers PreconditionJacobi::PreconditionJacobi(const MatrixBase & matrix, const AdditionalData &additional_data) + : PreconditionBase(matrix.get_mpi_communicator()) { initialize(matrix, additional_data); } + + void PreconditionJacobi::initialize() { @@ -154,12 +168,16 @@ namespace PETScWrappers AssertThrow(ierr == 0, ExcPETScError(ierr)); } + + void PreconditionJacobi::initialize(const MatrixBase & matrix_, const AdditionalData &additional_data_) { clear(); + mpi_communicator = matrix_.get_mpi_communicator(); + matrix = static_cast(matrix_); additional_data = additional_data_; @@ -172,9 +190,12 @@ namespace PETScWrappers /* ----------------- PreconditionBlockJacobi -------------------- */ + + PreconditionBlockJacobi::PreconditionBlockJacobi( const MPI_Comm & comm, const AdditionalData &additional_data_) + : PreconditionBase(comm) { additional_data = additional_data_; @@ -189,10 +210,13 @@ namespace PETScWrappers PreconditionBlockJacobi::PreconditionBlockJacobi( const MatrixBase & matrix, const AdditionalData &additional_data) + : PreconditionBase(matrix.get_mpi_communicator()) { initialize(matrix, additional_data); } + + void PreconditionBlockJacobi::initialize() { @@ -204,12 +228,15 @@ namespace PETScWrappers } + void PreconditionBlockJacobi::initialize(const MatrixBase & matrix_, const AdditionalData &additional_data_) { clear(); + mpi_communicator = matrix_.get_mpi_communicator(); + matrix = static_cast(matrix_); additional_data = additional_data_; @@ -231,6 +258,7 @@ namespace PETScWrappers PreconditionSOR::PreconditionSOR(const MatrixBase & matrix, const AdditionalData &additional_data) + : PreconditionBase(matrix.get_mpi_communicator()) { initialize(matrix, additional_data); } @@ -242,6 +270,8 @@ namespace PETScWrappers { clear(); + mpi_communicator = matrix_.get_mpi_communicator(); + matrix = static_cast(matrix_); additional_data = additional_data_; @@ -272,6 +302,7 @@ namespace PETScWrappers PreconditionSSOR::PreconditionSSOR(const MatrixBase & matrix, const AdditionalData &additional_data) + : PreconditionBase(matrix.get_mpi_communicator()) { initialize(matrix, additional_data); } @@ -283,6 +314,8 @@ namespace PETScWrappers { clear(); + mpi_communicator = matrix_.get_mpi_communicator(); + matrix = static_cast(matrix_); additional_data = additional_data_; @@ -318,6 +351,7 @@ namespace PETScWrappers PreconditionICC::PreconditionICC(const MatrixBase & matrix, const AdditionalData &additional_data) + : PreconditionBase(matrix.get_mpi_communicator()) { initialize(matrix, additional_data); } @@ -329,6 +363,8 @@ namespace PETScWrappers { clear(); + mpi_communicator = matrix_.get_mpi_communicator(); + matrix = static_cast(matrix_); additional_data = additional_data_; @@ -359,6 +395,7 @@ namespace PETScWrappers PreconditionILU::PreconditionILU(const MatrixBase & matrix, const AdditionalData &additional_data) + : PreconditionBase(matrix.get_mpi_communicator()) { initialize(matrix, additional_data); } @@ -370,6 +407,8 @@ namespace PETScWrappers { clear(); + mpi_communicator = matrix_.get_mpi_communicator(); + matrix = static_cast(matrix_); additional_data = additional_data_; @@ -501,6 +540,7 @@ namespace PETScWrappers PreconditionBoomerAMG::PreconditionBoomerAMG( const MPI_Comm & comm, const AdditionalData &additional_data_) + : PreconditionBase(comm) { additional_data = additional_data_; @@ -518,13 +558,17 @@ namespace PETScWrappers } + PreconditionBoomerAMG::PreconditionBoomerAMG( const MatrixBase & matrix, const AdditionalData &additional_data) + : PreconditionBase(matrix.get_mpi_communicator()) { initialize(matrix, additional_data); } + + void PreconditionBoomerAMG::initialize() { @@ -633,6 +677,8 @@ namespace PETScWrappers # endif } + + void PreconditionBoomerAMG::initialize(const MatrixBase & matrix_, const AdditionalData &additional_data_) @@ -640,6 +686,8 @@ namespace PETScWrappers # ifdef DEAL_II_PETSC_WITH_HYPRE clear(); + mpi_communicator = matrix_.get_mpi_communicator(); + matrix = static_cast(matrix_); additional_data = additional_data_; @@ -679,6 +727,7 @@ namespace PETScWrappers PreconditionParaSails::PreconditionParaSails( const MatrixBase & matrix, const AdditionalData &additional_data) + : PreconditionBase(matrix.get_mpi_communicator()) { initialize(matrix, additional_data); } @@ -690,6 +739,8 @@ namespace PETScWrappers { clear(); + mpi_communicator = matrix_.get_mpi_communicator(); + matrix = static_cast(matrix_); additional_data = additional_data_; @@ -773,6 +824,7 @@ namespace PETScWrappers PreconditionNone::PreconditionNone(const MatrixBase & matrix, const AdditionalData &additional_data) + : PreconditionBase(matrix.get_mpi_communicator()) { initialize(matrix, additional_data); } @@ -784,6 +836,8 @@ namespace PETScWrappers { clear(); + mpi_communicator = matrix_.get_mpi_communicator(); + matrix = static_cast(matrix_); additional_data = additional_data_; @@ -814,6 +868,7 @@ namespace PETScWrappers PreconditionLU::PreconditionLU(const MatrixBase & matrix, const AdditionalData &additional_data) + : PreconditionBase(matrix.get_mpi_communicator()) { initialize(matrix, additional_data); } @@ -825,6 +880,8 @@ namespace PETScWrappers { clear(); + mpi_communicator = matrix_.get_mpi_communicator(); + matrix = static_cast(matrix_); additional_data = additional_data_; @@ -866,10 +923,13 @@ namespace PETScWrappers , coords(coords) {} + + template PreconditionBDDC::PreconditionBDDC( const MPI_Comm comm, const AdditionalData &additional_data_) + : PreconditionBase(comm) { additional_data = additional_data_; @@ -879,14 +939,18 @@ namespace PETScWrappers initialize(); } + + template PreconditionBDDC::PreconditionBDDC(const MatrixBase & matrix, const AdditionalData &additional_data) + : PreconditionBase(matrix.get_mpi_communicator()) { initialize(matrix, additional_data); } + template void PreconditionBDDC::initialize() @@ -960,6 +1024,8 @@ namespace PETScWrappers # endif } + + template void PreconditionBDDC::initialize(const MatrixBase & matrix_, @@ -967,6 +1033,8 @@ namespace PETScWrappers { clear(); + mpi_communicator = matrix_.get_mpi_communicator(); + matrix = static_cast(matrix_); additional_data = additional_data_;