From 7f3174ef59afee13d5d0d5e6c461dfad4dfecccb Mon Sep 17 00:00:00 2001 From: Hrvoje Jasak Date: Tue, 25 Aug 2015 11:13:39 +0100 Subject: [PATCH] Bug fix: addressing memory leak in Block AMG --- .../BlockMatrixAgglomeration.C | 66 +++++++++++-------- .../BlockMatrixAgglomeration.H | 2 +- .../BlockMatrixCoarsening.C | 6 +- .../BlockMatrixCoarsening.H | 42 ++++++------ .../blockMatrixAgglomerations.C | 1 + .../scalarBlockMatrixAgglomeration.H | 6 +- .../tensorBlockMatrixAgglomeration.H | 8 ++- .../vectorBlockMatrixAgglomeration.H | 8 +-- .../BlockAmg/coarseBlockAmgLevel.C | 20 ++---- .../BlockAmg/coarseBlockAmgLevel.H | 8 ++- .../BlockAmg/fineBlockAmgLevel.C | 16 +---- .../BlockAmg/fineBlockAmgLevel.H | 3 +- .../BlockAmgPrecon/BlockAmgPrecon.C | 3 +- 13 files changed, 95 insertions(+), 94 deletions(-) diff --git a/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/BlockMatrixAgglomeration.C b/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/BlockMatrixAgglomeration.C index 7ae2fa450..d77f0b2d3 100644 --- a/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/BlockMatrixAgglomeration.C +++ b/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/BlockMatrixAgglomeration.C @@ -34,11 +34,12 @@ Author #include "BlockMatrixAgglomeration.H" #include "boolList.H" +#include "tolerancesSwitch.H" #include "coeffFields.H" #include "addToRunTimeSelectionTable.H" #include "BlockGAMGInterfaceField.H" #include "processorLduInterfaceField.H" -#include "tolerancesSwitch.H" +#include "coarseBlockAmgLevel.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -397,14 +398,14 @@ Foam::BlockMatrixAgglomeration::~BlockMatrixAgglomeration() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template -Foam::autoPtr > +Foam::autoPtr > Foam::BlockMatrixAgglomeration::restrictMatrix() const { if (!coarsen_) { FatalErrorIn ( - "autoPtr " + "autoPtr > " "BlockMatrixAgglomeration::restrictMatrix() const" ) << "Requesting coarse matrix when it cannot be created" << abort(FatalError); @@ -434,8 +435,8 @@ Foam::BlockMatrixAgglomeration::restrictMatrix() const { FatalErrorIn ( - "autoPtr > BlockMatrixAgglomeration::" - "restrictMatrix() const" + "autoPtr >" + "BlockMatrixAgglomeration::restrictMatrix() const" ) << "agglomIndex array does not correspond to fine level. " << endl << " Size: " << agglomIndex_.size() << " number of equations: " << matrix_.lduAddr().size() @@ -609,23 +610,23 @@ Foam::BlockMatrixAgglomeration::restrictMatrix() const const_cast&>(matrix_).interfaces(); // Set the coarse interfaces and coefficients - lduInterfacePtrsList* coarseInterfacesPtr = - new lduInterfacePtrsList(interfaceSize); - lduInterfacePtrsList& coarseInterfaces = *coarseInterfacesPtr; + lduInterfacePtrsList coarseInterfaces(interfaceSize); labelListList coarseInterfaceAddr(interfaceSize); // Add the coarse level // Set the coarse ldu addressing onto the list - lduPrimitiveMesh* coarseAddrPtr = + autoPtr coarseAddrPtr + ( new lduPrimitiveMesh ( nCoarseEqns_, coarseOwner, coarseNeighbour, true - ); + ) + ); // Initialise transfer of restrict addressing on the interface forAll (interfaceFields, intI) @@ -679,7 +680,7 @@ Foam::BlockMatrixAgglomeration::restrictMatrix() const intI, GAMGInterface::New ( - *coarseAddrPtr, + coarseAddrPtr(), fineInterface, fineInterface.interfaceInternalField(agglomIndex_), fineInterfaceAddr[intI] @@ -702,15 +703,17 @@ Foam::BlockMatrixAgglomeration::restrictMatrix() const // Add interfaces coarseAddrPtr->addInterfaces ( - *coarseInterfacesPtr, + coarseInterfaces, coarseInterfaceAddr, matrix_.patchSchedule() ); // Set the coarse level matrix - BlockLduMatrix* coarseMatrixPtr = - new BlockLduMatrix(*coarseAddrPtr); - BlockLduMatrix& coarseMatrix = *coarseMatrixPtr; + autoPtr > coarseMatrixPtr + ( + new BlockLduMatrix(coarseAddrPtr()) + ); + BlockLduMatrix& coarseMatrix = coarseMatrixPtr(); typename BlockLduInterfaceFieldPtrsList::Type& coarseInterfaceFieldsTransfer = @@ -818,7 +821,7 @@ Foam::BlockMatrixAgglomeration::restrictMatrix() const { FatalErrorIn ( - "autoPtr" + "autoPtr >" "BlockMatrixAgglomeration::restrictMatrix() const" ) << "Matrix diagonal of square type and upper of " << "linear type is not implemented" @@ -828,7 +831,7 @@ Foam::BlockMatrixAgglomeration::restrictMatrix() const { FatalErrorIn ( - "autoPtr" + "autoPtr >" "BlockMatrixAgglomeration::restrictMatrix() const" ) << "Matrix diagonal of square type and upper of " << "scalar type is not implemented" @@ -839,7 +842,7 @@ Foam::BlockMatrixAgglomeration::restrictMatrix() const { FatalErrorIn ( - "autoPtr" + "autoPtr >" "BlockMatrixAgglomeration::restrictMatrix() const" ) << "Matrix diagonal of linear type not implemented" << abort(FatalError); @@ -848,7 +851,7 @@ Foam::BlockMatrixAgglomeration::restrictMatrix() const { FatalErrorIn ( - "autoPtr" + "autoPtr >" "BlockMatrixAgglomeration::restrictMatrix() const" ) << "Matrix diagonal of scalar type not implemented" << abort(FatalError); @@ -900,7 +903,7 @@ Foam::BlockMatrixAgglomeration::restrictMatrix() const { FatalErrorIn ( - "autoPtr" + "autoPtr >" "BlockMatrixAgglomeration::restrictMatrix() const" ) << "Matrix diagonal of square type and upper of " << "linear type is not implemented" @@ -910,7 +913,7 @@ Foam::BlockMatrixAgglomeration::restrictMatrix() const { FatalErrorIn ( - "autoPtr" + "autoPtr >" "BlockMatrixAgglomeration::restrictMatrix() const" ) << "Matrix diagonal of square type and upper of " << "scalar type is not implemented" @@ -922,8 +925,8 @@ Foam::BlockMatrixAgglomeration::restrictMatrix() const { FatalErrorIn ( - "autoPtr BlockMatrixAgglomeration::" - "restrictMatrix() const" + "autoPtr > " + "BlockMatrixAgglomeration::restrictMatrix() const" ) << "Matrix diagonal of linear type not implemented" << abort(FatalError); } @@ -931,14 +934,25 @@ Foam::BlockMatrixAgglomeration::restrictMatrix() const { FatalErrorIn ( - "autoPtr BlockMatrixAgglomeration::" - "restrictMatrix() const" + "autoPtr > " + "BlockMatrixAgglomeration::restrictMatrix() const" ) << "Matrix diagonal of scalar type not implemented" << abort(FatalError); } } - return autoPtr >(coarseMatrixPtr); + return autoPtr > + ( + new coarseBlockAmgLevel + ( + coarseAddrPtr, + coarseMatrixPtr, + this->dict(), + this->type(), + this->groupSize(), + this->minCoarseEqns() + ) + ); } diff --git a/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/BlockMatrixAgglomeration.H b/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/BlockMatrixAgglomeration.H index c084077e2..6ce1df7d7 100644 --- a/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/BlockMatrixAgglomeration.H +++ b/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/BlockMatrixAgglomeration.H @@ -138,7 +138,7 @@ public: } //- Restrict matrix - virtual autoPtr > restrictMatrix() const; + virtual autoPtr > restrictMatrix() const; //- Restrict residual virtual void restrictResidual diff --git a/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/BlockMatrixCoarsening.C b/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/BlockMatrixCoarsening.C index 55edfc863..43238c4b2 100644 --- a/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/BlockMatrixCoarsening.C +++ b/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/BlockMatrixCoarsening.C @@ -31,7 +31,8 @@ Class // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * // template -Foam::autoPtr > Foam::BlockMatrixCoarsening::New +Foam::autoPtr > +Foam::BlockMatrixCoarsening::New ( const word& coarseningType, const BlockLduMatrix& matrix, @@ -47,7 +48,8 @@ Foam::autoPtr > Foam::BlockMatrixCoarsening > BlockMatrixCoarsening::New\n" + "autoPtr > " + "BlockMatrixCoarsening::New\n" "(\n" " const word& coarseningType,\n" " const lduMatrix& matrix,\n" diff --git a/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/BlockMatrixCoarsening.H b/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/BlockMatrixCoarsening.H index f928b1cf3..1e6d94fa7 100644 --- a/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/BlockMatrixCoarsening.H +++ b/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/BlockMatrixCoarsening.H @@ -91,29 +91,25 @@ class BlockMatrixCoarsening public: - //- Runtime type information - TypeName("BlockMatrixCoarsening"); - - // KRJ: 2012-12-15: Replaced by the above function - //- Runtime type information - //virtual const word& type() const = 0; + //- Runtime type information + TypeName("BlockMatrixCoarsening"); - // Declare run-time constructor selection tables + // Declare run-time constructor selection tables - declareRunTimeSelectionTable + declareRunTimeSelectionTable + ( + autoPtr, + BlockMatrixCoarsening, + matrix, ( - autoPtr, - BlockMatrixCoarsening, - matrix, - ( - const BlockLduMatrix& matrix, - const dictionary& dict, - const label groupSize, - const label minCoarseEqns - ), - (matrix, dict, groupSize, minCoarseEqns) - ); + const BlockLduMatrix& matrix, + const dictionary& dict, + const label groupSize, + const label minCoarseEqns + ), + (matrix, dict, groupSize, minCoarseEqns) + ); // Selectors @@ -154,6 +150,12 @@ public: // Member Functions + //- Return access to dictionary + const dictionary& dict() const + { + return dict_; + } + //- Return group size label groupSize() const { @@ -170,7 +172,7 @@ public: virtual bool coarsen() const = 0; //- Restrict matrix - virtual autoPtr > restrictMatrix + virtual autoPtr > restrictMatrix () const = 0; //- Restrict residual diff --git a/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/blockMatrixAgglomerations.C b/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/blockMatrixAgglomerations.C index 2102d57bb..091e04e04 100644 --- a/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/blockMatrixAgglomerations.C +++ b/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/blockMatrixAgglomerations.C @@ -25,6 +25,7 @@ License #include "blockMatrixAgglomerations.H" #include "blockMatrixCoarsenings.H" +#include "coarseBlockAmgLevel.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/scalarBlockMatrixAgglomeration.H b/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/scalarBlockMatrixAgglomeration.H index 9a5a62dc2..da030d2a5 100644 --- a/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/scalarBlockMatrixAgglomeration.H +++ b/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/scalarBlockMatrixAgglomeration.H @@ -58,18 +58,18 @@ namespace Foam //- Restrict matrix template<> -inline autoPtr > +inline autoPtr > BlockMatrixAgglomeration::restrictMatrix() const { FatalErrorIn ( - "autoPtr > " + "autoPtr > " "BlockMatrixAgglomeration::restrictMatrix() const" ) << "Function not implemented for Type=scalar. " << endl << abort(FatalError); // Dummy return to keep compiler happy - return autoPtr >(NULL); + return autoPtr >(NULL); } diff --git a/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/tensorBlockMatrixAgglomeration.H b/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/tensorBlockMatrixAgglomeration.H index 5413de4ef..4489b710a 100644 --- a/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/tensorBlockMatrixAgglomeration.H +++ b/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/tensorBlockMatrixAgglomeration.H @@ -52,18 +52,20 @@ namespace Foam //- Restrict matrix template<> -inline autoPtr > BlockMatrixAgglomeration::restrictMatrix +inline autoPtr > +BlockMatrixAgglomeration::restrictMatrix () const { FatalErrorIn ( - "autoPtr > BlockMatrixAgglomeration::" + "autoPtr > " + "BlockMatrixAgglomeration::" "restrictMatrix() const" ) << "Function not implemented for Type=tensor. " << endl << abort(FatalError); // Dummy return to keep compiler happy - return autoPtr >(NULL); + return autoPtr >(NULL); } diff --git a/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/vectorBlockMatrixAgglomeration.H b/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/vectorBlockMatrixAgglomeration.H index 17eb13377..e06052807 100644 --- a/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/vectorBlockMatrixAgglomeration.H +++ b/src/foam/matrices/blockLduMatrix/BlockAmg/BlockMatrixCoarsening/vectorBlockMatrixAgglomeration.H @@ -46,25 +46,25 @@ namespace Foam { /*---------------------------------------------------------------------------*\ - Class BlockMatrixAgglomeration Declaration + Class BlockMatrixAgglomeration Declaration \*---------------------------------------------------------------------------*/ //- Restrict matrix template<> -inline autoPtr > +inline autoPtr > BlockMatrixAgglomeration::restrictMatrix () const { FatalErrorIn ( - "autoPtr > BlockMatrixAgglomeration::" + "autoPtr > BlockMatrixAgglomeration::" "restrictMatrix() const" ) << "Function not implemented for Type=vector. " << endl << abort(FatalError); // Dummy return to keep compiler happy - return autoPtr >(NULL); + return autoPtr >(NULL); } diff --git a/src/foam/matrices/blockLduMatrix/BlockAmg/coarseBlockAmgLevel.C b/src/foam/matrices/blockLduMatrix/BlockAmg/coarseBlockAmgLevel.C index 6e9dd3b40..c8d291ae9 100644 --- a/src/foam/matrices/blockLduMatrix/BlockAmg/coarseBlockAmgLevel.C +++ b/src/foam/matrices/blockLduMatrix/BlockAmg/coarseBlockAmgLevel.C @@ -47,14 +47,15 @@ Author template Foam::coarseBlockAmgLevel::coarseBlockAmgLevel ( + autoPtr addrPtr, autoPtr > matrixPtr, const dictionary& dict, const word& coarseningType, const label groupSize, - const label minCoarseEqns, - const word& smootherType + const label minCoarseEqns ) : + addrPtr_(addrPtr), matrixPtr_(matrixPtr), x_(matrixPtr_->diag().size(),pTraits::zero), b_(matrixPtr_->diag().size(),pTraits::zero), @@ -388,23 +389,12 @@ Foam::coarseBlockAmgLevel::makeNextLevel() const { if (coarseningPtr_->coarsen()) { - return autoPtr > - ( - new coarseBlockAmgLevel - ( - coarseningPtr_->restrictMatrix(), - dict(), - coarseningPtr_->type(), - coarseningPtr_->groupSize(), - coarseningPtr_->minCoarseEqns(), - smootherPtr_->type() - ) - ); + return coarseningPtr_->restrictMatrix(); } else { // Final level: cannot coarsen - return autoPtr >(); + return autoPtr >(); } } diff --git a/src/foam/matrices/blockLduMatrix/BlockAmg/coarseBlockAmgLevel.H b/src/foam/matrices/blockLduMatrix/BlockAmg/coarseBlockAmgLevel.H index e356f5ff1..c7ecf3a6f 100644 --- a/src/foam/matrices/blockLduMatrix/BlockAmg/coarseBlockAmgLevel.H +++ b/src/foam/matrices/blockLduMatrix/BlockAmg/coarseBlockAmgLevel.H @@ -41,6 +41,7 @@ SourceFiles #include "BlockAmgLevel.H" #include "BlockLduSmoother.H" #include "BlockLduMatrix.H" +#include "lduPrimitiveMesh.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -58,6 +59,9 @@ class coarseBlockAmgLevel { // Private data + //- Matrix addressing object. This also contains coupling interfaces + autoPtr addrPtr_; + //- Matrix autoPtr > matrixPtr_; @@ -100,12 +104,12 @@ public: //- Construct from components coarseBlockAmgLevel ( + autoPtr addrPtr, autoPtr > matrixPtr, const dictionary& dict, const word& coarseningType, const label groupSize, - const label minCoarseEqns, - const word& smootherType + const label minCoarseEqns ); diff --git a/src/foam/matrices/blockLduMatrix/BlockAmg/fineBlockAmgLevel.C b/src/foam/matrices/blockLduMatrix/BlockAmg/fineBlockAmgLevel.C index a6af87e1c..0b91ee881 100644 --- a/src/foam/matrices/blockLduMatrix/BlockAmg/fineBlockAmgLevel.C +++ b/src/foam/matrices/blockLduMatrix/BlockAmg/fineBlockAmgLevel.C @@ -50,8 +50,7 @@ Foam::fineBlockAmgLevel::fineBlockAmgLevel const dictionary& dict, const word& coarseningType, const label groupSize, - const label minCoarseEqns, - const word& smootherType + const label minCoarseEqns ) : matrix_(matrix), @@ -314,18 +313,7 @@ Foam::fineBlockAmgLevel::makeNextLevel() const { if (coarseningPtr_->coarsen()) { - return autoPtr > - ( - new coarseBlockAmgLevel - ( - coarseningPtr_->restrictMatrix(), - dict(), - coarseningPtr_->type(), - coarseningPtr_->groupSize(), - coarseningPtr_->minCoarseEqns(), - smootherPtr_->type() - ) - ); + return coarseningPtr_->restrictMatrix(); } else { diff --git a/src/foam/matrices/blockLduMatrix/BlockAmg/fineBlockAmgLevel.H b/src/foam/matrices/blockLduMatrix/BlockAmg/fineBlockAmgLevel.H index df5ab9880..b26109e0a 100644 --- a/src/foam/matrices/blockLduMatrix/BlockAmg/fineBlockAmgLevel.H +++ b/src/foam/matrices/blockLduMatrix/BlockAmg/fineBlockAmgLevel.H @@ -108,8 +108,7 @@ public: const dictionary& dict, const word& coarseningType, const label groupSize, - const label minCoarseEqns, - const word& smootherType + const label minCoarseEqns ); diff --git a/src/foam/matrices/blockLduMatrix/BlockLduPrecons/BlockAmgPrecon/BlockAmgPrecon.C b/src/foam/matrices/blockLduMatrix/BlockLduPrecons/BlockAmgPrecon/BlockAmgPrecon.C index 5d2609f57..c08a93feb 100644 --- a/src/foam/matrices/blockLduMatrix/BlockLduPrecons/BlockAmgPrecon/BlockAmgPrecon.C +++ b/src/foam/matrices/blockLduMatrix/BlockLduPrecons/BlockAmgPrecon/BlockAmgPrecon.C @@ -70,8 +70,7 @@ Foam::BlockAmgPrecon::BlockAmgPrecon dict, dict.lookup("coarseningType"), readLabel(dict.lookup("groupSize")), - readLabel(dict.lookup("minCoarseEqns")), - dict.lookup("smoother") + readLabel(dict.lookup("minCoarseEqns")) ) ) )