opm-simulators
cusparseSolverBackend.hpp
1 /*
2  Copyright 2019 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_CUSPARSESOLVER_BACKEND_HEADER_INCLUDED
21 #define OPM_CUSPARSESOLVER_BACKEND_HEADER_INCLUDED
22 
23 
24 #include "cublas_v2.h"
25 #include "cusparse_v2.h"
26 
27 #include <opm/simulators/linalg/gpubridge/GpuResult.hpp>
28 #include <opm/simulators/linalg/gpubridge/GpuSolver.hpp>
29 #include <opm/simulators/linalg/gpubridge/WellContributions.hpp>
30 
31 namespace Opm::Accelerator {
32 
34 template<class Scalar, unsigned int block_size>
35 class cusparseSolverBackend : public GpuSolver<Scalar,block_size>
36 {
38 
39  using Base::N;
40  using Base::Nb;
41  using Base::nnz;
42  using Base::nnzb;
43  using Base::verbosity;
44  using Base::deviceID;
45  using Base::maxit;
46  using Base::tolerance;
47  using Base::initialized;
48 
49 private:
50  cublasHandle_t cublasHandle;
51  cusparseHandle_t cusparseHandle;
52  cudaStream_t stream;
53  cusparseMatDescr_t descr_B, descr_M, descr_L, descr_U;
54  bsrilu02Info_t info_M;
55  bsrsv2Info_t info_L, info_U;
56  // b: bsr matrix, m: preconditioner
57  Scalar *d_bVals, *d_mVals;
58  int *d_bCols, *d_mCols;
59  int *d_bRows, *d_mRows;
60  Scalar *d_x, *d_b, *d_r, *d_rw, *d_p; // vectors, used during linear solve
61  Scalar *d_pw, *d_s, *d_t, *d_v;
62  void *d_buffer;
63  Scalar *vals_contiguous; // only used if COPY_ROW_BY_ROW is true in cusparseSolverBackend.cpp
64 
65  bool analysis_done = false;
66 
67  bool useJacMatrix = false;
68  int nnzbs_prec; // number of nonzero blocks in the matrix for preconditioner
69  // could be jacMatrix or matrix
70 
71  double c_copy = 0.0; // cummulative timer measuring the total time it takes to transfer the data to the GPU
72 
76  void gpu_pbicgstab(WellContributions<Scalar>& wellContribs, GpuResult& res);
77 
81  void initialize(std::shared_ptr<BlockedMatrix<Scalar>> matrix,
82  std::shared_ptr<BlockedMatrix<Scalar>> jacMatrix);
83 
85  void finalize();
86 
92  void copy_system_to_gpu(std::shared_ptr<BlockedMatrix<Scalar>> matrix,
93  Scalar* b,
94  std::shared_ptr<BlockedMatrix<Scalar>> jacMatrix);
95 
101  void update_system_on_gpu(std::shared_ptr<BlockedMatrix<Scalar>> matrix,
102  Scalar* b,
103  std::shared_ptr<BlockedMatrix<Scalar>> jacMatrix);
104 
107  bool analyse_matrix();
108 
111  bool create_preconditioner();
112 
116  void solve_system(WellContributions<Scalar>& wellContribs, GpuResult &res);
117 
118 public:
124  cusparseSolverBackend(int linear_solver_verbosity, int maxit,
125  Scalar tolerance, unsigned int deviceID);
126 
129 
137  SolverStatus solve_system(std::shared_ptr<BlockedMatrix<Scalar>> matrix,
138  Scalar* b,
139  std::shared_ptr<BlockedMatrix<Scalar>> jacMatrix,
140  WellContributions<Scalar>& wellContribs,
141  GpuResult& res) override;
142 
145  void get_result(Scalar* x) override;
146 
147 }; // end class cusparseSolverBackend
148 
149 } // namespace Opm::Accelerator
150 
151 #endif
~cusparseSolverBackend()
Destroy a cusparseSolver, and free memory.
cusparseSolverBackend(int linear_solver_verbosity, int maxit, Scalar tolerance, unsigned int deviceID)
Construct a cusparseSolver.
Definition: amgclSolverBackend.cpp:49
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
void get_result(Scalar *x) override
Get resulting vector x after linear solve, also includes post processing if necessary.
This struct resembles a blocked csr matrix, like Dune::BCRSMatrix.
Definition: BlockedMatrix.hpp:28
This class implements a cusparse-based ilu0-bicgstab solver on GPU.
Definition: cusparseSolverBackend.hpp:35
This class serves to simplify choosing between different backend solvers, such as cusparseSolver and ...
Definition: GpuSolver.hpp:45