diff --git a/src/TNL/SharedPointer.h b/src/TNL/SharedPointer.h index e1beb88affce48cb0cee7f64af999f5a0455e46e..58c703346575d7252ef096eb06b55251a42adbe5 100644 --- a/src/TNL/SharedPointer.h +++ b/src/TNL/SharedPointer.h @@ -47,13 +47,8 @@ namespace TNL { -/*** - * Use the lazy mode if you do not want to call the object constructor in the - * shared pointer constructor. You may call it later via the method recreate. - */ template< typename Object, - typename Device = typename Object::DeviceType, - bool lazy = false > + typename Device = typename Object::DeviceType > class SharedPointer { static_assert( ! std::is_same< Device, void >::value, "The device cannot be void. You need to specify the device explicitly in your code." ); @@ -62,8 +57,8 @@ class SharedPointer /**** * Specialization for Devices::Host */ -template< typename Object, bool lazy > -class SharedPointer< Object, Devices::Host, lazy > : public SmartPointer +template< typename Object > +class SharedPointer< Object, Devices::Host > : public SmartPointer { private: // Convenient template alias for controlling the selection of copy- and @@ -75,14 +70,14 @@ class SharedPointer< Object, Devices::Host, lazy > : public SmartPointer std::is_same< typename std::remove_cv< Object >::type, Object_ >::value >; // friend class will be needed for templated assignment operators - template< typename Object_, typename Device_, bool lazy_ > + template< typename Object_, typename Device_ > friend class SharedPointer; public: typedef Object ObjectType; typedef Devices::Host DeviceType; - typedef SharedPointer< Object, Devices::Host, lazy > ThisType; + typedef SharedPointer< Object, Devices::Host > ThisType; template< typename... Args > explicit SharedPointer( Args... args ) @@ -91,8 +86,7 @@ class SharedPointer< Object, Devices::Host, lazy > : public SmartPointer #ifdef TNL_DEBUG_SHARED_POINTERS std::cerr << "Creating shared pointer to " << demangle(typeid(ObjectType).name()) << std::endl; #endif - if( ! lazy ) - this->allocate( args... ); + this->allocate( args... ); } // this is needed only to avoid the default compiler-generated constructor @@ -103,9 +97,9 @@ class SharedPointer< Object, Devices::Host, lazy > : public SmartPointer } // conditional constructor for non-const -> const data - template< typename Object_, bool lazy_, + template< typename Object_, typename = typename Enabler< Object_ >::type > - SharedPointer( const SharedPointer< Object_, DeviceType, lazy_ >& pointer ) + SharedPointer( const SharedPointer< Object_, DeviceType >& pointer ) : pd( (PointerData*) pointer.pd ) { this->pd->counter += 1; @@ -119,9 +113,9 @@ class SharedPointer< Object, Devices::Host, lazy > : public SmartPointer } // conditional constructor for non-const -> const data - template< typename Object_, bool lazy_, + template< typename Object_, typename = typename Enabler< Object_ >::type > - SharedPointer( SharedPointer< Object_, DeviceType, lazy_ >&& pointer ) + SharedPointer( SharedPointer< Object_, DeviceType >&& pointer ) : pd( (PointerData*) pointer.pd ) { pointer.pd = nullptr; @@ -201,9 +195,9 @@ class SharedPointer< Object, Devices::Host, lazy > : public SmartPointer } // conditional operator for non-const -> const data - template< typename Object_, bool lazy_, + template< typename Object_, typename = typename Enabler< Object_ >::type > - const ThisType& operator=( const SharedPointer< Object_, DeviceType, lazy_ >& ptr ) + const ThisType& operator=( const SharedPointer< Object_, DeviceType >& ptr ) { this->free(); this->pd = (PointerData*) ptr.pd; @@ -221,9 +215,9 @@ class SharedPointer< Object, Devices::Host, lazy > : public SmartPointer } // conditional operator for non-const -> const data - template< typename Object_, bool lazy_, + template< typename Object_, typename = typename Enabler< Object_ >::type > - const ThisType& operator=( SharedPointer< Object_, DeviceType, lazy_ >&& ptr ) + const ThisType& operator=( SharedPointer< Object_, DeviceType >&& ptr ) { this->free(); this->pd = (PointerData*) ptr.pd; @@ -235,6 +229,11 @@ class SharedPointer< Object, Devices::Host, lazy > : public SmartPointer { return true; } + + void clear() + { + this->free(); + } ~SharedPointer() { @@ -282,8 +281,8 @@ class SharedPointer< Object, Devices::Host, lazy > : public SmartPointer /**** * Specialization for CUDA */ -template< typename Object, bool lazy > -class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer +template< typename Object > +class SharedPointer< Object, Devices::Cuda > : public SmartPointer { private: // Convenient template alias for controlling the selection of copy- and @@ -295,22 +294,21 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer std::is_same< typename std::remove_cv< Object >::type, Object_ >::value >; // friend class will be needed for templated assignment operators - template< typename Object_, typename Device_, bool lazy_ > + template< typename Object_, typename Device_ > friend class SharedPointer; public: typedef Object ObjectType; typedef Devices::Cuda DeviceType; - typedef SharedPointer< Object, Devices::Cuda, lazy > ThisType; + typedef SharedPointer< Object, Devices::Cuda > ThisType; template< typename... Args > explicit SharedPointer( Args... args ) : pd( nullptr ), cuda_pointer( nullptr ) { - if( ! lazy ) - this->allocate( args... ); + this->allocate( args... ); } // this is needed only to avoid the default compiler-generated constructor @@ -322,9 +320,9 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer } // conditional constructor for non-const -> const data - template< typename Object_, bool lazy_, + template< typename Object_, typename = typename Enabler< Object_ >::type > - SharedPointer( const SharedPointer< Object_, DeviceType, lazy_ >& pointer ) + SharedPointer( const SharedPointer< Object_, DeviceType >& pointer ) : pd( (PointerData*) pointer.pd ), cuda_pointer( pointer.cuda_pointer ) { @@ -341,9 +339,9 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer } // conditional constructor for non-const -> const data - template< typename Object_, bool lazy_, + template< typename Object_, typename = typename Enabler< Object_ >::type > - SharedPointer( SharedPointer< Object_, DeviceType, lazy_ >&& pointer ) + SharedPointer( SharedPointer< Object_, DeviceType >&& pointer ) : pd( (PointerData*) pointer.pd ), cuda_pointer( pointer.cuda_pointer ) { @@ -450,9 +448,9 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer } // conditional operator for non-const -> const data - template< typename Object_, bool lazy_, + template< typename Object_, typename = typename Enabler< Object_ >::type > - const ThisType& operator=( const SharedPointer< Object_, DeviceType, lazy_ >& ptr ) + const ThisType& operator=( const SharedPointer< Object_, DeviceType >& ptr ) { this->free(); this->pd = (PointerData*) ptr.pd; @@ -479,9 +477,9 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer } // conditional operator for non-const -> const data - template< typename Object_, bool lazy_, + template< typename Object_, typename = typename Enabler< Object_ >::type > - const ThisType& operator=( SharedPointer< Object_, DeviceType, lazy_ >&& ptr ) + const ThisType& operator=( SharedPointer< Object_, DeviceType >&& ptr ) { this->free(); this->pd = (PointerData*) ptr.pd; @@ -518,6 +516,11 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer return false; #endif } + + void clear() + { + this->free(); + } ~SharedPointer() { diff --git a/src/TNL/Solvers/Linear/BICGStab.h b/src/TNL/Solvers/Linear/BICGStab.h index b03037aca74199931eeabf0e2e68fb7fc42ef5ba..ce9828cbe70f35cb1615eaeec85d7f7116c82da8 100644 --- a/src/TNL/Solvers/Linear/BICGStab.h +++ b/src/TNL/Solvers/Linear/BICGStab.h @@ -39,8 +39,8 @@ class BICGStab : public Object, typedef typename Matrix::DeviceType DeviceType; typedef Matrix MatrixType; typedef Preconditioner PreconditionerType; - typedef SharedPointer< const MatrixType, DeviceType, true > MatrixPointer; - typedef SharedPointer< const PreconditionerType, DeviceType, true > PreconditionerPointer; + typedef SharedPointer< const MatrixType, DeviceType > MatrixPointer; + typedef SharedPointer< const PreconditionerType, DeviceType > PreconditionerPointer; BICGStab(); diff --git a/src/TNL/Solvers/Linear/BICGStab_impl.h b/src/TNL/Solvers/Linear/BICGStab_impl.h index 888ea1b77c5e55d103e751658f101b806c0ad784..4ab5f6979229ce57fa0c6bb4906fd4c896d875e7 100644 --- a/src/TNL/Solvers/Linear/BICGStab_impl.h +++ b/src/TNL/Solvers/Linear/BICGStab_impl.h @@ -26,6 +26,11 @@ template< typename Matrix, typename Preconditioner > BICGStab< Matrix, Preconditioner > :: BICGStab() { + /**** + * Clearing the shared pointer means that there is no + * preconditioner set. + */ + this->preconditioner.clear(); } template< typename Matrix, diff --git a/src/TNL/Solvers/Linear/CG.h b/src/TNL/Solvers/Linear/CG.h index ac1f33fe46706f042f10ba61a58b238561ca26ea..2a7b2403da80dc9f0ad77d2ef3936a1fed8fb4a5 100644 --- a/src/TNL/Solvers/Linear/CG.h +++ b/src/TNL/Solvers/Linear/CG.h @@ -38,8 +38,8 @@ class CG : public Object, typedef typename Matrix::DeviceType DeviceType; typedef Matrix MatrixType; typedef Preconditioner PreconditionerType; - typedef SharedPointer< const MatrixType, DeviceType, true > MatrixPointer; - typedef SharedPointer< const PreconditionerType, DeviceType, true > PreconditionerPointer; + typedef SharedPointer< const MatrixType, DeviceType > MatrixPointer; + typedef SharedPointer< const PreconditionerType, DeviceType > PreconditionerPointer; CG(); diff --git a/src/TNL/Solvers/Linear/CG_impl.h b/src/TNL/Solvers/Linear/CG_impl.h index f1d8b10bddfcd7b77494480e3ee8ff7fe3ca38ff..ef83ce399de1249195e54f8bfa6a4e9e942d1bb1 100644 --- a/src/TNL/Solvers/Linear/CG_impl.h +++ b/src/TNL/Solvers/Linear/CG_impl.h @@ -18,6 +18,11 @@ template< typename Matrix, typename Preconditioner > CG< Matrix, Preconditioner > :: CG() { + /**** + * Clearing the shared pointer means that there is no + * preconditioner set. + */ + this->preconditioner.clear(); } template< typename Matrix, diff --git a/src/TNL/Solvers/Linear/CWYGMRES.h b/src/TNL/Solvers/Linear/CWYGMRES.h index 7e909b0e187416d5b48608f83b83da232027a49d..08a65a7bcc9f961c0994d8df28462ff3bfe0ddb3 100644 --- a/src/TNL/Solvers/Linear/CWYGMRES.h +++ b/src/TNL/Solvers/Linear/CWYGMRES.h @@ -27,8 +27,8 @@ public: typedef typename Matrix::DeviceType DeviceType; typedef Matrix MatrixType; typedef Preconditioner PreconditionerType; - typedef SharedPointer< const MatrixType, DeviceType, true > MatrixPointer; - typedef SharedPointer< const PreconditionerType, DeviceType, true > PreconditionerPointer; + typedef SharedPointer< const MatrixType, DeviceType > MatrixPointer; + typedef SharedPointer< const PreconditionerType, DeviceType > PreconditionerPointer; typedef Containers::Vector< RealType, DeviceType, IndexType > DeviceVector; typedef Containers::Vector< RealType, Devices::Host, IndexType > HostVector; diff --git a/src/TNL/Solvers/Linear/CWYGMRES_impl.h b/src/TNL/Solvers/Linear/CWYGMRES_impl.h index 692746a004046c136ef3b270c11078ff9328fede..2ec59b8edcd7863490fba2c1a203ea9fa83239f3 100644 --- a/src/TNL/Solvers/Linear/CWYGMRES_impl.h +++ b/src/TNL/Solvers/Linear/CWYGMRES_impl.h @@ -19,6 +19,11 @@ CWYGMRES() ldSize( 0 ), restarting( 10 ) { + /**** + * Clearing the shared pointer means that there is no + * preconditioner set. + */ + this->preconditioner.clear(); } template< typename Matrix, diff --git a/src/TNL/Solvers/Linear/GMRES.h b/src/TNL/Solvers/Linear/GMRES.h index d36d6527e4cc582bd9466f505bff219211bb75e1..95fdbd9e3f9ceb80792ff7e6c90d537c13257a09 100644 --- a/src/TNL/Solvers/Linear/GMRES.h +++ b/src/TNL/Solvers/Linear/GMRES.h @@ -37,8 +37,8 @@ public: typedef typename Matrix::DeviceType DeviceType; typedef Matrix MatrixType; typedef Preconditioner PreconditionerType; - typedef SharedPointer< const MatrixType, DeviceType, true > MatrixPointer; - typedef SharedPointer< const PreconditionerType, DeviceType, true > PreconditionerPointer; + typedef SharedPointer< const MatrixType, DeviceType > MatrixPointer; + typedef SharedPointer< const PreconditionerType, DeviceType > PreconditionerPointer; GMRES(); @@ -90,6 +90,7 @@ protected: IndexType size, restarting; MatrixPointer matrix; + PreconditionerPointer preconditioner; }; diff --git a/src/TNL/Solvers/Linear/GMRES_impl.h b/src/TNL/Solvers/Linear/GMRES_impl.h index 1eed12f7563805cbc781af8a95dcb970fbe73c9e..59aae2401d3c16f7cf851178cb28e85bb6283b01 100644 --- a/src/TNL/Solvers/Linear/GMRES_impl.h +++ b/src/TNL/Solvers/Linear/GMRES_impl.h @@ -6,6 +6,9 @@ email : tomas.oberhuber@fjfi.cvut.cz ***************************************************************************/ +#include "GMRES.h" + + /* See Copyright Notice in tnl/Copyright */ #pragma once @@ -21,6 +24,11 @@ GMRES() : size( 0 ), restarting( 10 ) { + /**** + * Clearing the shared pointer means that there is no + * preconditioner set. + */ + this->preconditioner.clear(); } template< typename Matrix, diff --git a/src/TNL/Solvers/Linear/SOR.h b/src/TNL/Solvers/Linear/SOR.h index 2a3528f05c86563f06d18aa705c71b94f0b71bb2..d1159884c1dc98883d782db832c936da71e03186 100644 --- a/src/TNL/Solvers/Linear/SOR.h +++ b/src/TNL/Solvers/Linear/SOR.h @@ -36,8 +36,8 @@ class SOR : public Object, typedef typename Matrix :: DeviceType DeviceType; typedef Matrix MatrixType; typedef Preconditioner PreconditionerType; - typedef SharedPointer< const MatrixType, DeviceType, true > MatrixPointer; - typedef SharedPointer< const PreconditionerType, DeviceType, true > PreconditionerPointer; + typedef SharedPointer< const MatrixType, DeviceType > MatrixPointer; + typedef SharedPointer< const PreconditionerType, DeviceType > PreconditionerPointer; SOR(); diff --git a/src/TNL/Solvers/Linear/SOR_impl.h b/src/TNL/Solvers/Linear/SOR_impl.h index 8b99ea392f6b94790bb0752767025284dc4cdd43..d7d883f161342a4311caf8a7a99411fc36683d17 100644 --- a/src/TNL/Solvers/Linear/SOR_impl.h +++ b/src/TNL/Solvers/Linear/SOR_impl.h @@ -18,6 +18,11 @@ template< typename Matrix, typename Preconditioner > SOR< Matrix, Preconditioner > :: SOR() : omega( 1.0 ) { + /**** + * Clearing the shared pointer means that there is no + * preconditioner set. + */ + this->preconditioner.clear(); } template< typename Matrix, typename Preconditioner > diff --git a/src/TNL/Solvers/Linear/TFQMR.h b/src/TNL/Solvers/Linear/TFQMR.h index 4356e437836caa7566e17b6e6b7a0e7c519c926d..94d1caabbb3a24957be6e23f3ee864fff7c0d5e5 100644 --- a/src/TNL/Solvers/Linear/TFQMR.h +++ b/src/TNL/Solvers/Linear/TFQMR.h @@ -39,8 +39,8 @@ class TFQMR : public Object, typedef typename Matrix::DeviceType DeviceType; typedef Matrix MatrixType; typedef Preconditioner PreconditionerType; - typedef SharedPointer< const MatrixType, DeviceType, true > MatrixPointer; - typedef SharedPointer< const PreconditionerType, DeviceType, true > PreconditionerPointer; + typedef SharedPointer< const MatrixType, DeviceType > MatrixPointer; + typedef SharedPointer< const PreconditionerType, DeviceType > PreconditionerPointer; TFQMR(); diff --git a/src/TNL/Solvers/Linear/TFQMR_impl.h b/src/TNL/Solvers/Linear/TFQMR_impl.h index b04c45d3c7371ff6e6e89280d7f45df9bcbeff9d..9e335aaf1e1722cdaf6a188e18ead8e2897651c3 100644 --- a/src/TNL/Solvers/Linear/TFQMR_impl.h +++ b/src/TNL/Solvers/Linear/TFQMR_impl.h @@ -19,6 +19,11 @@ template< typename Matrix, TFQMR< Matrix, Preconditioner > :: TFQMR() : size( 0 ) { + /**** + * Clearing the shared pointer means that there is no + * preconditioner set. + */ + this->preconditioner.clear(); } template< typename Matrix,