From abc52dec05318dd149c479d45fed85d453d3d895 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Mon, 7 Sep 2009 00:13:02 +0000 Subject: [PATCH] Implement the destroy_communicator() function. Use it for Trilinos objects, and make sure not to destroy the ones for the sparsity pattern since they apparently are still used. git-svn-id: https://svn.dealii.org/trunk@19404 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/source/utilities.cc | 22 ++++++++++++ deal.II/lac/include/lac/trilinos_vector.h | 7 +++- deal.II/lac/source/trilinos_sparse_matrix.cc | 5 ++- .../lac/source/trilinos_sparsity_pattern.cc | 36 ++++++++++++++----- deal.II/lac/source/trilinos_vector.cc | 10 ++++++ 5 files changed, 70 insertions(+), 10 deletions(-) diff --git a/deal.II/base/source/utilities.cc b/deal.II/base/source/utilities.cc index 6a14aa3f33..eb5062b970 100644 --- a/deal.II/base/source/utilities.cc +++ b/deal.II/base/source/utilities.cc @@ -665,6 +665,28 @@ namespace Utilities ExcInternalError()); return new Epetra_SerialComm(dynamic_cast(communicator)); } + + + + void destroy_communicator (Epetra_Comm &communicator) + { + Assert (&communicator != 0, ExcInternalError()); + + // save the communicator, reset + // the map, and delete the + // communicator if this whole + // thing was created as an MPI + // communicator + Epetra_MpiComm + *mpi_comm = dynamic_cast(&communicator); + if (mpi_comm != 0) + { + MPI_Comm comm = mpi_comm->GetMpiComm(); + *mpi_comm = Epetra_MpiComm(MPI_COMM_SELF); + MPI_Comm_free (&comm); + } + } + unsigned int get_n_mpi_processes (const Epetra_Comm &mpi_communicator) diff --git a/deal.II/lac/include/lac/trilinos_vector.h b/deal.II/lac/include/lac/trilinos_vector.h index c5dc2e01ae..7588fc8691 100755 --- a/deal.II/lac/include/lac/trilinos_vector.h +++ b/deal.II/lac/include/lac/trilinos_vector.h @@ -159,7 +159,7 @@ namespace TrilinosWrappers * @ingroup TrilinosWrappers * @ingroup Vectors * @see @ref SoftwareTrilinos - * @author Martin Kronbichler, Wolfgang Bangerth, 2008 + * @author Martin Kronbichler, Wolfgang Bangerth, 2008, 2009 */ class Vector : public VectorBase { @@ -219,6 +219,11 @@ namespace TrilinosWrappers explicit Vector (const Epetra_Map &InputMap, const dealii::Vector &v); + /** + * Destructor. + */ + ~Vector (); + /** * Reinit functionality. This * function destroys the old diff --git a/deal.II/lac/source/trilinos_sparse_matrix.cc b/deal.II/lac/source/trilinos_sparse_matrix.cc index bdd43ee75d..01ab086ff8 100755 --- a/deal.II/lac/source/trilinos_sparse_matrix.cc +++ b/deal.II/lac/source/trilinos_sparse_matrix.cc @@ -258,7 +258,9 @@ namespace TrilinosWrappers SparseMatrix::~SparseMatrix () - {} + { + Utilities::Trilinos::destroy_communicator (*communicator); + } @@ -517,6 +519,7 @@ namespace TrilinosWrappers // model the communicator on the // one used for the sparsity // pattern + Utilities::Trilinos::destroy_communicator (*communicator); communicator.reset (Utilities::Trilinos:: duplicate_communicator (sparsity_pattern.trilinos_communicator())); diff --git a/deal.II/lac/source/trilinos_sparsity_pattern.cc b/deal.II/lac/source/trilinos_sparsity_pattern.cc index a582dd8080..71a4ffe94e 100755 --- a/deal.II/lac/source/trilinos_sparsity_pattern.cc +++ b/deal.II/lac/source/trilinos_sparsity_pattern.cc @@ -127,7 +127,8 @@ namespace TrilinosWrappers SparsityPattern::SparsityPattern (const Epetra_Map &InputMap, const std::vector &n_entries_per_row) : - communicator (Utilities::Trilinos::duplicate_communicator (InputMap.Comm())), + communicator (Utilities::Trilinos:: + duplicate_communicator (InputMap.Comm())), row_map (Utilities::Trilinos::duplicate_map (InputMap, *communicator)), col_map (row_map), compressed (false), @@ -150,9 +151,12 @@ namespace TrilinosWrappers const Epetra_Map &InputColMap, const unsigned int n_entries_per_row) : - communicator (Utilities::Trilinos::duplicate_communicator (InputRowMap.Comm())), - row_map (Utilities::Trilinos::duplicate_map (InputRowMap, *communicator)), - col_map (Utilities::Trilinos::duplicate_map (InputColMap, *communicator)), + communicator (Utilities::Trilinos:: + duplicate_communicator (InputRowMap.Comm())), + row_map (Utilities::Trilinos:: + duplicate_map (InputRowMap, *communicator)), + col_map (Utilities::Trilinos:: + duplicate_map (InputColMap, *communicator)), compressed (false), graph (row_map.Comm().NumProc() > 1 ? (std::auto_ptr @@ -171,9 +175,12 @@ namespace TrilinosWrappers const Epetra_Map &InputColMap, const std::vector &n_entries_per_row) : - communicator (Utilities::Trilinos::duplicate_communicator (InputRowMap.Comm())), - row_map (Utilities::Trilinos::duplicate_map (InputRowMap, *communicator)), - col_map (Utilities::Trilinos::duplicate_map (InputColMap, *communicator)), + communicator (Utilities::Trilinos:: + duplicate_communicator (InputRowMap.Comm())), + row_map (Utilities::Trilinos:: + duplicate_map (InputRowMap, *communicator)), + col_map (Utilities::Trilinos:: + duplicate_map (InputColMap, *communicator)), compressed (false), graph (row_map.Comm().NumProc() > 1 ? (std::auto_ptr @@ -239,7 +246,16 @@ namespace TrilinosWrappers SparsityPattern::~SparsityPattern () - {} + { +// this is sorta tricky. we can't destroy the communicator here +// if we have initialized a matrix with it because the matrix +// keeps a reference count to the sparsity pattern and so +// the communicator has to stay alive even though the sparsity +// pattern goes out of scope :-( +// +// TODO: find a way to fix this +// Utilities::Trilinos::destroy_communicator (*communicator); + } @@ -268,6 +284,7 @@ namespace TrilinosWrappers const Epetra_Map &input_col_map, const unsigned int n_entries_per_row) { + Utilities::Trilinos::destroy_communicator (*communicator); communicator.reset (Utilities::Trilinos:: duplicate_communicator (input_row_map.Comm())); @@ -330,6 +347,7 @@ namespace TrilinosWrappers ExcDimensionMismatch (n_entries_per_row.size(), input_row_map.NumGlobalElements())); + Utilities::Trilinos::destroy_communicator (*communicator); communicator.reset (Utilities::Trilinos:: duplicate_communicator (input_row_map.Comm())); @@ -379,6 +397,7 @@ namespace TrilinosWrappers ExcDimensionMismatch (sp.n_cols(), input_col_map.NumGlobalElements())); + Utilities::Trilinos::destroy_communicator (*communicator); communicator.reset (Utilities::Trilinos:: duplicate_communicator (input_row_map.Comm())); @@ -447,6 +466,7 @@ namespace TrilinosWrappers ExcDimensionMismatch (sp.n_cols(), input_col_map.NumGlobalElements())); + Utilities::Trilinos::destroy_communicator (*communicator); communicator.reset (Utilities::Trilinos:: duplicate_communicator (input_row_map.Comm())); diff --git a/deal.II/lac/source/trilinos_vector.cc b/deal.II/lac/source/trilinos_vector.cc index b53d8bed25..923cccdf64 100755 --- a/deal.II/lac/source/trilinos_vector.cc +++ b/deal.II/lac/source/trilinos_vector.cc @@ -95,12 +95,20 @@ namespace TrilinosWrappers + Vector::~Vector () + { + Utilities::Trilinos::destroy_communicator (*communicator); + } + + + void Vector::reinit (const Epetra_Map &input_map, const bool fast) { vector.reset(); + Utilities::Trilinos::destroy_communicator (*communicator); communicator.reset (Utilities::Trilinos:: duplicate_communicator(input_map.Comm())); map = Utilities::Trilinos::duplicate_map (input_map, *communicator); @@ -136,6 +144,7 @@ namespace TrilinosWrappers if (vector->Map().SameAs(v.vector->Map()) == false) { vector.reset(); + Utilities::Trilinos::destroy_communicator (*communicator); communicator.reset (Utilities::Trilinos:: duplicate_communicator(v.trilinos_vector() .Map().Comm())); @@ -212,6 +221,7 @@ namespace TrilinosWrappers { vector.reset(); + Utilities::Trilinos::destroy_communicator (*communicator); communicator.reset (Utilities::Trilinos:: duplicate_communicator(v.trilinos_vector(). Map().Comm())); -- 2.39.5