Loading...
Searching...
No Matches
amgclSolverBackend.hpp
/*
Copyright 2020 Equinor ASA
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_AMGCLSOLVER_BACKEND_HEADER_INCLUDED
#define OPM_AMGCLSOLVER_BACKEND_HEADER_INCLUDED
#include <opm/simulators/linalg/gpubridge/GpuResult.hpp>
#include <opm/simulators/linalg/gpubridge/GpuSolver.hpp>
#include <opm/simulators/linalg/gpubridge/WellContributions.hpp>
#include <boost/property_tree/ptree.hpp>
#include <amgcl/amg.hpp>
#include <amgcl/backend/builtin.hpp>
#include <amgcl/adapter/crs_tuple.hpp>
#include <amgcl/make_block_solver.hpp>
#include <amgcl/relaxation/as_preconditioner.hpp>
#include <amgcl/relaxation/ilu0.hpp>
#include <amgcl/solver/bicgstab.hpp>
#include <amgcl/preconditioner/runtime.hpp>
#include <amgcl/value_type/static_matrix.hpp>
#include <memory>
#include <mutex>
#include <type_traits>
#include <vector>
namespace Opm::Accelerator {
template<class Scalar, unsigned int block_size>
class amgclSolverBackend : public GpuSolver<Scalar,block_size>
{
using Base = GpuSolver<Scalar,block_size>;
using Base::N;
using Base::Nb;
using Base::nnz;
using Base::nnzb;
using Base::verbosity;
using Base::platformID;
using Base::deviceID;
using Base::maxit;
using Base::tolerance;
using Base::initialized;
using dmat_type = amgcl::static_matrix<Scalar, block_size, block_size>; // matrix value type in double precision
using dvec_type = amgcl::static_matrix<Scalar, block_size, 1>; // the corresponding vector value type
using CPU_Backend = std::conditional_t<block_size == 1,
amgcl::backend::builtin<Scalar>,
amgcl::backend::builtin<dmat_type>>;
using CPU_Solver = amgcl::make_solver<amgcl::runtime::preconditioner<CPU_Backend>,
amgcl::runtime::solver::wrapper<CPU_Backend>>;
private:
// amgcl can use different backends, this lets the user choose
enum Amgcl_backend_type {
cpu,
cuda,
vexcl
};
// store matrix in CSR format
std::vector<unsigned> A_rows, A_cols;
std::vector<Scalar> A_vals, rhs;
std::vector<Scalar> x;
std::once_flag print_info;
Amgcl_backend_type backend_type = cpu;
boost::property_tree::ptree prm; // amgcl parameters
int iters = 0;
Scalar error = 0.0;
#if HAVE_CUDA
std::once_flag cuda_initialize;
void solve_cuda(Scalar* b);
#endif
#if HAVE_VEXCL
std::once_flag vexcl_initialize;
#endif
void initialize(int Nb, int nnzbs);
void convert_sparsity_pattern(const int *rows, const int *cols);
void convert_data(const Scalar* vals, const int* rows);
void solve_system(Scalar* b, GpuResult& res);
public:
amgclSolverBackend(int linear_solver_verbosity, int maxit,
Scalar tolerance, unsigned int platformID,
unsigned int deviceID);
~amgclSolverBackend() override;
SolverStatus solve_system(std::shared_ptr<BlockedMatrix<Scalar>> matrix,
Scalar* b,
std::shared_ptr<BlockedMatrix<Scalar>> jacMatrix,
WellContributions<Scalar>& wellContribs,
GpuResult& res) override;
void get_result(Scalar* x) override;
}; // end class amgclSolverBackend
} // namespace Opm::Accelerator
#endif
Opm::Accelerator::BlockedMatrix
This struct resembles a blocked csr matrix, like Dune::BCRSMatrix.
Definition BlockedMatrix.hpp:29
Opm::Accelerator::GpuResult
This class is based on InverseOperatorResult struct from dune/istl/solver.hh It is needed to prevent ...
Definition GpuResult.hpp:31
Opm::Accelerator::GpuSolver::GpuSolver
GpuSolver(int linear_solver_verbosity, int max_it, Scalar tolerance_)
Construct a GpuSolver.
Definition GpuSolver.hpp:73
Opm::Accelerator::amgclSolverBackend::amgclSolverBackend
amgclSolverBackend(int linear_solver_verbosity, int maxit, Scalar tolerance, unsigned int platformID, unsigned int deviceID)
Construct an amgcl solver.
Definition amgclSolverBackend.cpp:55
Opm::Accelerator::amgclSolverBackend::get_result
void get_result(Scalar *x) override
Get result after linear solve, and peform postprocessing if necessary.
Definition amgclSolverBackend.cpp:401
Opm::Accelerator::amgclSolverBackend::~amgclSolverBackend
~amgclSolverBackend() override
Destroy a openclSolver, and free memory.
Definition amgclSolverBackend.cpp:64
Opm::WellContributions
This class serves to eliminate the need to include the WellContributions into the matrix (with –matri...
Definition WellContributions.hpp:51