Loading...
Searching...
No Matches
openclSolverBackend.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_OPENCLSOLVER_BACKEND_HEADER_INCLUDED
#define OPM_OPENCLSOLVER_BACKEND_HEADER_INCLUDED
#include <opm/simulators/linalg/gpubridge/opencl/opencl.hpp>
#include <opm/simulators/linalg/gpubridge/GpuResult.hpp>
#include <opm/simulators/linalg/gpubridge/GpuSolver.hpp>
#include <opm/simulators/linalg/gpubridge/WellContributions.hpp>
#include <opm/simulators/linalg/gpubridge/opencl/openclPreconditioner.hpp>
namespace Opm::Accelerator {
template<class Scalar, unsigned int block_size>
class openclSolverBackend : 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;
private:
Scalar* h_b = nullptr; // b vector, on host
std::vector<Scalar> vals_contiguous; // only used if COPY_ROW_BY_ROW is true in openclSolverBackend.cpp
// OpenCL variables must be reusable, they are initialized in initialize()
cl::Buffer d_Avals, d_Acols, d_Arows; // matrix in BSR format on GPU
cl::Buffer d_x, d_b, d_rb, d_r, d_rw, d_p; // vectors, used during linear solve
cl::Buffer d_pw, d_s, d_t, d_v; // vectors, used during linear solve
cl::Buffer d_tmp; // used as tmp GPU buffer for dot() and norm()
std::vector<cl::Device> devices;
bool useJacMatrix = false;
std::unique_ptr<openclPreconditioner<Scalar,block_size>> prec;
// can perform blocked ILU0 and AMG on pressure component
bool is_root; // allow for nested solvers, the root solver is called by GpuBridge
bool analysis_done = false;
std::shared_ptr<BlockedMatrix<Scalar>> mat{}; // original matrix
std::shared_ptr<BlockedMatrix<Scalar>> jacMat{}; // matrix for preconditioner
bool opencl_ilu_parallel; // parallelize ILU operations (with level_scheduling)
std::vector<cl::Event> events;
cl_int err;
void gpu_pbicgstab(WellContributions<Scalar>& wellContribs, GpuResult& res);
void initialize(std::shared_ptr<BlockedMatrix<Scalar>> matrix,
std::shared_ptr<BlockedMatrix<Scalar>> jacMatrix);
void copy_system_to_gpu();
void update_system(Scalar* vals, Scalar* b);
void update_system_on_gpu();
bool analyze_matrix();
bool create_preconditioner();
void solve_system(WellContributions<Scalar>& wellContribs, GpuResult& res);
public:
std::shared_ptr<cl::Context> context{};
std::shared_ptr<cl::CommandQueue> queue{};
openclSolverBackend(int linear_solver_verbosity, int maxit, Scalar tolerance,
unsigned int platformID, unsigned int deviceID,
bool opencl_ilu_parallel, std::string linsolver);
openclSolverBackend(int linear_solver_verbosity, int maxit,
Scalar tolerance, bool opencl_ilu_parallel);
SolverStatus solve_system(std::shared_ptr<BlockedMatrix<Scalar>> matrix,
Scalar* b,
std::shared_ptr<BlockedMatrix<Scalar>> jacMatrix,
WellContributions<Scalar>& wellContribs,
GpuResult& res) override;
// SolverStatus solve_system(GpuResult &res);
void get_result(Scalar* x) override;
void setOpencl(std::shared_ptr<cl::Context>& context,
std::shared_ptr<cl::CommandQueue>& queue);
}; // end class openclSolverBackend
} // 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::openclSolverBackend::openclSolverBackend
openclSolverBackend(int linear_solver_verbosity, int maxit, Scalar tolerance, unsigned int platformID, unsigned int deviceID, bool opencl_ilu_parallel, std::string linsolver)
Construct a openclSolver.
Definition openclSolverBackend.cpp:46
Opm::Accelerator::openclSolverBackend::setOpencl
void setOpencl(std::shared_ptr< cl::Context > &context, std::shared_ptr< cl::CommandQueue > &queue)
Set OpenCL objects This class either creates them based on platformID and deviceID or receives them t...
Definition openclSolverBackend.cpp:241
Opm::Accelerator::openclSolverBackend::get_result
void get_result(Scalar *x) override
Solve scalar linear system, for example a coarse system of an AMG preconditioner Data is already on t...
Definition openclSolverBackend.cpp:666
Opm::WellContributions
This class serves to eliminate the need to include the WellContributions into the matrix (with –matri...
Definition WellContributions.hpp:51