From 170dfad9422e0bd4c7295e654d2406c6a8153578 Mon Sep 17 00:00:00 2001 From: Sam Reeve <6740307+streeve@users.noreply.github.com> Date: Fri, 19 Jan 2024 16:17:39 -0500 Subject: [PATCH 1/3] Use inherited hypre variables for setup --- .../src/Cabana_Grid_HypreStructuredSolver.hpp | 95 +++++++++++-------- 1 file changed, 55 insertions(+), 40 deletions(-) diff --git a/grid/src/Cabana_Grid_HypreStructuredSolver.hpp b/grid/src/Cabana_Grid_HypreStructuredSolver.hpp index 79d962158..dc0accb5a 100644 --- a/grid/src/Cabana_Grid_HypreStructuredSolver.hpp +++ b/grid/src/Cabana_Grid_HypreStructuredSolver.hpp @@ -346,7 +346,7 @@ class HypreStructuredSolver throw std::logic_error( "Cannot call setup() on preconditioners" ); // FIXME: appears to be a memory issue in the call to this function - this->setupImpl( _A, _b, _x ); + this->setupImpl(); } /*! @@ -448,8 +448,7 @@ class HypreStructuredSolver virtual void setPrintLevelImpl( const int print_level ) = 0; //! Setup implementation. - virtual void setupImpl( HYPRE_StructMatrix A, HYPRE_StructVector b, - HYPRE_StructVector x ) = 0; + virtual void setupImpl() = 0; //! Solver implementation. virtual void solveImpl( HYPRE_StructMatrix A, HYPRE_StructVector b, @@ -481,6 +480,14 @@ class HypreStructuredSolver } } + protected: + //! Matrix for the problem Ax = b. + HYPRE_StructMatrix _A; + //! Forcing term for the problem Ax = b. + HYPRE_StructVector _b; + //! Solution to the problem Ax = b. + HYPRE_StructVector _x; + private: MPI_Comm _comm; bool _is_preconditioner; @@ -489,9 +496,6 @@ class HypreStructuredSolver std::vector _upper; HYPRE_StructStencil _stencil; unsigned _stencil_size; - HYPRE_StructMatrix _A; - HYPRE_StructVector _b; - HYPRE_StructVector _x; std::shared_ptr> _preconditioner; }; @@ -504,12 +508,12 @@ class HypreStructPCG { public: //! Base HYPRE structured solver type. - using Base = HypreStructuredSolver; + using base_type = HypreStructuredSolver; //! Constructor template HypreStructPCG( const ArrayLayout_t& layout, const bool is_preconditioner = false ) - : Base( layout, is_preconditioner ) + : base_type( layout, is_preconditioner ) { if ( is_preconditioner ) throw std::logic_error( @@ -577,10 +581,9 @@ class HypreStructPCG this->checkHypreError( error ); } - void setupImpl( HYPRE_StructMatrix A, HYPRE_StructVector b, - HYPRE_StructVector x ) override + void setupImpl() override { - auto error = HYPRE_StructPCGSetup( _solver, A, b, x ); + auto error = HYPRE_StructPCGSetup( _solver, _A, _b, _x ); this->checkHypreError( error ); } @@ -621,6 +624,9 @@ class HypreStructPCG private: HYPRE_StructSolver _solver; + using base_type::_A; + using base_type::_b; + using base_type::_x; }; //---------------------------------------------------------------------------// @@ -631,12 +637,12 @@ class HypreStructGMRES { public: //! Base HYPRE structured solver type. - using Base = HypreStructuredSolver; + using base_type = HypreStructuredSolver; //! Constructor template HypreStructGMRES( const ArrayLayout_t& layout, const bool is_preconditioner = false ) - : Base( layout, is_preconditioner ) + : base_type( layout, is_preconditioner ) { if ( is_preconditioner ) throw std::logic_error( @@ -701,10 +707,9 @@ class HypreStructGMRES this->checkHypreError( error ); } - void setupImpl( HYPRE_StructMatrix A, HYPRE_StructVector b, - HYPRE_StructVector x ) override + void setupImpl() override { - auto error = HYPRE_StructGMRESSetup( _solver, A, b, x ); + auto error = HYPRE_StructGMRESSetup( _solver, _A, _b, _x ); this->checkHypreError( error ); } @@ -745,6 +750,9 @@ class HypreStructGMRES private: HYPRE_StructSolver _solver; + using base_type::_A; + using base_type::_b; + using base_type::_x; }; //---------------------------------------------------------------------------// @@ -755,12 +763,12 @@ class HypreStructBiCGSTAB { public: //! Base HYPRE structured solver type. - using Base = HypreStructuredSolver; + using base_type = HypreStructuredSolver; //! Constructor template HypreStructBiCGSTAB( const ArrayLayout_t& layout, const bool is_preconditioner = false ) - : Base( layout, is_preconditioner ) + : base_type( layout, is_preconditioner ) { if ( is_preconditioner ) throw std::logic_error( @@ -818,10 +826,9 @@ class HypreStructBiCGSTAB this->checkHypreError( error ); } - void setupImpl( HYPRE_StructMatrix A, HYPRE_StructVector b, - HYPRE_StructVector x ) override + void setupImpl() override { - auto error = HYPRE_StructBiCGSTABSetup( _solver, A, b, x ); + auto error = HYPRE_StructBiCGSTABSetup( _solver, _A, _b, _x ); this->checkHypreError( error ); } @@ -862,6 +869,9 @@ class HypreStructBiCGSTAB private: HYPRE_StructSolver _solver; + using base_type::_A; + using base_type::_b; + using base_type::_x; }; //---------------------------------------------------------------------------// @@ -872,12 +882,12 @@ class HypreStructPFMG { public: //! Base HYPRE structured solver type. - using Base = HypreStructuredSolver; + using base_type = HypreStructuredSolver; //! Constructor template HypreStructPFMG( const ArrayLayout_t& layout, const bool is_preconditioner = false ) - : Base( layout, is_preconditioner ) + : base_type( layout, is_preconditioner ) { auto error = HYPRE_StructPFMGCreate( layout.localGrid()->globalGrid().comm(), &_solver ); @@ -1006,10 +1016,9 @@ class HypreStructPFMG this->checkHypreError( error ); } - void setupImpl( HYPRE_StructMatrix A, HYPRE_StructVector b, - HYPRE_StructVector x ) override + void setupImpl() override { - auto error = HYPRE_StructPFMGSetup( _solver, A, b, x ); + auto error = HYPRE_StructPFMGSetup( _solver, _A, _b, _x ); this->checkHypreError( error ); } @@ -1046,6 +1055,9 @@ class HypreStructPFMG private: HYPRE_StructSolver _solver; + using base_type::_A; + using base_type::_b; + using base_type::_x; }; //---------------------------------------------------------------------------// @@ -1056,12 +1068,12 @@ class HypreStructSMG { public: //! Base HYPRE structured solver type. - using Base = HypreStructuredSolver; + using base_type = HypreStructuredSolver; //! Constructor template HypreStructSMG( const ArrayLayout_t& layout, const bool is_preconditioner = false ) - : Base( layout, is_preconditioner ) + : base_type( layout, is_preconditioner ) { auto error = HYPRE_StructSMGCreate( layout.localGrid()->globalGrid().comm(), &_solver ); @@ -1136,10 +1148,9 @@ class HypreStructSMG this->checkHypreError( error ); } - void setupImpl( HYPRE_StructMatrix A, HYPRE_StructVector b, - HYPRE_StructVector x ) override + void setupImpl() override { - auto error = HYPRE_StructSMGSetup( _solver, A, b, x ); + auto error = HYPRE_StructSMGSetup( _solver, _A, _b, _x ); this->checkHypreError( error ); } @@ -1176,6 +1187,9 @@ class HypreStructSMG private: HYPRE_StructSolver _solver; + using base_type::_A; + using base_type::_b; + using base_type::_x; }; //---------------------------------------------------------------------------// @@ -1186,12 +1200,12 @@ class HypreStructJacobi { public: //! Base HYPRE structured solver type. - using Base = HypreStructuredSolver; + using base_type = HypreStructuredSolver; //! Constructor template HypreStructJacobi( const ArrayLayout_t& layout, const bool is_preconditioner = false ) - : Base( layout, is_preconditioner ) + : base_type( layout, is_preconditioner ) { auto error = HYPRE_StructJacobiCreate( layout.localGrid()->globalGrid().comm(), &_solver ); @@ -1234,10 +1248,9 @@ class HypreStructJacobi // The Jacobi solver does not support a print level. } - void setupImpl( HYPRE_StructMatrix A, HYPRE_StructVector b, - HYPRE_StructVector x ) override + void setupImpl() override { - auto error = HYPRE_StructJacobiSetup( _solver, A, b, x ); + auto error = HYPRE_StructJacobiSetup( _solver, _A, _b, _x ); this->checkHypreError( error ); } @@ -1274,6 +1287,9 @@ class HypreStructJacobi private: HYPRE_StructSolver _solver; + using base_type::_A; + using base_type::_b; + using base_type::_x; }; //---------------------------------------------------------------------------// @@ -1284,12 +1300,12 @@ class HypreStructDiagonal { public: //! Base HYPRE structured solver type. - using Base = HypreStructuredSolver; + using base_type = HypreStructuredSolver; //! Constructor template HypreStructDiagonal( const ArrayLayout_t& layout, const bool is_preconditioner = false ) - : Base( layout, is_preconditioner ) + : base_type( layout, is_preconditioner ) { if ( !is_preconditioner ) throw std::logic_error( @@ -1325,8 +1341,7 @@ class HypreStructDiagonal "Diagonal preconditioner cannot be used as a solver" ); } - void setupImpl( HYPRE_StructMatrix, HYPRE_StructVector, - HYPRE_StructVector ) override + void setupImpl() override { throw std::logic_error( "Diagonal preconditioner cannot be used as a solver" ); From 7528d62491fbb7e35d457fdcd2ad204cbe434f6f Mon Sep 17 00:00:00 2001 From: Sam Reeve <6740307+streeve@users.noreply.github.com> Date: Fri, 19 Jan 2024 16:23:05 -0500 Subject: [PATCH 2/3] Use inherited hypre variables for solve --- .../src/Cabana_Grid_HypreStructuredSolver.hpp | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/grid/src/Cabana_Grid_HypreStructuredSolver.hpp b/grid/src/Cabana_Grid_HypreStructuredSolver.hpp index dc0accb5a..f8a8f7941 100644 --- a/grid/src/Cabana_Grid_HypreStructuredSolver.hpp +++ b/grid/src/Cabana_Grid_HypreStructuredSolver.hpp @@ -407,7 +407,7 @@ class HypreStructuredSolver checkHypreError( error ); // Solve the problem - this->solveImpl( _A, _b, _x ); + this->solveImpl(); // Extract the solution from the LHS error = HYPRE_StructVectorGetBoxValues( @@ -451,8 +451,7 @@ class HypreStructuredSolver virtual void setupImpl() = 0; //! Solver implementation. - virtual void solveImpl( HYPRE_StructMatrix A, HYPRE_StructVector b, - HYPRE_StructVector x ) = 0; + virtual void solveImpl() = 0; //! Get the number of iterations taken on the last solve. virtual int getNumIterImpl() = 0; @@ -587,10 +586,9 @@ class HypreStructPCG this->checkHypreError( error ); } - void solveImpl( HYPRE_StructMatrix A, HYPRE_StructVector b, - HYPRE_StructVector x ) override + void solveImpl() override { - auto error = HYPRE_StructPCGSolve( _solver, A, b, x ); + auto error = HYPRE_StructPCGSolve( _solver, _A, _b, _x ); this->checkHypreError( error ); } @@ -713,10 +711,9 @@ class HypreStructGMRES this->checkHypreError( error ); } - void solveImpl( HYPRE_StructMatrix A, HYPRE_StructVector b, - HYPRE_StructVector x ) override + void solveImpl() override { - auto error = HYPRE_StructGMRESSolve( _solver, A, b, x ); + auto error = HYPRE_StructGMRESSolve( _solver, _A, _b, _x ); this->checkHypreError( error ); } @@ -832,10 +829,9 @@ class HypreStructBiCGSTAB this->checkHypreError( error ); } - void solveImpl( HYPRE_StructMatrix A, HYPRE_StructVector b, - HYPRE_StructVector x ) override + void solveImpl() override { - auto error = HYPRE_StructBiCGSTABSolve( _solver, A, b, x ); + auto error = HYPRE_StructBiCGSTABSolve( _solver, _A, _b, _x ); this->checkHypreError( error ); } @@ -1022,10 +1018,9 @@ class HypreStructPFMG this->checkHypreError( error ); } - void solveImpl( HYPRE_StructMatrix A, HYPRE_StructVector b, - HYPRE_StructVector x ) override + void solveImpl() override { - auto error = HYPRE_StructPFMGSolve( _solver, A, b, x ); + auto error = HYPRE_StructPFMGSolve( _solver, _A, _b, _x ); this->checkHypreError( error ); } @@ -1154,10 +1149,9 @@ class HypreStructSMG this->checkHypreError( error ); } - void solveImpl( HYPRE_StructMatrix A, HYPRE_StructVector b, - HYPRE_StructVector x ) override + void solveImpl() override { - auto error = HYPRE_StructSMGSolve( _solver, A, b, x ); + auto error = HYPRE_StructSMGSolve( _solver, _A, _b, _x ); this->checkHypreError( error ); } @@ -1254,10 +1248,9 @@ class HypreStructJacobi this->checkHypreError( error ); } - void solveImpl( HYPRE_StructMatrix A, HYPRE_StructVector b, - HYPRE_StructVector x ) override + void solveImpl() override { - auto error = HYPRE_StructJacobiSolve( _solver, A, b, x ); + auto error = HYPRE_StructJacobiSolve( _solver, _A, _b, _x ); this->checkHypreError( error ); } @@ -1347,8 +1340,7 @@ class HypreStructDiagonal "Diagonal preconditioner cannot be used as a solver" ); } - void solveImpl( HYPRE_StructMatrix, HYPRE_StructVector, - HYPRE_StructVector ) override + void solveImpl() override { throw std::logic_error( "Diagonal preconditioner cannot be used as a solver" ); From 736ec453b23128787f642bd4810db753af3617d1 Mon Sep 17 00:00:00 2001 From: Sam Reeve <6740307+streeve@users.noreply.github.com> Date: Fri, 19 Jan 2024 16:33:53 -0500 Subject: [PATCH 3/3] Use inherited hypre variables for semi-struct --- .../Cabana_Grid_HypreSemiStructuredSolver.hpp | 85 ++++++++++--------- 1 file changed, 46 insertions(+), 39 deletions(-) diff --git a/grid/src/Cabana_Grid_HypreSemiStructuredSolver.hpp b/grid/src/Cabana_Grid_HypreSemiStructuredSolver.hpp index b551ea60c..9db313018 100644 --- a/grid/src/Cabana_Grid_HypreSemiStructuredSolver.hpp +++ b/grid/src/Cabana_Grid_HypreSemiStructuredSolver.hpp @@ -482,7 +482,7 @@ class HypreSemiStructuredSolver auto error = HYPRE_SStructMatrixAssemble( _A ); checkHypreError( error ); - this->setupImpl( _A, _b, _x ); + this->setupImpl(); } /*! @@ -556,7 +556,7 @@ class HypreSemiStructuredSolver checkHypreError( error ); // Solve the problem - this->solveImpl( _A, _b, _x ); + this->solveImpl(); // Extract the solution from the LHS for ( int var = 0; var < n_vars; ++var ) @@ -612,12 +612,10 @@ class HypreSemiStructuredSolver virtual void setPrintLevelImpl( const int print_level ) = 0; //! Setup implementation. - virtual void setupImpl( HYPRE_SStructMatrix A, HYPRE_SStructVector b, - HYPRE_SStructVector x ) = 0; + virtual void setupImpl() = 0; //! Solver implementation. - virtual void solveImpl( HYPRE_SStructMatrix A, HYPRE_SStructVector b, - HYPRE_SStructVector x ) = 0; + virtual void solveImpl() = 0; //! Get the number of iterations taken on the last solve. virtual int getNumIterImpl() = 0; @@ -645,6 +643,13 @@ class HypreSemiStructuredSolver } } + //! Matrix for the problem Ax = b. + HYPRE_SStructMatrix _A; + //! Forcing term for the problem Ax = b. + HYPRE_SStructVector _b; + //! Solution to the problem Ax = b. + HYPRE_SStructVector _x; + private: MPI_Comm _comm; bool _is_preconditioner; @@ -655,9 +660,6 @@ class HypreSemiStructuredSolver HYPRE_SStructGraph _graph; std::vector _stencil_size; std::vector> _stencil_index; - HYPRE_SStructMatrix _A; - HYPRE_SStructVector _b; - HYPRE_SStructVector _x; std::shared_ptr> _preconditioner; }; @@ -670,12 +672,13 @@ class HypreSemiStructPCG { public: //! Base HYPRE semi-structured solver type. - using Base = HypreSemiStructuredSolver; + using base_type = + HypreSemiStructuredSolver; //! Constructor template HypreSemiStructPCG( const ArrayLayout_t& layout, int n_vars, const bool is_preconditioner = false ) - : Base( layout, n_vars, is_preconditioner ) + : base_type( layout, n_vars, is_preconditioner ) { if ( is_preconditioner ) throw std::logic_error( @@ -743,17 +746,15 @@ class HypreSemiStructPCG this->checkHypreError( error ); } - void setupImpl( HYPRE_SStructMatrix A, HYPRE_SStructVector b, - HYPRE_SStructVector x ) override + void setupImpl() override { - auto error = HYPRE_SStructPCGSetup( _solver, A, b, x ); + auto error = HYPRE_SStructPCGSetup( _solver, _A, _b, _x ); this->checkHypreError( error ); } - void solveImpl( HYPRE_SStructMatrix A, HYPRE_SStructVector b, - HYPRE_SStructVector x ) override + void solveImpl() override { - auto error = HYPRE_SStructPCGSolve( _solver, A, b, x ); + auto error = HYPRE_SStructPCGSolve( _solver, _A, _b, _x ); this->checkHypreError( error ); } @@ -787,6 +788,9 @@ class HypreSemiStructPCG private: HYPRE_SStructSolver _solver; + using base_type::_A; + using base_type::_b; + using base_type::_x; }; //---------------------------------------------------------------------------// @@ -797,12 +801,13 @@ class HypreSemiStructGMRES { public: //! Base HYPRE semi-structured solver type. - using Base = HypreSemiStructuredSolver; + using base_type = + HypreSemiStructuredSolver; //! Constructor template HypreSemiStructGMRES( const ArrayLayout_t& layout, int n_vars, const bool is_preconditioner = false ) - : Base( layout, n_vars, is_preconditioner ) + : base_type( layout, n_vars, is_preconditioner ) { if ( is_preconditioner ) throw std::logic_error( @@ -867,17 +872,15 @@ class HypreSemiStructGMRES this->checkHypreError( error ); } - void setupImpl( HYPRE_SStructMatrix A, HYPRE_SStructVector b, - HYPRE_SStructVector x ) override + void setupImpl() override { - auto error = HYPRE_SStructGMRESSetup( _solver, A, b, x ); + auto error = HYPRE_SStructGMRESSetup( _solver, _A, _b, _x ); this->checkHypreError( error ); } - void solveImpl( HYPRE_SStructMatrix A, HYPRE_SStructVector b, - HYPRE_SStructVector x ) override + void solveImpl() override { - auto error = HYPRE_SStructGMRESSolve( _solver, A, b, x ); + auto error = HYPRE_SStructGMRESSolve( _solver, _A, _b, _x ); this->checkHypreError( error ); } @@ -911,6 +914,9 @@ class HypreSemiStructGMRES private: HYPRE_SStructSolver _solver; + using base_type::_A; + using base_type::_b; + using base_type::_x; }; //---------------------------------------------------------------------------// @@ -921,13 +927,14 @@ class HypreSemiStructBiCGSTAB { public: //! Base HYPRE semi-structured solver type. - using Base = HypreSemiStructuredSolver; + using base_type = + HypreSemiStructuredSolver; //! Constructor template HypreSemiStructBiCGSTAB( const ArrayLayout_t& layout, const bool is_preconditioner = false, int n_vars = 3 ) - : Base( layout, n_vars, is_preconditioner ) + : base_type( layout, n_vars, is_preconditioner ) { if ( is_preconditioner ) throw std::logic_error( @@ -985,17 +992,15 @@ class HypreSemiStructBiCGSTAB this->checkHypreError( error ); } - void setupImpl( HYPRE_SStructMatrix A, HYPRE_SStructVector b, - HYPRE_SStructVector x ) override + void setupImpl() override { - auto error = HYPRE_SStructBiCGSTABSetup( _solver, A, b, x ); + auto error = HYPRE_SStructBiCGSTABSetup( _solver, _A, _b, _x ); this->checkHypreError( error ); } - void solveImpl( HYPRE_SStructMatrix A, HYPRE_SStructVector b, - HYPRE_SStructVector x ) override + void solveImpl() override { - auto error = HYPRE_SStructBiCGSTABSolve( _solver, A, b, x ); + auto error = HYPRE_SStructBiCGSTABSolve( _solver, _A, _b, _x ); this->checkHypreError( error ); } @@ -1030,6 +1035,9 @@ class HypreSemiStructBiCGSTAB private: HYPRE_SStructSolver _solver; + using base_type::_A; + using base_type::_b; + using base_type::_x; }; //---------------------------------------------------------------------------// @@ -1040,13 +1048,14 @@ class HypreSemiStructDiagonal { public: //! Base HYPRE semi-structured solver type. - using Base = HypreSemiStructuredSolver; + using base_type = + HypreSemiStructuredSolver; //! Constructor template HypreSemiStructDiagonal( const ArrayLayout_t& layout, const bool is_preconditioner = false, int n_vars = 3 ) - : Base( layout, n_vars, is_preconditioner ) + : base_type( layout, n_vars, is_preconditioner ) { if ( !is_preconditioner ) throw std::logic_error( @@ -1082,15 +1091,13 @@ class HypreSemiStructDiagonal "Diagonal preconditioner cannot be used as a solver" ); } - void setupImpl( HYPRE_SStructMatrix, HYPRE_SStructVector, - HYPRE_SStructVector ) override + void setupImpl() override { throw std::logic_error( "Diagonal preconditioner cannot be used as a solver" ); } - void solveImpl( HYPRE_SStructMatrix, HYPRE_SStructVector, - HYPRE_SStructVector ) override + void solveImpl() override { throw std::logic_error( "Diagonal preconditioner cannot be used as a solver" );