From 75dbf9421c3c7dfd3f4338add19cae76a1d3957f Mon Sep 17 00:00:00 2001 From: turcksin Date: Thu, 5 Jun 2014 23:22:42 +0000 Subject: [PATCH] Add solvers and improve existing ones. git-svn-id: https://svn.dealii.org/branches/branch_paralution@33024 0785d39b-7218-0410-832d-ea1e28bc413d --- .../include/deal.II/lac/paralution_solver.h | 333 +++++++++++++++++- deal.II/source/lac/paralution_solver.cc | 239 ++++++++++++- 2 files changed, 555 insertions(+), 17 deletions(-) diff --git a/deal.II/include/deal.II/lac/paralution_solver.h b/deal.II/include/deal.II/lac/paralution_solver.h index 03ff4a06b2..ca3ba6df2a 100644 --- a/deal.II/include/deal.II/lac/paralution_solver.h +++ b/deal.II/include/deal.II/lac/paralution_solver.h @@ -78,6 +78,68 @@ namespace ParalutionWrappers + /** + * An implementation of the solver interface using the Paralution Richardson solver + * (paralution::fixed_point_iteration). + * + * @ingroup ParalutionWrappers + * @author Bruno Turcksin, 2014 + */ + class SolverRichardson : public SolverBase + { + public: + /** + * Standardized data struct to pipe additional data to the solver. + */ + struct AdditionalData + { + /** + * Constructor. By default, set the verbosity of the solver to zero and + * the relaxation parameter to one. + */ + AdditionalData (const unsigned int verbose = 0, + const double relaxation_parameter = 1.); + + /** + * Verbosity of the solver: 0 = no output, 1 = print information at the + * start and at the end, 3 = print residual at each iteration. + */ + unsigned int verbose; + + /** + * Relaxation parameter. + */ + double relaxation_parameter; + }; + + /** + * Constructor. AdditionalData is a structure that contains additional + * flags for tuning this particular solver. + */ + SolverRichardson (SolverControl &cn, + const AdditionalData &data = AdditionalData()); + + /** + * Solve the linear system Ax=b using the CG solver of + * Paralution. If the flag @p move_to_accelerator is set to true, the + * solver is built on the accelerator. + */ + template + void solve (const SparseMatrix &A, + Vector &x, + const Vector &b, + const PreconditionBase &preconditioner, + bool move_to_accelerator=false); + + private: + /** + * store a copy of the flags for this solver. + */ + const AdditionalData additional_data; + }; + + + /** * An implementation of the solver interface using the Paralution CG solver. * @@ -88,9 +150,33 @@ namespace ParalutionWrappers { public: /** - * Constructor. + * Standardized data struct to pipe additional data to the solver. + */ + struct AdditionalData + { + /** + * Constructor. By default, set the verbosity of the solver to zero. + */ + AdditionalData (const unsigned int verbose = 0); + + /** + * Verbosity of the solver: 0 = no output, 1 = print information at the + * start and at the end, 3 = print residual at each iteration. + */ + unsigned int verbose; + + /** + * Relaxation parameter. + */ + double relaxation_parameter; + }; + + /** + * Constructor. AdditionalData is a structure that contains additional + * flags for tuning this particular solver. */ - SolverCG (SolverControl &cn); + SolverCG (SolverControl &cn, + const AdditionalData &data = AdditionalData()); /** * Solve the linear system Ax=b using the CG solver of @@ -103,6 +189,131 @@ namespace ParalutionWrappers const Vector &b, const PreconditionBase &preconditioner, bool move_to_accelerator=false); + + private: + /** + * store a copy of the flags for this solver. + */ + const AdditionalData additional_data; + }; + + + + /** + * An implementation of the solver interface using the Paralution CR solver. + * + * @ingroup ParalutionWrappers + * @author Bruno Turcksin, 2014 + */ + class SolverCR : public SolverBase + { + public: + /** + * Standardized data struct to pipe additional data to the solver. + */ + struct AdditionalData + { + /** + * Constructor. By default, set the verbosity of the solver to zero. + */ + AdditionalData (const unsigned int verbose = 0); + + /** + * Verbosity of the solver: 0 = no output, 1 = print information at the + * start and at the end, 3 = print residual at each iteration. + */ + unsigned int verbose; + }; + + /** + * Constructor. AdditionalData is a structure that contains additional + * flags for tuning this particular solver. + */ + SolverCR (SolverControl &cn, + const AdditionalData &data = AdditionalData()); + + /** + * Solve the linear system Ax=b using the CR solver of + * Paralution. If the flag @p move_to_accelerator is set to true, the + * solver is built on the accelerator. + */ + template + void solve (const SparseMatrix &A, + Vector &x, + const Vector &b, + const PreconditionBase &preconditioner, + bool move_to_accelerator=false); + + private: + /** + * store a copy of the flags for this solver. + */ + const AdditionalData additional_data; + }; + + + + /** + * An implementation of the solver interface using the Paralution DPCG solver. + * DPCG stands for Deflated Preconditioned Conjugate Gradient. It is a + * two-level preconditioned CG algorithm to iteratively solve and + * ill-conditioned linear system. Deflation tries to remove the bad + * eigenvalues from the spectrum of the preconditioned system. + * + * @ingroup ParalutionWrappers + * @author Bruno Turcksin, 2014 + */ + class SolverDPCG : public SolverBase + { + public: + /** + * Standardized data struct to pipe additional data to the solver. + */ + struct AdditionalData + { + /** + * Constructor. By default, set the verbosity of the solver to zero and + * set the number of deflated vectors to two. + */ + AdditionalData (const unsigned int verbose = 0, + const unsigned int n_deflated_vectors = 2); + + /** + * Verbosity of the solver: 0 = no output, 1 = print information at the + * start and at the end, 3 = print residual at each iteration. + */ + unsigned int verbose; + + /** + * Number of deflated vectors. + */ + unsigned int n_deflated_vectors; + }; + + /** + * Constructor. AdditionalData is a structure that contains additional + * flags for tuning this particular solver. + */ + SolverDPCG (SolverControl &cn, + const AdditionalData &data = AdditionalData()); + + /** + * Solve the linear system Ax=b using the DPCG solver of + * Paralution. If the flag @p move_to_accelerator is set to true, the + * solver is built on the accelerator. + */ + template + void solve (const SparseMatrix &A, + Vector &x, + const Vector &b, + const PreconditionBase &preconditioner, + bool move_to_accelerator=false); + + private: + /** + * store a copy of the flags for this solver. + */ + const AdditionalData additional_data; }; @@ -110,14 +321,36 @@ namespace ParalutionWrappers /** * An implementation of the solver interface using the Paralution BiCGStab * solver. + * + * @ingroup ParalutionWrappers + * @author Bruno Turcksin, 2013 */ class SolverBicgstab : public SolverBase { public: /** - * Constructor. + * Standardized data struct to pipe additional data to the solver. + */ + struct AdditionalData + { + /** + * Constructor. By default, set the verbosity of the solver to zero. + */ + AdditionalData (const unsigned int verbose = 0); + + /** + * Verbosity of the solver: 0 = no output, 1 = print information at the + * start and at the end, 3 = print residual at each iteration. + */ + unsigned int verbose; + }; + + /** + * Constructor. AdditionalData is a structure that contains additional + * flags for tuning this particular solver. */ - SolverBicgstab (SolverControl &cn); + SolverBicgstab (SolverControl &cn, + const AdditionalData &data = AdditionalData()); /** * Solve the linear system Ax=b using the BiCGStab solver of @@ -130,6 +363,12 @@ namespace ParalutionWrappers const Vector &b, const PreconditionBase &preconditioner, bool move_to_accelerator=false); + + private: + /** + * store a copy of the flags for this solver. + */ + const AdditionalData additional_data; }; @@ -137,6 +376,9 @@ namespace ParalutionWrappers /** * An implementation of the solver interface using the Paralution GMRES * solver. + * + * @ingroup ParalutionWrappers + * @author Bruno Turcksin, 2013 */ class SolverGMRES : public SolverBase { @@ -147,10 +389,18 @@ namespace ParalutionWrappers struct AdditionalData { /** - * Constructor. By default, set the size of the Krylov space to 30, - * i.e. do a restart every 30 iterations. + * Constructor. By default, set the verbosity of the solver to zero and + * set the size of the Krylov space to 30, i.e. do a restart every 30 + * iterations. + */ + AdditionalData (const unsigned int verbose = 0, + const unsigned int restart_parameter = 30); + + /** + * Verbosity of the solver: 0 = no output, 1 = print information at the + * start and at the end, 3 = print residual at each iteration. */ - AdditionalData (const unsigned int restart_parameter = 30); + unsigned int verbose; /** * Size of the Krylov space. @@ -160,9 +410,9 @@ namespace ParalutionWrappers /** * Constructor. AdditionalData is a structure that contains additional - * flags for tuning this particulat solver. + * flags for tuning this particular solver. */ - SolverGMRES (SolverControl &cn, + SolverGMRES (SolverControl &cn, const AdditionalData &data = AdditionalData()); /** @@ -179,7 +429,70 @@ namespace ParalutionWrappers private: /** - * Store a copy of the flags for this solver. + * store a copy of the flags for this solver. + */ + const AdditionalData additional_data; + }; + + + + /** + * An implementation of the solver interface using the Paralution GMRES + * solver. + * + * @ingroup ParalutionWrappers + * @author Bruno Turcksin, 2014 + */ + class SolverFGMRES : public SolverBase + { + public: + /** + * Standardized data struct to pipe additional data to the solver. + */ + struct AdditionalData + { + /** + * Constructor. By default, set the verbosity of the solver to zero and + * set the size of the Krylov space to 30, i.e. do a restart every 30 + * iterations. + */ + AdditionalData (const unsigned int verbose = 0, + const unsigned int restart_parameter = 30); + + /** + * Verbosity of the solver: 0 = no output, 1 = print information at the + * start and at the end, 3 = print residual at each iteration. + */ + unsigned int verbose; + + /** + * Size of the Krylov space. + */ + unsigned int restart_parameter; + }; + + /** + * Constructor. AdditionalData is a structure that contains additional + * flags for tuning this particular solver. + */ + SolverFGMRES (SolverControl &cn, + const AdditionalData &data = AdditionalData()); + + /** + * Solve the linear system Ax=b using the GMRES solver of + * Paralution. If the flag @p move_to_accelerator is set to true, the + * solver is built on the accelerator. + */ + template + void solve (const SparseMatrix &A, + Vector &x, + const Vector &b, + const PreconditionBase &preconditioner, + bool move_to_accelerator=false); + + private: + /** + * store a copy of the flags for this solver. */ const AdditionalData additional_data; }; diff --git a/deal.II/source/lac/paralution_solver.cc b/deal.II/source/lac/paralution_solver.cc index 623a6f17d3..22c5278ebc 100644 --- a/deal.II/source/lac/paralution_solver.cc +++ b/deal.II/source/lac/paralution_solver.cc @@ -34,6 +34,8 @@ namespace ParalutionWrappers return solver_control; } + + template void SolverBase::execute_solve(std_cxx1x::shared_ptr,paralution::LocalVector,Number> > solver, @@ -64,11 +66,61 @@ namespace ParalutionWrappers - /* ---------------------- SolverCG ------------------------ */ + /* --------------------- SolverRichardson----------------- */ + + SolverRichardson::AdditionalData::AdditionalData(const unsigned int verbose, + const double relaxation_parameter) + : + verbose(verbose), + relaxation_parameter(relaxation_parameter) + {} + + + + SolverRichardson::SolverRichardson (SolverControl &cn, + const AdditionalData &data) + : + SolverBase (cn), + additional_data (data) + {} + + + + template + void SolverRichardson::solve(const SparseMatrix &A, + Vector &x, + const Vector &b, + const PreconditionBase &preconditioner, + bool move_to_accelerator) + { + std_cxx1x::shared_ptr, + paralution::LocalVector,Number> > solver(new paralution:: + FixedPoint, paralution::LocalVector,Number>); + + // Set the verbosity of the solver. + solver->Verbose(additional_data.verbose); + + // Set the relaxation parameter. + solver->SetRelaxation(additional_data.relaxation_parameter); + + this->execute_solve(solver,A,x,b,preconditioner,move_to_accelerator); + } + + + + /* ---------------------- SolverCG ------------------------- */ - SolverCG::SolverCG (SolverControl &cn) + SolverCG::AdditionalData::AdditionalData(const unsigned int verbose) : - SolverBase (cn) + verbose(verbose) + {} + + + + SolverCG::SolverCG (SolverControl &cn, const AdditionalData &data) + : + SolverBase (cn), + additional_data (data) {} @@ -84,6 +136,86 @@ namespace ParalutionWrappers paralution::LocalVector,Number> > solver(new paralution:: CG,paralution::LocalVector,Number>); + // Set the verbosity of the solver. + solver->Verbose(additional_data.verbose); + + this->execute_solve(solver,A,x,b,preconditioner,move_to_accelerator); + } + + + + /* ---------------------- SolverCR ------------------------- */ + + SolverCR::AdditionalData::AdditionalData(const unsigned int verbose) + : + verbose(verbose) + {} + + + + SolverCR::SolverCR (SolverControl &cn, const AdditionalData &data) + : + SolverBase (cn), + additional_data (data) + {} + + + + template + void SolverCR::solve(const SparseMatrix &A, + Vector &x, + const Vector &b, + const PreconditionBase &preconditioner, + bool move_to_accelerator) + { + std_cxx1x::shared_ptr, + paralution::LocalVector,Number> > solver(new paralution:: + CR,paralution::LocalVector,Number>); + + // Set the verbosity of the solver. + solver->Verbose(additional_data.verbose); + + this->execute_solve(solver,A,x,b,preconditioner,move_to_accelerator); + } + + + + /* ---------------------- SolverDPCG ----------------------- */ + + SolverDPCG::AdditionalData::AdditionalData(const unsigned int verbose, + const const unsigned int n_deflated_vectors) + : + verbose (verbose), + n_deflated_vectors(n_deflated_vectors) + {} + + + + SolverDPCG::SolverDPCG (SolverControl &cn, const AdditionalData &data) + : + SolverBase (cn), + additional_data (data) + {} + + + + template + void SolverDPCG::solve(const SparseMatrix &A, + Vector &x, + const Vector &b, + const PreconditionBase &preconditioner, + bool move_to_accelerator) + { + std_cxx1x::shared_ptr, + paralution::LocalVector,Number> > solver(new paralution:: + DPCG, paralution::LocalVector,Number>); + + // Set the verbosity of the solver. + solver->Verbose(additional_data.verbose); + + // Set the number of deflated vectors. + solver->SetNVectors(additional_data.n_deflated_vectors); + this->execute_solve(solver,A,x,b,preconditioner,move_to_accelerator); } @@ -91,9 +223,17 @@ namespace ParalutionWrappers /* -------------------- SolverBicgstab --------------------- */ - SolverBicgstab::SolverBicgstab (SolverControl &cn) + SolverBicgstab::AdditionalData::AdditionalData(const unsigned int verbose) : - SolverBase (cn) + verbose(verbose) + {} + + + + SolverBicgstab::SolverBicgstab (SolverControl &cn, const AdditionalData &data) + : + SolverBase (cn), + additional_data (data) {} @@ -109,6 +249,9 @@ namespace ParalutionWrappers paralution::LocalVector,Number> > solver(new paralution:: BiCGStab,paralution::LocalVector,Number>); + // Set the verbosity of the solver. + solver->Verbose(additional_data.verbose); + this->execute_solve(solver,A,x,b,preconditioner,move_to_accelerator); } @@ -116,14 +259,15 @@ namespace ParalutionWrappers /* --------------------- SolverGMRES ----------------------- */ - SolverGMRES::AdditionalData::AdditionalData(const unsigned int restart_parameter) + SolverGMRES::AdditionalData::AdditionalData(const unsigned int verbose, + const unsigned int restart_parameter) : + verbose(verbose), restart_parameter(restart_parameter) {} - SolverGMRES::SolverGMRES (SolverControl &cn, const AdditionalData &data) : @@ -144,6 +288,51 @@ namespace ParalutionWrappers paralution::LocalVector,Number> > solver(new paralution:: GMRES, paralution::LocalVector,Number>); + // Set the verbosity of the solver. + solver->Verbose(additional_data.verbose); + + // Set the restart parameter. + solver->SetBasisSize(additional_data.restart_parameter); + + this->execute_solve(solver,A,x,b,preconditioner,move_to_accelerator); + } + + + + /* --------------------- SolverFGMRES ---------------------- */ + + SolverFGMRES::AdditionalData::AdditionalData(const unsigned int verbose, + const unsigned int restart_parameter) + : + verbose(verbose), + restart_parameter(restart_parameter) + {} + + + + SolverFGMRES::SolverFGMRES (SolverControl &cn, + const AdditionalData &data) + : + SolverBase (cn), + additional_data (data) + {} + + + + template + void SolverFGMRES::solve(const SparseMatrix &A, + Vector &x, + const Vector &b, + const PreconditionBase &preconditioner, + bool move_to_accelerator) + { + std_cxx1x::shared_ptr, + paralution::LocalVector,Number> > solver(new paralution:: + FGMRES, paralution::LocalVector,Number>); + + // Set the verbosity of the solver. + solver->Verbose(additional_data.verbose); + // Set the restart parameter. solver->SetBasisSize(additional_data.restart_parameter); @@ -168,6 +357,30 @@ namespace ParalutionWrappers const PreconditionBase &preconditioner, bool move_to_accelerator); + template void SolverCR::solve(const SparseMatrix &A, + Vector &x, + const Vector &b, + const PreconditionBase &preconditioner, + bool move_to_accelerator); + + template void SolverCR::solve(const SparseMatrix &A, + Vector &x, + const Vector &b, + const PreconditionBase &preconditioner, + bool move_to_accelerator); + + template void SolverDPCG::solve(const SparseMatrix &A, + Vector &x, + const Vector &b, + const PreconditionBase &preconditioner, + bool move_to_accelerator); + + template void SolverDPCG::solve(const SparseMatrix &A, + Vector &x, + const Vector &b, + const PreconditionBase &preconditioner, + bool move_to_accelerator); + template void SolverBicgstab::solve(const SparseMatrix &A, Vector &x, const Vector &b, @@ -191,6 +404,18 @@ namespace ParalutionWrappers const Vector &b, const PreconditionBase &preconditioner, bool move_to_accelerator); + + template void SolverFGMRES::solve(const SparseMatrix &A, + Vector &x, + const Vector &b, + const PreconditionBase &preconditioner, + bool move_to_accelerator); + + template void SolverFGMRES::solve(const SparseMatrix &A, + Vector &x, + const Vector &b, + const PreconditionBase &preconditioner, + bool move_to_accelerator); } DEAL_II_NAMESPACE_CLOSE -- 2.39.5