opm-simulators
rocsparseSolverBackend.hpp
1 /*
2  Copyright 2023 Equinor ASA
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef OPM_ROCSPARSESOLVER_BACKEND_HEADER_INCLUDED
21 #define OPM_ROCSPARSESOLVER_BACKEND_HEADER_INCLUDED
22 
23 #include <memory>
24 
25 #include <opm/simulators/linalg/gpubridge/GpuResult.hpp>
26 #include <opm/simulators/linalg/gpubridge/GpuSolver.hpp>
27 #include <opm/simulators/linalg/gpubridge/WellContributions.hpp>
28 
29 #include <opm/simulators/linalg/gpubridge/rocm/rocsparsePreconditioner.hpp>
30 
31 #include <rocblas/rocblas.h>
32 #include <rocsparse/rocsparse.h>
33 
34 #include <hip/hip_version.h>
35 
36 namespace Opm::Accelerator {
37 
39 template<class Scalar, unsigned int block_size>
40 class rocsparseSolverBackend : public GpuSolver<Scalar,block_size>
41 {
43 
44  using Base::N;
45  using Base::Nb;
46  using Base::nnz;
47  using Base::nnzb;
48  using Base::verbosity;
49  using Base::platformID;
50  using Base::deviceID;
51  using Base::maxit;
52  using Base::tolerance;
53  using Base::initialized;
54 
55 private:
56  double c_copy = 0.0; // cummulative timer measuring the total time it takes to transfer the data to the GPU
57 
58  bool useJacMatrix = false;
59 
60  bool analysis_done = false;
61  std::shared_ptr<BlockedMatrix<Scalar>> mat{}; // original matrix
62  std::shared_ptr<BlockedMatrix<Scalar>> jacMat{}; // jacobi matrix
63 
64  rocsparse_direction dir = rocsparse_direction_row;
65  rocsparse_operation operation = rocsparse_operation_none;
66  rocsparse_handle handle;
67  rocblas_handle blas_handle;
68  rocsparse_mat_descr descr_A;
69 #if HIP_VERSION >= 50400000
70  rocsparse_mat_info spmv_info;
71 #endif
72  hipStream_t stream;
73 
74  rocsparse_int *d_Arows, *d_Acols;
75  Scalar *d_Avals;
76  Scalar *d_x, *d_b, *d_r, *d_rw, *d_p; // vectors, used during linear solve
77  Scalar *d_pw, *d_s, *d_t, *d_v;
78  int ver;
79  char rev[64];
80 
81  std::unique_ptr<rocsparsePreconditioner<Scalar, block_size> > prec; // can perform blocked ILU0 and AMG on pressure component
82 
86  void gpu_pbicgstab(WellContributions<Scalar>& wellContribs, GpuResult& res);
87 
91  void initialize(std::shared_ptr<BlockedMatrix<Scalar>> matrix,
92  std::shared_ptr<BlockedMatrix<Scalar>> jacMatrix);
93 
96  void copy_system_to_gpu(Scalar* b);
97 
100  void update_system_on_gpu(Scalar* vals, Scalar* b);
101 
104  bool analyze_matrix();
105 
108  bool create_preconditioner();
109 
113  void solve_system(WellContributions<Scalar>& wellContribs, GpuResult& res);
114 
115 public:
123  rocsparseSolverBackend(int linear_solver_verbosity,
124  int maxit,
125  Scalar tolerance,
126  unsigned int platformID,
127  unsigned int deviceID,
128  std::string linsolver);
129 
131  rocsparseSolverBackend(int linear_solver_verbosity, int maxit, Scalar tolerance, bool opencl_ilu_reorder);
132 
135 
143  SolverStatus solve_system(std::shared_ptr<BlockedMatrix<Scalar>> matrix,
144  Scalar* b,
145  std::shared_ptr<BlockedMatrix<Scalar>> jacMatrix,
146  WellContributions<Scalar>& wellContribs,
147  GpuResult& res) override;
148 
151  void get_result(Scalar* x) override;
152 
153 }; // end class rocsparseSolverBackend
154 
155 } // namespace Opm::Accelerator
156 
157 #endif
rocsparseSolverBackend(int linear_solver_verbosity, int maxit, Scalar tolerance, unsigned int platformID, unsigned int deviceID, std::string linsolver)
Construct a rocsparseSolver.
Definition: rocsparseSolverBackend.cpp:62
~rocsparseSolverBackend()
Destroy a openclSolver, and free memory.
Definition: rocsparseSolverBackend.cpp:113
Definition: amgclSolverBackend.cpp:49
This class implements a rocsparse-based ilu0-bicgstab solver on GPU.
Definition: rocsparseSolverBackend.hpp:40
void get_result(Scalar *x) override
Get result after linear solve, and peform postprocessing if necessary.
Definition: rocsparseSolverBackend.cpp:638
This class is based on InverseOperatorResult struct from dune/istl/solver.hh It is needed to prevent ...
Definition: GpuResult.hpp:30
This class serves to eliminate the need to include the WellContributions into the matrix (with –matr...
Definition: GpuBridge.hpp:30
This struct resembles a blocked csr matrix, like Dune::BCRSMatrix.
Definition: BlockedMatrix.hpp:28
This class serves to simplify choosing between different backend solvers, such as cusparseSolver and ...
Definition: GpuSolver.hpp:45